[Add]增加海龟策略的测试用数据,初步完成海龟策略
This commit is contained in:
parent
846e677bbc
commit
0f212f96f9
@ -8,10 +8,7 @@ from dataService import *
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
#downloadMinuteBarBySymbol('IF1812')
|
downloadMinuteBarBySymbol('CU99')
|
||||||
#downloadAllMinuteBar()
|
|
||||||
#downloadMinuteBarBySymbol('CU99')
|
|
||||||
downloadDailyBarBySymbol('CU99')
|
|
||||||
downloadDailyBarBySymbol('IF99')
|
downloadDailyBarBySymbol('IF99')
|
||||||
downloadDailyBarBySymbol('TA99')
|
downloadDailyBarBySymbol('TA99')
|
||||||
downloadDailyBarBySymbol('I99')
|
downloadDailyBarBySymbol('I99')
|
1424
examples/TurtleStrategy/000300.csv
Normal file
1424
examples/TurtleStrategy/000300.csv
Normal file
File diff suppressed because it is too large
Load Diff
1424
examples/TurtleStrategy/000905.csv
Normal file
1424
examples/TurtleStrategy/000905.csv
Normal file
File diff suppressed because it is too large
Load Diff
15
examples/TurtleStrategy/README.MD
Normal file
15
examples/TurtleStrategy/README.MD
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
## 海龟交易策略(完整版)
|
||||||
|
|
||||||
|
使用说明:
|
||||||
|
1. 在cmd中运行python loadCsv.py,将000300.csv中的沪深300指数数据加载到MongoDB数据库中
|
||||||
|
2. 使用Jupyter Notebook打开run.ipynb,并执行相应的回测分析
|
||||||
|
|
||||||
|
开发进度
|
||||||
|
1. 海龟交易信号(负责判断具体的买卖位置):已完成
|
||||||
|
2. 海龟投资组合(负责根据信号执行具体交易):已完成
|
||||||
|
3. 回测引擎配置组合和信号:已完成
|
||||||
|
4. 回测引擎加载历史数据:已完成
|
||||||
|
5. 回测引擎回放历史数据:已完成
|
||||||
|
6. 回测引擎计算每日盈亏情况:已完成
|
||||||
|
7. 回测引擎统计整体回测结果和画图:未完成
|
||||||
|
|
37
examples/TurtleStrategy/loadCsv.py
Normal file
37
examples/TurtleStrategy/loadCsv.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
|
||||||
|
from csv import DictReader
|
||||||
|
from datetime import datetime
|
||||||
|
from pymongo import MongoClient
|
||||||
|
|
||||||
|
from vnpy.trader.vtObject import VtBarData
|
||||||
|
|
||||||
|
from turtleEngine import DAILY_DB_NAME
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def loadCsv(filename):
|
||||||
|
""""""
|
||||||
|
symbol = filename.split('.')[0]
|
||||||
|
|
||||||
|
mc = MongoClient()
|
||||||
|
db = mc[DAILY_DB_NAME]
|
||||||
|
collection = db[symbol]
|
||||||
|
|
||||||
|
with open(filename) as f:
|
||||||
|
r = DictReader(f)
|
||||||
|
for d in r:
|
||||||
|
bar = VtBarData()
|
||||||
|
bar.datetime = datetime.strptime(d['date'], '%Y/%m/%d')
|
||||||
|
bar.vtSymbol = symbol
|
||||||
|
bar.open = float(d['open'])
|
||||||
|
bar.high = float(d['high'])
|
||||||
|
bar.low = float(d['low'])
|
||||||
|
bar.close = float(d['close'])
|
||||||
|
bar.volume= int(d['volume'])
|
||||||
|
|
||||||
|
collection.insert(bar.__dict__)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
loadCsv('000300.csv')
|
||||||
|
loadCsv('000905.csv')
|
File diff suppressed because one or more lines are too long
3
examples/TurtleStrategy/test.csv
Normal file
3
examples/TurtleStrategy/test.csv
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
vtSymbol,size,priceTick,variableCommission,fixedCommission,slippage
|
||||||
|
000300,1,0.2,0,0,0
|
||||||
|
000905,1,0.1,0,0,0
|
|
@ -72,6 +72,9 @@ class BacktestingEngine(object):
|
|||||||
|
|
||||||
self.portfolio = TurtlePortfolio(self)
|
self.portfolio = TurtlePortfolio(self)
|
||||||
self.portfolio.init(portfolioValue, self.vtSymbolList, SIZE_DICT)
|
self.portfolio.init(portfolioValue, self.vtSymbolList, SIZE_DICT)
|
||||||
|
|
||||||
|
self.writeLog(u'投资组合的合约代码%s' %(self.vtSymbolList))
|
||||||
|
self.writeLog(u'投资组合的初始价值%s' %(portfolioValue))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def loadData(self):
|
def loadData(self):
|
||||||
@ -100,6 +103,8 @@ class BacktestingEngine(object):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def runBacktesting(self):
|
def runBacktesting(self):
|
||||||
""""""
|
""""""
|
||||||
|
self.writeLog(u'开始回放K线数据')
|
||||||
|
|
||||||
for dt, barDict in self.dataDict.items():
|
for dt, barDict in self.dataDict.items():
|
||||||
self.currentDt = dt
|
self.currentDt = dt
|
||||||
|
|
||||||
@ -115,12 +120,18 @@ class BacktestingEngine(object):
|
|||||||
|
|
||||||
self.resultList.append(result)
|
self.resultList.append(result)
|
||||||
self.result = result
|
self.result = result
|
||||||
|
|
||||||
|
self.writeLog(u'K线数据回放结束')
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def calculateResult(self):
|
def calculateResult(self):
|
||||||
""""""
|
""""""
|
||||||
|
self.writeLog(u'开始统计回测结果')
|
||||||
|
|
||||||
for result in self.resultList:
|
for result in self.resultList:
|
||||||
result.calculatePnl()
|
result.calculatePnl()
|
||||||
|
|
||||||
|
self.writeLog(u'回测结果统计结束')
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def sendOrder(self, vtSymbol, direction, offset, price, volume):
|
def sendOrder(self, vtSymbol, direction, offset, price, volume):
|
||||||
@ -139,7 +150,7 @@ class BacktestingEngine(object):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def writeLog(self, content):
|
def writeLog(self, content):
|
||||||
""""""
|
""""""
|
||||||
print '%s:%s' %(datetime.now().strftime('%H:%M:%S.%f'), content)
|
print '%s:\t%s' %(datetime.now().strftime('%H:%M:%S.%f'), content)
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
|
@ -75,7 +75,7 @@ class TurtleSignal(object):
|
|||||||
self.shortEntry4 = 0
|
self.shortEntry4 = 0
|
||||||
self.shortStop = 0 # 空头止损位
|
self.shortStop = 0 # 空头止损位
|
||||||
|
|
||||||
self.unit = 0 # 信号持仓
|
self.unit = 0 # 信号持仓
|
||||||
self.result = None # 当前的交易
|
self.result = None # 当前的交易
|
||||||
self.resultList = [] # 交易列表
|
self.resultList = [] # 交易列表
|
||||||
self.bar = None # 最新K线
|
self.bar = None # 最新K线
|
||||||
|
Loading…
Reference in New Issue
Block a user