From c9c5f420c1df7e548af51c31806b83f40489a7c0 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 2 Jun 2017 15:56:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0script=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9setup.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {FAQ => archive/FAQ}/vn.py FAQ 20150526.docx | Bin {FAQ => archive/FAQ}/vn.py FAQ 20150527.docx | Bin {FAQ => archive/FAQ}/vn.py FAQ 20150608.docx | Bin {FAQ => archive/FAQ}/vn.py FAQ 20151213.docx | Bin .../runCtaBacktesting}/IF0000_1min.csv | 0 examples/runCtaBacktesting/loadCsv.py | 13 +++ examples/runCtaBacktesting/runBacktesting.py | 42 ++++++++ examples/runCtaBacktesting/runOptimization.py | 52 +++++++++ examples/runVnTrader/VnTrader.bat | 1 + .../vtMain.py => examples/runVnTrader/run.py | 0 setup.py | 4 +- vnpy/__init__.py | 2 +- vnpy/trader/app/ctaStrategy/ctaBacktesting.py | 42 +------- vnpy/trader/app/ctaStrategy/ctaHistoryData.py | 99 ++++++++---------- vnpy/trader/app/ctaStrategy/ctaTemplate.py | 2 +- .../ctaStrategy/strategy/strategyAtrRsi.py | 53 ---------- .../strategy/strategyDualThrust.py | 54 ---------- .../strategy/strategyKingKeltner.py | 35 ------- .../gateway/cshshlpGateway/cshshlpGateway.py | 5 +- vnpy/trader/gateway/ctpGateway/ctpGateway.py | 9 +- .../gateway/femasGateway/femasGateway.py | 9 +- .../gateway/ksotpGateway/ksotpGateway.py | 9 +- vnpy/trader/gateway/ltsGateway/ltsGateway.py | 13 +-- .../trader/gateway/sgitGateway/sgitGateway.py | 9 +- vnpy/trader/ico/__init__.py | 0 vnpy/trader/uiBasicWidget.py | 2 +- vnpy/trader/vtEngine.py | 8 +- vnpy/trader/vtFunction.py | 12 +++ 28 files changed, 194 insertions(+), 281 deletions(-) rename {FAQ => archive/FAQ}/vn.py FAQ 20150526.docx (100%) rename {FAQ => archive/FAQ}/vn.py FAQ 20150527.docx (100%) rename {FAQ => archive/FAQ}/vn.py FAQ 20150608.docx (100%) rename {FAQ => archive/FAQ}/vn.py FAQ 20151213.docx (100%) rename {vnpy/trader/app/ctaStrategy => examples/runCtaBacktesting}/IF0000_1min.csv (100%) create mode 100644 examples/runCtaBacktesting/loadCsv.py create mode 100644 examples/runCtaBacktesting/runBacktesting.py create mode 100644 examples/runCtaBacktesting/runOptimization.py create mode 100644 examples/runVnTrader/VnTrader.bat rename vnpy/trader/vtMain.py => examples/runVnTrader/run.py (100%) create mode 100644 vnpy/trader/ico/__init__.py diff --git a/FAQ/vn.py FAQ 20150526.docx b/archive/FAQ/vn.py FAQ 20150526.docx similarity index 100% rename from FAQ/vn.py FAQ 20150526.docx rename to archive/FAQ/vn.py FAQ 20150526.docx diff --git a/FAQ/vn.py FAQ 20150527.docx b/archive/FAQ/vn.py FAQ 20150527.docx similarity index 100% rename from FAQ/vn.py FAQ 20150527.docx rename to archive/FAQ/vn.py FAQ 20150527.docx diff --git a/FAQ/vn.py FAQ 20150608.docx b/archive/FAQ/vn.py FAQ 20150608.docx similarity index 100% rename from FAQ/vn.py FAQ 20150608.docx rename to archive/FAQ/vn.py FAQ 20150608.docx diff --git a/FAQ/vn.py FAQ 20151213.docx b/archive/FAQ/vn.py FAQ 20151213.docx similarity index 100% rename from FAQ/vn.py FAQ 20151213.docx rename to archive/FAQ/vn.py FAQ 20151213.docx diff --git a/vnpy/trader/app/ctaStrategy/IF0000_1min.csv b/examples/runCtaBacktesting/IF0000_1min.csv similarity index 100% rename from vnpy/trader/app/ctaStrategy/IF0000_1min.csv rename to examples/runCtaBacktesting/IF0000_1min.csv diff --git a/examples/runCtaBacktesting/loadCsv.py b/examples/runCtaBacktesting/loadCsv.py new file mode 100644 index 00000000..5758a2ce --- /dev/null +++ b/examples/runCtaBacktesting/loadCsv.py @@ -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') + diff --git a/examples/runCtaBacktesting/runBacktesting.py b/examples/runCtaBacktesting/runBacktesting.py new file mode 100644 index 00000000..2e267754 --- /dev/null +++ b/examples/runCtaBacktesting/runBacktesting.py @@ -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() \ No newline at end of file diff --git a/examples/runCtaBacktesting/runOptimization.py b/examples/runCtaBacktesting/runOptimization.py new file mode 100644 index 00000000..ccd20e26 --- /dev/null +++ b/examples/runCtaBacktesting/runOptimization.py @@ -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) \ No newline at end of file diff --git a/examples/runVnTrader/VnTrader.bat b/examples/runVnTrader/VnTrader.bat new file mode 100644 index 00000000..c2b01439 --- /dev/null +++ b/examples/runVnTrader/VnTrader.bat @@ -0,0 +1 @@ +python run.py \ No newline at end of file diff --git a/vnpy/trader/vtMain.py b/examples/runVnTrader/run.py similarity index 100% rename from vnpy/trader/vtMain.py rename to examples/runVnTrader/run.py diff --git a/setup.py b/setup.py index 6298d61c..3ec57f27 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ def getSubpackages(name): setup( name='vnpy', version=vnpy.__version__, - description='A framework for dveloping quantitative trading strategy', + description='A framework for developing quantitative trading strategy', long_description = long_desc, author=vnpy.__author__, author_email='vn.py@foxmail.com', @@ -34,7 +34,7 @@ setup( 'Programming Language :: Python :: 2.7', 'License :: OSI Approved :: MIT License'], packages=getSubpackages('vnpy'), - package_data={'': ['*.json', '*.md', + package_data={'': ['*.json', '*.md', '*.ico', '*.dll', '*.lib', '*.so', '*.pyd', '*.dat', '*.ini', '*.pfx', '*.scc', '*.crt', '*.key']}, ) \ No newline at end of file diff --git a/vnpy/__init__.py b/vnpy/__init__.py index 80c6c22b..438e3a68 100644 --- a/vnpy/__init__.py +++ b/vnpy/__init__.py @@ -1,4 +1,4 @@ # encoding: UTF-8 -__version__ = '1.6.2' +__version__ = '1.6.2b' __author__ = u'用Python的交易员' \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/ctaBacktesting.py b/vnpy/trader/app/ctaStrategy/ctaBacktesting.py index 3c217507..62b07b9d 100644 --- a/vnpy/trader/app/ctaStrategy/ctaBacktesting.py +++ b/vnpy/trader/app/ctaStrategy/ctaBacktesting.py @@ -102,10 +102,12 @@ class BacktestingEngine(object): def setEndDate(self, endDate=''): """设置回测的结束日期""" self.endDate = endDate + if endDate: - self.dataEndDate= datetime.strptime(endDate, '%Y%m%d') + self.dataEndDate = datetime.strptime(endDate, '%Y%m%d') + # 若不修改时间则会导致不包含dataEndDate当天数据 - self.dataEndDate.replace(hour=23, minute=59) + self.dataEndDate = self.dataEndDate.replace(hour=23, minute=59) #---------------------------------------------------------------------- def setBacktestingMode(self, mode): @@ -947,40 +949,4 @@ def optimize(strategyClass, setting, targetName, except KeyError: targetValue = 0 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() - \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py index 2c6de21e..5366468d 100644 --- a/vnpy/trader/app/ctaStrategy/ctaHistoryData.py +++ b/vnpy/trader/app/ctaStrategy/ctaHistoryData.py @@ -16,7 +16,6 @@ import pymongo from vnpy.trader.vtGlobal import globalSetting from vnpy.trader.vtConstant import * from vnpy.trader.vtObject import VtBarData - from vnpy.trader.app.ctaStrategy.datayesClient import DatayesClient @@ -314,53 +313,53 @@ class HistoryDataEngine(object): #---------------------------------------------------------------------- def downloadEquityDailyBarts(self, symbol): - """ - 下载股票的日行情,symbol是股票代码 - """ - print u'开始下载%s日行情' %symbol + """ + 下载股票的日行情,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:] - # 查询数据库中已有数据的最后日期 - 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 + data = ts.get_k_data(symbol,start) + + if not data.empty: + # 创建datetime索引 + self.dbClient[DAILY_DB_NAME][symbol].ensure_index([('datetime', pymongo.ASCENDING)], + unique=True) - if last: - start = last['date'][:4]+'-'+last['date'][4:6]+'-'+last['date'][6:] + 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 - 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: - # 创建datetime索引 - self.dbClient[DAILY_DB_NAME][symbol].ensure_index([('datetime', pymongo.ASCENDING)], - 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 + print u'%s下载完成' %symbol + else: + print u'找不到合约%s' %symbol #---------------------------------------------------------------------- def loadMcCsv(fileName, dbName, symbol): @@ -432,15 +431,3 @@ def loadTdxCsv(fileName, dbName, symbol): 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') diff --git a/vnpy/trader/app/ctaStrategy/ctaTemplate.py b/vnpy/trader/app/ctaStrategy/ctaTemplate.py index 89083b66..f9cf9459 100644 --- a/vnpy/trader/app/ctaStrategy/ctaTemplate.py +++ b/vnpy/trader/app/ctaStrategy/ctaTemplate.py @@ -8,6 +8,7 @@ from vnpy.trader.vtConstant import * from vnpy.trader.app.ctaStrategy.ctaBase import * + ######################################################################## class CtaTemplate(object): """CTA策略模板""" @@ -172,7 +173,6 @@ class CtaTemplate(object): return self.ctaEngine.engineType - ######################################################################## class TargetPosTemplate(CtaTemplate): """ diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py b/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py index b607f4ed..3669a66b 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyAtrRsi.py @@ -237,56 +237,3 @@ class AtrRsiStrategy(CtaTemplate): # 发出状态更新事件 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) \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py b/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py index 9a8f279d..bc5b99b5 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyDualThrust.py @@ -221,57 +221,3 @@ class DualThrustStrategy(CtaTemplate): def onTrade(self, trade): # 发出状态更新事件 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) \ No newline at end of file diff --git a/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py b/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py index a5ce2a78..5681a56d 100644 --- a/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py +++ b/vnpy/trader/app/ctaStrategy/strategy/strategyKingKeltner.py @@ -278,38 +278,3 @@ class KkStrategy(CtaTemplate): # 将委托号记录到列表中 self.orderList.append(self.buyOrderID) 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() \ No newline at end of file diff --git a/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py b/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py index 917601d8..49a7968e 100644 --- a/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py +++ b/vnpy/trader/gateway/cshshlpGateway/cshshlpGateway.py @@ -8,6 +8,7 @@ from time import sleep from vnpy.api.cshshlp import CsHsHlp from vnpy.api.ctp import MdApi from vnpy.trader.vtGateway import * +from vnpy.trader.vtFunction import getTempPath # 接口常量 @@ -943,9 +944,7 @@ class CshshlpMdApi(MdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcMdApi(path) # 注册服务器地址 diff --git a/vnpy/trader/gateway/ctpGateway/ctpGateway.py b/vnpy/trader/gateway/ctpGateway/ctpGateway.py index 69025301..619eacea 100644 --- a/vnpy/trader/gateway/ctpGateway/ctpGateway.py +++ b/vnpy/trader/gateway/ctpGateway/ctpGateway.py @@ -15,6 +15,7 @@ from datetime import datetime from vnpy.api.ctp import MdApi, TdApi, defineDict from vnpy.trader.vtGateway import * +from vnpy.trader.vtFunction import getTempPath from vnpy.trader.gateway.ctpGateway.language import text from vnpy.trader.vtConstant import GATEWAYTYPE_FUTURES @@ -393,9 +394,7 @@ class CtpMdApi(MdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcMdApi(path) # 注册服务器地址 @@ -1321,9 +1320,7 @@ class CtpTdApi(TdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcTraderApi(path) # 设置数据同步模式为推送从今日开始所有数据 diff --git a/vnpy/trader/gateway/femasGateway/femasGateway.py b/vnpy/trader/gateway/femasGateway/femasGateway.py index f5fdcbc3..35051d3f 100644 --- a/vnpy/trader/gateway/femasGateway/femasGateway.py +++ b/vnpy/trader/gateway/femasGateway/femasGateway.py @@ -11,6 +11,7 @@ import os import json from vnpy.api.femas import MdApi, TdApi, defineDict +from vnpy.trader.vtFunction import getTempPath from vnpy.trader.vtGateway import * # 以下为一些VT类型和CTP类型的映射字典 @@ -360,9 +361,7 @@ class FemasMdApi(MdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcMdApi(path) # 订阅主题 @@ -443,9 +442,7 @@ class FemasTdApi(TdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcTraderApi(path) # 订阅主题 diff --git a/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py b/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py index eca058de..f6dd286b 100644 --- a/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py +++ b/vnpy/trader/gateway/ksotpGateway/ksotpGateway.py @@ -9,6 +9,7 @@ import os import json from vnpy.api.ksotp import MdApi, TdApi, defineDict +from vnpy.trader.vtFunction import getTempPath from vnpy.trader.vtGateway import * # 以下为一些VT类型和CTP类型的映射字典 @@ -353,9 +354,7 @@ class KsotpMdApi(MdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createOTPMdApi(path) # 注册服务器地址 @@ -1139,9 +1138,7 @@ class KsotpTdApi(TdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createOTPTraderApi(path) # 注册服务器地址 diff --git a/vnpy/trader/gateway/ltsGateway/ltsGateway.py b/vnpy/trader/gateway/ltsGateway/ltsGateway.py index 89882951..5616c5c5 100644 --- a/vnpy/trader/gateway/ltsGateway/ltsGateway.py +++ b/vnpy/trader/gateway/ltsGateway/ltsGateway.py @@ -8,6 +8,7 @@ import os import json from vnpy.api.lts import MdApi, QryApi, TdApi, defineDict +from vnpy.trader.vtFunction import getTempPath from vnpy.trader.vtGateway import * @@ -372,9 +373,7 @@ class LtsMdApi(MdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcMdApi(path) # 注册服务器地址 @@ -726,9 +725,7 @@ class LtsTdApi(TdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcTraderApi(path) # 设置数据同步模式为推送从今日开始所有数据 @@ -1186,9 +1183,7 @@ class LtsQryApi(QryApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcQueryApi(path) # 注册服务器地址 diff --git a/vnpy/trader/gateway/sgitGateway/sgitGateway.py b/vnpy/trader/gateway/sgitGateway/sgitGateway.py index 35129f74..4e009e10 100644 --- a/vnpy/trader/gateway/sgitGateway/sgitGateway.py +++ b/vnpy/trader/gateway/sgitGateway/sgitGateway.py @@ -14,6 +14,7 @@ from copy import copy from datetime import datetime from vnpy.api.sgit import MdApi, TdApi, defineDict +from vnpy.trader.vtFunction import getTempPath from vnpy.trader.vtGateway import * @@ -238,9 +239,7 @@ class SgitMdApi(MdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcMdApi(path) # 注册服务器地址 @@ -470,9 +469,7 @@ class SgitTdApi(TdApi): # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: # 创建C++环境中的API对象,这里传入的参数是需要用来保存.con文件的文件夹路径 - path = os.getcwd() + '/temp/' + self.gatewayName + '/' - if not os.path.exists(path): - os.makedirs(path) + path = getTempPath(self.gatewayName + '_') self.createFtdcTraderApi(path) # 设置数据同步模式为推送从今日开始所有数据 diff --git a/vnpy/trader/ico/__init__.py b/vnpy/trader/ico/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vnpy/trader/uiBasicWidget.py b/vnpy/trader/uiBasicWidget.py index 6ca40d98..0a793642 100644 --- a/vnpy/trader/uiBasicWidget.py +++ b/vnpy/trader/uiBasicWidget.py @@ -137,7 +137,7 @@ class BidCell(QtWidgets.QTableWidgetItem): ######################################################################## class AskCell(QtWidgets.QTableWidgetItem): - """买价单元格""" + """卖价单元格""" #---------------------------------------------------------------------- def __init__(self, text=None, mainEngine=None): diff --git a/vnpy/trader/vtEngine.py b/vnpy/trader/vtEngine.py index a9d79d6a..70b087e5 100644 --- a/vnpy/trader/vtEngine.py +++ b/vnpy/trader/vtEngine.py @@ -13,6 +13,7 @@ from vnpy.trader.vtGlobal import globalSetting from vnpy.trader.vtEvent import * from vnpy.trader.vtGateway import * from vnpy.trader.language import text +from vnpy.trader.vtFunction import getTempPath ######################################################################## @@ -278,8 +279,7 @@ class MainEngine(object): class DataEngine(object): """数据引擎""" contractFileName = 'ContractData.vt' - path = os.path.abspath(os.path.dirname(__file__)) - contractFileName = os.path.join(path, 'temp', contractFileName) + contractFilePath = getTempPath(contractFileName) #---------------------------------------------------------------------- def __init__(self, eventEngine): @@ -324,14 +324,14 @@ class DataEngine(object): #---------------------------------------------------------------------- def saveContracts(self): """保存所有合约对象到硬盘""" - f = shelve.open(self.contractFileName) + f = shelve.open(self.contractFilePath) f['data'] = self.contractDict f.close() #---------------------------------------------------------------------- def loadContracts(self): """从硬盘读取合约对象""" - f = shelve.open(self.contractFileName) + f = shelve.open(self.contractFilePath) if 'data' in f: d = f['data'] for key, value in d.items(): diff --git a/vnpy/trader/vtFunction.py b/vnpy/trader/vtFunction.py index 3a1d76d6..189d1d82 100644 --- a/vnpy/trader/vtFunction.py +++ b/vnpy/trader/vtFunction.py @@ -52,5 +52,17 @@ def loadIconPath(iconName): global iconPathDict 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 +