Skip to content

Commit

Permalink
[Mod] change function names
Browse files Browse the repository at this point in the history
  • Loading branch information
vnpy committed Oct 18, 2019
1 parent d518566 commit 4336ccf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 50 deletions.
2 changes: 2 additions & 0 deletions examples/vn_trader/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from vnpy.gateway.da import DaGateway
from vnpy.gateway.coinbase import CoinbaseGateway
from vnpy.gateway.bitstamp import BitstampGateway
from vnpy.gateway.gateios import GateiosGateway

from vnpy.app.cta_strategy import CtaStrategyApp
# from vnpy.app.csv_loader import CsvLoaderApp
Expand Down Expand Up @@ -76,6 +77,7 @@ def main():
# main_engine.add_gateway(DaGateway)
# main_engine.add_gateway(CoinbaseGateway)
main_engine.add_gateway(BitstampGateway)
main_engine.add_gateway(GateiosGateway)

main_engine.add_app(CtaStrategyApp)
main_engine.add_app(CtaBacktesterApp)
Expand Down
2 changes: 1 addition & 1 deletion vnpy/gateway/gateios/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .gateiof_gateway import GateiofGateway
from .gateios_gateway import GateiosGateway
74 changes: 25 additions & 49 deletions vnpy/gateway/gateios/gateios_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}


class GateiofGateway(BaseGateway):
class GateiosGateway(BaseGateway):
"""
VN Trader Gateway for Gate.io future connection.
"""
Expand All @@ -69,8 +69,8 @@ def __init__(self, event_engine):
super().__init__(event_engine, "GATEIOS")
self.order_manager = LocalOrderManager(self)

self.ws_api = GateiofWebsocketApi(self)
self.rest_api = GateiofRestApi(self)
self.ws_api = GateiosWebsocketApi(self)
self.rest_api = GateiosRestApi(self)

def connect(self, setting: dict):
""""""
Expand Down Expand Up @@ -128,9 +128,9 @@ def init_query(self):
self.event_engine.register(EVENT_TIMER, self.process_timer_event)


class GateiofRestApi(RestClient):
class GateiosRestApi(RestClient):
"""
Gateiof REST API
Gateios REST API
"""

def __init__(self, gateway: BaseGateway):
Expand Down Expand Up @@ -455,7 +455,6 @@ def on_send_order_failed(self, status_code: str, request: Request):
self.gateway.on_order(order)

msg = f"委托失败,状态码:{status_code},信息:{request.response.text}"

self.gateway.write_log(msg)

def on_send_order_error(
Expand All @@ -478,40 +477,28 @@ def on_cancel_order(self, data, request):
local_orderid = cancel_request.orderid
order = self.order_manager.get_order_with_local_orderid(local_orderid)

if self.check_error(data, "撤单"):
order.status = Status.REJECTED
if data["status"] == "error":
error_code = data["err_code"]
error_msg = data["err_msg"]
self.gateway.write_log(f"撤单失败,错误代码:{error_code},信息:{error_msg}")
else:
order.status = Status.CANCELLED
self.gateway.write_log(f"委托撤单成功:{order.orderid}")

self.order_manager.on_order(order)
self.order_manager.on_order(order)

def on_cancel_order_failed(self, status_code: str, request: Request):
"""
Callback when canceling order failed on server.
"""

msg = f"撤单失败,状态码:{status_code},信息:{request.response.text}"
self.gateway.write_log(msg)

def check_error(self, data: dict, func: str = ""):
""""""
if data["status"] != "error":
return False

error_code = data["err_code"]
error_msg = data["err_msg"]

self.gateway.write_log(f"{func}请求出错,代码:{error_code},信息:{error_msg}")
return True


class GateiofWebsocketApi(WebsocketClient):
class GateiosWebsocketApi(WebsocketClient):
""""""

def __init__(self, gateway):
""""""
super(GateiofWebsocketApi, self).__init__()
super(GateiosWebsocketApi, self).__init__()

self.gateway = gateway
self.gateway_name = gateway.gateway_name
Expand Down Expand Up @@ -545,22 +532,22 @@ def connect(
self.init(WEBSOCKET_HOST, proxy_host, proxy_port)
else:
self.init(TESTNET_WEBSOCKET_HOST, proxy_host, proxy_port)

self.start()

def on_connected(self):
""""""
self.gateway.write_log("Websocket API连接成功")

for symbol in self.symbols:
update_order = self.construct_req(
update_order = self.generate_req(
channel="futures.orders",
event="subscribe",
pay_load=[self.account_id, symbol]
)
self.send_packet(update_order)

update_position = self.construct_req(
update_position = self.generate_req(
channel="futures.position_closes",
event="subscribe",
pay_load=[self.account_id, symbol]
Expand All @@ -580,35 +567,26 @@ def subscribe(self, req: SubscribeRequest):
)
self.ticks[req.symbol] = tick

self.subscribe_data(req.symbol)

def subscribe_data(self, symbol: str):
""""""
update_tick = self.construct_req(
tick_req = self.generate_req(
channel="futures.tickers",
event="subscribe",
pay_load=[symbol]
pay_load=[req.symbol]
)
self.send_packet(update_tick)
self.send_packet(tick_req)

update_depth = self.construct_req(
depth_req = self.generate_req(
channel="futures.order_book",
event="subscribe",
pay_load=[symbol, "5", "0"]
pay_load=[req.symbol, "5", "0"]
)
self.send_packet(update_depth)

def update_info(self, symbol: str, account_id: str):
""""""

self.send_packet(depth_req)

def on_disconnected(self):
""""""
self.gateway.write_log("Websocket API连接断开")

def on_packet(self, packet: dict):
""""""

timestamp = packet["time"]
channel = packet["channel"]
event = packet["event"]
Expand All @@ -619,14 +597,14 @@ def on_packet(self, packet: dict):
self.gateway.write_log("Websocket API报错:%s" % error)
return

print(packet)

if channel == "futures.tickers":
if event == "update":
self.on_tick(result, timestamp)

elif channel == "futures.order_book":
if event == "all":
self.on_depth(result, timestamp)

elif channel == "futures.orders":
if event == "update":
self.on_order(result, timestamp)
Expand All @@ -639,11 +617,10 @@ def on_error(self, exception_type: type, exception_value: Exception, tb):
sys.stderr.write(self.exception_detail(
exception_type, exception_value, tb))

def construct_req(self, channel: str, event: str, pay_load: list):
def generate_req(self, channel: str, event: str, pay_load: list):
""""""
expires = int(time.time())
signature = generate_websocket_sign(
self.secret, channel, event, expires)
signature = generate_websocket_sign(self.secret, channel, event, expires)

req = {
"time": expires,
Expand All @@ -661,7 +638,6 @@ def construct_req(self, channel: str, event: str, pay_load: list):

def on_tick(self, l: list, t: int):
""""""

d = l[0]
symbol = d["contract"]
tick = self.ticks.get(symbol, None)
Expand Down

0 comments on commit 4336ccf

Please sign in to comment.