vnpy/vn.strategy/strategydemo/demoBacktesting.py

49 lines
1.1 KiB
Python
Raw Normal View History

2015-05-28 05:52:59 +00:00
# encoding: UTF-8
from strategyEngine import *
from backtestingEngine import *
from demoStrategy import SimpleEmaStrategy
2015-10-07 16:27:06 +00:00
import decimal
2015-05-28 05:52:59 +00:00
2015-10-17 16:23:24 +00:00
def main():
"""回测程序主函数"""
# symbol = 'IF1506'
2015-11-13 07:59:25 +00:00
symbol = 'au'
2015-10-17 16:23:24 +00:00
2015-05-28 05:52:59 +00:00
# 创建回测引擎
be = BacktestingEngine()
2015-10-17 16:23:24 +00:00
2015-05-28 05:52:59 +00:00
# 创建策略引擎对象
se = StrategyEngine(be.eventEngine, be, backtesting=True)
be.setStrategyEngine(se)
2015-10-17 16:23:24 +00:00
2015-05-28 05:52:59 +00:00
# 初始化回测引擎
2015-10-17 16:23:24 +00:00
# be.connectMongo()
2015-10-07 16:27:06 +00:00
be.connectMysql()
2015-10-17 16:23:24 +00:00
# be.loadMongoDataHistory(symbol, datetime(2015,5,1), datetime.today())
# be.loadMongoDataHistory(symbol, datetime(2012,1,9), datetime(2012,1,14))
2015-10-24 16:56:43 +00:00
2015-11-13 07:59:25 +00:00
be.setDataHistory(symbol, datetime(2015,7,3), datetime(2015,7,30))
2015-10-07 16:27:06 +00:00
2015-05-28 05:52:59 +00:00
# 创建策略对象
setting = {}
setting['fastAlpha'] = 0.2
2015-11-13 07:59:25 +00:00
setting['slowAlpha'] = 0.05
2015-10-17 16:23:24 +00:00
# setting['startDate'] = datetime(year=2015, month=5, day=20)
2015-11-13 07:59:25 +00:00
setting['startDate'] = datetime(year=2015, month=7, day=3)
2015-10-07 16:27:06 +00:00
2015-05-28 05:52:59 +00:00
se.createStrategy(u'EMA演示策略', symbol, SimpleEmaStrategy, setting)
2015-10-07 16:27:06 +00:00
2015-05-28 05:52:59 +00:00
# 启动所有策略
se.startAll()
2015-10-07 16:27:06 +00:00
2015-05-28 05:52:59 +00:00
# 开始回测
be.startBacktesting()
2015-10-07 16:27:06 +00:00
2015-05-28 05:52:59 +00:00
2015-10-17 16:23:24 +00:00
# 回测脚本
if __name__ == '__main__':
main()