bybit-api icon indicating copy to clipboard operation
bybit-api copied to clipboard

Unsubscribing from subscribePublicSpotV1Kline

Open LukeKoz opened this issue 2 years ago • 3 comments

Is there a way to unsubscribe from subscribePublicSpotV1Kline. I have tried to make use of unsubscribe() with a topic like kline_1m like you would expect with other subscriptions (e.g. linear subscriptions) but not having any luck.

Not sure of the proper way to do this.

Thanks

LukeKoz avatar Apr 28 '22 23:04 LukeKoz

Admittedly not the most intuitive (or documented) at the moment, and more complicated since bybit's spot functionality is very different to the rest...unfortunately.

Under the hood, the subscribePublicSpotV1Kline method sends a sub event via the corresponding websocket: https://github.com/tiagosiebler/bybit-api/blob/master/src/websocket-client.ts#L689-L696

According to the docs, unsubscription from a topic also follows a similar method: https://bybit-exchange.github.io/docs/spot/#t-websocketfilters

{"symbol":"BTCUSDT","topic":"trade","event":"cancel","params":{"binary":false}}

Something like this should work, but would need to be added to the client (as the tryWsSend method is regrettably private):

  public unsubscribePublicSpotV1Kline(symbol: string, candleSize: KlineInterval, binary?: boolean) {
    if (!this.isSpot()) {
      throw this.wrongMarketError('spot');
    }

    return this.tryWsSend(wsKeySpotPublic, JSON.stringify({
      symbol,
      topic: 'kline_' + candleSize,
      event: 'cancel',
      params: {
        binary: false,
      },
    }));
  }

I'd welcome a small PR to add this. Also note that due to the quirks in how sub/unsub is very different for spot, I don't think the ws client can automatically resubscribe to necessary spot topics if the connection is lost or respawned. You may need to call subscribePublicSpotV1Kline again if the ws client ever emits a reconnected event.

I do want to make this more intuitive in future, though I think I overly rushed the spot implementation (the REST client needs a major revision for spot markets, that has been started with the help of others but not yet completed on my end).

tiagosiebler avatar Apr 28 '22 23:04 tiagosiebler

Awesome thanks - I will see if I can play around and get some results. If I do, I'll be sure to submit a PR on this.

I actually would rather work with the linear market since that is where I'm focusing my efforts but their docs claim that the push interval is anywhere between 1-60s whereas the push time for spot subscription was something like 300-400ms. Not only that, but bybit does not link their linear market prices to the livenet market prices which can make it difficult to do testing.

It seems to me that working with the spot market for market pricing would be more ideal in this case.

LukeKoz avatar Apr 29 '22 08:04 LukeKoz

I actually would rather work with the linear market since that is where I'm focusing my efforts but their docs claim that the push interval is anywhere between 1-60s whereas the push time for spot subscription was something like 300-400ms. Not only that, but bybit does not link their linear market prices to the livenet market prices which can make it difficult to do testing.

Interesting, that's good to know - thanks.

tiagosiebler avatar Apr 29 '22 08:04 tiagosiebler