[Mod]改为使用csv配置文件加载海龟组合
This commit is contained in:
parent
d223c9fea1
commit
30ab66928b
5
examples/TurtleStrategy/setting.csv
Normal file
5
examples/TurtleStrategy/setting.csv
Normal 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,5 +1,6 @@
|
|||||||
# encoding: UTF-8
|
# encoding: UTF-8
|
||||||
|
|
||||||
|
from csv import DictReader
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
@ -23,7 +24,14 @@ class BacktestingEngine(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Constructor"""
|
"""Constructor"""
|
||||||
self.portfolio = None
|
self.portfolio = None
|
||||||
|
|
||||||
|
# 合约配置信息
|
||||||
self.vtSymbolList = []
|
self.vtSymbolList = []
|
||||||
|
self.sizeDict = {} # 合约大小字典
|
||||||
|
self.priceTickDict = {} # 最小价格变动字典
|
||||||
|
self.variableCommissionDict = {} # 变动手续费字典
|
||||||
|
self.fixedCommissionDict = {} # 固定手续费字典
|
||||||
|
self.slippageDict = {} # 滑点成本字典
|
||||||
|
|
||||||
self.startDt = None
|
self.startDt = None
|
||||||
self.endDt = None
|
self.endDt = None
|
||||||
@ -42,12 +50,20 @@ class BacktestingEngine(object):
|
|||||||
self.endDt = endDt
|
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 = TurtlePortfolio(self)
|
||||||
self.portfolio.init(vtSymbolList)
|
self.portfolio.init(self.vtSymbolList)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def loadData(self):
|
def loadData(self):
|
||||||
@ -111,8 +127,8 @@ class BacktestingEngine(object):
|
|||||||
def writeLog(self, content):
|
def writeLog(self, content):
|
||||||
""""""
|
""""""
|
||||||
print '%s:%s' %(datetime.now().strftime('%H:%M:%S.%f'), content)
|
print '%s:%s' %(datetime.now().strftime('%H:%M:%S.%f'), content)
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
class TradeData(object):
|
class TradeData(object):
|
||||||
""""""
|
""""""
|
||||||
|
@ -7,8 +7,8 @@ from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT,
|
|||||||
from vnpy.trader.vtUtility import ArrayManager
|
from vnpy.trader.vtUtility import ArrayManager
|
||||||
|
|
||||||
|
|
||||||
MAX_PRODUCT_POS = 4
|
MAX_PRODUCT_POS = 4 # 单品种最大持仓
|
||||||
MAX_DIRECTION_POS = 10
|
MAX_DIRECTION_POS = 10 # 单方向最大持仓
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
@ -156,6 +156,7 @@ class TurtleSignal(object):
|
|||||||
self.entryUp, self.entryDown = self.am.donchian(self.entryWindow)
|
self.entryUp, self.entryDown = self.am.donchian(self.entryWindow)
|
||||||
self.exitUp, self.exitDown = self.am.donchian(self.exitWindow)
|
self.exitUp, self.exitDown = self.am.donchian(self.exitWindow)
|
||||||
|
|
||||||
|
# 有持仓后,ATR波动率和入场位等都不再变化
|
||||||
if not self.pos:
|
if not self.pos:
|
||||||
self.atrVolatility = self.am.atr(self.atrWindow)
|
self.atrVolatility = self.am.atr(self.atrWindow)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user