Still not get historical data,how to get?


  • getting this error:Instrument Token for NIFTY18APR2422500CE: ('NFO', '41756')
    Making API request with params: {'exchange': 'NSE', 'symboltoken': ('NFO', '41756'), 'interval': 'FIVE_MINUTE', 'fromdate': '2024-04-10 19:17', 'todate': '2024-04-12 19:17'}
    Error fetching candle data: Couldn't parse the JSON response received from the server: b''
    Failed to retrieve candle data. this is my function:def historical_data(symbol, interval):
    df = dataa()
    to_date = datetime.now().strftime("%Y-%m-%d %H:%M")
    from_date_calculate = datetime.now() - timedelta(days=2)
    from_date = from_date_calculate.strftime("%Y-%m-%d %H:%M")

    symbol_code = get_instrument_token_symbol(df, symbol)
    print(f"Instrument Token for {symbol}: {symbol_code}")
    
    if symbol_code is None:
        print(f"Error: Instrument token not found for symbol {symbol}")
        return None
    
    historicParam = {
        "exchange": "NSE",
        "symboltoken": symbol_code,
        "interval": interval,
        "fromdate": from_date,
        "todate": to_date
    }
    
    try:
        print("Making API request with params:", historicParam)
        data = obj.getCandleData(historicParam)
        print("API response:", data)
    
        if data and 'data' in data and data['data']:
            data_ohcl = pd.DataFrame(data['data'])
            data_ohcl.columns = ['timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']
            return data_ohcl
        else:
            print("No valid data found in the API response.")
            return None
    except Exception as e:
        print(f"Error fetching candle data: {e}")
        return None
    

    def dat():
    data_ohcl = historical_data("NIFTY18APR2422500CE", 'FIVE_MINUTE')

    if data_ohcl is not None:
        if isinstance(data_ohcl, pd.DataFrame) and not data_ohcl.empty:
            print("Candle Data:")
            print(data_ohcl)
        else:
            print("No valid candle data returned.")
    else:
        print("Failed to retrieve candle data.")