[Del] remove _split_url

This commit is contained in:
vn.py 2019-06-11 23:46:54 +08:00
parent 4ae3b2acae
commit 93b4ff40a3

View File

@ -11,7 +11,6 @@ import time
from copy import copy
from datetime import datetime
from vnpy.event import Event
from vnpy.api.rest import RestClient, Request
from vnpy.api.websocket import WebsocketClient
from vnpy.trader.constant import (
@ -21,7 +20,7 @@ from vnpy.trader.constant import (
Status,
OrderType
)
from vnpy.trader.gateway import BaseGateway, LocalOrderManager
from vnpy.trader.gateway import BaseGateway
from vnpy.trader.object import (
TickData,
OrderData,
@ -32,11 +31,11 @@ from vnpy.trader.object import (
CancelRequest,
SubscribeRequest
)
from vnpy.trader.event import EVENT_TIMER
REST_HOST = "https://www.binance.com"
WEBSOCKET_TRADE_HOST = "wss://stream.binance.com:9443/ws/" # Account and Order
# Account and Order
WEBSOCKET_TRADE_HOST = "wss://stream.binance.com:9443/ws/"
WEBSOCKET_DATA_HOST = "wss://stream.binance.com:9443/stream?streams=" # Market Data
STATUS_BINANCE2VT = {
@ -84,8 +83,6 @@ class BinanceGateway(BaseGateway):
"""Constructor"""
super().__init__(event_engine, "BINANCE")
self.order_manager = LocalOrderManager(self)
self.rest_api = BinanceRestApi(self)
self.trade_ws_api = BinanceTradeWebsocketApi(self)
self.market_ws_api = BinanceDataWebsocketApi(self)
@ -100,13 +97,13 @@ class BinanceGateway(BaseGateway):
self.rest_api.connect(key, secret, session_number,
proxy_host, proxy_port)
# self.trade_ws_api.connect(key, secret, proxy_host, proxy_port)
# self.market_ws_api.connect(key, secret, proxy_host, proxy_port)
self.trade_ws_api.connect(key, secret, proxy_host, proxy_port)
self.market_ws_api.connect(key, secret, proxy_host, proxy_port)
def subscribe(self, req: SubscribeRequest):
""""""
self.market_ws_api.subscribe(req)
# self.trade_ws_api.subscribe(req)
self.trade_ws_api.subscribe(req)
def send_order(self, req: OrderRequest):
""""""
@ -136,21 +133,21 @@ class BinanceRestApi(RestClient):
BINANCE REST API
"""
def __init__(self, gateway: BaseGateway):
def __init__(self, gateway: BinanceGateway):
""""""
super(BinanceRestApi, self).__init__()
super().__init__()
self.gateway = gateway
self.gateway_name = gateway.gateway_name
self.order_manager = gateway.order_manager
self.trade_ws_api = BinanceTradeWebsocketApi(self.gateway)
self.host = ""
self.trade_ws_api = self.gateway.trade_ws_api
self.key = ""
self.secret = ""
self.userStreamKey = ""
self.keepaliveCount = 0
self.recvWindow = 5000
self.user_stream_key = ""
self.keep_alive_count = 0
self.recv_window = 5000
self.time_offset = 0
self.cancel_requests = {}
@ -165,34 +162,44 @@ class BinanceRestApi(RestClient):
else:
request.params = dict()
path = request.path
security = "NONE"
if request.data:
security = request.data['security']
if security == "SIGNED":
timestamp = int(time.time() * 1000)
if self.time_offset > 0:
timestamp -= abs(self.time_offset)
elif self.time_offset < 0:
timestamp += abs(self.time_offset)
request.params['timestamp'] = timestamp
request.params['recvWindow'] = self.recvWindow
request.params['recv_window'] = self.recv_window
query = urllib.parse.urlencode(sorted(request.params.items()))
signature = hmac.new(self.secret, query.encode('utf-8'), hashlib.sha256).hexdigest()
signature = hmac.new(self.secret, query.encode(
'utf-8'), hashlib.sha256).hexdigest()
query += "&signature={}".format(signature)
path = request.path + "?" + query
request.path = path
request.params = {}
request.data = {}
# Add headers
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"X-MBX-APIKEY": self.key
}
if security == "SIGNED" or security == "API-KEY":
request.headers = headers
return request
def connect(
@ -211,8 +218,6 @@ class BinanceRestApi(RestClient):
self.proxy_port = proxy_port
self.proxy_host = proxy_host
self.host, _ = _split_url(REST_HOST)
self.init(REST_HOST, proxy_host, proxy_port)
self.start(session_number)
@ -222,7 +227,7 @@ class BinanceRestApi(RestClient):
self.query_account()
self.query_order()
self.query_contract()
self.start_userStream()
self.start_user_stream()
def query_time(self):
""""""
@ -230,6 +235,7 @@ class BinanceRestApi(RestClient):
"security": "NONE"
}
path = '/api/v1/time'
return self.add_request(
"GET",
path,
@ -331,7 +337,7 @@ class BinanceRestApi(RestClient):
print("撤单本地id", req.orderid, "撤单远端id", sys_orderid)
def start_userStream(self):
def start_user_stream(self):
""""""
data = {
"security": "API-KEY"
@ -339,20 +345,20 @@ class BinanceRestApi(RestClient):
self.add_request(
method="POST",
path='/api/v1/userDataStream',
callback=self.on_start_userStream,
callback=self.on_start_user_stream,
data=data
)
def keepalive_userStream(self):
""""""
self.keepaliveCount += 1
if self.keepaliveCount < 1800:
self.keep_alive_count += 1
if self.keep_alive_count < 1800:
return
data = {
"security": "SIGNED"
}
params = {
'listenKey': self.userStreamKey
'listenKey': self.user_stream_key
}
self.add_request(
method='PUT',
@ -514,10 +520,10 @@ class BinanceRestApi(RestClient):
self.order_manager.on_order(order)
def on_start_userStream(self, data, request):
self.userStreamKey = data['listenKey']
self.keepaliveCount = 0
url = WEBSOCKET_TRADE_HOST + self.userStreamKey
def on_start_user_stream(self, data, request):
self.user_stream_key = data['listenKey']
self.keep_alive_count = 0
url = WEBSOCKET_TRADE_HOST + self.user_stream_key
self.trade_ws_api.connect(
key=self.key,
secret=self.secret,
@ -527,9 +533,9 @@ class BinanceRestApi(RestClient):
def on_keepalive_userStream(self, data, request):
self.gateway.write_log("交易推送刷新成功")
if self.keepaliveCount >= 1800:
self.keepaliveCount = 0
self.keepalive_userStream(self.userStreamKey)
if self.keep_alive_count >= 1800:
self.keep_alive_count = 0
self.keepalive_userStream(self.user_stream_key)
def on_close_userStream(self, listenKey):
self.gateway.write_log("交易推送关闭")
@ -635,6 +641,7 @@ class BinanceWebsocketApiBase(WebsocketClient):
class BinanceTradeWebsocketApi(BinanceWebsocketApiBase):
""""""
def __init__(self, gateway):
""""""
super().__init__(gateway)
@ -843,13 +850,3 @@ class BinanceDataWebsocketApi(BinanceWebsocketApiBase):
if tick.bid_price_1:
self.gateway.on_tick(copy(tick))
def _split_url(url):
"""
将url拆分为host和path
:return: host, path
"""
result = re.match("\w+://([^/]*)(.*)", url) # noqa
if result:
return result.group(1), result.group(2)