增加script目录,修改setup.py
This commit is contained in:
parent
2100318dd9
commit
c9c5f420c1
Can't render this file because it is too large.
|
13
examples/runCtaBacktesting/loadCsv.py
Normal file
13
examples/runCtaBacktesting/loadCsv.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
导入MC导出的CSV历史数据到MongoDB中
|
||||||
|
"""
|
||||||
|
|
||||||
|
from vnpy.trader.app.ctaStrategy.ctaBase import MINUTE_DB_NAME
|
||||||
|
from vnpy.trader.app.ctaStrategy.ctaHistoryData import loadMcCsv
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
loadMcCsv('IF0000_1min.csv', MINUTE_DB_NAME, 'IF0000')
|
||||||
|
|
42
examples/runCtaBacktesting/runBacktesting.py
Normal file
42
examples/runCtaBacktesting/runBacktesting.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
展示如何执行策略回测。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import division
|
||||||
|
|
||||||
|
|
||||||
|
from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, MINUTE_DB_NAME
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy
|
||||||
|
|
||||||
|
# 创建回测引擎
|
||||||
|
engine = BacktestingEngine()
|
||||||
|
|
||||||
|
# 设置引擎的回测模式为K线
|
||||||
|
engine.setBacktestingMode(engine.BAR_MODE)
|
||||||
|
|
||||||
|
# 设置回测用的数据起始日期
|
||||||
|
engine.setStartDate('20120101')
|
||||||
|
|
||||||
|
# 设置产品相关参数
|
||||||
|
engine.setSlippage(0.2) # 股指1跳
|
||||||
|
engine.setRate(0.3/10000) # 万0.3
|
||||||
|
engine.setSize(300) # 股指合约大小
|
||||||
|
engine.setPriceTick(0.2) # 股指最小价格变动
|
||||||
|
|
||||||
|
# 设置使用的历史数据库
|
||||||
|
engine.setDatabase(MINUTE_DB_NAME, 'IF0000')
|
||||||
|
|
||||||
|
# 在引擎中创建策略对象
|
||||||
|
d = {'atrLength': 11}
|
||||||
|
engine.initStrategy(AtrRsiStrategy, d)
|
||||||
|
|
||||||
|
# 开始跑回测
|
||||||
|
engine.runBacktesting()
|
||||||
|
|
||||||
|
# 显示回测结果
|
||||||
|
engine.showBacktestingResult()
|
52
examples/runCtaBacktesting/runOptimization.py
Normal file
52
examples/runCtaBacktesting/runOptimization.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# encoding: UTF-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
展示如何执行参数优化。
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import division
|
||||||
|
|
||||||
|
|
||||||
|
from vnpy.trader.app.ctaStrategy.ctaBacktesting import BacktestingEngine, MINUTE_DB_NAME, OptimizationSetting
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from vnpy.trader.app.ctaStrategy.strategy.strategyAtrRsi import AtrRsiStrategy
|
||||||
|
|
||||||
|
# 创建回测引擎
|
||||||
|
engine = BacktestingEngine()
|
||||||
|
|
||||||
|
# 设置引擎的回测模式为K线
|
||||||
|
engine.setBacktestingMode(engine.BAR_MODE)
|
||||||
|
|
||||||
|
# 设置回测用的数据起始日期
|
||||||
|
engine.setStartDate('20120101')
|
||||||
|
|
||||||
|
# 设置产品相关参数
|
||||||
|
engine.setSlippage(0.2) # 股指1跳
|
||||||
|
engine.setRate(0.3/10000) # 万0.3
|
||||||
|
engine.setSize(300) # 股指合约大小
|
||||||
|
engine.setPriceTick(0.2) # 股指最小价格变动
|
||||||
|
|
||||||
|
# 设置使用的历史数据库
|
||||||
|
engine.setDatabase(MINUTE_DB_NAME, 'IF0000')
|
||||||
|
|
||||||
|
# 跑优化
|
||||||
|
setting = OptimizationSetting() # 新建一个优化任务设置对象
|
||||||
|
setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利
|
||||||
|
setting.addParameter('atrLength', 12, 20, 2) # 增加第一个优化参数atrLength,起始12,结束20,步进2
|
||||||
|
setting.addParameter('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa,起始20,结束30,步进5
|
||||||
|
setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数
|
||||||
|
|
||||||
|
# 性能测试环境:I7-3770,主频3.4G, 8核心,内存16G,Windows 7 专业版
|
||||||
|
# 测试时还跑着一堆其他的程序,性能仅供参考
|
||||||
|
import time
|
||||||
|
start = time.time()
|
||||||
|
|
||||||
|
# 运行单进程优化函数,自动输出结果,耗时:359秒
|
||||||
|
#engine.runOptimization(AtrRsiStrategy, setting)
|
||||||
|
|
||||||
|
# 多进程优化,耗时:89秒
|
||||||
|
engine.runParallelOptimization(AtrRsiStrategy, setting)
|
||||||
|
|
||||||
|
print u'耗时:%s' %(time.time()-start)
|
1
examples/runVnTrader/VnTrader.bat
Normal file
1
examples/runVnTrader/VnTrader.bat
Normal file
@ -0,0 +1 @@
|
|||||||
|
python run.py
|
4
setup.py
4
setup.py
@ -23,7 +23,7 @@ def getSubpackages(name):
|
|||||||
setup(
|
setup(
|
||||||
name='vnpy',
|
name='vnpy',
|
||||||
version=vnpy.__version__,
|
version=vnpy.__version__,
|
||||||
description='A framework for dveloping quantitative trading strategy',
|
description='A framework for developing quantitative trading strategy',
|
||||||
long_description = long_desc,
|
long_description = long_desc,
|
||||||
author=vnpy.__author__,
|
author=vnpy.__author__,
|
||||||
author_email='vn.py@foxmail.com',
|
author_email='vn.py@foxmail.com',
|
||||||
@ -34,7 +34,7 @@ setup(
|
|||||||
'Programming Language :: Python :: 2.7',
|
'Programming Language :: Python :: 2.7',
|
||||||
'License :: OSI Approved :: MIT License'],
|
'License :: OSI Approved :: MIT License'],
|
||||||
packages=getSubpackages('vnpy'),
|
packages=getSubpackages('vnpy'),
|
||||||
package_data={'': ['*.json', '*.md',
|
package_data={'': ['*.json', '*.md', '*.ico',
|
||||||
'*.dll', '*.lib', '*.so', '*.pyd',
|
'*.dll', '*.lib', '*.so', '*.pyd',
|
||||||
'*.dat', '*.ini', '*.pfx', '*.scc', '*.crt', '*.key']},
|
'*.dat', '*.ini', '*.pfx', '*.scc', '*.crt', '*.key']},
|
||||||
)
|
)
|
@ -1,4 +1,4 @@
|
|||||||
# encoding: UTF-8
|
# encoding: UTF-8
|
||||||
|
|
||||||
__version__ = '1.6.2'
|
__version__ = '1.6.2b'
|
||||||
__author__ = u'用Python的交易员'
|
__author__ = u'用Python的交易员'
|
@ -102,10 +102,12 @@ class BacktestingEngine(object):
|
|||||||
def setEndDate(self, endDate=''):
|
def setEndDate(self, endDate=''):
|
||||||
"""设置回测的结束日期"""
|
"""设置回测的结束日期"""
|
||||||
self.endDate = endDate
|
self.endDate = endDate
|
||||||
|
|
||||||
if endDate:
|
if endDate:
|
||||||
self.dataEndDate= datetime.strptime(endDate, '%Y%m%d')
|
self.dataEndDate = datetime.strptime(endDate, '%Y%m%d')
|
||||||
|
|
||||||
# 若不修改时间则会导致不包含dataEndDate当天数据
|
# 若不修改时间则会导致不包含dataEndDate当天数据
|
||||||
self.dataEndDate.replace(hour=23, minute=59)
|
self.dataEndDate = self.dataEndDate.replace(hour=23, minute=59)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def setBacktestingMode(self, mode):
|
def setBacktestingMode(self, mode):
|
||||||
@ -947,40 +949,4 @@ def optimize(strategyClass, setting, targetName,
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
targetValue = 0
|
targetValue = 0
|
||||||
return (str(setting), targetValue)
|
return (str(setting), targetValue)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
# 以下内容是一段回测脚本的演示,用户可以根据自己的需求修改
|
|
||||||
# 建议使用ipython notebook或者spyder来做回测
|
|
||||||
# 同样可以在命令模式下进行回测(一行一行输入运行)
|
|
||||||
from strategy.strategyEmaDemo import *
|
|
||||||
|
|
||||||
# 创建回测引擎
|
|
||||||
engine = BacktestingEngine()
|
|
||||||
|
|
||||||
# 设置引擎的回测模式为K线
|
|
||||||
engine.setBacktestingMode(engine.BAR_MODE)
|
|
||||||
|
|
||||||
# 设置回测用的数据起始日期
|
|
||||||
engine.setStartDate('20110101')
|
|
||||||
|
|
||||||
# 载入历史数据到引擎中
|
|
||||||
engine.setDatabase(MINUTE_DB_NAME, 'IF0000')
|
|
||||||
|
|
||||||
# 设置产品相关参数
|
|
||||||
engine.setSlippage(0.2) # 股指1跳
|
|
||||||
engine.setRate(0.3/10000) # 万0.3
|
|
||||||
engine.setSize(300) # 股指合约大小
|
|
||||||
|
|
||||||
# 在引擎中创建策略对象
|
|
||||||
engine.initStrategy(EmaDemoStrategy, {})
|
|
||||||
|
|
||||||
# 开始跑回测
|
|
||||||
engine.runBacktesting()
|
|
||||||
|
|
||||||
# 显示回测结果
|
|
||||||
# spyder或者ipython notebook中运行时,会弹出盈亏曲线图
|
|
||||||
# 直接在cmd中回测则只会打印一些回测数值
|
|
||||||
engine.showBacktestingResult()
|
|
||||||
|
|
||||||
|
|
@ -16,7 +16,6 @@ import pymongo
|
|||||||
from vnpy.trader.vtGlobal import globalSetting
|
from vnpy.trader.vtGlobal import globalSetting
|
||||||
from vnpy.trader.vtConstant import *
|
from vnpy.trader.vtConstant import *
|
||||||
from vnpy.trader.vtObject import VtBarData
|
from vnpy.trader.vtObject import VtBarData
|
||||||
|
|
||||||
from vnpy.trader.app.ctaStrategy.datayesClient import DatayesClient
|
from vnpy.trader.app.ctaStrategy.datayesClient import DatayesClient
|
||||||
|
|
||||||
|
|
||||||
@ -314,53 +313,53 @@ class HistoryDataEngine(object):
|
|||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def downloadEquityDailyBarts(self, symbol):
|
def downloadEquityDailyBarts(self, symbol):
|
||||||
"""
|
"""
|
||||||
下载股票的日行情,symbol是股票代码
|
下载股票的日行情,symbol是股票代码
|
||||||
"""
|
"""
|
||||||
print u'开始下载%s日行情' %symbol
|
print u'开始下载%s日行情' %symbol
|
||||||
|
|
||||||
|
# 查询数据库中已有数据的最后日期
|
||||||
|
cl = self.dbClient[DAILY_DB_NAME][symbol]
|
||||||
|
cx = cl.find(sort=[('datetime', pymongo.DESCENDING)])
|
||||||
|
if cx.count():
|
||||||
|
last = cx[0]
|
||||||
|
else:
|
||||||
|
last = ''
|
||||||
|
# 开始下载数据
|
||||||
|
import tushare as ts
|
||||||
|
|
||||||
|
if last:
|
||||||
|
start = last['date'][:4]+'-'+last['date'][4:6]+'-'+last['date'][6:]
|
||||||
|
|
||||||
# 查询数据库中已有数据的最后日期
|
data = ts.get_k_data(symbol,start)
|
||||||
cl = self.dbClient[DAILY_DB_NAME][symbol]
|
|
||||||
cx = cl.find(sort=[('datetime', pymongo.DESCENDING)])
|
if not data.empty:
|
||||||
if cx.count():
|
# 创建datetime索引
|
||||||
last = cx[0]
|
self.dbClient[DAILY_DB_NAME][symbol].ensure_index([('datetime', pymongo.ASCENDING)],
|
||||||
else:
|
unique=True)
|
||||||
last = ''
|
|
||||||
# 开始下载数据
|
|
||||||
import tushare as ts
|
|
||||||
|
|
||||||
if last:
|
for index, d in data.iterrows():
|
||||||
start = last['date'][:4]+'-'+last['date'][4:6]+'-'+last['date'][6:]
|
bar = VtBarData()
|
||||||
|
bar.vtSymbol = symbol
|
||||||
|
bar.symbol = symbol
|
||||||
|
try:
|
||||||
|
bar.open = d.get('open')
|
||||||
|
bar.high = d.get('high')
|
||||||
|
bar.low = d.get('low')
|
||||||
|
bar.close = d.get('close')
|
||||||
|
bar.date = d.get('date').replace('-', '')
|
||||||
|
bar.time = ''
|
||||||
|
bar.datetime = datetime.strptime(bar.date, '%Y%m%d')
|
||||||
|
bar.volume = d.get('volume')
|
||||||
|
except KeyError:
|
||||||
|
print d
|
||||||
|
|
||||||
data = ts.get_k_data(symbol,start)
|
flt = {'datetime': bar.datetime}
|
||||||
|
self.dbClient[DAILY_DB_NAME][symbol].update_one(flt, {'$set':bar.__dict__}, upsert=True)
|
||||||
|
|
||||||
if not data.empty:
|
print u'%s下载完成' %symbol
|
||||||
# 创建datetime索引
|
else:
|
||||||
self.dbClient[DAILY_DB_NAME][symbol].ensure_index([('datetime', pymongo.ASCENDING)],
|
print u'找不到合约%s' %symbol
|
||||||
unique=True)
|
|
||||||
|
|
||||||
for index, d in data.iterrows():
|
|
||||||
bar = VtBarData()
|
|
||||||
bar.vtSymbol = symbol
|
|
||||||
bar.symbol = symbol
|
|
||||||
try:
|
|
||||||
bar.open = d.get('open')
|
|
||||||
bar.high = d.get('high')
|
|
||||||
bar.low = d.get('low')
|
|
||||||
bar.close = d.get('close')
|
|
||||||
bar.date = d.get('date').replace('-', '')
|
|
||||||
bar.time = ''
|
|
||||||
bar.datetime = datetime.strptime(bar.date, '%Y%m%d')
|
|
||||||
bar.volume = d.get('volume')
|
|
||||||
except KeyError:
|
|
||||||
print d
|
|
||||||
|
|
||||||
flt = {'datetime': bar.datetime}
|
|
||||||
self.dbClient[DAILY_DB_NAME][symbol].update_one(flt, {'$set':bar.__dict__}, upsert=True)
|
|
||||||
|
|
||||||
print u'%s下载完成' %symbol
|
|
||||||
else:
|
|
||||||
print u'找不到合约%s' %symbol
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def loadMcCsv(fileName, dbName, symbol):
|
def loadMcCsv(fileName, dbName, symbol):
|
||||||
@ -432,15 +431,3 @@ def loadTdxCsv(fileName, dbName, symbol):
|
|||||||
print u'插入完毕,耗时:%s' % (time()-start)
|
print u'插入完毕,耗时:%s' % (time()-start)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
## 简单的测试脚本可以写在这里
|
|
||||||
#from time import sleep
|
|
||||||
#e = HistoryDataEngine()
|
|
||||||
#sleep(1)
|
|
||||||
#e.downloadEquityDailyBar('000001')
|
|
||||||
#e.downloadEquityDailyBarts('000001')
|
|
||||||
|
|
||||||
# 这里将项目中包含的股指日内分钟线csv导入MongoDB,作者电脑耗时大约3分钟
|
|
||||||
loadMcCsv('IF0000_1min.csv', MINUTE_DB_NAME, 'IF0000')
|
|
||||||
#导入通达信历史分钟数据
|
|
||||||
#loadTdxCsv('CL8.csv', MINUTE_DB_NAME, 'c0000')
|
|
||||||
|
@ -8,6 +8,7 @@ from vnpy.trader.vtConstant import *
|
|||||||
|
|
||||||
from vnpy.trader.app.ctaStrategy.ctaBase import *
|
from vnpy.trader.app.ctaStrategy.ctaBase import *
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
class CtaTemplate(object):
|
class CtaTemplate(object):
|
||||||
"""CTA策略模板"""
|
"""CTA策略模板"""
|
||||||
@ -172,7 +173,6 @@ class CtaTemplate(object):
|
|||||||
return self.ctaEngine.engineType
|
return self.ctaEngine.engineType
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
class TargetPosTemplate(CtaTemplate):
|
class TargetPosTemplate(CtaTemplate):
|
||||||
"""
|
"""
|
||||||
|
@ -237,56 +237,3 @@ class AtrRsiStrategy(CtaTemplate):
|
|||||||
# 发出状态更新事件
|
# 发出状态更新事件
|
||||||
self.putEvent()
|
self.putEvent()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
# 提供直接双击回测的功能
|
|
||||||
# 导入PyQt4的包是为了保证matplotlib使用PyQt4而不是PySide,防止初始化出错
|
|
||||||
from vnpy.trader.app.ctaStrategy.ctaBacktesting import *
|
|
||||||
from PyQt4 import QtCore, QtGui
|
|
||||||
|
|
||||||
# 创建回测引擎
|
|
||||||
engine = BacktestingEngine()
|
|
||||||
|
|
||||||
# 设置引擎的回测模式为K线
|
|
||||||
engine.setBacktestingMode(engine.BAR_MODE)
|
|
||||||
|
|
||||||
# 设置回测用的数据起始日期
|
|
||||||
engine.setStartDate('20120101')
|
|
||||||
|
|
||||||
# 设置产品相关参数
|
|
||||||
engine.setSlippage(0.2) # 股指1跳
|
|
||||||
engine.setRate(0.3/10000) # 万0.3
|
|
||||||
engine.setSize(300) # 股指合约大小
|
|
||||||
engine.setPriceTick(0.2) # 股指最小价格变动
|
|
||||||
|
|
||||||
# 设置使用的历史数据库
|
|
||||||
engine.setDatabase(MINUTE_DB_NAME, 'IF0000')
|
|
||||||
|
|
||||||
# 在引擎中创建策略对象
|
|
||||||
d = {'atrLength': 11}
|
|
||||||
engine.initStrategy(AtrRsiStrategy, d)
|
|
||||||
|
|
||||||
# 开始跑回测
|
|
||||||
engine.runBacktesting()
|
|
||||||
|
|
||||||
# 显示回测结果
|
|
||||||
engine.showBacktestingResult()
|
|
||||||
|
|
||||||
## 跑优化
|
|
||||||
#setting = OptimizationSetting() # 新建一个优化任务设置对象
|
|
||||||
#setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利
|
|
||||||
#setting.addParameter('atrLength', 12, 20, 2) # 增加第一个优化参数atrLength,起始11,结束12,步进1
|
|
||||||
#setting.addParameter('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa,起始20,结束30,步进1
|
|
||||||
#setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数
|
|
||||||
|
|
||||||
## 性能测试环境:I7-3770,主频3.4G, 8核心,内存16G,Windows 7 专业版
|
|
||||||
## 测试时还跑着一堆其他的程序,性能仅供参考
|
|
||||||
#import time
|
|
||||||
#start = time.time()
|
|
||||||
|
|
||||||
## 运行单进程优化函数,自动输出结果,耗时:359秒
|
|
||||||
#engine.runOptimization(AtrRsiStrategy, setting)
|
|
||||||
|
|
||||||
## 多进程优化,耗时:89秒
|
|
||||||
##engine.runParallelOptimization(AtrRsiStrategy, setting)
|
|
||||||
|
|
||||||
#print u'耗时:%s' %(time.time()-start)
|
|
@ -221,57 +221,3 @@ class DualThrustStrategy(CtaTemplate):
|
|||||||
def onTrade(self, trade):
|
def onTrade(self, trade):
|
||||||
# 发出状态更新事件
|
# 发出状态更新事件
|
||||||
self.putEvent()
|
self.putEvent()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
# 提供直接双击回测的功能
|
|
||||||
# 导入PyQt4的包是为了保证matplotlib使用PyQt4而不是PySide,防止初始化出错
|
|
||||||
from ctaBacktesting import *
|
|
||||||
from PyQt4 import QtCore, QtGui
|
|
||||||
|
|
||||||
# 创建回测引擎
|
|
||||||
engine = BacktestingEngine()
|
|
||||||
|
|
||||||
# 设置引擎的回测模式为K线
|
|
||||||
engine.setBacktestingMode(engine.BAR_MODE)
|
|
||||||
|
|
||||||
# 设置回测用的数据起始日期
|
|
||||||
engine.setStartDate('20120101')
|
|
||||||
|
|
||||||
# 设置产品相关参数
|
|
||||||
engine.setSlippage(0.2) # 股指1跳
|
|
||||||
engine.setRate(0.3/10000) # 万0.3
|
|
||||||
engine.setSize(300) # 股指合约大小
|
|
||||||
engine.setPriceTick(0.2) # 股指最小价格变动
|
|
||||||
|
|
||||||
# 设置使用的历史数据库
|
|
||||||
engine.setDatabase(MINUTE_DB_NAME, 'IF0000')
|
|
||||||
|
|
||||||
# 在引擎中创建策略对象
|
|
||||||
engine.initStrategy(DualThrustStrategy, {})
|
|
||||||
|
|
||||||
# 开始跑回测
|
|
||||||
engine.runBacktesting()
|
|
||||||
|
|
||||||
# 显示回测结果
|
|
||||||
engine.showBacktestingResult()
|
|
||||||
|
|
||||||
## 跑优化
|
|
||||||
#setting = OptimizationSetting() # 新建一个优化任务设置对象
|
|
||||||
#setting.setOptimizeTarget('capital') # 设置优化排序的目标是策略净盈利
|
|
||||||
#setting.addParameter('atrLength', 12, 20, 2) # 增加第一个优化参数atrLength,起始11,结束12,步进1
|
|
||||||
#setting.addParameter('atrMa', 20, 30, 5) # 增加第二个优化参数atrMa,起始20,结束30,步进1
|
|
||||||
#setting.addParameter('rsiLength', 5) # 增加一个固定数值的参数
|
|
||||||
|
|
||||||
## 性能测试环境:I7-3770,主频3.4G, 8核心,内存16G,Windows 7 专业版
|
|
||||||
## 测试时还跑着一堆其他的程序,性能仅供参考
|
|
||||||
#import time
|
|
||||||
#start = time.time()
|
|
||||||
|
|
||||||
## 运行单进程优化函数,自动输出结果,耗时:359秒
|
|
||||||
#engine.runOptimization(AtrRsiStrategy, setting)
|
|
||||||
|
|
||||||
## 多进程优化,耗时:89秒
|
|
||||||
##engine.runParallelOptimization(AtrRsiStrategy, setting)
|
|
||||||
|
|
||||||
#print u'耗时:%s' %(time.time()-start)
|
|
@ -278,38 +278,3 @@ class KkStrategy(CtaTemplate):
|
|||||||
# 将委托号记录到列表中
|
# 将委托号记录到列表中
|
||||||
self.orderList.append(self.buyOrderID)
|
self.orderList.append(self.buyOrderID)
|
||||||
self.orderList.append(self.shortOrderID)
|
self.orderList.append(self.shortOrderID)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
# 提供直接双击回测的功能
|
|
||||||
# 导入PyQt4的包是为了保证matplotlib使用PyQt4而不是PySide,防止初始化出错
|
|
||||||
from ctaBacktesting import *
|
|
||||||
from PyQt4 import QtCore, QtGui
|
|
||||||
|
|
||||||
# 创建回测引擎
|
|
||||||
engine = BacktestingEngine()
|
|
||||||
|
|
||||||
# 设置引擎的回测模式为K线
|
|
||||||
engine.setBacktestingMode(engine.BAR_MODE)
|
|
||||||
|
|
||||||
# 设置回测用的数据起始日期
|
|
||||||
engine.setStartDate('20130101')
|
|
||||||
|
|
||||||
# 设置产品相关参数
|
|
||||||
engine.setSlippage(0.2) # 股指1跳
|
|
||||||
engine.setRate(0.3/10000) # 万0.3
|
|
||||||
engine.setSize(300) # 股指合约大小
|
|
||||||
engine.setPriceTick(0.2) # 股指最小价格变动
|
|
||||||
|
|
||||||
# 设置使用的历史数据库
|
|
||||||
engine.setDatabase(MINUTE_DB_NAME, 'IF0000')
|
|
||||||
|
|
||||||
# 在引擎中创建策略对象
|
|
||||||
d = {}
|
|
||||||
engine.initStrategy(KkStrategy, d)
|
|
||||||
|
|
||||||
# 开始跑回测
|
|
||||||
engine.runBacktesting()
|
|
||||||
|
|
||||||
# 显示回测结果
|
|
||||||
engine.showBacktestingResult()
|
|
@ -8,6 +8,7 @@ from time import sleep
|
|||||||
from vnpy.api.cshshlp import CsHsHlp
|
from vnpy.api.cshshlp import CsHsHlp
|
||||||
from vnpy.api.ctp import MdApi
|
from vnpy.api.ctp import MdApi
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
|
|
||||||
|
|
||||||
# 接口常量
|
# 接口常量
|
||||||
@ -943,9 +944,7 @@ class CshshlpMdApi(MdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcMdApi(path)
|
self.createFtdcMdApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
|
@ -15,6 +15,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
from vnpy.api.ctp import MdApi, TdApi, defineDict
|
from vnpy.api.ctp import MdApi, TdApi, defineDict
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
from vnpy.trader.gateway.ctpGateway.language import text
|
from vnpy.trader.gateway.ctpGateway.language import text
|
||||||
from vnpy.trader.vtConstant import GATEWAYTYPE_FUTURES
|
from vnpy.trader.vtConstant import GATEWAYTYPE_FUTURES
|
||||||
|
|
||||||
@ -393,9 +394,7 @@ class CtpMdApi(MdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcMdApi(path)
|
self.createFtdcMdApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
@ -1321,9 +1320,7 @@ class CtpTdApi(TdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcTraderApi(path)
|
self.createFtdcTraderApi(path)
|
||||||
|
|
||||||
# 设置数据同步模式为推送从今日开始所有数据
|
# 设置数据同步模式为推送从今日开始所有数据
|
||||||
|
@ -11,6 +11,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from vnpy.api.femas import MdApi, TdApi, defineDict
|
from vnpy.api.femas import MdApi, TdApi, defineDict
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
|
|
||||||
# 以下为一些VT类型和CTP类型的映射字典
|
# 以下为一些VT类型和CTP类型的映射字典
|
||||||
@ -360,9 +361,7 @@ class FemasMdApi(MdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcMdApi(path)
|
self.createFtdcMdApi(path)
|
||||||
|
|
||||||
# 订阅主题
|
# 订阅主题
|
||||||
@ -443,9 +442,7 @@ class FemasTdApi(TdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcTraderApi(path)
|
self.createFtdcTraderApi(path)
|
||||||
|
|
||||||
# 订阅主题
|
# 订阅主题
|
||||||
|
@ -9,6 +9,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from vnpy.api.ksotp import MdApi, TdApi, defineDict
|
from vnpy.api.ksotp import MdApi, TdApi, defineDict
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
|
|
||||||
# 以下为一些VT类型和CTP类型的映射字典
|
# 以下为一些VT类型和CTP类型的映射字典
|
||||||
@ -353,9 +354,7 @@ class KsotpMdApi(MdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createOTPMdApi(path)
|
self.createOTPMdApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
@ -1139,9 +1138,7 @@ class KsotpTdApi(TdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createOTPTraderApi(path)
|
self.createOTPTraderApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
|
@ -8,6 +8,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from vnpy.api.lts import MdApi, QryApi, TdApi, defineDict
|
from vnpy.api.lts import MdApi, QryApi, TdApi, defineDict
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
|
|
||||||
|
|
||||||
@ -372,9 +373,7 @@ class LtsMdApi(MdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcMdApi(path)
|
self.createFtdcMdApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
@ -726,9 +725,7 @@ class LtsTdApi(TdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcTraderApi(path)
|
self.createFtdcTraderApi(path)
|
||||||
|
|
||||||
# 设置数据同步模式为推送从今日开始所有数据
|
# 设置数据同步模式为推送从今日开始所有数据
|
||||||
@ -1186,9 +1183,7 @@ class LtsQryApi(QryApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcQueryApi(path)
|
self.createFtdcQueryApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
|
@ -14,6 +14,7 @@ from copy import copy
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from vnpy.api.sgit import MdApi, TdApi, defineDict
|
from vnpy.api.sgit import MdApi, TdApi, defineDict
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
|
|
||||||
|
|
||||||
@ -238,9 +239,7 @@ class SgitMdApi(MdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcMdApi(path)
|
self.createFtdcMdApi(path)
|
||||||
|
|
||||||
# 注册服务器地址
|
# 注册服务器地址
|
||||||
@ -470,9 +469,7 @@ class SgitTdApi(TdApi):
|
|||||||
# 如果尚未建立服务器连接,则进行连接
|
# 如果尚未建立服务器连接,则进行连接
|
||||||
if not self.connectionStatus:
|
if not self.connectionStatus:
|
||||||
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
# 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径
|
||||||
path = os.getcwd() + '/temp/' + self.gatewayName + '/'
|
path = getTempPath(self.gatewayName + '_')
|
||||||
if not os.path.exists(path):
|
|
||||||
os.makedirs(path)
|
|
||||||
self.createFtdcTraderApi(path)
|
self.createFtdcTraderApi(path)
|
||||||
|
|
||||||
# 设置数据同步模式为推送从今日开始所有数据
|
# 设置数据同步模式为推送从今日开始所有数据
|
||||||
|
0
vnpy/trader/ico/__init__.py
Normal file
0
vnpy/trader/ico/__init__.py
Normal file
@ -137,7 +137,7 @@ class BidCell(QtWidgets.QTableWidgetItem):
|
|||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
class AskCell(QtWidgets.QTableWidgetItem):
|
class AskCell(QtWidgets.QTableWidgetItem):
|
||||||
"""买价单元格"""
|
"""卖价单元格"""
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __init__(self, text=None, mainEngine=None):
|
def __init__(self, text=None, mainEngine=None):
|
||||||
|
@ -13,6 +13,7 @@ from vnpy.trader.vtGlobal import globalSetting
|
|||||||
from vnpy.trader.vtEvent import *
|
from vnpy.trader.vtEvent import *
|
||||||
from vnpy.trader.vtGateway import *
|
from vnpy.trader.vtGateway import *
|
||||||
from vnpy.trader.language import text
|
from vnpy.trader.language import text
|
||||||
|
from vnpy.trader.vtFunction import getTempPath
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
@ -278,8 +279,7 @@ class MainEngine(object):
|
|||||||
class DataEngine(object):
|
class DataEngine(object):
|
||||||
"""数据引擎"""
|
"""数据引擎"""
|
||||||
contractFileName = 'ContractData.vt'
|
contractFileName = 'ContractData.vt'
|
||||||
path = os.path.abspath(os.path.dirname(__file__))
|
contractFilePath = getTempPath(contractFileName)
|
||||||
contractFileName = os.path.join(path, 'temp', contractFileName)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def __init__(self, eventEngine):
|
def __init__(self, eventEngine):
|
||||||
@ -324,14 +324,14 @@ class DataEngine(object):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def saveContracts(self):
|
def saveContracts(self):
|
||||||
"""保存所有合约对象到硬盘"""
|
"""保存所有合约对象到硬盘"""
|
||||||
f = shelve.open(self.contractFileName)
|
f = shelve.open(self.contractFilePath)
|
||||||
f['data'] = self.contractDict
|
f['data'] = self.contractDict
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def loadContracts(self):
|
def loadContracts(self):
|
||||||
"""从硬盘读取合约对象"""
|
"""从硬盘读取合约对象"""
|
||||||
f = shelve.open(self.contractFileName)
|
f = shelve.open(self.contractFilePath)
|
||||||
if 'data' in f:
|
if 'data' in f:
|
||||||
d = f['data']
|
d = f['data']
|
||||||
for key, value in d.items():
|
for key, value in d.items():
|
||||||
|
@ -52,5 +52,17 @@ def loadIconPath(iconName):
|
|||||||
global iconPathDict
|
global iconPathDict
|
||||||
return iconPathDict.get(iconName, '')
|
return iconPathDict.get(iconName, '')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
def getTempPath(name):
|
||||||
|
"""获取存放临时文件的路径"""
|
||||||
|
tempPath = os.path.join(os.getcwd(), 'temp')
|
||||||
|
if not os.path.exists(tempPath):
|
||||||
|
os.makedirs(tempPath)
|
||||||
|
|
||||||
|
path = os.path.join(tempPath, name)
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user