vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 22
声望: 0

持有隔夜仓,Vntrader加载后显示的开仓成本和期货公司终端显示的开仓成本不一致,Vntrader显示的开仓成本发生了变化。请问什么情况会出现这个问题,第一次碰到该现象?谢谢!

Administrator
avatar
加入于:
帖子: 1908
声望: 85

麻烦提供下数据截图?

Member
avatar
加入于:
帖子: 22
声望: 0

用Python的交易员 wrote:

麻烦提供下数据截图?

同一时刻的两个像是:一个是Vntrader端、一个是文华财经手机端

开仓成本价,当日开仓的没有问题,只要隔夜就不一样了!

description

description

Administrator
avatar
加入于:
帖子: 1908
声望: 85

vn.py中取的是你的持仓成本,也就是逐日盯市,收盘清算后的结果(前一日的结算价)

Member
avatar
加入于:
帖子: 22
声望: 0

用Python的交易员 wrote:

vn.py中取的是你的持仓成本,也就是逐日盯市,收盘清算后的结果(前一日的结算价)

VNPY 取开仓成本是不是会更合理一些 ; 或者自己可以修改这个字段取开仓成本吗,怎么修改 ?谢谢~

Administrator
avatar
加入于:
帖子: 163
声望: 21

在ctp_gateway.py的持仓查询回报函数可以修改插入字段和逻辑,下面是该函数

    def onRspQryInvestorPosition(self, data: dict, error: dict, reqid: int, last: bool):
        """"""
        if not data:
            return

        # Get buffered position object
        key = f"{data['InstrumentID'], data['PosiDirection']}"
        position = self.positions.get(key, None)
        if not position:
            position = PositionData(
                symbol=data["InstrumentID"],
                exchange=symbol_exchange_map[data["InstrumentID"]],
                direction=DIRECTION_CTP2VT[data["PosiDirection"]],
                gateway_name=self.gateway_name
            )
            self.positions[key] = position

        # Get contract size, return if size value not collected
        size = symbol_size_map.get(position.symbol, None)
        if not size:
            return

        # For SHFE position data update
        if position.exchange == Exchange.SHFE:
            if data["YdPosition"] and not data["TodayPosition"]:
                position.yd_volume = data["Position"]
        # For other exchange position data update
        else:
            position.yd_volume = data["Position"] - data["TodayPosition"]

        # Calculate previous position cost
        cost = position.price * position.volume * size

        # Update new position volume
        position.volume += data["Position"]
        position.pnl += data["PositionProfit"]

        # Calculate average position price
        if position.volume:
            cost += data["PositionCost"]
            position.price = cost / (position.volume * size)

        # Get frozen volume
        if position.direction == Direction.LONG:
            position.frozen += data["ShortFrozen"]
        else:
            position.frozen += data["LongFrozen"]

        if last:
            for position in self.positions.values():
                self.gateway.on_position(position)

            self.positions.clear()
Member
avatar
加入于:
帖子: 22
声望: 0

KeKe wrote:

在ctp_gateway.py的持仓查询回报函数可以修改插入字段和逻辑,下面是该函数

    def onRspQryInvestorPosition(self, data: dict, error: dict, reqid: int, last: bool):
        """"""
        if not data:
            return
        
        # Get buffered position object
        key = f"{data['InstrumentID'], data['PosiDirection']}"
        position = self.positions.get(key, None)
        if not position:
            position = PositionData(
                symbol=data["InstrumentID"],
                exchange=symbol_exchange_map[data["InstrumentID"]],
                direction=DIRECTION_CTP2VT[data["PosiDirection"]],
                gateway_name=self.gateway_name
            )
            self.positions[key] = position
        
        # Get contract size, return if size value not collected
        size = symbol_size_map.get(position.symbol, None)
        if not size:
            return
        
        # For SHFE position data update
        if position.exchange == Exchange.SHFE:
            if data["YdPosition"] and not data["TodayPosition"]:
                position.yd_volume = data["Position"]
        # For other exchange position data update
        else:
            position.yd_volume = data["Position"] - data["TodayPosition"]
        
        # Calculate previous position cost
        cost = position.price * position.volume * size
        
        # Update new position volume
        position.volume += data["Position"]
        position.pnl += data["PositionProfit"]
        
        # Calculate average position price
        if position.volume:
            cost += data["PositionCost"]
            position.price = cost / (position.volume * size)
        
        # Get frozen volume
        if position.direction == Direction.LONG:
            position.frozen += data["ShortFrozen"]
        else:
            position.frozen += data["LongFrozen"]
        
        if last:
            for position in self.positions.values():
                self.gateway.on_position(position)
                
            self.positions.clear()

谢谢~~

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