PyalgoTrade 打印收盘价(二)

让我们从一个简单的策略开始,就是在打印收盘价格的过程中:

from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed


class MyStrategy(strategy.BacktestingStrategy):
    def __init__(self, feed, instrument):
        super(MyStrategy, self).__init__(feed)
        self.__instrument = instrument

    def onBars(self, bars):
        bar = bars[self.__instrument]
        self.info(bar.getClose())

# Load the yahoo feed from the CSV file
feed = yahoofeed.Feed()
feed.addBarsFromCSV("orcl", "orcl-2000.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed, "orcl")
myStrategy.run()

代码做三件主要事情:

  • 声明新策略 只有一种必须定义的方法,onBars,它被称为Feed中的每个栏。
  • 从CSV文件加载Feed。
  • 使用Feed提供的栏来运行策略。
    如果您运行脚本,您应该按顺序看到收盘价:
    2000-01-03 00:00:00 strategy [INFO] 118.12
    2000-01-04 00:00:00 strategy [INFO] 107.69
    2000-01-05 00:00:00 strategy [INFO] 102.0
    .
    .
    .
    2000-12-27 00:00:00 strategy [INFO] 30.69
    2000-12-28 00:00:00 strategy [INFO] 31.06
    2000-12-29 00:00:00 strategy [INFO] 29.06



作者:readilen
链接:http://www.jianshu.com/p/9f9658474df2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

posted @ 2017-09-21 16:21  zl306222525  阅读(277)  评论(0编辑  收藏  举报