VNPY2.9版本,在脚本策略上跑上期所的品种,今开的仓位平仓时出现平昨仓位不足,查找原因后发现脚本策略没有自动处理平今平昨的功能,参考CTA策略模块添加了该功能,代码如下:
def send_order(
self,
vt_symbol: str,
price: float,
volume: float,
direction: Direction,
offset: Offset,
order_type: OrderType,
stop: bool = False,
lock: bool = False,
net: bool = False
):
""""""
contract: Optional[ContractData] = self.get_contract(vt_symbol)
if not contract:
self.write_log(f"委托失败,找不到合约:{vt_symbol}", vt_symbol)
return ""
# Round order price and volume to nearest incremental value
price = round_to(price, contract.pricetick)
volume = round_to(volume, contract.min_volume)
original_req: OrderRequest = OrderRequest(
symbol=contract.symbol,
exchange=contract.exchange,
direction=direction,
type=order_type,
volume=volume,
price=price,
offset=offset,
reference=APP_NAME
)
print(original_req)
# Convert with offset converter
req_list = self.offset_converter.convert_order_request(original_req, lock, net)
print(req_list)
# Send Orders
vt_orderids = []
for req in req_list:
vt_orderid = self.main_engine.send_order(req, contract.gateway_name)
# Check if sending order successful
if not vt_orderid:
continue
vt_orderids.append(vt_orderid)
self.offset_converter.update_order_request(req, vt_orderid)
return vt_orderids
#vt_orderid: str = self.main_engine.send_order(req, contract.gateway_name)
#return vt_orderid
打印出来之后,original_req有数据
OrderRequest(symbol='ru2209', exchange=<Exchange.SHFE: 'SHFE'>, direction=<Direction.SHORT: '空'>, type=<OrderType.LIMIT: '限价'>, volume=7.0, price=12825.0, offset=<Offset.CLOSE: '平'>, reference='ScriptTrader'),
但是转换之后的req_list为空[],请问一下这是什么原因?如何进行处理?谢谢!