PyAlgoTrade慣れてきた気がするので、早速トレーディングしてみる。といってもシミュレーションだけどね。
売買ルールはとても簡単
15日単純移動平均株価を終値が上回ったら買う
15日単純移動平均株価を終値が下回って、株を持っていたら売る
15日単純移動平均株価を終値が下回って、株を持っていたら売る
それが以下のプログラムになる
from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import ma class MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument, smaPeriod): strategy.BacktestingStrategy.__init__(self, feed, 1000) self.__position = None self.__instrument = instrument # We'll use adjusted close values instead of regular close values. self.setUseAdjustedValues(True) self.__sma = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod) def onEnterOk(self, position): execInfo = position.getEntryOrder().getExecutionInfo() self.info("BUY at $%.2f" % (execInfo.getPrice())) def onEnterCanceled(self, position): self.__position = None def onExitOk(self, position): execInfo = position.getExitOrder().getExecutionInfo() self.info("SELL at $%.2f" % (execInfo.getPrice())) self.__position = None def onExitCanceled(self, position): # If the exit was canceled, re-submit it. self.__position.exitMarket() def onBars(self, bars): # Wait for enough bars to be available to calculate a SMA. if self.__sma[-1] is None: return bar = bars[self.__instrument] # If a position was not opened, check if we should enter a long position. if self.__position is None: if bar.getPrice() > self.__sma[-1]: # Enter a buy market order for 10 shares. The order is good till canceled. self.__position = self.enterLong(self.__instrument, 10, True) # Check if we have to exit the position. elif bar.getPrice() < self.__sma[-1] and not self.__position.exitActive(): self.__position.exitMarket() def run_strategy(smaPeriod): # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.addBarsFromCSV("orcl", "orcl-2000.csv") # Evaluate the strategy with the feed. myStrategy = MyStrategy(feed, "orcl", smaPeriod) myStrategy.run() print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity() run_strategy(15)
python -c "from pyalgotrade.tools import yahoofinance; yahoofinance.download_daily_bars('orcl', 2000, 'orcl-2000.csv')"
結果は以下のようになる。
2000-01-26 00:00:00 strategy [INFO] BUY at $27.26 2000-01-28 00:00:00 strategy [INFO] SELL at $24.74 2000-02-03 00:00:00 strategy [INFO] BUY at $26.60 2000-02-22 00:00:00 strategy [INFO] SELL at $28.40 2000-02-23 00:00:00 strategy [INFO] BUY at $28.91 2000-03-31 00:00:00 strategy [INFO] SELL at $38.51 2000-04-07 00:00:00 strategy [INFO] BUY at $40.19 2000-04-12 00:00:00 strategy [INFO] SELL at $37.44 2000-04-19 00:00:00 strategy [INFO] BUY at $37.76 2000-04-20 00:00:00 strategy [INFO] SELL at $35.45 2000-04-28 00:00:00 strategy [INFO] BUY at $37.70 2000-05-05 00:00:00 strategy [INFO] SELL at $35.54 2000-05-08 00:00:00 strategy [INFO] BUY at $36.17 2000-05-09 00:00:00 strategy [INFO] SELL at $35.39 2000-05-16 00:00:00 strategy [INFO] BUY at $37.28 2000-05-19 00:00:00 strategy [INFO] SELL at $34.58 2000-05-31 00:00:00 strategy [INFO] BUY at $35.18 2000-06-23 00:00:00 strategy [INFO] SELL at $38.81 2000-06-27 00:00:00 strategy [INFO] BUY at $39.56 2000-06-28 00:00:00 strategy [INFO] SELL at $39.42 2000-06-29 00:00:00 strategy [INFO] BUY at $39.41 2000-06-30 00:00:00 strategy [INFO] SELL at $38.60 2000-07-03 00:00:00 strategy [INFO] BUY at $38.96 2000-07-05 00:00:00 strategy [INFO] SELL at $36.89 2000-07-21 00:00:00 strategy [INFO] BUY at $37.19 2000-07-24 00:00:00 strategy [INFO] SELL at $37.04 2000-07-26 00:00:00 strategy [INFO] BUY at $35.93 2000-07-28 00:00:00 strategy [INFO] SELL at $36.08 2000-08-01 00:00:00 strategy [INFO] BUY at $36.11 2000-08-02 00:00:00 strategy [INFO] SELL at $35.06 2000-08-04 00:00:00 strategy [INFO] BUY at $37.61 2000-09-11 00:00:00 strategy [INFO] SELL at $41.34 2000-09-29 00:00:00 strategy [INFO] BUY at $39.07 2000-10-02 00:00:00 strategy [INFO] SELL at $38.30 2000-10-20 00:00:00 strategy [INFO] BUY at $34.71 2000-10-31 00:00:00 strategy [INFO] SELL at $31.34 2000-11-20 00:00:00 strategy [INFO] BUY at $23.35 2000-11-21 00:00:00 strategy [INFO] SELL at $23.83 2000-12-01 00:00:00 strategy [INFO] BUY at $25.33 2000-12-21 00:00:00 strategy [INFO] SELL at $26.72 2000-12-22 00:00:00 strategy [INFO] BUY at $29.17 Final portfolio value: $979.44
最後のFinal portfolio value: $979.44が最終評価額。ちなみにスタート時は$1000持っている設定だから損をしている。
えー?15日平均がいけなったのかもしれないね。ということで10から30まで順番に試してみよう
run_strategy(15)のところを以下のように書き換える。pythonなのでインデントには注意
えー?15日平均がいけなったのかもしれないね。ということで10から30まで順番に試してみよう
run_strategy(15)のところを以下のように書き換える。pythonなのでインデントには注意
for i in range(10, 30): run_strategy(i)これを実行すると以下のような結果になる18日~25日でプラスになる
10 Final portfolio value: $881.22 11 Final portfolio value: $880.92 12 Final portfolio value: $831.48 13 Final portfolio value: $894.46 14 Final portfolio value: $897.38 15 Final portfolio value: $974.33 16 Final portfolio value: $959.99 17 Final portfolio value: $950.26 18 Final portfolio value: $1011.70 19 Final portfolio value: $1041.54 20 Final portfolio value: $1077.24 21 Final portfolio value: $1058.22 22 Final portfolio value: $1060.85 23 Final portfolio value: $1022.82 24 Final portfolio value: $1019.89 25 Final portfolio value: $1013.17 26 Final portfolio value: $998.24 27 Final portfolio value: $982.15 28 Final portfolio value: $975.72 29 Final portfolio value: $983.32
パラメータが1個だと簡単だけど複数だと大変だよね。ということで、その3最適化に続く
0 件のコメント:
コメントを投稿