[Mod] complete test of mini_gateway

This commit is contained in:
vn.py 2019-07-16 12:31:01 +08:00
parent c7621cbfa3
commit cf7b89365a

View File

@ -469,50 +469,47 @@ class MiniTdApi(TdApi):
def onRspQryInvestorPosition(self, data: dict, error: dict, reqid: int, last: bool): def onRspQryInvestorPosition(self, data: dict, error: dict, reqid: int, last: bool):
"""""" """"""
if not data: if data:
print(data, error, reqid, last) # Get buffered position object
return 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_MINI2VT[data["PosiDirection"]],
gateway_name=self.gateway_name
)
self.positions[key] = position
# Get buffered position object # For SHFE position data update
key = f"{data['InstrumentID'], data['PosiDirection']}" if position.exchange == Exchange.SHFE:
position = self.positions.get(key, None) if data["YdPosition"] and not data["TodayPosition"]:
if not position: position.yd_volume = data["Position"]
position = PositionData( # For other exchange position data update
symbol=data["InstrumentID"], else:
exchange=symbol_exchange_map[data["InstrumentID"]], position.yd_volume = data["Position"] - data["TodayPosition"]
direction=DIRECTION_MINI2VT[data["PosiDirection"]],
gateway_name=self.gateway_name
)
self.positions[key] = position
# For SHFE position data update # Get contract size (spread contract has no size value)
if position.exchange == Exchange.SHFE: size = symbol_size_map.get(position.symbol, 0)
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) # Calculate previous position cost
size = symbol_size_map.get(position.symbol, 0) cost = position.price * position.volume * size
# Calculate previous position cost # Update new position volume
cost = position.price * position.volume * size position.volume += data["Position"]
position.pnl += data["PositionProfit"]
# Update new position volume # Calculate average position price
position.volume += data["Position"] if position.volume and size:
position.pnl += data["PositionProfit"] cost += data["PositionCost"]
position.price = cost / (position.volume * size)
# Calculate average position price # Get frozen volume
if position.volume and size: if position.direction == Direction.LONG:
cost += data["PositionCost"] position.frozen += data["ShortFrozen"]
position.price = cost / (position.volume * size) else:
position.frozen += data["LongFrozen"]
# Get frozen volume
if position.direction == Direction.LONG:
position.frozen += data["ShortFrozen"]
else:
position.frozen += data["LongFrozen"]
if last: if last:
for position in self.positions.values(): for position in self.positions.values():