Volume calculation

Volume calculation logic from tick data for 1 min candle? FYER V3 API

livedata = pd.DataFrame(columns=[‘datetime’, ‘symbol’, ‘ltp’, ‘vol_traded_today’])

def onmessage(message):
global livedata
if ‘symbol’ in message:
c = datetime.now()
current_time = c.strftime(‘%Y-%m-%d %H:%M:%S’)
symb = message[‘symbol’]
ltp = message[‘ltp’]
vol = message[‘vol_traded_today’]

    # Convert UTC time to IST
    epoch_time = message['last_traded_time']
    ist_time = datetime.fromtimestamp(epoch_time, tz=timezone.utc).astimezone(pytz.timezone('Asia/Kolkata'))
    ist_time_str = ist_time.strftime("%H:%M:%S")

    print("Last traded time in Indian Standard Time (IST):", ist_time_str)
    print(f"Success: {message}")

    # Calculate volume difference from previous record
    if len(livedata) > 0:
        prev_vol = livedata.iloc[-1]['vol_traded_today']
        vol_diff = vol - prev_vol
    else:
        vol_diff = vol  # If it's the first record, use the current volume as difference
        
    new_row = pd.DataFrame({'datetime': [current_time], 'symbol': [symb], 'ltp': [ltp], 'vol_traded_today': [vol_diff]})
    livedata = pd.concat([livedata, new_row], ignore_index=True)
    livedata['datetime'] = pd.to_datetime(livedata['datetime'])

    # Resample data to one-minute intervals
    #resampled_data = livedata.set_index('datetime').resample('1Min').agg({'ltp': 'ohlc', 'vol_traded_today': 'last'})
    # Resample data to one-minute intervals
    resampled_data = livedata.set_index('datetime').resample('1Min').agg({'ltp': 'ohlc'})
    
    print(resampled_data)
else:
    print("Error: The key 'symbol' is missing in the response.")

Hi @112493655679596874531 ,

Please use history API for one minute candles.