From 4c83b08315c76ef3ae2e17d4f3a8009976358718 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sat, 13 Apr 2019 08:44:43 +0800 Subject: [PATCH] [Fix]spread contract with no size info will cause no position update --- vnpy/gateway/ctp/ctp_gateway.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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)