Skip to content

Instantly share code, notes, and snippets.

@shredding
Created June 20, 2017 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shredding/e449e1f06cbcf2007e28094cc2e30338 to your computer and use it in GitHub Desktop.
Save shredding/e449e1f06cbcf2007e28094cc2e30338 to your computer and use it in GitHub Desktop.
def connect_to_stream(self, callback):
self.order_book = GDAXOrderBook(api=self, callback=callback, product_id=self.currency_pair)
self.order_book.start()
return self
class GDAXOrderBook(gdax_api.OrderBook):
def __init__(self, api, callback, *args, **kwargs):
super().__init__(*args, **kwargs)
self.api = api
self.callback = callback
def onOpen(self):
logging.getLogger('notifications').info('GDAX Order Book is connected to the trading stream for {}.'.format(
self.api.currency_pair
), extra={'channel': self.api.currency_pair})
def onClose(self):
logging.getLogger('notifications').info(
'GDAX Order Book has been disconnected from the trading stream for {}.'.format(self.api.currency_pair),
extra={'channel': self.api.currency_pair}
)
self.api.connect_to_stream(self.callback)
def onMessage(self, message):
super().onMessage(message)
bid = self.get_bid()
bids = self.get_bids(bid)
bid_depth = sum([b['size'] for b in bids])
ask = self.get_ask()
asks = self.get_asks(ask)
ask_depth = sum([a['size'] for a in asks])
self.callback(self.api, {
'bid_depth': bid_depth,
'bid':bid,
'ask_depth': ask_depth,
'ask': ask
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment