[Mod] change gateio gateway name
This commit is contained in:
parent
99500a71fc
commit
d518566e55
@ -10,6 +10,7 @@ from copy import copy
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from vnpy.api.rest import Request, RestClient
|
from vnpy.api.rest import Request, RestClient
|
||||||
from vnpy.api.websocket import WebsocketClient
|
from vnpy.api.websocket import WebsocketClient
|
||||||
@ -23,6 +24,7 @@ from vnpy.trader.object import (AccountData, BarData, CancelRequest,
|
|||||||
OrderRequest, PositionData, SubscribeRequest,
|
OrderRequest, PositionData, SubscribeRequest,
|
||||||
TickData, TradeData)
|
TickData, TradeData)
|
||||||
|
|
||||||
|
|
||||||
TESTNET_REST_HOST = "https://fx-api-testnet.gateio.ws/"
|
TESTNET_REST_HOST = "https://fx-api-testnet.gateio.ws/"
|
||||||
REST_HOST = "https://api.gateio.ws"
|
REST_HOST = "https://api.gateio.ws"
|
||||||
|
|
||||||
@ -55,7 +57,6 @@ class GateiofGateway(BaseGateway):
|
|||||||
default_setting = {
|
default_setting = {
|
||||||
"API Key": "",
|
"API Key": "",
|
||||||
"Secret Key": "",
|
"Secret Key": "",
|
||||||
"会话数": 3,
|
|
||||||
"服务器": ["REAL", "TESTNET"],
|
"服务器": ["REAL", "TESTNET"],
|
||||||
"代理地址": "",
|
"代理地址": "",
|
||||||
"代理端口": "",
|
"代理端口": "",
|
||||||
@ -65,7 +66,7 @@ class GateiofGateway(BaseGateway):
|
|||||||
|
|
||||||
def __init__(self, event_engine):
|
def __init__(self, event_engine):
|
||||||
"""Constructor"""
|
"""Constructor"""
|
||||||
super(GateiofGateway, self).__init__(event_engine, "GATEIO")
|
super().__init__(event_engine, "GATEIOS")
|
||||||
self.order_manager = LocalOrderManager(self)
|
self.order_manager = LocalOrderManager(self)
|
||||||
|
|
||||||
self.ws_api = GateiofWebsocketApi(self)
|
self.ws_api = GateiofWebsocketApi(self)
|
||||||
@ -75,7 +76,6 @@ class GateiofGateway(BaseGateway):
|
|||||||
""""""
|
""""""
|
||||||
key = setting["API Key"]
|
key = setting["API Key"]
|
||||||
secret = setting["Secret Key"]
|
secret = setting["Secret Key"]
|
||||||
session_number = setting["会话数"]
|
|
||||||
server = setting["服务器"]
|
server = setting["服务器"]
|
||||||
proxy_host = setting["代理地址"]
|
proxy_host = setting["代理地址"]
|
||||||
proxy_port = setting["代理端口"]
|
proxy_port = setting["代理端口"]
|
||||||
@ -85,13 +85,9 @@ class GateiofGateway(BaseGateway):
|
|||||||
else:
|
else:
|
||||||
proxy_port = 0
|
proxy_port = 0
|
||||||
|
|
||||||
self.rest_api.connect(key, secret, session_number, server,
|
self.rest_api.connect(key, secret, server, proxy_host, proxy_port)
|
||||||
proxy_host, proxy_port)
|
|
||||||
self.ws_api.connect(key, secret, server, proxy_host, proxy_port)
|
|
||||||
|
|
||||||
self.query_account()
|
self.init_query()
|
||||||
self.query_position()
|
|
||||||
self.cycle_query()
|
|
||||||
|
|
||||||
def subscribe(self, req: SubscribeRequest):
|
def subscribe(self, req: SubscribeRequest):
|
||||||
""""""
|
""""""
|
||||||
@ -103,7 +99,7 @@ class GateiofGateway(BaseGateway):
|
|||||||
|
|
||||||
def cancel_order(self, req: CancelRequest):
|
def cancel_order(self, req: CancelRequest):
|
||||||
""""""
|
""""""
|
||||||
self.rest_api.cancel_order(req)
|
self.order_manager.cancel_order(req)
|
||||||
|
|
||||||
def query_account(self):
|
def query_account(self):
|
||||||
""""""
|
""""""
|
||||||
@ -124,17 +120,11 @@ class GateiofGateway(BaseGateway):
|
|||||||
|
|
||||||
def process_timer_event(self, event: Event):
|
def process_timer_event(self, event: Event):
|
||||||
""""""
|
""""""
|
||||||
if self.rest_api.cycle_query:
|
self.query_account()
|
||||||
self.count += 1
|
self.query_position()
|
||||||
if self.count < 3:
|
|
||||||
return
|
|
||||||
|
|
||||||
self.query_account()
|
def init_query(self):
|
||||||
self.query_position()
|
|
||||||
|
|
||||||
def cycle_query(self):
|
|
||||||
""""""
|
""""""
|
||||||
self.count = 0
|
|
||||||
self.event_engine.register(EVENT_TIMER, self.process_timer_event)
|
self.event_engine.register(EVENT_TIMER, self.process_timer_event)
|
||||||
|
|
||||||
|
|
||||||
@ -150,28 +140,22 @@ class GateiofRestApi(RestClient):
|
|||||||
self.gateway = gateway
|
self.gateway = gateway
|
||||||
self.gateway_name = gateway.gateway_name
|
self.gateway_name = gateway.gateway_name
|
||||||
self.order_manager = gateway.order_manager
|
self.order_manager = gateway.order_manager
|
||||||
self.ws_api = self.gateway.ws_api
|
self.ws_api = gateway.ws_api
|
||||||
|
|
||||||
self.key = ""
|
self.key = ""
|
||||||
self.secret = ""
|
self.secret = ""
|
||||||
self.account_id = ""
|
self.account_id = ""
|
||||||
|
self.server = ""
|
||||||
|
self.proxy_host = ""
|
||||||
|
self.proxy_port = 0
|
||||||
|
|
||||||
self.order_count = 10000
|
self.symbols = []
|
||||||
self.trade_count = 0
|
|
||||||
self.order_count_lock = Lock()
|
|
||||||
self.connect_date = 0
|
|
||||||
|
|
||||||
self.contracts = []
|
|
||||||
|
|
||||||
self.cycle_query = False
|
|
||||||
self.contract_query = False
|
|
||||||
|
|
||||||
def sign(self, request):
|
def sign(self, request):
|
||||||
"""
|
"""
|
||||||
Generate HBDM signature.
|
Generate HBDM signature.
|
||||||
"""
|
"""
|
||||||
|
request.headers = generate_sign(
|
||||||
headers_with_signature = generate_sign(
|
|
||||||
self.key,
|
self.key,
|
||||||
self.secret,
|
self.secret,
|
||||||
request.method,
|
request.method,
|
||||||
@ -180,14 +164,6 @@ class GateiofRestApi(RestClient):
|
|||||||
get_data=request.data
|
get_data=request.data
|
||||||
)
|
)
|
||||||
|
|
||||||
request.headers = headers_with_signature
|
|
||||||
|
|
||||||
# if request.params:
|
|
||||||
# query = urlencode(request.params)
|
|
||||||
# path = request.path + "?" + query
|
|
||||||
# else:
|
|
||||||
# path = request.path
|
|
||||||
|
|
||||||
if not request.data:
|
if not request.data:
|
||||||
request.data = ""
|
request.data = ""
|
||||||
|
|
||||||
@ -197,7 +173,6 @@ class GateiofRestApi(RestClient):
|
|||||||
self,
|
self,
|
||||||
key: str,
|
key: str,
|
||||||
secret: str,
|
secret: str,
|
||||||
session_number: int,
|
|
||||||
server: str,
|
server: str,
|
||||||
proxy_host: str,
|
proxy_host: str,
|
||||||
proxy_port: int
|
proxy_port: int
|
||||||
@ -207,6 +182,9 @@ class GateiofRestApi(RestClient):
|
|||||||
"""
|
"""
|
||||||
self.key = key
|
self.key = key
|
||||||
self.secret = secret
|
self.secret = secret
|
||||||
|
self.server = server
|
||||||
|
self.proxy_host = proxy_host
|
||||||
|
self.proxy_port = proxy_port
|
||||||
|
|
||||||
self.connect_date = int(datetime.now().strftime("%y%m%d"))
|
self.connect_date = int(datetime.now().strftime("%y%m%d"))
|
||||||
|
|
||||||
@ -215,7 +193,7 @@ class GateiofRestApi(RestClient):
|
|||||||
else:
|
else:
|
||||||
self.init(TESTNET_REST_HOST, proxy_host, proxy_port)
|
self.init(TESTNET_REST_HOST, proxy_host, proxy_port)
|
||||||
|
|
||||||
self.start(session_number)
|
self.start(3)
|
||||||
|
|
||||||
self.gateway.write_log("REST API启动成功")
|
self.gateway.write_log("REST API启动成功")
|
||||||
|
|
||||||
@ -237,7 +215,7 @@ class GateiofRestApi(RestClient):
|
|||||||
|
|
||||||
def query_order(self):
|
def query_order(self):
|
||||||
""""""
|
""""""
|
||||||
for contract in self.contracts:
|
for contract in self.symbols:
|
||||||
params = {
|
params = {
|
||||||
"contract": contract,
|
"contract": contract,
|
||||||
"status": "open",
|
"status": "open",
|
||||||
@ -250,14 +228,6 @@ class GateiofRestApi(RestClient):
|
|||||||
params=params
|
params=params
|
||||||
)
|
)
|
||||||
|
|
||||||
def query_trade(self):
|
|
||||||
""""""
|
|
||||||
self.add_request(
|
|
||||||
method="GET",
|
|
||||||
path="/api/v4/futures/my_trades",
|
|
||||||
callback=self.on_query_trade
|
|
||||||
)
|
|
||||||
|
|
||||||
def query_contract(self):
|
def query_contract(self):
|
||||||
""""""
|
""""""
|
||||||
self.add_request(
|
self.add_request(
|
||||||
@ -315,22 +285,13 @@ class GateiofRestApi(RestClient):
|
|||||||
|
|
||||||
return history
|
return history
|
||||||
|
|
||||||
def new_local_orderid(self):
|
|
||||||
""""""
|
|
||||||
with self.order_count_lock:
|
|
||||||
self.order_count += 1
|
|
||||||
local_orderid = f"{self.connect_date}{self.order_count}"
|
|
||||||
return local_orderid
|
|
||||||
|
|
||||||
def send_order(self, req: OrderRequest):
|
def send_order(self, req: OrderRequest):
|
||||||
""""""
|
""""""
|
||||||
local_orderid = self.new_local_orderid()
|
local_orderid = self.order_manager.new_local_orderid()
|
||||||
|
|
||||||
order = req.create_order_data(
|
order = req.create_order_data(
|
||||||
local_orderid,
|
local_orderid,
|
||||||
self.gateway_name
|
self.gateway_name
|
||||||
)
|
)
|
||||||
|
|
||||||
order.time = datetime.now().strftime("%H:%M:%S")
|
order.time = datetime.now().strftime("%H:%M:%S")
|
||||||
|
|
||||||
if req.direction == Direction.SHORT:
|
if req.direction == Direction.SHORT:
|
||||||
@ -386,18 +347,16 @@ class GateiofRestApi(RestClient):
|
|||||||
)
|
)
|
||||||
self.gateway.on_account(account)
|
self.gateway.on_account(account)
|
||||||
|
|
||||||
if not self.contract_query:
|
if not self.symbols:
|
||||||
self.query_contract()
|
self.query_contract()
|
||||||
|
|
||||||
def on_query_position(self, data, request):
|
def on_query_position(self, data, request):
|
||||||
""""""
|
""""""
|
||||||
for d in data:
|
for d in data:
|
||||||
volume = float(d["size"])
|
|
||||||
|
|
||||||
position = PositionData(
|
position = PositionData(
|
||||||
symbol=d["contract"],
|
symbol=d["contract"],
|
||||||
exchange=Exchange.GATEIO,
|
exchange=Exchange.GATEIO,
|
||||||
volume=volume,
|
volume=float(d["size"]),
|
||||||
direction=Direction.NET,
|
direction=Direction.NET,
|
||||||
price=float(d["entry_price"]),
|
price=float(d["entry_price"]),
|
||||||
pnl=float(d["unrealised_pnl"]),
|
pnl=float(d["unrealised_pnl"]),
|
||||||
@ -405,49 +364,9 @@ class GateiofRestApi(RestClient):
|
|||||||
)
|
)
|
||||||
self.gateway.on_position(position)
|
self.gateway.on_position(position)
|
||||||
|
|
||||||
def on_query_trade(self, data, request):
|
|
||||||
""""""
|
|
||||||
data = list(
|
|
||||||
reversed(data)
|
|
||||||
)
|
|
||||||
|
|
||||||
for d in data:
|
|
||||||
local_orderid = self.new_local_orderid()
|
|
||||||
self.trade_count += 1
|
|
||||||
|
|
||||||
volume = d["size"]
|
|
||||||
if volume > 0:
|
|
||||||
direction = Direction.LONG
|
|
||||||
else:
|
|
||||||
direction = Direction.SHORT
|
|
||||||
|
|
||||||
dt = datetime.fromtimestamp(d["create_time"]).strftime("%H:%M:%S")
|
|
||||||
|
|
||||||
trade = TradeData(
|
|
||||||
symbol=d["contract"],
|
|
||||||
exchange=Exchange.GATEIO,
|
|
||||||
orderid=local_orderid,
|
|
||||||
tradeid=str(self.trade_count).rjust(8, "0"),
|
|
||||||
direction=direction,
|
|
||||||
price=d["price"],
|
|
||||||
volume=abs(volume),
|
|
||||||
time=dt,
|
|
||||||
gateway_name=self.gateway_name,
|
|
||||||
)
|
|
||||||
self.gateway.on_trade(trade)
|
|
||||||
|
|
||||||
self.gateway.write_log("成交查询成功")
|
|
||||||
self.ws_api.trade_count = self.trade_count
|
|
||||||
self.query_order()
|
|
||||||
|
|
||||||
def on_query_order(self, data, request):
|
def on_query_order(self, data, request):
|
||||||
|
|
||||||
""""""
|
""""""
|
||||||
if not data:
|
|
||||||
return
|
|
||||||
|
|
||||||
for d in data:
|
for d in data:
|
||||||
|
|
||||||
local_orderid = self.new_local_orderid()
|
local_orderid = self.new_local_orderid()
|
||||||
sys_orderid = str(d["id"])
|
sys_orderid = str(d["id"])
|
||||||
|
|
||||||
@ -461,8 +380,8 @@ class GateiofRestApi(RestClient):
|
|||||||
direction = Direction.LONG
|
direction = Direction.LONG
|
||||||
else:
|
else:
|
||||||
direction = Direction.SHORT
|
direction = Direction.SHORT
|
||||||
|
|
||||||
dt = datetime.fromtimestamp(d["create_time"]).strftime("%H:%M:%S")
|
dt = datetime.fromtimestamp(d["create_time"])
|
||||||
|
|
||||||
order = OrderData(
|
order = OrderData(
|
||||||
orderid=local_orderid,
|
orderid=local_orderid,
|
||||||
@ -473,7 +392,7 @@ class GateiofRestApi(RestClient):
|
|||||||
type=OrderType.LIMIT,
|
type=OrderType.LIMIT,
|
||||||
direction=direction,
|
direction=direction,
|
||||||
status=STATUS_GATEIO2VT[d["status"]],
|
status=STATUS_GATEIO2VT[d["status"]],
|
||||||
time=dt,
|
time=dt.strftime("%H:%M:%S"),
|
||||||
gateway_name=self.gateway_name,
|
gateway_name=self.gateway_name,
|
||||||
)
|
)
|
||||||
self.order_manager.on_order(order)
|
self.order_manager.on_order(order)
|
||||||
@ -484,7 +403,7 @@ class GateiofRestApi(RestClient):
|
|||||||
""""""
|
""""""
|
||||||
for d in data:
|
for d in data:
|
||||||
symbol = d["name"]
|
symbol = d["name"]
|
||||||
self.contracts.append(symbol)
|
self.symbols.append(symbol)
|
||||||
|
|
||||||
contract = ContractData(
|
contract = ContractData(
|
||||||
symbol=symbol,
|
symbol=symbol,
|
||||||
@ -501,23 +420,22 @@ class GateiofRestApi(RestClient):
|
|||||||
|
|
||||||
self.gateway.write_log("合约信息查询成功")
|
self.gateway.write_log("合约信息查询成功")
|
||||||
|
|
||||||
self.contract_query = True
|
# Connect websocket api after account id and contract symbols collected
|
||||||
|
self.ws_api.connect(
|
||||||
self.query_trade()
|
self.key,
|
||||||
self.update_info(self.contracts, self.account_id)
|
self.secret,
|
||||||
|
self.server,
|
||||||
def update_info(self, symbols, account_id):
|
self.proxy_host,
|
||||||
""""""
|
self.proxy_port,
|
||||||
self.cycle_query = True
|
self.account_id,
|
||||||
|
self.symbols
|
||||||
for symbol in symbols:
|
)
|
||||||
self.ws_api.update_info(symbol, account_id)
|
|
||||||
|
|
||||||
def on_send_order(self, data, request):
|
def on_send_order(self, data, request):
|
||||||
""""""
|
""""""
|
||||||
order = request.extra
|
order = request.extra
|
||||||
order.status = STATUS_GATEIO2VT[data["status"]]
|
order.status = STATUS_GATEIO2VT[data["status"]]
|
||||||
|
|
||||||
if order.status == Status.ALLTRADED:
|
if order.status == Status.ALLTRADED:
|
||||||
order.traded = order.volume
|
order.traded = order.volume
|
||||||
|
|
||||||
@ -602,8 +520,9 @@ class GateiofWebsocketApi(WebsocketClient):
|
|||||||
self.key = ""
|
self.key = ""
|
||||||
self.secret = ""
|
self.secret = ""
|
||||||
self.account_id = ""
|
self.account_id = ""
|
||||||
self.trade_count = 0
|
self.symbols = []
|
||||||
|
|
||||||
|
self.trade_count = 0
|
||||||
self.ticks = {}
|
self.ticks = {}
|
||||||
|
|
||||||
def connect(
|
def connect(
|
||||||
@ -612,22 +531,42 @@ class GateiofWebsocketApi(WebsocketClient):
|
|||||||
secret: str,
|
secret: str,
|
||||||
server: str,
|
server: str,
|
||||||
proxy_host: str,
|
proxy_host: str,
|
||||||
proxy_port: int
|
proxy_port: int,
|
||||||
|
account_id: str,
|
||||||
|
symbols: List[str]
|
||||||
):
|
):
|
||||||
""""""
|
""""""
|
||||||
self.key = key
|
self.key = key
|
||||||
self.secret = secret
|
self.secret = secret
|
||||||
|
self.account_id = account_id
|
||||||
|
self.symbols = symbols
|
||||||
|
|
||||||
if server == "REAL":
|
if server == "REAL":
|
||||||
self.init(WEBSOCKET_HOST, proxy_host, proxy_port)
|
self.init(WEBSOCKET_HOST, proxy_host, proxy_port)
|
||||||
else:
|
else:
|
||||||
self.init(TESTNET_WEBSOCKET_HOST, proxy_host, proxy_port)
|
self.init(TESTNET_WEBSOCKET_HOST, proxy_host, proxy_port)
|
||||||
|
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
def on_connected(self):
|
def on_connected(self):
|
||||||
""""""
|
""""""
|
||||||
self.gateway.write_log("Websocket API连接成功")
|
self.gateway.write_log("Websocket API连接成功")
|
||||||
|
|
||||||
|
for symbol in self.symbols:
|
||||||
|
update_order = self.construct_req(
|
||||||
|
channel="futures.orders",
|
||||||
|
event="subscribe",
|
||||||
|
pay_load=[self.account_id, symbol]
|
||||||
|
)
|
||||||
|
self.send_packet(update_order)
|
||||||
|
|
||||||
|
update_position = self.construct_req(
|
||||||
|
channel="futures.position_closes",
|
||||||
|
event="subscribe",
|
||||||
|
pay_load=[self.account_id, symbol]
|
||||||
|
)
|
||||||
|
self.send_packet(update_position)
|
||||||
|
|
||||||
def subscribe(self, req: SubscribeRequest):
|
def subscribe(self, req: SubscribeRequest):
|
||||||
"""
|
"""
|
||||||
Subscribe to tick data upate.
|
Subscribe to tick data upate.
|
||||||
@ -661,20 +600,7 @@ class GateiofWebsocketApi(WebsocketClient):
|
|||||||
|
|
||||||
def update_info(self, symbol: str, account_id: str):
|
def update_info(self, symbol: str, account_id: str):
|
||||||
""""""
|
""""""
|
||||||
|
|
||||||
update_order = self.construct_req(
|
|
||||||
channel="futures.orders",
|
|
||||||
event="subscribe",
|
|
||||||
pay_load=[account_id, symbol]
|
|
||||||
)
|
|
||||||
self.send_packet(update_order)
|
|
||||||
|
|
||||||
update_position = self.construct_req(
|
|
||||||
channel="futures.position_closes",
|
|
||||||
event="subscribe",
|
|
||||||
pay_load=[account_id, symbol]
|
|
||||||
)
|
|
||||||
self.send_packet(update_position)
|
|
||||||
|
|
||||||
def on_disconnected(self):
|
def on_disconnected(self):
|
||||||
""""""
|
""""""
|
||||||
@ -809,7 +735,7 @@ class GateiofWebsocketApi(WebsocketClient):
|
|||||||
price=float(d["fill_price"]),
|
price=float(d["fill_price"]),
|
||||||
volume=order.volume,
|
volume=order.volume,
|
||||||
time=order.time,
|
time=order.time,
|
||||||
gateway_name=self.gateway_name,
|
gateway_name=self.gateway_name,
|
||||||
)
|
)
|
||||||
self.gateway.on_trade(trade)
|
self.gateway.on_trade(trade)
|
||||||
|
|
||||||
@ -841,7 +767,7 @@ def generate_sign(key, secret, method, path, get_params=None, get_data=None):
|
|||||||
"Timestamp": str(timestamp),
|
"Timestamp": str(timestamp),
|
||||||
"SIGN": signature
|
"SIGN": signature
|
||||||
}
|
}
|
||||||
|
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user