From 30ab66928b0045ca3ffb69399c74142a165bd7e9 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 11 Nov 2018 14:29:04 +0800 Subject: [PATCH] =?UTF-8?q?[Mod]=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8csv?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=8A=A0=E8=BD=BD=E6=B5=B7?= =?UTF-8?q?=E9=BE=9F=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/TurtleStrategy/setting.csv | 5 ++++ examples/TurtleStrategy/turtleEngine.py | 28 ++++++++++++++++++----- examples/TurtleStrategy/turtleStrategy.py | 5 ++-- 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 examples/TurtleStrategy/setting.csv diff --git a/examples/TurtleStrategy/setting.csv b/examples/TurtleStrategy/setting.csv new file mode 100644 index 00000000..31d334f7 --- /dev/null +++ b/examples/TurtleStrategy/setting.csv @@ -0,0 +1,5 @@ +vtSymbol,size,priceTick,variableCommission,fixedCommission,slippage +IF99,300,0.2,0.00003,0,0.2 +I99,100,0.5,0.00006,0,0.5 +CU99,5,10,0.00005,0,10 +TA99,5,2,0,12,2 diff --git a/examples/TurtleStrategy/turtleEngine.py b/examples/TurtleStrategy/turtleEngine.py index 2455d72a..ff15d54b 100644 --- a/examples/TurtleStrategy/turtleEngine.py +++ b/examples/TurtleStrategy/turtleEngine.py @@ -1,5 +1,6 @@ # encoding: UTF-8 +from csv import DictReader from datetime import datetime from pymongo import MongoClient @@ -23,7 +24,14 @@ class BacktestingEngine(object): def __init__(self): """Constructor""" self.portfolio = None + + # 合约配置信息 self.vtSymbolList = [] + self.sizeDict = {} # 合约大小字典 + self.priceTickDict = {} # 最小价格变动字典 + self.variableCommissionDict = {} # 变动手续费字典 + self.fixedCommissionDict = {} # 固定手续费字典 + self.slippageDict = {} # 滑点成本字典 self.startDt = None self.endDt = None @@ -42,12 +50,20 @@ class BacktestingEngine(object): self.endDt = endDt #---------------------------------------------------------------------- - def initPortfolio(self, vtSymbolList): + def initPortfolio(self, filename): """""" - self.vtSymbolList = vtSymbolList - + with open(filename) as f: + r = DictReader(f) + for d in r: + self.vtSymbolList.append(d['vtSymbol']) + self.sizeDict[d['vtSymbol']] = int(d['size']) + self.priceTickDict[d['priceTick']] = float(d['priceTick']) + self.variableCommissionDict[d['vtSymbol']] = float(d['variableCommission']) + self.fixedCommissionDict[d['vtSymbol']] = float(d['fixedCommission']) + self.slippageDict[d['vtSymbol']] = float(d['slippage']) + self.portfolio = TurtlePortfolio(self) - self.portfolio.init(vtSymbolList) + self.portfolio.init(self.vtSymbolList) #---------------------------------------------------------------------- def loadData(self): @@ -111,8 +127,8 @@ class BacktestingEngine(object): def writeLog(self, content): """""" print '%s:%s' %(datetime.now().strftime('%H:%M:%S.%f'), content) - - + + ######################################################################## class TradeData(object): """""" diff --git a/examples/TurtleStrategy/turtleStrategy.py b/examples/TurtleStrategy/turtleStrategy.py index d33d6b08..0bbe14bd 100644 --- a/examples/TurtleStrategy/turtleStrategy.py +++ b/examples/TurtleStrategy/turtleStrategy.py @@ -7,8 +7,8 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT, from vnpy.trader.vtUtility import ArrayManager -MAX_PRODUCT_POS = 4 -MAX_DIRECTION_POS = 10 +MAX_PRODUCT_POS = 4 # 单品种最大持仓 +MAX_DIRECTION_POS = 10 # 单方向最大持仓 ######################################################################## @@ -156,6 +156,7 @@ class TurtleSignal(object): self.entryUp, self.entryDown = self.am.donchian(self.entryWindow) self.exitUp, self.exitDown = self.am.donchian(self.exitWindow) + # 有持仓后,ATR波动率和入场位等都不再变化 if not self.pos: self.atrVolatility = self.am.atr(self.atrWindow)