ccxws icon indicating copy to clipboard operation
ccxws copied to clipboard

Huobi: add order book depth step parameter

Open MidoMiddle opened this issue 3 years ago • 0 comments

I propose add l2depthStep:

// ccxws/src/exchanges/huobi-client.js
class HuobiClient extends HuobiBase {
  constructor({ wssPath = "wss://api.huobi.pro/ws", watcherMs, l2depthStep } = {}) { // <=== l2depthStep
    super({ name: "Huobi", wssPath, watcherMs, l2depthStep });  // <=== l2depthStep
  }
}

// ccxws/src/exchanges/huobi-base.js
class HuobiBase extends BasicClient {
  constructor({ name, wssPath, watcherMs, l2depthStep = 0 }) {  // <=== l2depthStep
    super(wssPath, name, undefined, watcherMs);
    this.l2depthStep = l2depthStep;                   // <=== l2depthStep
  }

  _sendSubLevel2Snapshots(remote_id) {
    this._wss.send(
      JSON.stringify({
        sub: `market.${remote_id}.depth.step${this.l2depthStep}`,  // <=== this.l2depthStep
        id: "depth_" + remote_id,
      })
    );
  }

  _sendUnsubLevel2Snapshots(remote_id) {
    this._wss.send(
      JSON.stringify({
        unsub: `market.${remote_id}.depth.step${this.l2depthStep}`,   // <=== this.l2depthStep
      })
    );
  }

_onMessage(raw) {
    zlib.unzip(raw, (err, resp) => {
      if (err) {
        this.emit("error", err);
        return;
      }

      let msgs = JSON.parse(resp);


      // l2snapshot
      if (msgs.ch.endsWith(`depth.step${this.l2depthStep}`)) {   // <=== this.l2depthStep
        let remoteId = msgs.ch.split(".")[1];
        let market = this._level2SnapshotSubs.get(remoteId);
        if (!market) return;

        let snapshot = this._constructLevel2Snapshot(msgs, market);
        this.emit("l2snapshot", snapshot, market);
        return;
      }
    });
  }


MidoMiddle avatar Nov 12 '20 11:11 MidoMiddle