[Mod] complete general test of coinbase gateway
This commit is contained in:
parent
019da35a55
commit
77c2b52842
@ -28,6 +28,7 @@ from vnpy.gateway.okexs import OkexsGateway
|
|||||||
# from vnpy.gateway.tora import ToraGateway
|
# from vnpy.gateway.tora import ToraGateway
|
||||||
# from vnpy.gateway.alpaca import AlpacaGateway
|
# from vnpy.gateway.alpaca import AlpacaGateway
|
||||||
from vnpy.gateway.da import DaGateway
|
from vnpy.gateway.da import DaGateway
|
||||||
|
from vnpy.gateway.coinbase import CoinbaseGateway
|
||||||
|
|
||||||
from vnpy.app.cta_strategy import CtaStrategyApp
|
from vnpy.app.cta_strategy import CtaStrategyApp
|
||||||
# from vnpy.app.csv_loader import CsvLoaderApp
|
# from vnpy.app.csv_loader import CsvLoaderApp
|
||||||
@ -69,8 +70,9 @@ def main():
|
|||||||
# main_engine.add_gateway(TapGateway)
|
# main_engine.add_gateway(TapGateway)
|
||||||
# main_engine.add_gateway(ToraGateway)
|
# main_engine.add_gateway(ToraGateway)
|
||||||
# main_engine.add_gateway(AlpacaGateway)
|
# main_engine.add_gateway(AlpacaGateway)
|
||||||
main_engine.add_gateway(OkexsGateway)
|
# main_engine.add_gateway(OkexsGateway)
|
||||||
main_engine.add_gateway(DaGateway)
|
# main_engine.add_gateway(DaGateway)
|
||||||
|
main_engine.add_gateway(CoinbaseGateway)
|
||||||
|
|
||||||
main_engine.add_app(CtaStrategyApp)
|
main_engine.add_app(CtaStrategyApp)
|
||||||
main_engine.add_app(CtaBacktesterApp)
|
main_engine.add_app(CtaBacktesterApp)
|
||||||
|
@ -41,6 +41,7 @@ from vnpy.trader.object import (
|
|||||||
|
|
||||||
REST_HOST = "https://api.pro.coinbase.com"
|
REST_HOST = "https://api.pro.coinbase.com"
|
||||||
WEBSOCKET_HOST = "wss://ws-feed.pro.coinbase.com"
|
WEBSOCKET_HOST = "wss://ws-feed.pro.coinbase.com"
|
||||||
|
|
||||||
SANDBOX_REST_HOST = "https://api-public.sandbox.pro.coinbase.com"
|
SANDBOX_REST_HOST = "https://api-public.sandbox.pro.coinbase.com"
|
||||||
SANDBOX_WEBSOCKET_HOST = "wss://ws-feed-public.sandbox.pro.coinbase.com"
|
SANDBOX_WEBSOCKET_HOST = "wss://ws-feed-public.sandbox.pro.coinbase.com"
|
||||||
|
|
||||||
@ -68,6 +69,7 @@ TIMEDELTA_MAP = {
|
|||||||
cancelDict = {} # orderid:cancelreq
|
cancelDict = {} # orderid:cancelreq
|
||||||
orderDict = {} # sysid:order
|
orderDict = {} # sysid:order
|
||||||
orderSysDict = {} # orderid:sysid
|
orderSysDict = {} # orderid:sysid
|
||||||
|
symbol_name_map = {}
|
||||||
|
|
||||||
|
|
||||||
class CoinbaseGateway(BaseGateway):
|
class CoinbaseGateway(BaseGateway):
|
||||||
@ -84,11 +86,13 @@ class CoinbaseGateway(BaseGateway):
|
|||||||
"proxy_host": "",
|
"proxy_host": "",
|
||||||
"proxy_port": "",
|
"proxy_port": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
exchanges = [Exchange.COINBASE]
|
exchanges = [Exchange.COINBASE]
|
||||||
|
|
||||||
def __init__(self, event_engine):
|
def __init__(self, event_engine):
|
||||||
"""Constructor"""
|
"""Constructor"""
|
||||||
super(CoinbaseGateway, self).__init__(event_engine, "COINBASE")
|
super(CoinbaseGateway, self).__init__(event_engine, "COINBASE")
|
||||||
|
|
||||||
self.rest_api = CoinbaseRestApi(self)
|
self.rest_api = CoinbaseRestApi(self)
|
||||||
self.ws_api = CoinbaseWebsocketApi(self)
|
self.ws_api = CoinbaseWebsocketApi(self)
|
||||||
|
|
||||||
@ -107,8 +111,15 @@ class CoinbaseGateway(BaseGateway):
|
|||||||
else:
|
else:
|
||||||
proxy_port = 0
|
proxy_port = 0
|
||||||
|
|
||||||
self.rest_api.connect(key, secret, passphrase, session_number, server,
|
self.rest_api.connect(
|
||||||
proxy_host, proxy_port)
|
key,
|
||||||
|
secret,
|
||||||
|
passphrase,
|
||||||
|
session_number,
|
||||||
|
server,
|
||||||
|
proxy_host,
|
||||||
|
proxy_port
|
||||||
|
)
|
||||||
|
|
||||||
self.ws_api.connect(
|
self.ws_api.connect(
|
||||||
key,
|
key,
|
||||||
@ -186,8 +197,8 @@ class CoinbaseWebsocketApi(WebsocketClient):
|
|||||||
"done": self.on_order_done,
|
"done": self.on_order_done,
|
||||||
"match": self.on_order_match,
|
"match": self.on_order_match,
|
||||||
}
|
}
|
||||||
self.ticks = {}
|
|
||||||
|
|
||||||
|
self.ticks = {}
|
||||||
self.accounts = {}
|
self.accounts = {}
|
||||||
self.orderbooks = {}
|
self.orderbooks = {}
|
||||||
|
|
||||||
@ -349,10 +360,9 @@ class CoinbaseWebsocketApi(WebsocketClient):
|
|||||||
orderid=order.orderid,
|
orderid=order.orderid,
|
||||||
tradeid=packet['trade_id'],
|
tradeid=packet['trade_id'],
|
||||||
direction=DIRECTION_COINBASE2VT[packet['side']],
|
direction=DIRECTION_COINBASE2VT[packet['side']],
|
||||||
price=packet['price'],
|
price=float(packet['price']),
|
||||||
volume=packet['size'],
|
volume=float(packet['size']),
|
||||||
time=datetime.strptime(
|
time=packet['time'],
|
||||||
packet['time'], "%Y-%m-%dT%H:%M:%S.%fZ"),
|
|
||||||
gateway_name=self.gateway_name,
|
gateway_name=self.gateway_name,
|
||||||
)
|
)
|
||||||
self.gateway.on_trade(trade)
|
self.gateway.on_trade(trade)
|
||||||
@ -372,7 +382,12 @@ class OrderBook():
|
|||||||
self.bids = dict()
|
self.bids = dict()
|
||||||
self.gateway = gateway
|
self.gateway = gateway
|
||||||
self.newest_tick = TickData(
|
self.newest_tick = TickData(
|
||||||
"COINBASE", symbol, exchange, datetime.now())
|
symbol=symbol,
|
||||||
|
exchange=exchange,
|
||||||
|
name=symbol_name_map.get(symbol, ""),
|
||||||
|
datetime=datetime.now(),
|
||||||
|
gateway_name=gateway.gateway_name,
|
||||||
|
)
|
||||||
self.first_update = False
|
self.first_update = False
|
||||||
|
|
||||||
def on_message(self, d: dict):
|
def on_message(self, d: dict):
|
||||||
@ -583,16 +598,19 @@ class CoinbaseRestApi(RestClient):
|
|||||||
def on_query_account(self, data, request):
|
def on_query_account(self, data, request):
|
||||||
""""""
|
""""""
|
||||||
for acc in data:
|
for acc in data:
|
||||||
account_id = str(acc['id'])
|
account_id = str(acc['currency'])
|
||||||
|
|
||||||
account = self.accounts.get(account_id, None)
|
account = self.accounts.get(account_id, None)
|
||||||
if not account:
|
if not account:
|
||||||
account = AccountData(accountid=account_id,
|
account = AccountData(
|
||||||
gateway_name=self.gateway_name)
|
accountid=account_id,
|
||||||
|
gateway_name=self.gateway_name
|
||||||
|
)
|
||||||
self.accounts[account_id] = account
|
self.accounts[account_id] = account
|
||||||
|
|
||||||
account.balance = acc.get("balance", account.balance)
|
account.balance = float(acc.get("balance", account.balance))
|
||||||
account.available = acc.get("available", account.available)
|
account.available = float(acc.get("available", account.available))
|
||||||
account.frozen = acc.get("hold", account.frozen)
|
account.frozen = float(acc.get("hold", account.frozen))
|
||||||
|
|
||||||
self.gateway.on_account(copy(account))
|
self.gateway.on_account(copy(account))
|
||||||
|
|
||||||
@ -670,17 +688,18 @@ class CoinbaseRestApi(RestClient):
|
|||||||
exchange=Exchange.COINBASE,
|
exchange=Exchange.COINBASE,
|
||||||
name=d['display_name'],
|
name=d['display_name'],
|
||||||
product=Product.SPOT,
|
product=Product.SPOT,
|
||||||
pricetick=d['quote_increment'],
|
pricetick=float(d['quote_increment']),
|
||||||
size=d['base_min_size'],
|
size=1,
|
||||||
stop_supported=False,
|
min_volume=float(d['base_min_size']),
|
||||||
net_position=True,
|
net_position=True,
|
||||||
history_data=False,
|
history_data=False,
|
||||||
gateway_name=self.gateway_name,
|
gateway_name=self.gateway_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.gateway.on_contract(contract)
|
self.gateway.on_contract(contract)
|
||||||
|
symbol_name_map[contract.symbol] = contract.name
|
||||||
|
|
||||||
self.gateway.write_log("")
|
self.gateway.write_log("合约信息查询成功")
|
||||||
|
|
||||||
def send_order(self, req: OrderRequest):
|
def send_order(self, req: OrderRequest):
|
||||||
""""""
|
""""""
|
||||||
|
Loading…
Reference in New Issue
Block a user