Skip to content

Commit

Permalink
Update okexv5_gateway.py
Browse files Browse the repository at this point in the history
  • Loading branch information
noranhe committed Apr 23, 2021
1 parent 43cdfb5 commit 32b0845
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions vnpy_okex/okexv5_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ def connect(
server: str
) -> None:
""""""

if server == "REAL":
self.init(PUBLIC_WEBSOCKET_HOST, proxy_host, proxy_port)
else:
Expand All @@ -672,7 +671,6 @@ def subscribe(self, req: SubscribeRequest):
"""
Subscribe to tick data upate.
"""

if req.symbol not in symbol_contract_map:
self.gateway.write_log(f"找不到该合约代码{req.symbol}")
return
Expand Down Expand Up @@ -700,24 +698,16 @@ def subscribe(self, req: SubscribeRequest):

req = {
"op": "subscribe",
"args": [channel_ticker]
}
self.send_packet(req)

req = {
"op": "subscribe",
"args": [channel_depth]
"args": [channel_ticker, channel_depth]
}
self.send_packet(req)

def on_connected(self) -> None:
""""""
self.gateway.write_log("Websocket Public API连接成功")
self.callbacks["ticker"] = self.on_ticker
self.callbacks["books5"] = self.on_depth
self.subscribe_public_topic()

for req in list(self.subscribed.values()):
pass
self.subscribe(req)

def on_disconnected(self):
Expand All @@ -736,7 +726,7 @@ def on_packet(self, packet: dict):
self.gateway.write_log(f"Websocket Public API请求异常, 状态码:{code}, 信息{msg}")

else:
channel = packet["arg"]
channel = packet["arg"]["channel"]
data = packet["data"]
callback = self.callbacks.get(channel, None)

Expand All @@ -753,9 +743,19 @@ def on_error(self, exception_type: type, exception_value: Exception, tb):
self.exception_detail(exception_type, exception_value, tb)
)

def subscribe_public_topic(self):
"""
Subscribe to public topics.
"""
self.callbacks["tickers"] = self.on_ticker
self.callbacks["books5"] = self.on_depth

req = SubscribeRequest("BTC-USDT", Exchange.OKEX)
self.subscribe(req)

def on_ticker(self, d):
""""""
symbol = d["insrId"]
symbol = d["instId"]
tick = self.ticks.get(symbol, None)
if not tick:
return
Expand All @@ -766,6 +766,7 @@ def on_ticker(self, d):
return

tick.last_price = last_price
tick.open_price = float(d["open24h"])
tick.high_price = float(d["high24h"])
tick.low_price = float(d["low24h"])
tick.volume = float(d["vol24h"])
Expand All @@ -785,14 +786,14 @@ def on_depth(self, d):
for n in range(min(5, len(bids))):
price, volume, _, _ = bids[n]
tick.__setattr__("bid_price_%s" % (n + 1), float(price))
tick.__setattr__("bid_volume_%s" % (n + 1), int(volume))
tick.__setattr__("bid_volume_%s" % (n + 1), float(volume))

for n in range(min(5, len(asks))):
price, volume, _, _ = asks[n]
tick.__setattr__("ask_price_%s" % (n + 1), float(price))
tick.__setattr__("ask_volume_%s" % (n + 1), int(volume))
tick.__setattr__("ask_volume_%s" % (n + 1), float(volume))

tick.datetime = _parse_timestamp(d["timestamp"])
tick.datetime = _parse_timestamp(d["ts"])
self.gateway.on_tick(copy(tick))


Expand Down Expand Up @@ -894,6 +895,7 @@ def login(self):
]
}
self.send_packet(req)

self.callbacks["login"] = self.on_login

def subscribe_private_topic(self):
Expand Down

0 comments on commit 32b0845

Please sign in to comment.