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-10-23 17:12:08 +00:00
symbol = 'a'
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
be.setDataHistory(symbol, datetime(2012,1,1), datetime(2012,12,31))
2015-10-07 16:27:06 +00:00
2015-05-28 05:52:59 +00:00
# 创建策略对象
setting = {}
setting['fastAlpha'] = 0.2
2015-10-07 16:27:06 +00:00
setting['slowAlpha'] = 0.05
2015-10-17 16:23:24 +00:00
# setting['startDate'] = datetime(year=2015, month=5, day=20)
2015-10-23 17:12:08 +00:00
setting['startDate'] = datetime(year=2012, month=1, day=1)
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()