Historical data is not fetching for few symbols


  • @webseos please find below code

    import json
    from datetime import datetime, timedelta
    from time import strftime
    import time

    package import statement

    from smartapi import SmartConnect

    symbolslist = [['IDEA','1111'],['ADANIENT','2222'],['GAIL','3333']] # 157 symbols

    API_KEY = ""
    SEC_KEY = ""

    CLIENT_ID = ""
    PASSSWORD = ""

    feedToken = None
    userProfile = None
    smartObj = None

    days_to_subtract = 100 # Specify in terms days
    days_to_subtract_hours = 50 # Specify in terms days
    days_to_subtract_5mins = 5 # Specify in terms days

    def smartApiInitialization():
    try:
    global feedToken
    global userProfile
    global smartObj
    handleLoggerInitialization()
    createKPIBackupFile()

        # import smartapi.smartExceptions(for smartExceptions)
        # create object of call
        smartObj =SmartConnect(api_key=API_KEY)
        # login api call
        data = smartObj.generateSession(CLIENT_ID ,PASSSWORD)
        refreshToken= data['data']['refreshToken']
    
        # fetch the feedtoken
        feedToken =smartObj.getfeedToken()
        # fetch User Profile
        userProfile= smartObj.getProfile(refreshToken)
    
    except Exception as ex:
        logging.error("Exception in smartApiInitialization method : {}".format(str(ex)))
        print("Exception in smartApiInitialization method : {}".format(str(ex)))
    

    def smartAppLogout():
    try:
    global smartObj

        logout = smartObj.terminateSession(CLIENT_ID)
        print("Logout Successfull")
        logging.info("Logout Successfull")
    
    except Exception as ex:
        print("Exception in smartAppLogout method : {}".format(str(ex)))
        logging.error("Exception in smartAppLogout method : {}".format(str(ex)))
    

    def getDayHistoricalData():

    try:
        global smartObj
        global days_to_subtract
        global symbolslist
    
        lastDate = datetime.today()
        fromDate = lastDate - timedelta(days=days_to_subtract)
        lastDate = lastDate.strftime("%Y-%m-%d %H:%M")
        fromDate = fromDate.strftime("%Y-%m-%d %H:%M")
    
        for symbol in symbolslist :
            print(symbol)
            historicParam = {
                "exchange": "NSE",
                "symboltoken": symbol[1],
                "interval": "ONE_DAY",
                "fromdate": fromDate,
                "todate": lastDate
            }
            respdatajson = smartObj.getCandleData(historicParam)
    
            #print(respdatajson)
            respdata = respdatajson['data']
            if len(respdata) == 0:
                print("No Historical Data for this symbol : {}".format(symbol))
                logging.info("No Historical Data for this symbol : {}".format(symbol))
                time.sleep(2)
                continue
            df = pd.DataFrame(respdata)
            print(df.head()
            
            time.sleep(2)
    
    except Exception as ex:
        print("Exception in getHistoricalData method : {}".format(str(ex)))
        logging.error("Exception in getHistoricalData method : {}".format(str(ex)))
    

    if name == 'main':
    smartApiInitialization()
    getDayHistoricalData()

    smartAppLogout()

  • @admin Full Code required, I sense request frequency problematic


  • @Ramesh said in Historical data is not fetching for few symbols:

    AARTIIND

    Please type your code sample witout API Key and User/Password ,make them ***** or XXXX

    Without code sample its useless to address problems


  • @Ramesh Show full Code

    Partial code is not helpful

    Code in Python

    obj=SmartConnect(api_key="XXX")

    #Historic api
    print("Get 5 Min OHLC Data for 3045 SBIN EQ")
    try:
    historicParam={
    "exchange": "NSE",
    "symboltoken": "3045",
    "interval": "FIFTEEN_MINUTE",
    "fromdate": "2021-08-10 09:15",
    "todate": "2021-08-10 13:16"
    }
    print(obj.getCandleData(historicParam))
    except Exception as e:
    print("Historic Api failed: {}".format(e.message))

    Output

    Get 5 Min OHLC Data for 3045 SBIN EQ

    {'status': True, 'message': 'SUCCESS', 'errorcode': '', 'data': [['2021-08-10T09:15:00+05:30', 434.4, 436.4, 433.9, 435.45, 1694733], ['2021-08-10T09:30:00+05:30', 435.65, 435.8, 433.5, 433.65, 1081541], ['2021-08-10T09:45:00+05:30', 433.6, 434.7, 433.0, 433.35, 913274], ['2021-08-10T10:00:00+05:30', 433.3, 433.45, 431.7, 432.35, 869497], ['2021-08-10T10:15:00+05:30', 432.4, 432.4, 430.2, 431.3, 1285541], ['2021-08-10T10:30:00+05:30', 431.25, 431.45, 429.65, 430.2, 1052319], ['2021-08-10T10:45:00+05:30', 430.2, 432.25, 429.5, 431.8, 1296572], ['2021-08-10T11:00:00+05:30', 431.7, 432.8, 431.0, 431.2, 978197], ['2021-08-10T11:15:00+05:30', 431.1, 431.5, 430.7, 430.75, 482722], ['2021-08-10T11:30:00+05:30', 430.75, 431.8, 430.25, 430.5, 710667], ['2021-08-10T11:45:00+05:30', 430.55, 430.7, 429.9, 430.4, 770203], ['2021-08-10T12:00:00+05:30', 430.4, 430.55, 429.6, 430.0, 500362], ['2021-08-10T12:15:00+05:30', 429.95, 430.35, 429.6, 430.15, 433561], ['2021-08-10T12:30:00+05:30', 430.05, 430.25, 428.6, 428.9, 894144], ['2021-08-10T12:45:00+05:30', 428.85, 428.9, 426.5, 427.0, 1954868], ['2021-08-10T13:00:00+05:30', 427.05, 429.0, 426.8, 427.75, 913878], ['2021-08-10T13:15:00+05:30', 427.65, 427.7, 426.7, 426.9, 680186]]}


  • This post is deleted!

  • @admin Please find below request details for one day (not for 1 min)
    historicParam = {
    "exchange": "NSE",
    "symboltoken": "XXXX",
    "interval": "ONE_DAY",
    "fromdate": fromDate,
    "todate": lastDate
    }
    respdatajson = smartObj.getCandleData(historicParam)


  • @admin Please find below request details for one day (not for 1 min)
    historicParam = {
    "exchange": "NSE",
    "symboltoken": "XXXX",
    "interval": "ONE_DAY",
    "fromdate": fromDate,
    "todate": lastDate
    }
    respdatajson = smartObj.getCandleData(historicParam)


  • HI @Ramesh We need your request to evaluate what parameters your are passing in to the historical api.


  • @admin I want to fetch historical data for all nifty 50 stock with 1min and 5 min and getting the aboe message. Can you please let me know if i have missed anything?


  • HI @Ramesh Post your request an response here.