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.