コード例 #1
0
 def get_position_by_direction(self, direction):
     if direction == Direction.LONG:
         return PositionData(
             symbol=self.symbol,
             volume=self.long_pos,
             exchange=EXCHANGE_MAPPING[self.exchange],
             direction=direction,
             pnl=self.long_pnl,
             price=self.long_price,
             frozen=self.long_pos_frozen,
             open_price=self.long_open_price,
             yd_volume=self.long_yd,
             float_pnl=self.long_stare_pnl,
         )
     elif direction == Direction.SHORT:
         return PositionData(
             symbol=self.symbol,
             volume=self.short_pos,
             exchange=EXCHANGE_MAPPING[self.exchange],
             direction=direction,
             pnl=self.short_pnl,
             price=self.short_price,
             frozen=self.short_pos_frozen,
             yd_volume=self.short_yd,
             float_pnl=self.short_stare_pnl,
             open_price=self.short_open_price,
         )
     return None
コード例 #2
0
 def get_all_position_objects(self):
     """ 返回PositionData格式的持仓数据 """
     pos = []
     for x in self.values():
         if len(x.local_symbol) == 0:
             continue
         if x.long_pos != 0:
             p = PositionData(symbol=x.symbol,
                              exchange=x.exchange,
                              direction=Direction.LONG,
                              volume=x.long_pos,
                              frozen=x.long_pos_frozen,
                              price=x.long_price,
                              pnl=x.long_pnl,
                              yd_volume=x.long_yd)
             pos.append(p)
         if x.short_pos != 0:
             p = PositionData(symbol=x.symbol,
                              exchange=x.exchange,
                              direction=Direction.SHORT,
                              volume=x.short_pos,
                              frozen=x.short_pos_frozen,
                              price=x.short_price,
                              pnl=x.short_pnl,
                              yd_volume=x.short_yd)
             pos.append(p)
     return pos
コード例 #3
0
ファイル: td_api.py プロジェクト: dengwen168/ctpbee
    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

        # 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"]

        # Get contract size (spread contract has no size value)
        size = symbol_size_map.get(position.symbol, 0)

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

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

        if position.direction == Direction.LONG:
            self.open_cost_dict[position.symbol]["long"] = data['OpenCost']
        elif position.direction == Direction.SHORT:
            self.open_cost_dict[position.symbol]["short"] = data['OpenCost']
        # Calculate average position price
        if position.volume and size:
            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.on_event(type=EVENT_POSITION, data=position)
            self.positions.clear()
コード例 #4
0
 def to_position(self):
     """ 返回为持仓 """
     try:
         return PositionData(symbol=self.local_symbol.split(".")[0],
                             exchange=self.exchange,
                             volume=self.volume,
                             price=self.price)
     except Exception:
         raise ValueError(f"本地维护符号有问题,请检查,当前符号为{self.local_symbol}")