Fetch Historical Data using fyers api v3

				
					import datetime
import pandas as pd

def fetch_historical_data(symbol, start_date, end_date, interval="3"):
    def historical_bydate(symbol, sd, ed, interval="3"):
        data = {
            "symbol": symbol, 
            "resolution": interval, 
            "date_format": "1", 
            "range_from": str(sd), 
            "range_to": str(ed), 
            "cont_flag": "1"
        }
        nx = fyers.history(data)
        cols = ['date', 'open', 'high', 'low', 'close', 'volume']
        df = pd.DataFrame.from_dict(nx['candles'])
        df.columns = cols
        df['date'] = pd.to_datetime(df['date'], unit="s")
        df['date'] = df['date'].dt.tz_localize('utc').dt.tz_convert('Asia/Kolkata')
        df['date'] = df['date'].dt.tz_localize(None)
        return df

    dfs = []
    n = abs((start_date - end_date).days)
    ab = None

    while ab is None:
        sd = (end_date - datetime.timedelta(days=n))
        ed = (sd + datetime.timedelta(days=99 if n > 100 else n)).strftime("%Y-%m-%d")
        sd = sd.strftime("%Y-%m-%d")
        dx = historical_bydate(symbol, sd, ed, interval)
        dfs.append(dx)
        n = n - 100 if n > 100 else 0
        print(n)
        if n == 0:
            ab = "done"

    df = pd.concat(dfs, ignore_index=True)
    return df



				
			
				
					# Example usage:
start_date = datetime.date(2024, 6, 25)
end_date = datetime.date(2024, 8, 1)
df = fetch_historical_data("NSE:NIFTY24AUGFUT", start_date, end_date, interval="3")