VNPY 批量策略回測和統計結果的excel輸出

張國平發表於2018-06-21
做VNPY這段時間,發現主要就是回測,和策略優化;然後就是有批量測試和引數集合效果匯出excel分析要求。還有就是一個策略對不同品種效果驗證。

然後想想,就自己寫了一個BatchBackTest 類,其實很簡答,就是輸入品種佇列,和策略佇列,然後就會迴圈跑出結果,輸出到指定路徑excel。 還有就是策略是元組,是由一個策略和對應引數組合。

程式碼如下。使用方法就是在 vnpy/example/CtaBacktesting 路徑下面新疆一個py,放入下面程式碼就可以

點選(此處)摺疊或開啟

  1. # encoding: UTF-8
  2. from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, MINUTE_DB_NAME
  3. import pandas as pd
  4. from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy
  5. from vnpy.trader.app.ctaStrategy.strategy.strategyBollChannel import BollChannelStrategy
  6. from vnpy.trader.app.ctaStrategy.strategy.strategyDoubleMa import DoubleMaStrategy
  7. from vnpy.trader.app.ctaStrategy.strategy.strategyDualThrust import DualThrustStrategy
  8. from vnpy.trader.app.ctaStrategy.strategy.strategyKingKeltner import KkStrategy
  9. from vnpy.trader.app.ctaStrategy.strategy.strategyMultiSignal import MultiSignalStrategy
  10. from vnpy.trader.app.ctaStrategy.strategy.strategyMultiTimeframe import MultiTimeframeStrategy

  11. class BatchBackTest(object):
  12.     def __init__(self):
  13.         ""
  14.     def calculateBacktesting(self,symbollist,strategylist):
  15.         #填入品種佇列和策略佇列,返回結果resultlist, 為了輸出方便檢索,加入品種名稱,策略名稱和策略引數
  16.         resultlist = []
  17.         for symbol in symbollist:
  18.             for strategy in strategylist:
  19.                 result = self.runBacktesting(symbol,strategy)
  20.                 #加入品種名稱,策略名稱和策略引數
  21.                 result["Symbolname"] = str(symbol["vtSymbol"])
  22.                 result["strategyname"] = str(strategy[0])
  23.                 result["strategysetting"] = str(strategy[1])
  24.                 resultlist.append(result)
  25.         return resultlist


  26.     def runBacktesting(self, symbol, strategy ):
  27.         #寫入測試品種和引數, 返回回測資料集包含回測結果

  28.         # 在引擎中建立策略物件
  29.         # 建立回測引擎
  30.         engine = BacktestingEngine()
  31.         # 設定引擎的回測模式為K線
  32.         engine.setBacktestingMode(engine.BAR_MODE)
  33.         # 設定回測用的資料起始日期
  34.         engine.setStartDate(symbol["StartDate"])
  35.         engine.setSlippage(symbol["Slippage"]) # 1跳
  36.         engine.setRate(symbol["Rate"]) # 佣金大小
  37.         engine.setSize(symbol["Size"]) # 合約大小
  38.         engine.setPriceTick(symbol["Slippage"]) # 最小价格變動
  39.         engine.setCapital(symbol["Capital"])

  40.         # 設定使用的歷史資料庫
  41.         engine.setDatabase(MINUTE_DB_NAME, symbol["vtSymbol"])
  42.         #設定策略,策略元組中第一個是策略,第二個引數
  43.         engine.initStrategy(strategy[0], strategy[1])
  44.         engine.runBacktesting()
  45.         df = engine.calculateDailyResult()
  46.         result = []
  47.         dfp,result = engine.calculateDailyStatistics(df)
  48.         engine.output(u'輸出統計資料')
  49.         # engine.showDailyResult(dfp, result)
  50.         return result

  51.     def toExcel(self, resultlist, path = "C:\data\datframe.xlsx"):
  52.         #按照輸入統計資料佇列和路徑,輸出excel,這裡不提供新增模式,如果想,可以改
  53.         #dft.to_csv(path,index=False,header=True, mode = 'a')
  54.         summayKey = resultlist[0].keys()
  55.         # summayValue = result.values()

  56.         dft = pd.DataFrame(columns=summayKey)
  57.         for result in resultlist:
  58.             new = pd.DataFrame(result, index=["0"])
  59.             dft = dft.append(new,ignore_index=True)
  60.         dft.to_excel(path,index=False,header=True)
  61.         print "回測統計結果輸出到" + path


  62. if __name__ == "__main__":
  63.     #建立品種佇列,這裡可以用json匯入,為了方便使用直接寫了。
  64.     symbollist = [{
  65.         "vtSymbol": 'm1809',
  66.         "StartDate": "20180101",
  67.         "Slippage": 1,
  68.         "Size": 10,
  69.         "Rate": 2 / 10000,
  70.         "Capital": 10000
  71.             },
  72.         {
  73.             "vtSymbol": 'rb0000',
  74.             "StartDate": "20180101",
  75.             "Slippage": 1,
  76.             "Size": 10,
  77.             "Rate": 2 / 10000,
  78.             "Capital": 10000
  79.         }]
  80.     #這裡定義策略,策略引數先為空;策略加引數是一個元組
  81.     setting = {}
  82.     Strategylist = [(AtrRsiStrategy, setting),
  83.                     (BollChannelStrategy, setting),
  84.                     (DoubleMaStrategy, setting),
  85.                     (DualThrustStrategy, setting),
  86.                     (KkStrategy, setting),
  87.                     (MultiSignalStrategy, setting),
  88.                     (MultiTimeframeStrategy, setting)]
  89.     # 這裡是同一個策略,不同引數的情況,當然可以有多個策略和多個引數組合
  90.     Strategylist2 = []
  91.     # 策略list
  92.     settinglist =[
  93.         {'kdlimit': 40, 'barmins': 9, 'cciWindow': 22},
  94.         {'kdlimit': 30, 'barmins': 13, 'cciWindow': 20}]
  95.     # 合併一個元組
  96.     if settinglist != []:
  97.         for para1 in settinglist:
  98.             Strategylist2.append((BollChannelStrategy, para1))

  99.     NT = BatchBackTest()
  100.     resultlist = NT.calculateBacktesting(symbollist,Strategylist)
  101.     #定義路徑
  102.     path = "C:\Project\BackTestResult.xlsx"
  103.     NT.toExcel(resultlist,path)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22259926/viewspace-2156480/,如需轉載,請註明出處,否則將追究法律責任。

相關文章