Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Unable to recreate order book with l2 data #781

@benjamin-wilson

Description

@benjamin-wilson

I am trying to recreate the coinbase pro orderbook however it doesn't seem to be reflecting what I see though the coinbase pro UI and the longer the code runs the wider the spread gets, indicating something isn't right.

    this.coinbasePro.ws.on(WebSocketEvent.ON_OPEN, async () => {
      await this.coinbasePro.ws.subscribe([
        {
          name: WebSocketChannelName.LEVEL2,
          product_ids: ['BTC-USD']
        }
      ]);
    });

    this.coinbasePro.ws.connect({ debug: false });

    this.coinbaseOrderBook$ = new Observable((subscriber) => {

      this.coinbasePro.ws.on(WebSocketEvent.ON_MESSAGE_L2SNAPSHOT, async snapshot => {

        let asks = {};
        let bids = {};

        snapshot.asks.forEach(([price, amount]) => (asks[price] = parseFloat(amount)));
        snapshot.bids.forEach(([price, amount]) => (bids[price] = parseFloat(amount)));

        this.coinbasePro.ws.on(WebSocketEvent.ON_MESSAGE_L2UPDATE, async (snapshotUpdate) => {

          snapshotUpdate.changes.forEach(([action, price, amount]) => {
            const obj = action == 'buy' ? bids : asks;
            if (amount != '0.00000000') {
              obj[price] = parseFloat(amount);
            }
            else {
              // Sometimes data doesn't exist... I don't think this should happen?
              if (obj[price] != null) {
                delete obj[price];
              }
            }
          });

          subscriber.next({ bids, asks });
        });

      });
    });

I also noticed the snapshotUpdate.changes length is always 1 whereas the coinbase pro GUI has variable length, not sure if that has anything to do with it.

Often changes come through with the amount '0.00000000' indicating they need to be deleted from the order book but they don't exist. It feels like there is missing data somewhere.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions