订单1 【多开】50
订单2 【多平】50
-> 此时 多空双方的持仓都是0,long_td,short_td 都是50
def update_trade(self, trade: TradeData) -> None:
""""""
if trade.direction == Direction.LONG:
if trade.offset == Offset.OPEN:
self.long_td += trade.volume # 订单1 【多开】50 -> long_td = 50
elif trade.offset == Offset.CLOSETODAY:
self.short_td -= trade.volume
elif trade.offset == Offset.CLOSEYESTERDAY:
self.short_yd -= trade.volume
elif trade.offset == Offset.CLOSE:
if trade.exchange in [Exchange.SHFE, Exchange.INE]:
self.short_yd -= trade.volume
else:
self.short_td -= trade.volume # 订单2 【多平】50 -> short_td = 50
if self.short_td < 0:
self.short_yd += self.short_td
self.short_td = 0
此时,请求订单3 做空 50
-> 由于持仓是0,理论上应该做空开,convert_order_request_net 逻辑中,由于long_pos=50>0,转为了 空平,方向不一样了
def convert_order_request_net(self, req: OrderRequest) -> List[OrderRequest]:
""""""
if req.direction == Direction.LONG:
pos_available = self.short_pos - self.short_pos_frozen
td_available = self.short_td - self.short_td_frozen
yd_available = self.short_yd - self.short_yd_frozen
else:
pos_available = self.long_pos - self.long_pos_frozen # pos_available = long_pos = 50
td_available = self.long_td - self.long_td_frozen
yd_available = self.long_yd - self.long_yd_frozen
# Split close order to close today/yesterday for SHFE/INE exchange
if req.exchange in {Exchange.SHFE, Exchange.INE}:
# ... ...
# Just use close for other exchanges
else:
reqs = []
volume_left = req.volume
if pos_available:
close_volume = min(pos_available, volume_left)
volume_left -= pos_available
close_req = copy(req)
close_req.offset = Offset.CLOSE # 订单3 就变成了平? 是不是应该做空开?
close_req.volume = close_volume
reqs.append(close_req)
if volume_left > 0:
open_volume = volume_left
open_req = copy(req)
open_req.offset = Offset.OPEN
open_req.volume = open_volume
reqs.append(open_req)
return reqs
另外,系统中的Direction.LONG/SHOR 应该理解为多空还是买卖?