127 views
0

position = api.get_position(SYMBOL)时,我要确认position更新完毕(一定是最新持仓信息后)才到while True…执行,查遍所有文档未得到确切答案,请官方给个例子,谢谢!

例:
api = TqApi()
klines = api.get_kline_serial(SYMBOL, 60 * 60 * 24, N)
quote = api.get_quote(SYMBOL)
target_pos = TargetPosTask(api, SYMBOL)
position = api.get_position(SYMBOL)
# 这里我要确认position更新完毕才到while True…执行

# 写法1:
while api.wait_update():
print(position.float_profit_long , position.float_profit_short)
break
# 写法1存在多个待更新信息,不确定position是最新的

# 写法2:
deadline = time.time() + 1
while api.wait_update(deadline=deadline):
pass
# 可能已经更新了,但是不保险

# 写法3:
while True:
api.wait_update()
if api.is_position_ready(position)
break
# 有is_serial_ready,没有is_position_ready

while True:
api.wait_update()
if api.is_changing(klines.iloc[-1], “datetime”):
short_avg = ma(klines[“close”], SHORT)
long_avg = ma(klines[“close”], LONG)
if long_avg.iloc[-2] < short_avg.iloc[-2] and long_avg.iloc[-1] > short_avg.iloc[-1]:
target_pos.set_target_volume(-3)
if short_avg.iloc[-2] < long_avg.iloc[-2] and short_avg.iloc[-1] > long_avg.iloc[-1]:
target_pos.set_target_volume(3)

Answered question
0

可以在每次wait_update()之后调用is_changing()判断position中的相关字段是不是已经更新,

is_changing()文档:https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.api.html#tqsdk.api.TqApi.is_changing

Posted new comment

谢谢!

试过该方法,如果原来持仓为0,position将 永远 不会改变,程序一直卡在那里等待。
目前,以:
quote = api.get_quote(SYMBOL)
deadline = time.time() + 1
while api.wait_update(deadline=deadline):
pass
等待1秒缓解该问题。

You are viewing 1 out of 1 answers, click here to view all answers.