vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 6
声望: 2
    def on_query_position(self, data, request):
        """"""
        if not data["holding"]:
            return

        for pos_data in data["holding"][0]:
            if int(pos_data["long_qty"]) > 0:
                pos = PositionData(
                    symbol=pos_data["instrument_id"].upper(),
                    exchange=Exchange.OKEX,
                    direction=Direction.LONG,
                    volume=int(pos_data["long_qty"]),
                    frozen=float(pos_data["long_qty"]) - float(pos_data["long_avail_qty"]),
                    price=float(pos_data["long_avg_cost"]),
                    pnl=float(pos_data.get('unrealized_pnl', 0)) + float(pos_data.get('realized_pnl', 0)),
                    gateway_name=self.gateway_name,
                )
                self.gateway.on_position(pos)

            if int(pos_data["short_qty"]) > 0:
                pos = PositionData(
                    symbol=pos_data["instrument_id"],
                    exchange=Exchange.OKEX,
                    direction=Direction.SHORT,
                    volume=int(pos_data["short_qty"]),
                    frozen=float(pos_data["short_qty"]) - float(pos_data["short_avail_qty"]),
                    price=float(pos_data["short_avg_cost"]),
                    pnl=float(pos_data.get('unrealized_pnl', 0)) + float(pos_data.get('realized_pnl', 0)),
                    gateway_name=self.gateway_name,
                )
                self.gateway.on_position(pos)

按照目前写法,只有long_qty或short_qty大于0时才会推送持仓事件。已有仓位平掉时,数值变为0,不会推送。这样在mainEngine中一直还保持着之前持仓的数据吧?我理解的对吗?

Member
avatar
加入于:
帖子: 214
声望: 7

应该是的,如果需要用函数获取仓位的话可能需要自己维护一下

Member
avatar
加入于:
帖子: 6
声望: 2

我直接改成这样了,试了一下感觉挺正常的

    def on_query_position(self, data, request):
        """"""
        if not data["holding"]:
            return

        for pos_data in data["holding"][0]:
            pos = PositionData(
                symbol=pos_data["instrument_id"].upper(),
                exchange=Exchange.OKEX,
                direction=Direction.LONG,
                volume=int(pos_data["long_qty"]),
                frozen=float(pos_data["long_qty"]) - float(pos_data["long_avail_qty"]),
                price=float(pos_data["long_avg_cost"]),
                pnl=float(pos_data.get('long_unrealized_pnl', 0)),
                gateway_name=self.gateway_name,
            )
            self.gateway.on_position(pos)

            pos = PositionData(
                symbol=pos_data["instrument_id"],
                exchange=Exchange.OKEX,
                direction=Direction.SHORT,
                volume=int(pos_data["short_qty"]),
                frozen=float(pos_data["short_qty"]) - float(pos_data["short_avail_qty"]),
                price=float(pos_data["short_avg_cost"]),
                pnl=float(pos_data.get('short_unrealized_pnl', 0)),
                gateway_name=self.gateway_name,
            )
            self.gateway.on_position(pos)
Administrator
avatar
加入于:
帖子: 4646
声望: 266

这应该是个BUG,我们来修复下

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

沪公网安备 31011502017034号