さて、指標を使う上でのいくつかの補足。
まず終値を当日だけでなくそれ以前も参照したいときは、あらかじめgetPriceDataSeries()の値をストラテジーのメンバ変数に代入しておく。
2つ目は、仕様がクロスしたときの検出、例えば平均移動線を価格が抜いていったときのゴールデンクロスの検出はcross_aboveを使う
例によってテストしたコードは下の通り
#!/usr/bin/python # -*- coding: utf-8 -*- import logging from pyalgotrade.technical import ma from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import cross logging.basicConfig(filename='/tmp/exec.log') class MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument): strategy.BacktestingStrategy.__init__(self, feed) self.__prices = feed[instrument].getPriceDataSeries() self.__sma = ma.SMA(feed[instrument].getCloseDataSeries(), 15) self.__instrument = instrument def onBars(self, bars): if self.__sma[-1] is None: return bar = bars[self.__instrument] flag1 = 1 if self.__prices[-1] > self.__sma[-1] else 0 flag2 = cross.cross_above(self.__prices,self.__sma) self.info("%f %f SMA:%f %d %d " % (bar.getClose(),self.__prices[-1],self.__sma[-1],flag1,flag2)) # 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()
で、結果は以下の通り
2000-11-14 00:00:00 strategy [INFO] 28.375000 28.375000 SMA:29.570833 0 0 2000-11-15 00:00:00 strategy [INFO] 28.875000 28.875000 SMA:29.204167 0 0 2000-11-16 00:00:00 strategy [INFO] 27.375000 27.375000 SMA:28.758333 0 0 2000-11-17 00:00:00 strategy [INFO] 28.812500 28.812500 SMA:28.400000 1 1 2000-11-20 00:00:00 strategy [INFO] 24.750000 24.750000 SMA:27.941667 0 0 2000-11-21 00:00:00 strategy [INFO] 23.875000 23.875000 SMA:27.333333 0 0 2000-11-22 00:00:00 strategy [INFO] 22.312500 22.312500 SMA:26.729167 0 0 2000-11-24 00:00:00 strategy [INFO] 24.125000 24.125000 SMA:26.366667 0 0 2000-11-27 00:00:00 strategy [INFO] 23.125000 23.125000 SMA:25.887500 0 0 2000-11-28 00:00:00 strategy [INFO] 22.656250 22.656250 SMA:25.535417 0 0 2000-11-29 00:00:00 strategy [INFO] 22.875000 22.875000 SMA:25.289583 0 0 2000-11-30 00:00:00 strategy [INFO] 26.500000 26.500000 SMA:25.402083 1 1 2000-12-01 00:00:00 strategy [INFO] 26.437500 26.437500 SMA:25.352083 1 0 2000-12-04 00:00:00 strategy [INFO] 28.187500 28.187500 SMA:25.535417 1 0 2000-12-05 00:00:00 strategy [INFO] 31.500000 31.500000 SMA:25.985417 1 0 2000-12-06 00:00:00 strategy [INFO] 30.187500 30.187500 SMA:26.106250 1 0 2000-12-07 00:00:00 strategy [INFO] 28.312500 28.312500 SMA:26.068750 1 0 2000-12-08 00:00:00 strategy [INFO] 30.062500 30.062500 SMA:26.247917 1 0 2000-12-11 00:00:00 strategy [INFO] 31.937500 31.937500 SMA:26.456250 1 0 2000-12-12 00:00:00 strategy [INFO] 30.750000 30.750000 SMA:26.856250 1 0 2000-12-13 00:00:00 strategy [INFO] 28.375000 28.375000 SMA:27.156250 1 0 2000-12-14 00:00:00 strategy [INFO] 27.500000 27.500000 SMA:27.502083 0 0 2000-12-15 00:00:00 strategy [INFO] 28.562500 28.562500 SMA:27.797917 1 1 2000-12-18 00:00:00 strategy [INFO] 32.000000 32.000000 SMA:28.389583 1 0 2000-12-19 00:00:00 strategy [INFO] 30.625000 30.625000 SMA:28.920833 1 0 2000-12-20 00:00:00 strategy [INFO] 28.500000 28.500000 SMA:29.295833 0 0 2000-12-21 00:00:00 strategy [INFO] 29.500000 29.500000 SMA:29.495833 1 1 2000-12-22 00:00:00 strategy [INFO] 31.875000 31.875000 SMA:29.858333 1 0 2000-12-26 00:00:00 strategy [INFO] 30.937500 30.937500 SMA:30.041667 1 0 2000-12-27 00:00:00 strategy [INFO] 30.687500 30.687500 SMA:29.987500 1 0 2000-12-28 00:00:00 strategy [INFO] 31.062500 31.062500 SMA:30.045833 1 0 2000-12-29 00:00:00 strategy [INFO] 29.062500 29.062500 SMA:30.095833 0 0
・getPriceDataSeries()と__pricesは同じ値段になっている。
・単純比較とは違って、下から上にクロスしたときだけcross_aboveが1になっている。
同様にデッドクロスを検出するためのcross_belowもある。
これらは第3引数に何本前の値までさかのぼるかを指定できるが、デフォルトでは-2で2本分だけ。
0 件のコメント:
コメントを投稿