vn.py官网
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 10
声望: 0

订单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 应该理解为多空还是买卖?

Member
avatar
加入于:
帖子: 2748
声望: 189

方向没变,开平变了,因为这是净持仓逻辑。不希望这样交易就net填false就好了。
buy:买入开仓(Direction:LONG,Offset:OPEN)

sell:卖出平仓(Direction:SHORT,Offset:CLOSE)

short:卖出开仓(Direction:SHORT,Offset:OPEN)

cover:买入平仓(Direction:LONG,Offset:CLOSE)

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号