This commit is contained in:
vn.py 2019-09-20 11:37:32 +08:00
parent bb03627627
commit 846ea95093
5 changed files with 76 additions and 72 deletions

View File

@ -8,7 +8,7 @@ from vnpy.trader.ui import MainWindow, create_qapp
from vnpy.gateway.bitmex import BitmexGateway from vnpy.gateway.bitmex import BitmexGateway
# from vnpy.gateway.futu import FutuGateway # from vnpy.gateway.futu import FutuGateway
# from vnpy.gateway.ib import IbGateway # from vnpy.gateway.ib import IbGateway
# from vnpy.gateway.ctp import CtpGateway from vnpy.gateway.ctp import CtpGateway
# from vnpy.gateway.ctptest import CtptestGateway # from vnpy.gateway.ctptest import CtptestGateway
# from vnpy.gateway.mini import MiniGateway # from vnpy.gateway.mini import MiniGateway
# from vnpy.gateway.sopt import SoptGateway # from vnpy.gateway.sopt import SoptGateway
@ -50,7 +50,7 @@ def main():
main_engine = MainEngine(event_engine) main_engine = MainEngine(event_engine)
# main_engine.add_gateway(BinanceGateway) # main_engine.add_gateway(BinanceGateway)
# main_engine.add_gateway(CtpGateway) main_engine.add_gateway(CtpGateway)
# main_engine.add_gateway(CtptestGateway) # main_engine.add_gateway(CtptestGateway)
# main_engine.add_gateway(MiniGateway) # main_engine.add_gateway(MiniGateway)
# main_engine.add_gateway(SoptGateway) # main_engine.add_gateway(SoptGateway)

View File

@ -479,46 +479,48 @@ class CtpTdApi(TdApi):
if not data: if not data:
return return
# Get buffered position object # Check if contract data received
key = f"{data['InstrumentID'], data['PosiDirection']}" if data["InstrumentID"] in symbol_exchange_map:
position = self.positions.get(key, None) # Get buffered position object
if not position: key = f"{data['InstrumentID'], data['PosiDirection']}"
position = PositionData( position = self.positions.get(key, None)
symbol=data["InstrumentID"], if not position:
exchange=symbol_exchange_map[data["InstrumentID"]], position = PositionData(
direction=DIRECTION_CTP2VT[data["PosiDirection"]], symbol=data["InstrumentID"],
gateway_name=self.gateway_name exchange=symbol_exchange_map[data["InstrumentID"]],
) direction=DIRECTION_CTP2VT[data["PosiDirection"]],
self.positions[key] = position gateway_name=self.gateway_name
)
self.positions[key] = position
# For SHFE position data update # For SHFE position data update
if position.exchange == Exchange.SHFE: if position.exchange == Exchange.SHFE:
if data["YdPosition"] and not data["TodayPosition"]: if data["YdPosition"] and not data["TodayPosition"]:
position.yd_volume = data["Position"] position.yd_volume = data["Position"]
# For other exchange position data update # For other exchange position data update
else: else:
position.yd_volume = data["Position"] - data["TodayPosition"] position.yd_volume = data["Position"] - data["TodayPosition"]
# Get contract size (spread contract has no size value) # Get contract size (spread contract has no size value)
size = symbol_size_map.get(position.symbol, 0) size = symbol_size_map.get(position.symbol, 0)
# Calculate previous position cost # Calculate previous position cost
cost = position.price * position.volume * size cost = position.price * position.volume * size
# Update new position volume # Update new position volume
position.volume += data["Position"] position.volume += data["Position"]
position.pnl += data["PositionProfit"] position.pnl += data["PositionProfit"]
# Calculate average position price # Calculate average position price
if position.volume and size: if position.volume and size:
cost += data["PositionCost"] cost += data["PositionCost"]
position.price = cost / (position.volume * size) position.price = cost / (position.volume * size)
# Get frozen volume # Get frozen volume
if position.direction == Direction.LONG: if position.direction == Direction.LONG:
position.frozen += data["ShortFrozen"] position.frozen += data["ShortFrozen"]
else: else:
position.frozen += data["LongFrozen"] position.frozen += data["LongFrozen"]
if last: if last:
for position in self.positions.values(): for position in self.positions.values():

View File

@ -478,46 +478,48 @@ class CtpTdApi(TdApi):
if not data: if not data:
return return
# Get buffered position object # Check if contract data received
key = f"{data['InstrumentID'], data['PosiDirection']}" if data["InstrumentID"] in symbol_exchange_map:
position = self.positions.get(key, None) # Get buffered position object
if not position: key = f"{data['InstrumentID'], data['PosiDirection']}"
position = PositionData( position = self.positions.get(key, None)
symbol=data["InstrumentID"], if not position:
exchange=symbol_exchange_map[data["InstrumentID"]], position = PositionData(
direction=DIRECTION_CTP2VT[data["PosiDirection"]], symbol=data["InstrumentID"],
gateway_name=self.gateway_name exchange=symbol_exchange_map[data["InstrumentID"]],
) direction=DIRECTION_CTP2VT[data["PosiDirection"]],
self.positions[key] = position gateway_name=self.gateway_name
)
self.positions[key] = position
# For SHFE position data update # For SHFE position data update
if position.exchange == Exchange.SHFE: if position.exchange == Exchange.SHFE:
if data["YdPosition"] and not data["TodayPosition"]: if data["YdPosition"] and not data["TodayPosition"]:
position.yd_volume = data["Position"] position.yd_volume = data["Position"]
# For other exchange position data update # For other exchange position data update
else: else:
position.yd_volume = data["Position"] - data["TodayPosition"] position.yd_volume = data["Position"] - data["TodayPosition"]
# Get contract size (spread contract has no size value) # Get contract size (spread contract has no size value)
size = symbol_size_map.get(position.symbol, 0) size = symbol_size_map.get(position.symbol, 0)
# Calculate previous position cost # Calculate previous position cost
cost = position.price * position.volume * size cost = position.price * position.volume * size
# Update new position volume # Update new position volume
position.volume += data["Position"] position.volume += data["Position"]
position.pnl += data["PositionProfit"] position.pnl += data["PositionProfit"]
# Calculate average position price # Calculate average position price
if position.volume and size: if position.volume and size:
cost += data["PositionCost"] cost += data["PositionCost"]
position.price = cost / (position.volume * size) position.price = cost / (position.volume * size)
# Get frozen volume # Get frozen volume
if position.direction == Direction.LONG: if position.direction == Direction.LONG:
position.frozen += data["ShortFrozen"] position.frozen += data["ShortFrozen"]
else: else:
position.frozen += data["LongFrozen"] position.frozen += data["LongFrozen"]
if last: if last:
for position in self.positions.values(): for position in self.positions.values():

View File

@ -491,7 +491,7 @@ 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 data: if data and data["InstrumentID"] in symbol_exchange_map:
# Get buffered position object # Get buffered position object
key = f"{data['InstrumentID'], data['PosiDirection']}" key = f"{data['InstrumentID'], data['PosiDirection']}"
position = self.positions.get(key, None) position = self.positions.get(key, None)

View File

@ -491,7 +491,7 @@ 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 data: if data and data["InstrumentID"] in symbol_exchange_map:
# Get buffered position object # Get buffered position object
key = f"{data['InstrumentID'], data['PosiDirection']}" key = f"{data['InstrumentID'], data['PosiDirection']}"
position = self.positions.get(key, None) position = self.positions.get(key, None)