Closed iguoke closed 5 years ago
价差的例子可以参考 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/t80.py 回测的话在创建 api 实例时传入 TqBacktest 就会进入回测模式 回测的例子可以参考 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/backtest.py
价差的例子可以参考 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/t80.py 回测的话在创建 api 实例时传入 TqBacktest 就会进入回测模式 回测的例子可以参考 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/backtest.py
可以回测价差策略么?我试了下好像不行,输出成交记录的时候报错,,,
错误是啥呢?贴一下 看看
错误是啥呢?贴一下 看看
==============================策略代码=======================================
author = 'chengzhi'
from tqsdk import TqApi, TqSim, TargetPosTask,TqBacktest from datetime import date
''' 价差回归 当近月-远月的价差大于200时做空近月,做多远月 当价差小于150时平仓 ''' api = TqApi(TqSim(), backtest=TqBacktest(start_dt=date(2018, 11, 1), end_dt=date(2018, 12, 1)))
klines = api.get_kline_serial("SHFE.rb1901", 560, data_length=15) klines2 = api.get_kline_serial("SHFE.rb1905", 560, data_length=15)
target_pos_near = TargetPosTask(api, "SHFE.rb1901")
target_pos_deferred = TargetPosTask(api, "SHFE.rb1905") while True: api.wait_update() if api.is_changing(klines): spread = klines.close[-1] - klines2.close[-1] print("当前价差:", spread) if spread > -400: print("目标持仓: 空近月,多远月")
target_pos_near.set_target_volume(-1)
target_pos_deferred.set_target_volume(1)
elif spread < -300:
print("目标持仓: 空仓")
target_pos_near.set_target_volume(0)
target_pos_deferred.set_target_volume(0)
==============================错误信息=========================================
Traceback (most recent call last):
File "t80.py", line 22, in
回测结束会抛出 BacktestFinished 例外 另外如果不掉用 api.close(), 在程序退出的时候 python 会报一大堆 "Task was destroyed but it is pending" 之类的错误 ,因此正确做法是参照 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/backtest.py 使用 with closing(api) 或者完全不管也可以,毕竟已经回测完了
回测结束会抛出 BacktestFinished 例外 另外如果不掉用 api.close(), 在程序退出的时候 python 会报一大堆 "Task was destroyed but it is pending" 之类的错误 ,因此正确做法是参照 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/backtest.py 使用 with closing(api) 或者完全不管也可以,毕竟已经回测完了 但是,回测结果是没有的,模拟交易成交记录没有,也看不到结果,
回测结束会抛出 BacktestFinished 例外 另外如果不掉用 api.close(), 在程序退出的时候 python 会报一大堆 "Task was destroyed but it is pending" 之类的错误 ,因此正确做法是参照 https://github.com/shinnytech/tqsdk-python/blob/master/tqsdk/demo/backtest.py 使用 with closing(api) 或者完全不管也可以,毕竟已经回测完了 但是,回测结果是没有的,模拟交易成交记录没有,也看不到结果,
额,现在已经可以了 =============一下是通过测试的一个策略==================
author = 'liujianping'
from tqsdk import TqApi, TqSim, TargetPosTask from datetime import date from contextlib import closing from tqsdk import TqApi, TqSim, TqBacktest, TargetPosTask import numpy as np from scipy.optimize import leastsq import statsmodels.formula.api as smf import matplotlib.pyplot as plt ''' 价差回归 当近月-远月的价差大于200时做空近月,做多远月 当价差小于150时平仓 ''' api = TqApi(TqSim(), backtest=TqBacktest(start_dt=date(2018, 9, 1), end_dt=date(2018, 12, 27))) klines = api.get_kline_serial("DCE.j1901", 560, data_length=60) klines2 = api.get_kline_serial("DCE.j1905", 560, data_length=60)
target_pos_near = TargetPosTask(api, "DCE.j1901")
target_pos_deferred = TargetPosTask(api, "DCE.j1905") spread=[] with closing(api): while True: api.wait_update() if api.is_changing(klines): spread.append(klines[-1]['close']-klines2[-1]['close']) if len(spread)==30: ma=sum(spread)/30 spread.remove(spread[0])
if spread[-1]>ma:
print("目标持仓: 空近月,多远月")
# 设置目标持仓为正数表示多头,负数表示空头,0表示空仓
target_pos_near.set_target_volume(-1)
target_pos_deferred.set_target_volume(1)
elif spread[-1] < ma:
print("目标持仓: 空仓")
target_pos_near.set_target_volume(0)
target_pos_deferred.set_target_volume(0)
请问怎样实现套利策略?比如rb1901和rb1905价差 怎样实现同时下单2个合约,并且回测