Created
June 20, 2017 15:44
-
-
Save shredding/e449e1f06cbcf2007e28094cc2e30338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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