diff --git a/vnpy/gateway/ctp/ctp_gateway.py b/vnpy/gateway/ctp/ctp_gateway.py index 057e6db8..6dd92450 100644 --- a/vnpy/gateway/ctp/ctp_gateway.py +++ b/vnpy/gateway/ctp/ctp_gateway.py @@ -487,11 +487,6 @@ class CtpTdApi(TdApi): ) 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"]: @@ -500,6 +495,9 @@ class CtpTdApi(TdApi): 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 @@ -508,7 +506,7 @@ class CtpTdApi(TdApi): position.pnl += data["PositionProfit"] # Calculate average position price - if position.volume: + if position.volume and size: cost += data["PositionCost"] position.price = cost / (position.volume * size)