Irregular Timestamps in "kline" Stream via WebSocket – Missing Candlesticks?

Hi everyone,

I’m using Binance’s WebSocket API to stream real-time kline/candlestick data. My goal is to capture only the closed candlesticks accurately as they come in. However, I’ve noticed that some candles appear to be missing or delayed. Here’s the Python code I’m using:

import websocket
import json

def on_message(ws, message):
    data = json.loads(message)
    kline = data['k']
    if kline['x']:  # only process closed candles
        print(f"Symbol: {data['s']} | Close Time: {kline['T']} | Close Price: {kline['c']}")

def on_error(ws, error):
    print(f"Error: {error}")

def on_close(ws, close_status_code, close_msg):
    print("Connection closed.")

def on_open(ws):
    params = {
        "method": "SUBSCRIBE",
        "params": [
            "btcusdt@kline_1m"
        ],
        "id": 1
    }
    ws.send(json.dumps(params))

ws = websocket.WebSocketApp("wss://stream.binance.com:9443/ws",
                            on_open=on_open,
                            on_message=on_message,
                            on_error=on_error,
                            on_close=on_close)

ws.run_forever()

Issues I’m Facing:

  1. Some candlesticks have timestamps (kline['T']) that are off — there are occasional 1–2 minute gaps in the expected flow.
  2. The missing candles do show up correctly in other tools like TradingView, but not in my app.

My Questions:

  • Is keeping the WebSocket connection open like this reliable for long-term streaming? Should I implement a reconnection mechanism?
  • Could the missing data be caused by something on Binance’s end, or is it more likely a problem in my client code?
  • What are the best practices for consuming kline data over WebSocket in a reliable, lossless way?