diff --git a/tests/trader/run.py b/tests/trader/run.py index 783c88d9..130aa088 100644 --- a/tests/trader/run.py +++ b/tests/trader/run.py @@ -4,28 +4,28 @@ from vnpy.trader.engine import MainEngine from vnpy.trader.ui import MainWindow, create_qapp from vnpy.gateway.binance import BinanceGateway -#from vnpy.gateway.bitmex import BitmexGateway -#from vnpy.gateway.futu import FutuGateway -#from vnpy.gateway.ib import IbGateway -#from vnpy.gateway.ctp import CtpGateway +# from vnpy.gateway.bitmex import BitmexGateway +# from vnpy.gateway.futu import FutuGateway +# from vnpy.gateway.ib import IbGateway +# from vnpy.gateway.ctp import CtpGateway # from vnpy.gateway.ctptest import CtptestGateway -#from vnpy.gateway.femas import FemasGateway -#from vnpy.gateway.tiger import TigerGateway -#from vnpy.gateway.oes import OesGateway -#from vnpy.gateway.okex import OkexGateway -#from vnpy.gateway.huobi import HuobiGateway -#from vnpy.gateway.bitfinex import BitfinexGateway -#from vnpy.gateway.onetoken import OnetokenGateway -#from vnpy.gateway.okexf import OkexfGateway -#from vnpy.gateway.xtp import XtpGateway -#from vnpy.gateway.hbdm import HbdmGateway +# from vnpy.gateway.femas import FemasGateway +# from vnpy.gateway.tiger import TigerGateway +# from vnpy.gateway.oes import OesGateway +# from vnpy.gateway.okex import OkexGateway +# from vnpy.gateway.huobi import HuobiGateway +# from vnpy.gateway.bitfinex import BitfinexGateway +# from vnpy.gateway.onetoken import OnetokenGateway +# from vnpy.gateway.okexf import OkexfGateway +# from vnpy.gateway.xtp import XtpGateway +# from vnpy.gateway.hbdm import HbdmGateway -#from vnpy.app.cta_strategy import CtaStrategyApp -#from vnpy.app.csv_loader import CsvLoaderApp -#from vnpy.app.algo_trading import AlgoTradingApp -#from vnpy.app.cta_backtester import CtaBacktesterApp -#from vnpy.app.data_recorder import DataRecorderApp -#from vnpy.app.risk_manager import RiskManagerApp +# from vnpy.app.cta_strategy import CtaStrategyApp +# from vnpy.app.csv_loader import CsvLoaderApp +# from vnpy.app.algo_trading import AlgoTradingApp +# from vnpy.app.cta_backtester import CtaBacktesterApp +# from vnpy.app.data_recorder import DataRecorderApp +# from vnpy.app.risk_manager import RiskManagerApp def main(): @@ -37,28 +37,28 @@ def main(): main_engine = MainEngine(event_engine) main_engine.add_gateway(BinanceGateway) - #main_engine.add_gateway(XtpGateway) - #main_engine.add_gateway(CtpGateway) + # main_engine.add_gateway(XtpGateway) + # main_engine.add_gateway(CtpGateway) # main_engine.add_gateway(CtptestGateway) - #main_engine.add_gateway(FemasGateway) - #main_engine.add_gateway(IbGateway) - #main_engine.add_gateway(FutuGateway) - #main_engine.add_gateway(BitmexGateway) - #main_engine.add_gateway(TigerGateway) - #main_engine.add_gateway(OesGateway) - #main_engine.add_gateway(OkexGateway) - #main_engine.add_gateway(HuobiGateway) - #main_engine.add_gateway(BitfinexGateway) - #main_engine.add_gateway(OnetokenGateway) - #main_engine.add_gateway(OkexfGateway) - #main_engine.add_gateway(HbdmGateway) + # main_engine.add_gateway(FemasGateway) + # main_engine.add_gateway(IbGateway) + # main_engine.add_gateway(FutuGateway) + # main_engine.add_gateway(BitmexGateway) + # main_engine.add_gateway(TigerGateway) + # main_engine.add_gateway(OesGateway) + # main_engine.add_gateway(OkexGateway) + # main_engine.add_gateway(HuobiGateway) + # main_engine.add_gateway(BitfinexGateway) + # main_engine.add_gateway(OnetokenGateway) + # main_engine.add_gateway(OkexfGateway) + # main_engine.add_gateway(HbdmGateway) - #main_engine.add_app(CtaStrategyApp) - #main_engine.add_app(CtaBacktesterApp) - #main_engine.add_app(CsvLoaderApp) - #main_engine.add_app(AlgoTradingApp) - #main_engine.add_app(DataRecorderApp) - #main_engine.add_app(RiskManagerApp) + # main_engine.add_app(CtaStrategyApp) + # main_engine.add_app(CtaBacktesterApp) + # main_engine.add_app(CsvLoaderApp) + # main_engine.add_app(AlgoTradingApp) + # main_engine.add_app(DataRecorderApp) + # main_engine.add_app(RiskManagerApp) main_window = MainWindow(main_engine, event_engine) main_window.showMaximized() diff --git a/vnpy/gateway/binance/binance_gateway.py b/vnpy/gateway/binance/binance_gateway.py index 3c67ff2e..0415f186 100644 --- a/vnpy/gateway/binance/binance_gateway.py +++ b/vnpy/gateway/binance/binance_gateway.py @@ -1,20 +1,15 @@ -# encoding: UTF-8 - """ -币安交易接口 +Gateway for Binance Crypto Exchange. """ import re import urllib -import base64 import json -import zlib import hashlib import hmac import time from copy import copy from datetime import datetime -from threading import Thread from vnpy.event import Event from vnpy.api.rest import RestClient, Request @@ -57,16 +52,15 @@ ORDERTYPE_VT2BINANCE = { OrderType.MARKET: "MARKET", OrderType.STOP: "STOP_LOSS", } - ORDERTYPE_BINANCE2VT = {v: k for k, v in ORDERTYPE_VT2BINANCE.items()} DIRECTION_VT2BINANCE = { Direction.LONG: "BUY", Direction.SHORT: "SELL" } - DIRECTION_BINANCE2VT = {v: k for k, v in DIRECTION_VT2BINANCE.items()} + binance_symbols = set() symbol_name_map = {} @@ -80,15 +74,15 @@ class BinanceGateway(BaseGateway): "key": "", "secret": "", "session_number": 3, - "proxy_host": "127.0.0.1", - "proxy_port": 2000, + "proxy_host": "", + "proxy_port": 0, } exchanges = [Exchange.BINANCE] def __init__(self, event_engine): """Constructor""" - super(BinanceGateway, self).__init__(event_engine, "BINANCE") + super().__init__(event_engine, "BINANCE") self.order_manager = LocalOrderManager(self) @@ -106,15 +100,15 @@ 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) - #self.init_query() + # self.init_query() 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): """""" @@ -151,6 +145,7 @@ class BinanceGateway(BaseGateway): self.count = 0 self.event_engine.register(EVENT_TIMER, self.process_timer_event) + class BinanceRestApi(RestClient): """ BINANCE REST API @@ -448,7 +443,7 @@ class BinanceRestApi(RestClient): gateway_name=self.gateway_name, ) - print("委托查询--远端id:",sys_orderid, "本地Id:", local_orderid) + print("委托查询--远端id:", sys_orderid, "本地Id:", local_orderid) self.order_manager.on_order(order) self.gateway.write_log("委托信息查询成功") @@ -578,7 +573,7 @@ class BinanceWebsocketApiBase(WebsocketClient): self.key = "" self.secret = "" - #self.sign_host = "" + # self.sign_host = "" self.path = "" def connect( @@ -593,9 +588,9 @@ class BinanceWebsocketApiBase(WebsocketClient): self.key = key self.secret = secret - #host, path = _split_url(url) - #self.sign_host = host - #self.path = path + # host, path = _split_url(url) + # self.sign_host = host + # self.path = path self.init(url, proxy_host, proxy_port) self.start() @@ -603,7 +598,7 @@ class BinanceWebsocketApiBase(WebsocketClient): def login(self): """""" params = {"op": "auth"} - #params.update(create_signature(self.key, "GET", self.sign_host, self.path, self.secret)) + # params.update(create_signature(self.key, "GET", self.sign_host, self.path, self.secret)) return self.send_packet(params) def on_login(self, packet): @@ -616,14 +611,14 @@ class BinanceWebsocketApiBase(WebsocketClient): print("==============unpack_data============") print(data) return json.loads(data) - #return json.loads(zlib.decompress(data, zlib.Z_BEST_COMPRESSION)) + # return json.loads(zlib.decompress(data, zlib.Z_BEST_COMPRESSION)) def on_packet(self, packet): """""" print("=============on_packet=============") - print("event type:"+packet["e"]) + print("event type:" + packet["e"]) print(packet) - #if packet["e"] == "executionReport": + # if packet["e"] == "executionReport": if "ping" in packet: req = {"pong": packet["ping"]} self.send_packet(req) @@ -731,7 +726,7 @@ class BinanceTradeWebsocketApi(BinanceWebsocketApiBase): # Push order event order.traded += traded_volume - order.status = STATUS_BINANCE2VT.get(packet["X"], None) + order.status = STATUS_BINANCE2VT.get(data["X"], None) order.price = float(data["L"]) order.time = data["O"] order.symbol = data["s"] @@ -760,6 +755,7 @@ class BinanceTradeWebsocketApi(BinanceWebsocketApiBase): """""" self.gateway.on_account(data) + class BinanceDataWebsocketApi(BinanceWebsocketApiBase): """""" @@ -783,7 +779,7 @@ class BinanceDataWebsocketApi(BinanceWebsocketApiBase): """""" symbol = req.symbol print("============BinanceDataWebsocketApi.subscribe===========") - print("symbol"+symbol) + print("symbol" + symbol) # Create tick data buffer tick = TickData( symbol=symbol, @@ -871,4 +867,4 @@ def _split_url(url): """ result = re.match("\w+://([^/]*)(.*)", url) # noqa if result: - return result.group(1), result.group(2) \ No newline at end of file + return result.group(1), result.group(2)