# https://docs.pro.coinbase.com/#websocket-feed import base class Coinbase(base.Base): def onOpen(self): for instrument in base.instruments.instruments['coinbase']: params = { "type": "subscribe", "channels": [{"name": "matches", "product_ids": [instrument]}] } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('coinbaseTrades', payload) base.createConnection("wss://ws-feed.pro.coinbase.com", 443, Coinbase)
import base class Zb(base.Base): def onOpen(self): for instrument in base.instruments.instruments['zb']: params = { "event": "addChannel", "channel": "{}_trades".format(instrument) } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('zbTrades', payload) base.createConnection("wss://api.zb.com:9999/websocket", 443, Zb)
import base class Phemex(base.Base): def sendPingToServer(self): # Check if websocket connection is open if self.state == 3: pingMsg = {"id": 1234, "method": "server.ping", "params": []} self.sendMessage(str(pingMsg).encode()) def onOpen(self): for instrument in base.instruments.instruments['phemex']: params = { "id": 1234, "method": "trade.subscribe", "params": [instrument] } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) heartbeat = base.task.LoopingCall(self.sendPingToServer) heartbeat.start(5) def onMessage(self, payload, isBinary): self.producer.send('phemexTrades', payload) base.createConnection("wss://phemex.com/ws", 443, Phemex)
# https://github.com/binance-us/binance-official-api-docs/blob/master/web-socket-streams.md import base class BinanceUs(base.Base): def onMessage(self, payload, isBinary): self.producer.send('binanceUsTrades', payload) class BinanceUsOB(base.Base): def onMessage(self, payload, isBinary): self.producer.send('binanceUsOrderBooks', payload) base.createConnection( "wss://stream.binance.us:9443/stream?streams={}".format( base.instruments.instruments['binanceus']), 9443, BinanceUs) base.createConnection( "wss://stream.binance.us:9443/stream?streams={}".format( base.instruments.instruments['binanceusOB']), 9443, BinanceUsOB)
import base class Okex(base.Base): def onOpen(self): params = { "op": "subscribe", "args": base.instruments.instruments['okex'] } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('okexTrades', payload) base.createConnection("wss://real.okex.com:8443/ws/v3", 8443, Okex)
import base class Kraken(base.Base): def onOpen(self): params = { "event": "subscribe", "pair": base.instruments.instruments['kraken'], "subscription": { "name": "trade" } } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('krakenTrades', payload) base.createConnection("wss://ws.kraken.com", 443, Kraken)
class BitfinexOB175(BitfinexOB): begin, end = 175, 210 class BitfinexOB210(BitfinexOB): begin, end = 210, 245 class BitfinexOB245(BitfinexOB): begin, end = 245, 272 # Multiple connections to bypass rate limits base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex35) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex70) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex105) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex140) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex175) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex210) base.createConnection("wss://api.bitfinex.com/ws/2", 443, Bitfinex245) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB35) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB70) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB105) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB140) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB175) base.createConnection("wss://api.bitfinex.com/ws/2", 443, BitfinexOB210)
class Bibox(base.Base): def onOpen(self): for instrument in base.instruments.instruments['bibox']: params = { "event": "addChannel", "channel": "bibox_sub_spot_{}_deals".format(instrument) } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('biboxTrades', payload) class BiboxOB(base.Base): def onOpen(self): for instrument in base.instruments.instruments['bibox']: params = { "event": "addChannel", "channel": "bibox_sub_spot_{}_depth".format(instrument) } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('biboxOrderBooks', payload) base.createConnection("wss://push.bibox.com/", 443, Bibox) base.createConnection("wss://push.bibox.com/", 443, BiboxOB)
"channel": 'lightning_executions_{}'.format(instrument) } } subscription = base.json.dumps(msgParams) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('bitflyerTrades', payload) class BitflyerOB(Bitflyer): def onOpen(self): for instrument in base.instruments.instruments['bitflyer']: msgParams = { "method": "subscribe", "params": { "channel": 'lightning_board_snapshot_{}'.format(instrument) } } subscription = base.json.dumps(msgParams) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('bitflyerOrderBooks', payload) base.createConnection("wss://ws.lightstream.bitflyer.com/json-rpc", 443, Bitflyer) base.createConnection("wss://ws.lightstream.bitflyer.com/json-rpc", 443, BitflyerOB)
# https://docs.deribit.com/?python#trades-instrument_name-interval import base class Deribit(base.Base): def onOpen(self): params = { "jsonrpc": "2.0", "method": "public/subscribe", "id": 42, "params": { "channels": ["trades.BTC-PERPETUAL.raw", "trades.ETH-PERPETUAL.raw"] } } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('deribitTrades', payload) base.createConnection("wss://test.deribit.com/ws/api/v2", 443, Deribit)
# https://www.bitmex.com/app/wsAPI import base class Bitmex(base.Base): def onMessage(self, payload, isBinary): self.producer.send('bitmexTrades', payload) class BitmexOB(Bitmex): def onMessage(self, payload, isBinary): self.producer.send('bitmexOrderBooks', payload) base.createConnection( "wss://www.bitmex.com/realtime?subscribe={}".format( base.instruments.instruments['bitmex']), 443, Bitmex) base.createConnection( "wss://www.bitmex.com/realtime?subscribe={}".format( base.instruments.instruments['bitmexOB']), 443, BitmexOB)
import base class Poloniex(base.Base): def onOpen(self): for instrument in base.instruments.instruments['poloniex']: params = { 'command': 'subscribe', 'channel': '{}'.format(instrument) } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('poloniexTrades', payload) base.createConnection("wss://api2.poloniex.com/", 443, Poloniex)
if element['price'] == ask[0]: del self.ob[msg['params'][-1]]['asks'][cnt] self.ob[msg['params'][-1]]['time'] = msg[ 'params'][1]['time'] break else: self.ob[msg['params'][-1]]['asks'].append({ 'price': float(ask[0]), 'amount': float(ask[1]) }) self.ob[msg['params'] [-1]]['time'] = msg['params'][1]['time'] print(self.ob) except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno) #self.producer.send('coinexOrderBooks', payload) base.createConnection("wss://socket.coinex.com/", 443, CoinEx) #base.createConnection("wss://socket.coinex.com/", 443, CoinExOB) #base.reactor.run()
if payload != b'pong_p': self.producer.send('bitforexTrades', payload) class BitforexOB(Bitforex): def onOpen(self): for instrument in base.instruments.instruments['bitforex']: params = [{ "type": "subHq", "event": "depth10", "param": { "businessType": "coin-{}".format(instrument), "dType": 0 } }] subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) heartbeat = base.task.LoopingCall(self.sendPingToServer) heartbeat.start(60) def onMessage(self, payload, isBinary): if payload != b'pong_p': self.producer.send('bitforexOrderBooks', payload) base.createConnection("wss://www.bitforex.com/mkapi/coinGroup1/ws", 443, Bitforex) base.createConnection("wss://www.bitforex.com/mkapi/coinGroup1/ws", 443, BitforexOB)
import base class Hitbtc(base.Base): def onOpen(self): for instrument in base.instruments.instruments['hitbtc']: params = { "method": "subscribeTrades", "params": { "symbol": "{}".format(instrument), "limit": 1 }, "id": 123 } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('hitbtcTrades', payload) base.createConnection("wss://api.hitbtc.com/api/2/ws", 443, Hitbtc)
import base class Huobi(base.Base): def sendPingToServer(self): # Check if websocket connection is open if self.state == 3: msg = base.json.dumps({'pong': 123}) self.sendMessage(msg.encode('utf-8')) def onOpen(self): for instrument in base.instruments.instruments['huobi']: msgParams = { "sub": "market.{}.trade.detail".format(instrument), "id": "id1" } subscription = base.json.dumps(msgParams) self.sendMessage(subscription.encode('utf8')) heartbeat = base.task.LoopingCall(self.sendPingToServer) heartbeat.start(5) def onMessage(self, payload, isBinary): self.producer.send('huobiTrades', payload) base.createConnection("wss://api.huobi.pro/ws", 443, Huobi)
# https://docs.gemini.com/websocket-api/ import base import instruments class Gemini(base.Base): def onOpen(self): params = '{"type": "subscribe","subscriptions":[{"name":"l2","symbols":' + ( instruments.instruments['gemini']) + '}]}' self.sendMessage(params.encode('utf8')) def onMessage(self, payload, isBinary): payload = base.json.loads(payload.decode('utf-8')) if payload['type'] == 'trade': self.producer.send('geminiTrades', base.json.dumps(payload).encode('utf-8')) base.createConnection("wss://api.gemini.com/v2/marketdata", 443, Gemini)
# https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md import base class Binance(base.Base): def onMessage(self, payload, isBinary): self.producer.send('binanceTrades', payload) class BinanceOB(Binance): def onMessage(self, payload, isBinary): self.producer.send('binanceOrderBooks', payload) base.createConnection( "wss://stream.binance.com:9443/stream?streams={}".format( base.instruments.instruments['binance']), 9443, Binance) base.createConnection( "wss://stream.binance.com:9443/stream?streams={}".format( base.instruments.instruments['binanceOB']), 9443, BinanceOB)
'event': 'bts:subscribe', 'data': { 'channel': 'live_trades_{}'.format(instrument) } } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('bitstampTrades', payload) class BitstampOB(Bitstamp): def onOpen(self): for instrument in base.instruments.instruments['bitstamp']: params = { 'event': 'bts:subscribe', 'data': { 'channel': 'order_book_{}'.format(instrument) } } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('bitstampOrderBooks', payload) base.createConnection("wss://ws.bitstamp.net", 443, Bitstamp) base.createConnection("wss://ws.bitstamp.net", 443, BitstampOB)
heartbeat = base.task.LoopingCall(self.sendPingToServer) heartbeat.start(50) def onMessage(self, payload, isBinary): self.producer.send('kucoinTrades', payload) class Kucoin100(Kucoin): start, end = 99, 198 class Kucoin200(Kucoin): start, end = 198, 297 class Kucoin300(Kucoin): start, end = 297, 396 class Kucoin400(Kucoin): start, end = 396, 430 # Request token to establish websocket connection getToken = base.json.loads((base.requests.post('https://api.kucoin.com/api/v1/bullet-public').content).decode('utf-8')) base.createConnection("wss://push-private.kucoin.com/endpoint?token={}".format(getToken['data']['token']), 443, Kucoin) base.createConnection("wss://push-private.kucoin.com/endpoint?token={}".format(getToken['data']['token']), 443, Kucoin100) base.createConnection("wss://push-private.kucoin.com/endpoint?token={}".format(getToken['data']['token']), 443, Kucoin200) base.createConnection("wss://push-private.kucoin.com/endpoint?token={}".format(getToken['data']['token']), 443, Kucoin300) base.createConnection("wss://push-private.kucoin.com/endpoint?token={}".format(getToken['data']['token']), 443, Kucoin400)
# https://www.gate.io/docs/websocket/index.html import base class Gate(base.Base): def sendPingToServer(self): if self.state == 3: self.sendMessage( '{"id":12312, "method":"server.ping", "params":[]}'.encode()) def onOpen(self): params = { "id": 12312, "method": "trades.subscribe", "params": base.instruments.instruments['gate'] } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) heartbeat = base.task.LoopingCall(self.sendPingToServer) heartbeat.start(60) def onMessage(self, payload, isBinary): self.producer.send('gateTrades', payload) base.createConnection("wss://ws.gate.io/v3/", 443, Gate)
"channel": "market_{}_trade_ticker".format(instrument), "cb_id": "Customer" } } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('bikiTrades', payload) class BikiOB(Biki): def onOpen(self): for instrument in base.instruments.instruments['biki']: params = { "event": "sub", "params": { "channel": "market_{}_depth_step1".format(instrument), "cb_id": "Customer" } } subscription = base.json.dumps(params) self.sendMessage(subscription.encode('utf8')) def onMessage(self, payload, isBinary): self.producer.send('bikiOrderBooks', payload) base.createConnection("wss://ws.biki.com/kline-api/ws", 443, Biki) base.createConnection("wss://ws.biki.com/kline-api/ws", 443, BikiOB)