Market Data & Web Scraping Intermediate

Stream Real-Time 1-Minute Kline Candlesticks via WebSocket in Python

Connect to Binance public WebSockets in Python using asyncio and websockets to stream live real-time 1-minute OHLCV candlesticks without API keys.

#websockets

Problem Statement

Polling REST APIs every second for live prices adds latency and triggers IP bans. This event-driven WebSocket client pushes sub-second candle updates directly into your strategy.

Formula / Core Logic

Subscribes to wss://stream.binance.com:9443/ws/@kline_1m and parses payload['k'] for Open, High, Low, Close, and is_closed flag.

Installation

pip install websockets
crypto-candlestick-live-websocket-feed-python.py
import asyncio
import json
import websockets

async def stream_candles(symbol='btcusdt', interval='1m'):
    url = f'wss://stream.binance.com:9443/ws/{symbol}@kline_{interval}'
    async with websockets.connect(url) as ws:
        print(f'Connected to live {symbol.upper()} {interval} feed...')
        while True:
            msg = json.loads(await ws.recv())
            k = msg['k']
            if k['x']:  # True only when the 1-min candle officially closes
                print(f"CLOSED CANDLE | Close: {k['c']} | Vol: {k['v']}")

if __name__ == '__main__':
    asyncio.run(stream_candles())

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: What does `if k['x']:` check in the script?

`k['x']` is a boolean flag indicating whether the current candle has finalized, preventing premature signal execution mid-candle.