Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the excel output of VNPY batch policy backtest and statistical results?

Shulou Source: shulou.com Published: 2022-06-01 02:21:21 10月01日 Update

Today, the editor will take you to understand what the excel output of VNPY batch strategy backtest and statistical results is like. The knowledge points in this article are introduced in great detail. Friends who think it is helpful can browse the content of the article together with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "VNPY batch strategy test and excel output of statistical results".

During this period of time doing VNPY, it is found that it is mainly back-testing and strategy optimization, and then there are batch testing and parameter set effects to export excel analysis requirements. There is also a strategy to verify the effect of different varieties.

Then think about it, I wrote a BatchBackTest class, which is actually a very simple answer, which is to enter the variety queue and the policy queue, and then loop out the result and output it to the specified path excel. In addition, the policy is a tuple, which is a combination of a policy and the corresponding parameters.

The code is as follows. The way to use it is to put a py in Xinjiang under the vnpy/example/CtaBacktesting path and put it in the following code.

# encoding: UTF-8

From vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, MINUTE_DB_NAME

Import pandas as pd

From vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy

From vnpy.trader.app.ctaStrategy.strategy.strategyBollChannel import BollChannelStrategy

From vnpy.trader.app.ctaStrategy.strategy.strategyDoubleMa import DoubleMaStrategy

From vnpy.trader.app.ctaStrategy.strategy.strategyDualThrust import DualThrustStrategy

From vnpy.trader.app.ctaStrategy.strategy.strategyKingKeltner import KkStrategy

From vnpy.trader.app.ctaStrategy.strategy.strategyMultiSignal import MultiSignalStrategy

From vnpy.trader.app.ctaStrategy.strategy.strategyMultiTimeframe import MultiTimeframeStrategy

Class BatchBackTest (object):

Def _ init__ (self):

"

Def calculateBacktesting (self,symbollist,strategylist):

# enter the variety queue and policy queue, return the result resultlist, and add the variety name, policy name and policy parameters in order to output for easy retrieval

Resultlist = []

For symbol in symbollist:

For strategy in strategylist:

Result = self.runBacktesting (symbol,strategy)

# add variety name, policy name and policy parameters

Result ["Symbolname"] = str (symbol ["vtSymbol"])

Result ["strategyname"] = str (strategy [0])

Result ["strategysetting"] = str (strategy [1])

Resultlist.append (result)

Return resultlist

Def runBacktesting (self, symbol, strategy):

# write the test variety and parameters, and return the test data set including the test results

# create a policy object in the engine

# create a backtest engine

Engine = BacktestingEngine ()

# set the backtest mode of the engine to K line

Engine.setBacktestingMode (engine.BAR_MODE)

# set the data start date for back testing

Engine.setStartDate (symbol ["StartDate"])

Engine.setSlippage (symbol ["Slippage"]) # 1 Hop

Engine.setRate (symbol ["Rate"]) # Commission size

Engine.setSize (symbol ["Size"]) # contract size

Engine.setPriceTick (symbol ["Slippage"]) # minimum price change

Engine.setCapital (symbol ["Capital"])

# set the history database used

Engine.setDatabase (MINUTE_DB_NAME, symbol ["vtSymbol"])

# set policy. The first one in the policy tuple is the policy, and the second parameter

Engine.initStrategy (strategy [0], strategy [1])

Engine.runBacktesting ()

Df = engine.calculateDailyResult ()

Result = []

Dfp,result = engine.calculateDailyStatistics (df)

Engine.output (u' output statistics')

# engine.showDailyResult (dfp, result)

Return result

Def toExcel (self, resultlist, path = "C:\ data\ datframe.xlsx"):

# output excel according to input statistics queue and path. No new mode is provided here. If you want, you can change it.

# dft.to_csv (path,index=False,header=True, mode ='a')

SummayKey = resultlist [0] .keys ()

# summayValue = result.values ()

Dft = pd.DataFrame (columns=summayKey)

For result in resultlist:

New = pd.DataFrame (result, index= ["0"])

Dft = dft.append (new,ignore_index=True)

Dft.to_excel (path,index=False,header=True)

Print "output statistical results of back test to" + path "

If _ name__ = = "_ _ main__":

# create a variety queue. Here you can import it with json, and write it directly for ease of use.

Symbollist = [{

"vtSymbol": 'm1809'

"StartDate": "20180101"

"Slippage": 1

"Size": 10

"Rate": 2 / 10000

"Capital": 10000

}

{

"vtSymbol": 'rb0000'

"StartDate": "20180101"

"Slippage": 1

"Size": 10

"Rate": 2 / 10000

"Capital": 10000

}]

# the policy is defined here, and the policy parameter is empty first; the policy plus parameter is a tuple

Setting = {}

Strategylist = [(AtrRsiStrategy, setting)

(BollChannelStrategy, setting)

(DoubleMaStrategy, setting)

(DualThrustStrategy, setting)

(KkStrategy, setting)

(MultiSignalStrategy, setting)

(MultiTimeframeStrategy, setting)]

# here is the case of the same policy and different parameters, of course, there can be multiple policies and multiple parameter combinations

Strategylist2 = []

# Policy list

Settinglist = [

{'kdlimit': 40,' barmins': 9, 'cciWindow': 22}

{'kdlimit': 30,' barmins': 13, 'cciWindow': 20}]

# merge a tuple

If settinglist! = []:

For para1 in settinglist:

Strategylist2.append ((BollChannelStrategy, para1))

NT = BatchBackTest ()

Resultlist = NT.calculateBacktesting (symbollist,Strategylist)

# define the path

Path = "C:\ Project\ BackTestResult.xlsx"

NT.toExcel (resultlist,path)

Thank you for your reading, the above is the whole content of "VNPY batch strategy back test and excel output of statistical results", learn friends to hurry up to operate it. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!

Tags: Policies parameters output results varieties that is statistics queues data names paths engines friends different code content multiple size effects articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS Shulou Information MariaDB Huawei