咱們繼續採起簡單的策略,此次模擬實際交易。這個想法很簡單:python
from pyalgotrade import strategy from pyalgotrade.barfeed import yahoofeed from pyalgotrade.technical import ma class MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument, smaPeriod): super(MyStrategy, self).__init__(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)
運行策略後看到以下結果測試
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
若是調整sma的測試周期,講獲得不同的結果ui
for i in range(10, 30): run_strategy(i)
咱們發現sma(20)的結果最好lua
Final portfolio value: $1075.38
做者:readilen
連接:http://www.jianshu.com/p/3ac363f931d3
來源:簡書
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。spa