可以给一个最小复现代码看看
在使用天勤量化之前,默认您已经知晓并同意以下免责条款,如果不同意请立即停止使用:https://www.shinnytech.com/blog/disclaimer/
多单开仓: 价格=69430.0,open_time1=14:30:00,API时间=2021-04-21 14:30:00
INFO – 模拟交易下单 TQSIM, PYSDK_target_111db03b83181727a8456d0cd015132c: 时间: 2021-04-21 22:30:00.000000, 合约: SHFE.cu2106, 开平: OPEN, 方向: BUY, 手数: 1, 价格: 69440.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_111db03b83181727a8456d0cd015132c: 全部成交
多单收盘盈不足0.5%平仓: 价格=69630.0,target_time1=14:55:00,API时间=2021-04-21 14:55:00
INFO – 模拟交易下单 TQSIM, PYSDK_target_fca9933acb617c53488b9a90d55e8bbf: 时间: 2021-04-21 22:55:00.000000, 合约: SHFE.cu2106, 开平: CLOSETODAY, 方向: SELL, 手数: 1, 价格: 69630.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_fca9933acb617c53488b9a90d55e8bbf: 全部成交
多单开仓: 价格=69200.0,open_time1=14:30:00,API时间=2021-04-22 14:30:00
INFO – 模拟交易下单 TQSIM, PYSDK_target_11c5af8ace34d26523d5f4c7fc6168a4: 时间: 2021-04-22 22:30:00.000000, 合约: SHFE.cu2106, 开平: OPEN, 方向: BUY, 手数: 1, 价格: 69210.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_11c5af8ace34d26523d5f4c7fc6168a4: 全部成交
多单收盘盈不足0.5%平仓: 价格=69340.0,target_time1=14:55:00,API时间=2021-04-22 14:55:00
INFO – 模拟交易下单 TQSIM, PYSDK_target_e4e45256d537d9d70fe47d2e4479cd35: 时间: 2021-04-22 22:55:00.000000, 合约: SHFE.cu2106, 开平: CLOSETODAY, 方向: SELL, 手数: 1, 价格: 69330.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_e4e45256d537d9d70fe47d2e4479cd35: 全部成交
多单开仓: 价格=69960.0,open_time1=14:30:00,API时间=2021-04-23 14:30:00
INFO – 模拟交易下单 TQSIM, PYSDK_target_43a3154f68ab13840a26696bdee37e11: 时间: 2021-04-23 22:30:00.000000, 合约: SHFE.cu2106, 开平: OPEN, 方向: BUY, 手数: 1, 价格: 69970.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_43a3154f68ab13840a26696bdee37e11: 全部成交
INFO – 模拟交易委托单 TQSIM, PYSDK_target_43a3154f68ab13840a26696bdee37e11: 全部成交
多单收盘盈不足0.5%平仓: 价格=69940.0,target_time1=14:55:00,API时间=2021-04-23 14:55:00
INFO – 模拟交易下单 TQSIM, PYSDK_target_7020b4c6aef18f12abaabc08dbd3ea03: 时间: 2021-04-23 22:55:00.000000, 合约: SHFE.cu2106, 开平: CLOSETODAY, 方向: SELL, 手数: 1, 价格: 69930.0
INFO – 模拟交易委托单 TQSIM, PYSDK_target_7020b4c6aef18f12abaabc08dbd3ea03: 全部成交
INFO – 回测结束
老师抽空看看是怎么回事?谢谢
还是时区问题,这个地方转换不对
current_datetime = pd.to_datetime(klines_1min.datetime.iloc[-1], unit=’ns’)
可以用官方函数time_to_str来转换
有其他问题也欢迎加入官方群聊611806823一起讨论
backtest_config1 = {
“start_dt”: datetime(2021,4,21,9,0,0),
“end_dt”: datetime(2021,4,25,15,0,0),
}
api = TqApi(account=TqSim(init_balance=100000),backtest=TqBacktest(start_dt=backtest_config1[“start_dt”],
end_dt=backtest_config1[“end_dt”]),
auth=TqAuth(“******”, “******”))
open_time1 = time(14,30)
open_time2 = time(14,31)
target_time1 = time(14,55)
target_time2 = time(14,59)
current_datetime = None
current_time = None
current_price = None
entry_price = None
current_position = 0
symbol_index = “KQ.m@SHFE.cu”
current_main = api.get_quote(symbol_index).underlying_symbol
klines_1min = api.get_kline_serial(symbol_index, 60 * 1)
target_pos = TargetPosTask(api, current_main)
try:
while True:
api.wait_update()
if api.is_changing(klines_1min.iloc[-1], “datetime”):
current_price = float(klines_1min.close.iloc[-1])
current_datetime = pd.to_datetime(klines_1min.datetime.iloc[-1], unit=’ns’)
current_time = current_datetime.time()
if open_time1 <= current_time < open_time2 and current_position == 0:
entry_price = current_price
current_position = 1
target_pos.set_target_volume(1)
print(f"多单开仓: 价格={current_price},open_time1={open_time1},API时间={current_datetime}")
elif target_time1 <= current_time < target_time2 and current_position == 1:
if current_price <= entry_price * (1 + 0.005):
target_pos.set_target_volume(0)
entry_price = None
current_position = 0
print(f"多单收盘盈不足0.5%平仓: 价格={current_price},target_time1={target_time1},API时间={current_datetime}")
except BacktestFinished as e:
print(f"策略运行异常: {e}")
api.close()