[Mod]改为使用csv配置文件加载海龟组合

This commit is contained in:
vn.py 2018-11-11 14:29:04 +08:00
parent d223c9fea1
commit 30ab66928b
3 changed files with 30 additions and 8 deletions

View File

@ -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
1 vtSymbol size priceTick variableCommission fixedCommission slippage
2 IF99 300 0.2 0.00003 0 0.2
3 I99 100 0.5 0.00006 0 0.5
4 CU99 5 10 0.00005 0 10
5 TA99 5 2 0 12 2

View File

@ -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):
""""""

View File

@ -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)