Volume calculation

I have been unsuccessful in computing volumes for a given period of the chart (say, one-minute) correctly. In the following tick data:
'instrument_token': 53490439,
'mode': 'full',
'volume': 12510,
'last_price': 4084.0,
'average_price': 4086.55,
'last_quantity': 1,
I have used the following computations for every tick in one-minute duration:

volume = volume + tick['volume']


volume = volume + tick['last_quantity']

For a one-minute chart, I keep doing this addition for a minute. But neither calculation gives correct volume, when compared with Kite chart. OHLC values matches with a marginal error.

What is the correct way to compute volume from tick data for a given period of the chart?
  • shortwire
    Volume in tick would be the daily volume of the instrument. Its not specific to that tick or minute. Its a cumulative volume till that time of the day as represented by the tick.

    When you receive the first tick of the minute, you should set your local volume variable to 0 and keep taking the delta between two ticks volume and add on to your localvolume variable till your minute gets over.
  • vijoeyz

    I am not sure if I understood you completely. As per your description, for following PNB ticks, I would:

    volume = 0 # Initial value

    Time: 2019-02-21 09:15:07:000000
    Symbol: PNB
    Price: 74.70
    Qty: 799
    last_tick_qty = 799 # Save current tick's last qty
    volume = 0 # volume does not change
    Time: 2019-02-21 09:15:11:000000
    Symbol: PNB
    Price: 74.50
    Qty: 749
    volume = volume + abs(last_tick_qty - 749)
    last_tick_qty = 749

    Is this how I should compute a given period's volume? I am lost here. Please help me understand with an example. Thank you.

  • shortwire
    The "volume" field that comes with every tick is the aggregated volume for that day till the point of time represented by the tick. Will suggest not to create any logic based on Qty as if you miss any tick (which you will), the calculations will go out of sync.

    So lets say you have a 1 minute chart volume to be calculated. And its now 9:15:01 and the first tick comes.
    Volume field in that tick is 200. So your candle_volume_field = Volume field.
    Next tick comes at 9:15:31 where Volume field is 420.
    So your candle_volume_field = Volume field. (aka 420)

    Now say at 09:16:01 the first tick for the second minute comes as volume field here is 600

    So now your candle_volume_field = volume - Previous candle_volume_field
    ie. 600-420 = 180

    And this goes on now for that minute. WHen the minute changes again, the same logic will apply.
  • vijoeyz
    Wonderful! Thank you, @shortwire !
This discussion has been closed.