Merge pull request #1697 from 1122455801/tiger_01

[Mod] tiger_gateway.py
This commit is contained in:
vn.py 2019-05-14 22:15:51 +08:00 committed by GitHub
commit 2ba6edea05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -92,8 +92,8 @@ class TigerGateway(BaseGateway):
default_setting = { default_setting = {
"tiger_id": "", "tiger_id": "",
"account": "", "account": "",
"standard_account": "", "服务器": ["标准", "环球", "仿真"],
"private_key": '', "private_key": "",
} }
exchanges = [ exchanges = [
@ -109,8 +109,7 @@ class TigerGateway(BaseGateway):
self.tiger_id = "" self.tiger_id = ""
self.account = "" self.account = ""
self.standard_account = "" self.server = ""
self.paper_account = ""
self.language = "" self.language = ""
self.client_config = None self.client_config = None
@ -149,11 +148,10 @@ class TigerGateway(BaseGateway):
def connect(self, setting: dict): def connect(self, setting: dict):
"""""" """"""
self.private_key = setting['private_key'] self.private_key = setting["private_key"]
self.tiger_id = setting["tiger_id"] self.tiger_id = setting["tiger_id"]
self.server = setting["服务器"]
self.account = setting["account"] self.account = setting["account"]
self.standard_account = setting["standard_account"]
self.paper_account = setting["account"]
self.languege = Language.zh_CN self.languege = Language.zh_CN
# Start thread pool for REST call # Start thread pool for REST call
@ -167,14 +165,12 @@ class TigerGateway(BaseGateway):
self.add_task(self.connect_trade) self.add_task(self.connect_trade)
self.add_task(self.connect_push) self.add_task(self.connect_push)
def init_client_config(self, sandbox=True): def init_client_config(self, sandbox=False):
"""""" """"""
self.client_config = TigerOpenClientConfig(sandbox_debug=sandbox) self.client_config = TigerOpenClientConfig(sandbox_debug=sandbox)
self.client_config.private_key = self.private_key self.client_config.private_key = self.private_key
self.client_config.tiger_id = self.tiger_id self.client_config.tiger_id = self.tiger_id
self.client_config.account = self.account self.client_config.account = self.account
self.client_config.standard_account = self.standard_account
self.client_config.paper_account = self.paper_account
self.client_config.language = self.language self.client_config.language = self.language
def connect_quote(self): def connect_quote(self):
@ -213,12 +209,13 @@ class TigerGateway(BaseGateway):
Connect to push server. Connect to push server.
""" """
protocol, host, port = self.client_config.socket_host_port protocol, host, port = self.client_config.socket_host_port
self.push_client = PushClient(host, port, (protocol == 'ssl')) self.push_client = PushClient(host, port, (protocol == "ssl"))
self.push_client.quote_changed = self.on_quote_change self.push_client.quote_changed = self.on_quote_change
self.push_client.asset_changed = self.on_asset_change self.push_client.asset_changed = self.on_asset_change
self.push_client.position_changed = self.on_position_change self.push_client.position_changed = self.on_position_change
self.push_client.order_changed = self.on_order_change self.push_client.order_changed = self.on_order_change
self.push_client.connect_callback = self.on_push_connected
self.push_client.connect( self.push_client.connect(
self.client_config.tiger_id, self.client_config.private_key) self.client_config.tiger_id, self.client_config.private_key)
@ -257,18 +254,17 @@ class TigerGateway(BaseGateway):
) )
self.ticks[symbol] = tick self.ticks[symbol] = tick
tick.datetime = datetime.fromtimestamp(data["latest_time"] / 1000) tick.datetime = datetime.fromtimestamp(int(data["latest_time"]) / 1000)
tick.pre_close = data.get("prev_close", 0) tick.pre_close = data.get("prev_close", tick.pre_close)
tick.last_price = data.get("latest_price", 0) tick.last_price = data.get("latest_price", tick.last_price)
tick.volume = data.get("volume", 0) tick.volume = data.get("volume", tick.volume)
tick.open_price = data.get("open", 0) tick.open_price = data.get("open", tick.open_price)
tick.open_price = data.get("open", 0) tick.high_price = data.get("high", tick.high_price)
tick.high_price = data.get("high", 0) tick.low_price = data.get("low", tick.low_price)
tick.low_price = data.get("low", 0) tick.ask_price_1 = data.get("ask_price", tick.ask_price_1)
tick.ask_price_1 = data.get("ask_price", 0) tick.bid_price_1 = data.get("bid_price", tick.bid_price_1)
tick.bid_price_1 = data.get("bid_price", 0) tick.ask_volume_1 = data.get("ask_size", tick.ask_volume_1)
tick.ask_volume_1 = data.get("ask_size", 0) tick.bid_volume_1 = data.get("bid_size", tick.bid_volume_1)
tick.bid_volume_1 = data.get("bid_size", 0)
self.on_tick(copy(tick)) self.on_tick(copy(tick))
@ -392,7 +388,7 @@ class TigerGateway(BaseGateway):
self.write_log(f"撤单失败:{req.orderid}") self.write_log(f"撤单失败:{req.orderid}")
if not data: if not data:
self.write_log('撤单成功') self.write_log("撤单成功")
def query_contract(self): def query_contract(self):
"""""" """"""
@ -401,20 +397,20 @@ class TigerGateway(BaseGateway):
symbols_names_HK = self.quote_client.get_symbol_names( symbols_names_HK = self.quote_client.get_symbol_names(
lang=Language.zh_CN, market=Market.HK) lang=Language.zh_CN, market=Market.HK)
contract_names_HK = DataFrame( contract_names_HK = DataFrame(
symbols_names_HK, columns=['symbol', 'name']) symbols_names_HK, columns=["symbol", "name"])
contractList = list(contract_names_HK["symbol"]) contractList = list(contract_names_HK["symbol"])
i, n = 0, len(contractList) i, n = 0, len(contractList)
result = pd.DataFrame() result = pd.DataFrame()
while i < n: while i < n:
i += 500 i += 50
c = contractList[i - 500:i] c = contractList[i - 50:i]
r = self.quote_client.get_trade_metas(c) r = self.quote_client.get_trade_metas(c)
result = result.append(r) result = result.append(r)
contract_detail_HK = result.sort_values(by="symbol", ascending=True) contract_detail_HK = result.sort_values(by="symbol", ascending=True)
contract_HK = pd.merge( contract_HK = pd.merge(
contract_names_HK, contract_detail_HK, how='left', on='symbol') contract_names_HK, contract_detail_HK, how="left", on="symbol")
for ix, row in contract_HK.iterrows(): for ix, row in contract_HK.iterrows():
contract = ContractData( contract = ContractData(
@ -423,6 +419,7 @@ class TigerGateway(BaseGateway):
name=row["name"], name=row["name"],
product=Product.EQUITY, product=Product.EQUITY,
size=1, size=1,
min_volume=row["lot_size"],
pricetick=row["min_tick"], pricetick=row["min_tick"],
net_position=True, net_position=True,
gateway_name=self.gateway_name, gateway_name=self.gateway_name,
@ -433,7 +430,7 @@ class TigerGateway(BaseGateway):
# US Stock # US Stock
symbols_names_US = self.quote_client.get_symbol_names( symbols_names_US = self.quote_client.get_symbol_names(
lang=Language.zh_CN, market=Market.US) lang=Language.zh_CN, market=Market.US)
contract_US = DataFrame(symbols_names_US, columns=['symbol', 'name']) contract_US = DataFrame(symbols_names_US, columns=["symbol", "name"])
for ix, row in contract_US.iterrows(): for ix, row in contract_US.iterrows():
contract = ContractData( contract = ContractData(
@ -442,6 +439,7 @@ class TigerGateway(BaseGateway):
name=row["name"], name=row["name"],
product=Product.EQUITY, product=Product.EQUITY,
size=1, size=1,
min_volume=100,
pricetick=0.001, pricetick=0.001,
gateway_name=self.gateway_name, gateway_name=self.gateway_name,
) )
@ -451,7 +449,7 @@ class TigerGateway(BaseGateway):
# CN Stock # CN Stock
symbols_names_CN = self.quote_client.get_symbol_names( symbols_names_CN = self.quote_client.get_symbol_names(
lang=Language.zh_CN, market=Market.CN) lang=Language.zh_CN, market=Market.CN)
contract_CN = DataFrame(symbols_names_CN, columns=['symbol', 'name']) contract_CN = DataFrame(symbols_names_CN, columns=["symbol", "name"])
for ix, row in contract_CN.iterrows(): for ix, row in contract_CN.iterrows():
symbol = row["symbol"] symbol = row["symbol"]
@ -463,6 +461,7 @@ class TigerGateway(BaseGateway):
name=row["name"], name=row["name"],
product=Product.EQUITY, product=Product.EQUITY,
size=1, size=1,
min_volume=100,
pricetick=0.001, pricetick=0.001,
gateway_name=self.gateway_name, gateway_name=self.gateway_name,
) )