bitfinexcom / bitfinex-api-py

Apache License 2.0
200 stars 125 forks source link

Candle data from wss is delayed behind rest #226

Closed NotoriousPyro closed 6 months ago

NotoriousPyro commented 1 year ago

I'm submitting a...

What is the expected behaviour?

WSS retrieves only future candle updates.

What is the current behaviour?

WSS retrieves candle updates which are from the past. A candle was retrieved from WSS which is behind the REST socket candles: image

Possible solution (optional)

I haven't got one yet, other than checking the deque for duplicates.

Steps to reproduce (for bugs)

  1. Retrieving all candles from a 5 minute window using REST and then append these to a deque.
  2. Subscribe to WSS candles_update and append the data to the same list.

Reproduction code:

from bfxapi import Client, PUB_REST_HOST, PUB_WSS_HOST
from bfxapi.types import Candle
from bfxapi.websocket.subscriptions import Candles
from bfxapi.websocket.enums import Channel

from collections import deque
from datetime import datetime, timedelta

bfx = Client(rest_host=PUB_REST_HOST, wss_host=PUB_WSS_HOST)

@bfx.wss.on("open")
async def on_open():
    await bfx.wss.subscribe(Channel.CANDLES, key="trade:1m:tBTCUSD")

class CandleHolder():
    candles: deque[Candle] = deque()

candle_holder = CandleHolder()

@bfx.wss.on("candles_update")
def on_candles_update(subscription: Candles, data: Candle):
    candle_holder.candles.append(data)

def main():
    start_date = int((datetime.now() - timedelta(minutes=5)).timestamp())
    candle_holder.candles = bfx.rest.public.get_candles_hist(
        tf='1m',
        symbol='tBTCUSD',
        sort="+1",
        start=f"{start_date}000"
    )

    if not any(candle_holder.candles):
        raise ValueError("No candles.")

    bfx.wss.run()

if __name__ == "__main__":
    main()

Python version

Python 3.11.4 x64

Mypy version

Not installed and not relevant to the issue.

Davi0kProgramsThings commented 6 months ago

Hi @NotoriousPyro, this isn't a bitfinex-api-py's bug. This weird behavior is due to how the API works. We discourage using REST and WSS together.