PyalgoTrade 打印收盘价(二)

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

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()

代码作三件主要事情:lua

  • 声明新策略 只有一种必须定义的方法,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
來源:简书
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。spa

相关文章
相关标签/搜索