626 浏览
0

TqSim() 账号,有双向持仓(多、空)各1手的情况下,使用挂价单平仓异常,一方显示正常报单,一方报错”平今仓手数不足”.

其他双向持仓(多>1手、空>1手)的情况下,正常。

#### 异常报错
# 开仓
sell_price = quote.bid_price1 
buy_price = quote.ask_price1
sell_order = api.insert_order(symbol=cont[0], direction="SELL", offset="OPEN",
                        limit_price=sell_price,  volume=1)       
buy_order = api.insert_order(symbol=cont[0], direction="BUY", offset="OPEN",
                        limit_price=buy_price,  volume=1)      
api.wait_update()
#平仓
sell_price = quote.ask_price1 + 20
buy_price = quote.bid_price1 - 20
sell_order = api.insert_order(symbol=cont[0], direction="SELL", offset="CLOSETODAY",
                        limit_price=sell_price,  volume=1)    
buy_order = api.insert_order(symbol=cont[0], direction="BUY", offset="CLOSETODAY",
                        limit_price=buy_price,  volume=1)   
api.wait_update()
 #### 正常
# 开仓
sell_price = quote.bid_price1 
buy_price = quote.ask_price1
sell_order = api.insert_order(symbol=cont[0], direction="SELL", offset="OPEN",
                        limit_price=sell_price,  volume=2)       
buy_order = api.insert_order(symbol=cont[0], direction="BUY", offset="OPEN",
                        limit_price=buy_price,  volume=2)      
api.wait_update()
# 平仓
sell_price = quote.ask_price1 + 20
buy_price = quote.bid_price1 - 20
sell_order = api.insert_order(symbol=cont[0], direction="SELL", offset="CLOSETODAY",
                        limit_price=sell_price,  volume=1)    
buy_order = api.insert_order(symbol=cont[0], direction="BUY", offset="CLOSETODAY",
                        limit_price=buy_price,  volume=1)   
api.wait_update()

李思恒 已回答的问题 2023年6月7日
0

下单不是立马全部成交的,是通过wait_update下任务的,就算是没有错,这个写法实盘问题也会非常大,还有wait_update写多个没有太大的意义,反而每次遇到wait_update都会等待1秒,可以参考下我们的文档里的示例架构,用while循环里一个wait_update就够了

aaron_qt 编辑评论 2023年6月25日

以上代码只是做一个报错复现的示例。
当下单成交后,position.pos_long = 1, position.pos_short = 1 时,同时下单双向平仓也是报一样的问题。position.pos_long = 2, position.pos_short = 2 时,同样的下单脚本就不会报错。