danpaquin / coinbasepro-python

The unofficial Python client for the Coinbase Pro API
MIT License
1.82k stars 738 forks source link

Websocket missing data #327

Closed bastulli closed 5 years ago

bastulli commented 5 years ago

I'm listening to onMessage's matching orders and plotting the OHLC with volume. I noticed that there are times where the data is missing and no messages come in.

btcplot ltccap

Does anyone else have any issues with missing Websocket data? It may be the way i'm saving the data.

class myWebsocketClient(cbpro.WebsocketClient):
    def on_open(self):
        self.url = "wss://ws-feed.pro.coinbase.com/"
        self.products = ["BTC-USD", "ETH-USD", "LTC-USD"]
        self.message_count = 0
        self.data_ltc = pd.DataFrame()
        self.data_eth = pd.DataFrame()
        self.data_btc = pd.DataFrame()

        print("Starting cbpro.WebsocketClient!")

    def on_message(self, msg):

        self.message_count += 1

        if self.message_count >= 100000:
            self.message_count = 0

        else:
            pass

        if  msg['product_id'] == 'LTC-USD' and msg['type'] == 'match':

            try:

                time = ciso8601.parse_datetime_as_naive(msg['time'])
                self.data_ltc = self.data_ltc.append(pd.DataFrame({'product': msg['product_id'], 'close': msg["price"],'volume':msg["size"]},index=[time]))
                self.data_ltc.sort_index(axis=0, inplace=True)
                cutout = self.data_ltc.index[-1] - dt.timedelta(minutes = 5)
                self.data_ltc = self.data_ltc[self.data_ltc.index >= cutout]

            except:
                print('LTC Error')

        elif msg['product_id'] == 'ETH-USD' and msg['type'] == 'match':

            try:
                time = ciso8601.parse_datetime_as_naive(msg['time'])
                self.data_eth = self.data_eth.append(pd.DataFrame({'product': msg['product_id'], 'close': msg["price"],'volume':msg["size"]},index=[time]))
                self.data_eth.sort_index(axis=0, inplace=True)
                cutout = self.data_eth.index[-1] - dt.timedelta(minutes = 5)
                self.data_eth = self.data_eth[self.data_eth.index >= cutout]

            except:
                print('ETH Error')

        elif msg['product_id'] == 'BTC-USD' and msg['type'] == 'match':

            try:
                time = ciso8601.parse_datetime_as_naive(msg['time'])
                self.data_btc = self.data_btc.append(pd.DataFrame({'product': msg['product_id'], 'close': msg["price"],'volume':msg["size"]},index=[time]))
                self.data_btc.sort_index(axis=0, inplace=True)
                cutout = self.data_btc.index[-1] - dt.timedelta(minutes = 5)
                self.data_btc = self.data_btc[self.data_btc.index >= cutout]

            except:
                print('BTC Error')

        else:
            pass

    def on_close(self):
        print("-- Goodbye! --")
bastulli commented 5 years ago

Update, listening to matching orders with the websocket is works just fine. I had to rewrite the way I save and plot the data. No more missing data

plot