How to get tick-by-tick volume from Zerodha’s WebSocket ?

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

I’m using broker APIs like Zerodha and Angel One, and I noticed that they give cumulative volume instead of per-tick or per-candle volume.Because of this, I’m not able to calculate correct volume for my candles.How do we convert this cumulative volume into actual traded tick volume ?

Posted Apr 24
Reply

    1 Reply

    Support AlgoDelta
    Support AlgoDelta AlgoDelta Support ·

    You are right some brokers doesn’t give the direct per-tick volume we have to calculate that by comparing the tick cumulative volume with previous candle stored volume.

    So, like that we will have to convert volume manually.

    Below logic and code, you can refer.

    Delta Volume = Current Volume – Previous Volume
    Below code that you can refer:

    def calculate_delta_volume(ticks):
        prev_volume = None
        result = []
        for tick in ticks:
            current_volume = tick["volume"]
            if prev_volume is None:
                delta = 0  # first tick
            else:
                delta = current_volume - prev_volume
    
                # safety check (in case of reset or bad data)
                if delta < 0:
                    delta = 0
            tick["delta_volume"] = delta
            result.append(tick)
            prev_volume = current_volume
        return result
    
    # usage
    ticks = [
        {"price": 100, "volume": 1000},
        {"price": 101, "volume": 1200},
        {"price": 102, "volume": 1500},
    ]
    
    output = calculate_delta_volume(ticks)
    
    for t in output:
        print(t)

    Later can use in the building candles from live data.

    Verified Solution
    • Great explanation with code. I implemented the code and instruction mentioned above. The problem is resolved now. – Sejpalsinh Jadeja Team Jun 16
    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.