Revert "Revert "[Add]net_position member of ContractData""

This reverts commit 289bcfa13e.
This commit is contained in:
vn.py 2019-03-25 23:11:12 +08:00
parent 0fa314faa8
commit e143f6582e
6 changed files with 83 additions and 62 deletions

View File

@ -607,6 +607,7 @@ class BitmexWebsocketApi(WebsocketClient):
pricetick=d["tickSize"],
size=d["lotSize"],
stop_supported=True,
net_position=True,
gateway_name=self.gateway_name,
)

View File

@ -303,6 +303,7 @@ class FutuGateway(BaseGateway):
product=product,
size=1,
pricetick=0.001,
net_position=True,
gateway_name=self.gateway_name,
)
self.on_contract(contract)

View File

@ -459,6 +459,7 @@ class IbApi(EWrapper):
product=PRODUCT_IB2VT[ib_product],
size=ib_size,
pricetick=contractDetails.minTick,
net_position=True,
gateway_name=self.gateway_name,
)

View File

@ -468,6 +468,7 @@ class OesTdApi:
name=data.securityName,
product=PRODUCT_OES2VT[data.mktId],
size=data.buyQtyUnit,
net_position=True,
pricetick=data.priceUnit,
)
self.gateway.on_contract(contract)

View File

@ -172,7 +172,8 @@ class TigerGateway(BaseGateway):
"""
try:
self.quote_client = QuoteClient(self.client_config)
self.symbol_names = dict(self.quote_client.get_symbol_names(lang=Language.zh_CN))
self.symbol_names = dict(
self.quote_client.get_symbol_names(lang=Language.zh_CN))
self.query_contract()
except ApiException:
self.write_log("查询合约失败")
@ -202,7 +203,8 @@ class TigerGateway(BaseGateway):
"""
protocol, host, port = self.client_config.socket_host_port
self.push_client = PushClient(host, port, (protocol == 'ssl'))
self.push_client.connect(self.client_config.tiger_id, self.client_config.private_key)
self.push_client.connect(
self.client_config.tiger_id, self.client_config.private_key)
self.push_client.quote_changed = self.on_quote_change
self.push_client.asset_changed = self.on_asset_change
@ -283,20 +285,23 @@ class TigerGateway(BaseGateway):
def on_order_change(self, tiger_account: str, data: list):
""""""
data = dict(data)
print("委托推送", data["origin_symbol"], data["order_id"], data["filled"], data["status"])
print("委托推送", data["origin_symbol"],
data["order_id"], data["filled"], data["status"])
symbol, exchange = convert_symbol_tiger2vt(data["origin_symbol"])
status = PUSH_STATUS_TIGER2VT[data["status"]]
order = OrderData(
symbol=symbol,
exchange=exchange,
orderid=self.ID_TIGER2VT.get(str(data["order_id"]), self.get_new_local_id()),
orderid=self.ID_TIGER2VT.get(
str(data["order_id"]), self.get_new_local_id()),
direction=Direction.NET,
price=data.get("limit_price", 0),
volume=data["quantity"],
traded=data["filled"],
status=status,
time=datetime.fromtimestamp(data["order_time"] / 1000).strftime("%H:%M:%S"),
time=datetime.fromtimestamp(
data["order_time"] / 1000).strftime("%H:%M:%S"),
gateway_name=self.gateway_name,
)
self.on_order(order)
@ -312,7 +317,8 @@ class TigerGateway(BaseGateway):
orderid=self.ID_TIGER2VT[str(data["order_id"])],
price=data["avg_fill_price"],
volume=data["filled"],
time=datetime.fromtimestamp(data["trade_time"] / 1000).strftime("%H:%M:%S"),
time=datetime.fromtimestamp(
data["trade_time"] / 1000).strftime("%H:%M:%S"),
gateway_name=self.gateway_name,
)
self.on_trade(trade)
@ -334,7 +340,8 @@ class TigerGateway(BaseGateway):
""""""
currency = config_symbol_currency(req.symbol)
try:
contract = self.trade_client.get_contracts(symbol=req.symbol, currency=currency)[0]
contract = self.trade_client.get_contracts(
symbol=req.symbol, currency=currency)[0]
order = self.trade_client.create_order(
account=self.account,
contract=contract,
@ -347,7 +354,8 @@ class TigerGateway(BaseGateway):
self.ID_VT2TIGER[local_id] = str(order.order_id)
self.trade_client.place_order(order)
print("发单:", order.contract.symbol, order.order_id, order.quantity, order.status)
print("发单:", order.contract.symbol,
order.order_id, order.quantity, order.status)
except: # noqa
traceback.print_exc()
@ -373,8 +381,10 @@ class TigerGateway(BaseGateway):
""""""
# HK Stock
symbols_names_HK = self.quote_client.get_symbol_names(lang=Language.zh_CN, market=Market.HK)
contract_names_HK = DataFrame(symbols_names_HK, columns=['symbol', 'name'])
symbols_names_HK = self.quote_client.get_symbol_names(
lang=Language.zh_CN, market=Market.HK)
contract_names_HK = DataFrame(
symbols_names_HK, columns=['symbol', 'name'])
contractList = list(contract_names_HK["symbol"])
i, n = 0, len(contractList)
@ -386,7 +396,8 @@ class TigerGateway(BaseGateway):
result = result.append(r)
contract_detail_HK = result.sort_values(by="symbol", ascending=True)
contract_HK = pd.merge(contract_names_HK, contract_detail_HK, how='left', on='symbol')
contract_HK = pd.merge(
contract_names_HK, contract_detail_HK, how='left', on='symbol')
for ix, row in contract_HK.iterrows():
contract = ContractData(
@ -396,13 +407,15 @@ class TigerGateway(BaseGateway):
product=Product.EQUITY,
size=1,
pricetick=row["min_tick"],
net_position=True,
gateway_name=self.gateway_name,
)
self.on_contract(contract)
self.contracts[contract.vt_symbol] = contract
# US Stock
symbols_names_US = self.quote_client.get_symbol_names(lang=Language.zh_CN, market=Market.US)
symbols_names_US = self.quote_client.get_symbol_names(
lang=Language.zh_CN, market=Market.US)
contract_US = DataFrame(symbols_names_US, columns=['symbol', 'name'])
for ix, row in contract_US.iterrows():
@ -419,7 +432,8 @@ class TigerGateway(BaseGateway):
self.contracts[contract.vt_symbol] = contract
# CN Stock
symbols_names_CN = self.quote_client.get_symbol_names(lang=Language.zh_CN, market=Market.CN)
symbols_names_CN = self.quote_client.get_symbol_names(
lang=Language.zh_CN, market=Market.CN)
contract_CN = DataFrame(symbols_names_CN, columns=['symbol', 'name'])
for ix, row in contract_CN.iterrows():
@ -515,7 +529,8 @@ class TigerGateway(BaseGateway):
volume=i.quantity,
traded=i.filled,
status=STATUS_TIGER2VT[i.status],
time=datetime.fromtimestamp(i.order_time / 1000).strftime("%H:%M:%S"),
time=datetime.fromtimestamp(
i.order_time / 1000).strftime("%H:%M:%S"),
gateway_name=self.gateway_name,
)
self.ID_TIGER2VT[str(i.order_id)] = local_id
@ -542,7 +557,8 @@ class TigerGateway(BaseGateway):
orderid=self.ID_TIGER2VT[str(i.order_id)],
price=i.avg_fill_price,
volume=i.filled,
time=datetime.fromtimestamp(i.trade_time / 1000).strftime("%H:%M:%S"),
time=datetime.fromtimestamp(
i.trade_time / 1000).strftime("%H:%M:%S"),
gateway_name=self.gateway_name,
)

View File

@ -234,6 +234,7 @@ class ContractData(BaseData):
pricetick: float
stop_supported: bool = False # whether server supports stop order
net_position: bool = False # whether gateway uses net position volume
option_strike: float = 0
option_underlying: str = "" # vt_symbol of underlying contract