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

相關文章
相關標籤/搜索