def run_strategy(cash): instruments = ["000001"] feeds = tools.build_feed(instruments, 2016, 2018, "histdata") # 设置手续费 broker_commision = broker.backtesting.TradePercentage(0.0003) brk = broker.backtesting.Broker(cash, feeds, broker_commision) myStrategy = MyStrategy(feeds, instruments[0], brk) retAnalyzer = returns.Returns() myStrategy.attachAnalyzer(retAnalyzer) sharpeAnalyzer = sharpe.SharpeRatio() myStrategy.attachAnalyzer(sharpeAnalyzer) drawDownAnalyzer = drawdown.DrawDown() myStrategy.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() myStrategy.attachAnalyzer(tradesAnalyzer) # 以沪深300为基准# instruments300 = ["510300"] feeds300 = tools.build_feed(instruments300, 2016, 2018, "histdata300") # 设置手续费 broker_commision = broker.backtesting.TradePercentage(0.0003) brk = broker.backtesting.Broker(cash, feeds300, broker_commision) myStrategy300 = MyStrategy(feeds300, instruments300[0], brk) retAnalyzer300 = returns.Returns() myStrategy300.attachAnalyzer(retAnalyzer300) sharpeAnalyzer300 = sharpe.SharpeRatio() myStrategy300.attachAnalyzer(sharpeAnalyzer300) drawDownAnalyzer300 = drawdown.DrawDown() myStrategy300.attachAnalyzer(drawDownAnalyzer300) tradesAnalyzer300 = trades.Trades() myStrategy300.attachAnalyzer(tradesAnalyzer300) plter = plotter.StrategyPlotter(myStrategy) plter.getOrCreateSubplot("return").addDataSeries("retuens", retAnalyzer.getReturns()) plter.getOrCreateSubplot("CumReturn").addDataSeries( "CumReturn", retAnalyzer.getCumulativeReturns()) plter.getOrCreateSubplot("return").addDataSeries( "retuens300", retAnalyzer300.getReturns()) plter.getOrCreateSubplot("CumReturn").addDataSeries( "CumReturn300", retAnalyzer300.getCumulativeReturns()) myStrategy.run() myStrategy300.run() plter.savePlot("testdata.png") # print("交易次数:%d" % (tradesAnalyzer.getCount())) print("策略回测指标") analyzer((retAnalyzer, sharpeAnalyzer, drawDownAnalyzer, tradesAnalyzer)) print("基准回测指标") analyzer((retAnalyzer300, sharpeAnalyzer300, drawDownAnalyzer300, tradesAnalyzer300)) # 计算阿尔法 β 信息比例等值 calculater(retAnalyzer, retAnalyzer300) return (myStrategy, retAnalyzer, sharpeAnalyzer, drawDownAnalyzer, tradesAnalyzer)
def run_strategy(cash): instruments = ["000001"] feeds = tools.build_feed(instruments, 2016, 2018, "histdata") # 设置手续费 broker_commision = broker.backtesting.TradePercentage(0.0003) brk = broker.backtesting.Broker(cash, feeds, broker_commision) myStrategy = MyStrategy(feeds, instruments[0], brk) retAnalyzer = returns.Returns() myStrategy.attachAnalyzer(retAnalyzer) sharpeAnalyzer = sharpe.SharpeRatio() myStrategy.attachAnalyzer(sharpeAnalyzer) drawDownAnalyzer = drawdown.DrawDown() myStrategy.attachAnalyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() myStrategy.attachAnalyzer(tradesAnalyzer) plter = plotter.StrategyPlotter(myStrategy) plter.getOrCreateSubplot("return").addDataSeries("retuens", retAnalyzer.getReturns()) plter.getOrCreateSubplot("CumReturn").addDataSeries( "CumReturn", retAnalyzer.getCumulativeReturns()) myStrategy.run() plter.savePlot("testdata.png") print("交易次数:%d" % (tradesAnalyzer.getCount())) return (myStrategy, retAnalyzer, sharpeAnalyzer, drawDownAnalyzer, tradesAnalyzer)
def main(): instruments = ["600036"] feeds = tools.build_feed(instruments, 2013, 2018, "histdata") # 3.实例化策略 strat = MyStrategy(feeds, instruments[0]) # 4.设置指标和绘图 ratio = sharpe.SharpeRatio() strat.attachAnalyzer(ratio) plter = plotter.StrategyPlotter(strat) # 5.运行策略 strat.run() strat.info("最终收益: %.2f" % strat.getResult()) # 6.输出夏普率、绘图 strat.info("夏普比率: " + str(ratio.getSharpeRatio(0))) plter.plot()
def __createBarfeed(self): self.__feed = tools.build_feed(self.__instrument, self.__startYear, self.__endYear, "histdata") self.__feedBase = tools.build_feed(self.__base, self.__startYear, self.__endYear, "histdata")