Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
36 lines (32 sloc) 1.34 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chengzhi'
from datetime import date
from contextlib import closing
from tqsdk import TqApi, TqSim, TqBacktest, TargetPosTask
'''
如果当前价格大于5分钟K线的MA15则开多仓
如果小于则平仓
回测从 2018-05-01 到 2018-10-01
'''
# 在创建 api 实例时传入 TqBacktest 就会进入回测模式
api = TqApi(TqSim(), backtest=TqBacktest(start_dt=date(2018, 5, 1), end_dt=date(2018, 10, 1)))
# 获得 m1901 5分钟K线的引用
klines = api.get_kline_serial("DCE.m1901", 5*60, data_length=15)
# 创建 m1901 的目标持仓 task,该 task 负责调整 m1901 的仓位到指定的目标仓位
target_pos = TargetPosTask(api, "DCE.m1901")
# 使用with closing机制确保回测完成后释放对应的资源
with closing(api):
while True:
api.wait_update()
if api.is_changing(klines):
ma = sum(klines.close[-15:])/15
print("最新价", klines.close[-1], "MA", ma)
if klines.close[-1] > ma:
print("最新价大于MA: 目标多头5手")
# 设置目标持仓为多头5手
target_pos.set_target_volume(5)
elif klines.close[-1] < ma:
print("最新价小于MA: 目标空仓")
# 设置目标持仓为空仓
target_pos.set_target_volume(0)