sync vnpy1.6.2
This commit is contained in:
parent
2a24888018
commit
5bda6088ba
File diff suppressed because one or more lines are too long
257
examples/CtaBacktesting/backtesting.ipynb
Normal file
257
examples/CtaBacktesting/backtesting.ipynb
Normal file
File diff suppressed because one or more lines are too long
13
examples/CtaBacktesting/loadCsv.py
Normal file
13
examples/CtaBacktesting/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/CtaBacktesting/runBacktesting.py
Normal file
42
examples/CtaBacktesting/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/CtaBacktesting/runOptimization.py
Normal file
52
examples/CtaBacktesting/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/VnTrader/VnTrader.bat
Normal file
1
examples/VnTrader/VnTrader.bat
Normal file
@ -0,0 +1 @@
|
||||
python run.py
|
58
examples/VnTrader/run.py
Normal file
58
examples/VnTrader/run.py
Normal file
@ -0,0 +1,58 @@
|
||||
# encoding: UTF-8
|
||||
|
||||
# 重载sys模块,设置默认字符串编码方式为utf8
|
||||
import sys
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf8')
|
||||
|
||||
# vn.trader模块
|
||||
from vnpy.event import EventEngine
|
||||
from vnpy.trader.vtEngine import MainEngine
|
||||
from vnpy.trader.uiQt import qApp
|
||||
from vnpy.trader.uiMainWindow import MainWindow
|
||||
|
||||
# 加载底层接口
|
||||
from vnpy.trader.gateway import (ctpGateway, femasGateway, xspeedGateway,
|
||||
sgitGateway, oandaGateway, ibGateway,
|
||||
shzdGateway, huobiGateway, okcoinGateway)
|
||||
|
||||
# 加载上层应用
|
||||
from vnpy.trader.app import (riskManager, dataRecorder,
|
||||
ctaStrategy)
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def main():
|
||||
"""主程序入口"""
|
||||
# 创建事件引擎
|
||||
ee = EventEngine()
|
||||
|
||||
# 创建主引擎
|
||||
me = MainEngine(ee)
|
||||
|
||||
# 添加交易接口
|
||||
me.addGateway(ctpGateway)
|
||||
me.addGateway(femasGateway)
|
||||
me.addGateway(xspeedGateway)
|
||||
me.addGateway(sgitGateway)
|
||||
me.addGateway(oandaGateway)
|
||||
me.addGateway(ibGateway)
|
||||
me.addGateway(shzdGateway)
|
||||
me.addGateway(huobiGateway)
|
||||
me.addGateway(okcoinGateway)
|
||||
|
||||
# 添加上层应用
|
||||
me.addApp(riskManager)
|
||||
me.addApp(dataRecorder)
|
||||
me.addApp(ctaStrategy)
|
||||
|
||||
# 创建主窗口
|
||||
mw = MainWindow(me, ee)
|
||||
mw.showMaximized()
|
||||
|
||||
# 在主线程中启动Qt事件循环
|
||||
sys.exit(qApp.exec_())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user