Python量化交易实战-38使用开源项目回测双均线策略

网友投稿 1184 2022-11-09

Python量化交易实战-38使用开源项目回测双均线策略

Python量化交易实战-38使用开源项目回测双均线策略

使用PyAlgoTrade回测双均线策略

双均线策略:长短周期均线,通过​​金叉​​​,​​死叉​​的方式买入卖出股票,获取收益的策略。

回顾上节课代码的部分,上节课完成了可视化代码的部分, 主要使用​​PyAlgoTrade​​​的​​plotter​​​模块,对​​SMA​​​的数据,​​Simple Returns​​​的数据进行了可视化,我们也利用了​​returns​​​这个类来获取收益,然后将实例化的returns类去赋予给​​mystrategy​​。

你会发现,所有的核心就是:我计算出来的数据,将这个数据作为​​mystrategy​​​的属性,这样就可以在​​mystrategy​​里面给各个函数使用了。

现在是单均线,如果我要使用双均线应该怎么实现呢?

一、实操找到最优质双均线

创建变量 修改构造函数:

class MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument, smaPeriod1, smaPeriod2): super(MyStrategy, self).__init__(feed, 10000) self.__position = None self.__instrument = instrument # # We'll use adjusted close values instead of regular close values. # self.setUseAdjustedValues(True) self.__sma1 = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod1) self.__sma2 = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod2)

设置2个返回值函数:

def getSMA1(self): return self.__sma1 def getSMA2(self): return self.__sma2

修改策略:

def onBars(self, bars): # Wait for enough bars to be available to calculate a SMA. if self.__sma2[-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 self.__sma1[-1] > self.__sma2[-1]: # Enter a buy market order for 10 shares. The order is good till canceled. self.__position = self.enterLong(self.__instrument, 100, True) # 短期均价<长期均价卖出 elif self.__sma1[-1] < self.__sma2[-1] and not self.__position.exitActive(): self.__position.exitMarket()

运行策略:

def run_strategy(smaPeriod1, smaPeriod2): # Load the bar feed from the CSV file instruments = ["000001"] feeds = tools.build_feed(instruments, 2018, 2021, "histdata") #print(feeds) # Evaluate the strategy with the feed's bars. myStrategy = MyStrategy(feeds, instruments[0], smaPeriod1, smaPeriod2) myStrategy.run() # 打印总持仓:(cash + shares * price) (持有现金 + 股数*价格) print(smaPeriod1, smaPeriod2, "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()) # Attach a returns analyzers to the strategy. # returnsAnalyzer = returns.Returns() # myStrategy.attachAnalyzer(returnsAnalyzer) # # Attach the plotter to the strategy. # plt = plotter.StrategyPlotter(myStrategy) # # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. # plt.getInstrumentSubplot(instruments[0]).addDataSeries("SMA1", myStrategy.getSMA1()) # plt.getInstrumentSubplot(instruments[0]).addDataSeries("SMA2", myStrategy.getSMA2()) # # Plot the simple returns on each bar. # plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # # Run the strategy. # myStrategy.run() # myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult()) # # Plot the strategy. # plt.plot()

调用运行策略:

ma1 = [5, 10, 15]ma2 = [15, 20, 35]for i in ma1: for j in ma2: if i

运行:

结果来看,数据表现最好的是10661

二、可视化最优双均线

#使用pyalgotrade进行数据回测import pyalgotradefrom pyalgotrade import strategyfrom pyalgotrade.barfeed import quandlfeedfrom pyalgotrade_tushare import tools, barfeedfrom pyalgotrade.technical import mafrom pyalgotrade import plotterfrom pyalgotrade.barfeed import quandlfeedfrom pyalgotrade.stratanalyzer import returnsdef safe_round(value, digits): if value is not None: value = round(value, digits) return valueclass MyStrategy(strategy.BacktestingStrategy): def __init__(self, feed, instrument, smaPeriod1, smaPeriod2): super(MyStrategy, self).__init__(feed, 10000) self.__position = None self.__instrument = instrument # # We'll use adjusted close values instead of regular close values. # self.setUseAdjustedValues(True) self.__sma1 = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod1) self.__sma2 = ma.SMA(feed[instrument].getPriceDataSeries(), smaPeriod2) def getSMA1(self): return self.__sma1 def getSMA2(self): return self.__sma2 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.__sma2[-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 self.__sma1[-1] > self.__sma2[-1]: # Enter a buy market order for 10 shares. The order is good till canceled. self.__position = self.enterLong(self.__instrument, 100, True) # 短期均价<长期均价卖出 elif self.__sma1[-1] < self.__sma2[-1] and not self.__position.exitActive(): self.__position.exitMarket()def run_strategy(smaPeriod1, smaPeriod2): # Load the bar feed from the CSV file instruments = ["000001"] feeds = tools.build_feed(instruments, 2018, 2021, "histdata") #print(feeds) # Evaluate the strategy with the feed's bars. myStrategy = MyStrategy(feeds, instruments[0], smaPeriod1, smaPeriod2) # myStrategy.run() # # 打印总持仓:(cash + shares * price) (持有现金 + 股数*价格) # print(smaPeriod1, smaPeriod2, "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot(instruments[0]).addDataSeries("SMA1", myStrategy.getSMA1()) plt.getInstrumentSubplot(instruments[0]).addDataSeries("SMA2", myStrategy.getSMA2()) # Plot the simple returns on each bar. plt.getOrCreateSubplot("returns").addDataSeries("Simple returns", returnsAnalyzer.getReturns()) # Run the strategy. myStrategy.run() myStrategy.info("Final portfolio value: $%.2f" % myStrategy.getResult()) # Plot the strategy. plt.plot()run_strategy(5, 30)

运行:

对于双均线可视化图表的部分

蓝色的颜色的是K线,浅蓝色是短期均线, 紫色是长期均线,更平缓,然后就是买入卖出标记,这里的买入卖出标记比起单均线的策略明显要少很多,也就是开仓次数要少很多

接着看单次收益率图,从图中看到,收益并没有很好,上下浮动差不多。所以在这个情况下,可以基于很多因素进行优化。

最后看一下累计收益图,可以看到整体曲线和行情数据表现差不多 那么以上就是关于如何去利用一个已有的开源框线对我们的双均线策略进行回测的内同。到这里如果说你完全知道代码是怎么写的,那么你应该也能够实现其他的策略,比如说布林策略,通过改写这里的开仓和平仓条件就能够快速的

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C++ 产生UUID
下一篇:HDU 1052:Tian Ji -- The Horse Racing
相关文章

 发表评论

暂时没有评论,来抢沙发吧~