3.37K 浏览
0

代码1:

from datetime import date
import sys
import time
from tqsdk import TqApi, TqAccount, TargetPosTask, BacktestFinished, TqBacktest, TqSim, tafunc
 def test3():
    api = None
    ticks = None
    SYMBOL = "DCE.m2005"
    try:
        api = TqApi(backtest=TqBacktest(start_dt=date(2020, 3, 5), end_dt=date(2020, 3, 5)))
        # ticks = api.get_tick_serial(symbol=SYMBOL, data_length=8964)
        quote = api.get_quote(SYMBOL)
        klines_1min = api.get_kline_serial(symbol=SYMBOL, duration_seconds=60)
        pass
    except Exception as e:
        print("进入Exception")
        pass
     try:
        while True:
            sys.stdout.flush()
            api.wait_update()
             klines_1min_last_datetime = str(tafunc.time_to_datetime(klines_1min.iloc[-1]["datetime"]))
            print("quote.last_price:%f ask_price1:%f bid_price1:%f datetime:%s" % (quote.last_price, quote.ask_price1, quote.bid_price1, str(tafunc.time_to_datetime(quote.datetime))))
            print("close:%f kline_last_datetime:%s" % (klines_1min.iloc[-1]["close"], klines_1min_last_datetime))
            # print("ticks", ticks.iloc[-1].last_price, ticks.iloc[-1].ask_price1, ticks.iloc[-1].bid_price1, str(tafunc.time_to_datetime(ticks.iloc[-1]["datetime"])))
             if klines_1min_last_datetime.find("14:02:00") != -1:
                sys.exit()
            continue
     except BacktestFinished as e:
        api.close()
        print("进入BacktestFinished")
        pass
     return 123
 test3()

执行代码得到如下图结果中quote 在 14:00:00 这一根1min k线 开始的时候 都出现了异常。例如在14:00:00开始的quote的last_price 2725没有和ask_price1、bid_price1其中一个重合,其中卖一是2726,买一是2724。照理说在行情波动不剧烈波动的时候 不会出现这样两跳的缝隙,last_price应该与买一或卖一价重合的。检查发现全部quote都有这个问题。

代码2:

from datetime import date
import sys
import time
from tqsdk import TqApi, TqAccount, TargetPosTask, BacktestFinished, TqBacktest, TqSim, tafunc
 def test3():
    api = None
    ticks = None
    SYMBOL = "DCE.m2005"
    try:
        api = TqApi(backtest=TqBacktest(start_dt=date(2020, 3, 5), end_dt=date(2020, 3, 5)))
        ticks = api.get_tick_serial(symbol=SYMBOL, data_length=8964)
        quote = api.get_quote(SYMBOL)
        klines_1min = api.get_kline_serial(symbol=SYMBOL, duration_seconds=60)
        pass
    except Exception as e:
        print("进入Exception")
        pass
     try:
        while True:
            sys.stdout.flush()
            api.wait_update()
             klines_1min_last_datetime = str(tafunc.time_to_datetime(klines_1min.iloc[-1]["datetime"]))
            print("quote.last_price:%f ask_price1:%f bid_price1:%f datetime:%s" % (quote.last_price, quote.ask_price1, quote.bid_price1, str(tafunc.time_to_datetime(quote.datetime))))
            print("close:%f kline_last_datetime:%s" % (klines_1min.iloc[-1]["close"], klines_1min_last_datetime))
            print("ticks", ticks.iloc[-1].last_price, ticks.iloc[-1].ask_price1, ticks.iloc[-1].bid_price1, str(tafunc.time_to_datetime(ticks.iloc[-1]["datetime"])))
             if klines_1min_last_datetime.find("14:02:00") != -1:
                sys.exit()
            continue
     except BacktestFinished as e:
        api.close()
        print("进入BacktestFinished")
        pass
     return 123
 test3()

执行代码得到如下图结果中quote 在 14:00:00 这一根1min k线 开始的时候,tick和quote各个价格信息上都有出现 代码1结果中的 2724价格。

问题:

1,quote价格在是否使用tick数据后出现了不一致,是什么原因导致的?

2,ctp普通行情数据 交易所只会传递tick数据出来,quote应该就等于某一根tick数据吧?

west 已回答的问题 2020年3月25日
0

只要订阅了 tick, 则对应合约的 quote 就会使用 tick 生成, 更新频率也和 tick 一致

查看文档回测规则:

https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.backtest.html#tqsdk.backtest.TqBacktest

west 已回答的问题 2020年3月25日
您正在查看1个答案中的1个,单击此处查看所有答案。