How to build 1-minute candles from live ticks using Zerodha WebSocket?

Asked by Sejpalsinh Jadeja · SOLVED
1 reply 1,444 views Jump to solution Reply
Sejpalsinh Jadeja Original Post
AlgoDelta Team

I am receiving live tick data from Zerodha WebSocket, but indicators like Supertrend and others require candle data.How can I correctly convert live ticks into 1-minute candles in real-time?Since i use the boiler plate of the websocket from the Zerodha’s python examples Repo from github.

Posted Apr 25
Reply

    1 Reply

    Support AlgoDelta
    Support AlgoDelta AlgoDelta Support ·

    Yes, Zerodha’s WebSocket provides the tick-tick data, we will have to aggregate the ticks and convert that into candle of different timeframes whatever you want.

    To do that can refer below code:

    from datetime import datetime
    
    CANDLE_STATE = {}
    
    def build_candle(symbol, tick, timeframe=1):
        ts = tick["timestamp"]
        price = tick["price"]
    
        # Adject timeframe
        minute = (ts.minute // timeframe) * timeframe
        bucket = ts.replace(minute=minute, second=0, microsecond=0)
    
        if symbol not in CANDLE_STATE:
            CANDLE_STATE[symbol] = {}
    
        candles = CANDLE_STATE[symbol]
    
        # Create new candle
        if bucket not in candles:
            candles[bucket] = {
                "open": price,
                "high": price,
                "low": price,
                "close": price
            }
        else:
            c = candles[bucket]
            c["high"] = max(c["high"], price)
            c["low"] = min(c["low"], price)
            c["close"] = price
    
        return candles
    # Replace the tick's static data with the live feed
    ticks = [
        {"timestamp": datetime(2026, 4, 24, 9, 15, 5), "price": 100},
        {"timestamp": datetime(2026, 4, 24, 9, 15, 20), "price": 102},
        {"timestamp": datetime(2026, 4, 24, 9, 15, 40), "price": 99},
        {"timestamp": datetime(2026, 4, 24, 9, 16, 10), "price": 105},
    ]
    
    symbol = "TEST"
    
    for t in ticks:
        candles = build_candle(symbol, t, timeframe=1)
    
    # Print result
    for time, candle in candles.items():
        print(time, candle)
    Verified Solution
    • Thanks for explanation and also for the code. Now I can be able to get the live data. – Sejpalsinh Jadeja Team Jul 28
    Markdown supported · ```json for code blocks · paste or drop screenshots

    Be specific and kind. Never share API keys, passwords or personal account details.

    Welcome to the community

    Log in or create your account in under a minute.

    1. 1Mobile
    2. 2E-mail
    3. 3Password
    +91

    Have a password?

    By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.