PyalgoTrade 計算權重平滑平均價(三)

本節介紹如何使用收盤價的SMA價格的策略python

from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import ma class MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument): super(MyStrategy, self).__init__(feed) # We want a 15 period SMA over the closing prices. self.__sma = ma.SMA(feed[instrument].getCloseDataSeries(), 15) self.__instrument = instrument def onBars(self, bars): bar = bars[self.__instrument] self.info("%s %s" % (bar.getClose(), self.__sma[-1])) # 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

  • 用收盤價格數據系列中初始化SMA過濾器。
  • 打印當前的SMA值以及收盤價。
    若是您運行腳本,您應該看到收盤價格和相應的SMA值,但在這種狀況下,前14個SMA值爲空。那是由於咱們須要至少15個值來求取SMA:
    2000-01-03 00:00:00 strategy [INFO] 118.12 None
    2000-01-04 00:00:00 strategy [INFO] 107.69 None
    2000-01-05 00:00:00 strategy [INFO] 102.0 None
    2000-01-06 00:00:00 strategy [INFO] 96.0 None
    2000-01-07 00:00:00 strategy [INFO] 103.37 None
    2000-01-10 00:00:00 strategy [INFO] 115.75 None
    2000-01-11 00:00:00 strategy [INFO] 112.37 None
    2000-01-12 00:00:00 strategy [INFO] 105.62 None
    2000-01-13 00:00:00 strategy [INFO] 105.06 None
    2000-01-14 00:00:00 strategy [INFO] 106.81 None
    2000-01-18 00:00:00 strategy [INFO] 111.25 None
    2000-01-19 00:00:00 strategy [INFO] 57.13 None
    2000-01-20 00:00:00 strategy [INFO] 59.25 None
    2000-01-21 00:00:00 strategy [INFO] 59.69 None
    2000-01-24 00:00:00 strategy [INFO] 54.19 94.2866666667
    2000-01-25 00:00:00 strategy [INFO] 56.44 90.1746666667
    .
    .
    .
    2000-12-27 00:00:00 strategy [INFO] 30.69 29.9866666667
    2000-12-28 00:00:00 strategy [INFO] 31.06 30.0446666667
    2000-12-29 00:00:00 strategy [INFO] 29.06 30.0946666667



做者:readilen
連接:http://www.jianshu.com/p/7a9e2635bfa1
來源:簡書
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。spa

相關文章
相關標籤/搜索