Historical data is not fetching for few symbols


  • This post is deleted!

  • @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]]}


  • @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


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


  • @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()
    

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

    PI Key and User/Password ,make them ***** or XXXX

    have already shared the code, please do let me know if you need more details


  • @Ramesh

    **I shall suggest you to look at this line

    respdatajson = smartObj.getCandleData(historicParam)**

    This will take time, so your program should wait to get response. After getting candles ohlc data rest of code will proceed.

    just like AJAX synchronous call to server. So just think about it and let me know what you think

    By the way the following 2 functions , I dont know what they were doing

    handleLoggerInitialization()
    createKPIBackupFile()

    ** Also I did not understand these lines, what is pd, df.head() doing what

    df = pd.DataFrame(respdata)
    print(df.head()


  • @webseos I'm getting data for few symbols and not for others, please see below response, where getting data for GAIL and not for TATACHEM and AARTIIND. Please guide

    TATACHEM
    {'status': True, 'message': 'SUCCESS', 'errorcode': '', 'data': []}
    No Historical Data for this symbol : TATACHEM
    GAIL
    {'status': True, 'message': 'SUCCESS', 'errorcode': '', 'data': [['2021-05-04T00:00:00+05:30', 138.5, 145.65, 137.35, 143.75, 31185770], ['2021-05-05T00:00:00+05:30', 147.3, 148.85, 143.0, 145.05, 21912068], ['2021-05-06T00:00:00+05:30', 145.9, 151.85, 145.6, 151.15, 26499738], ['2021-05-07T00:00:00+05:30', 151.3, 152.65, 148.7, 150.25, 18927457], ['2021-05-10T00:00:00+05:30', 150.25, 155.5, 150.25, 154.55, 16390776], ['2021-05-11T00:00:00+05:30', 153.0, 163.5, 152.2, 161.9, 35767570], ['2021-05-12T00:00:00+05:30', 162.6, 165.75, 158.7, 161.05, 26642955], ['2021-05-14T00:00:00+05:30', 161.5, 161.75, 152.15, 152.7, 20845453], ['2021-05-17T00:00:00+05:30', 154.0, 154.8, 150.35, 153.15, 14494672], ['2021-05-18T00:00:00+05:30', 155.0, 158.0, 152.5, 152.8, 14263487], ['2021-05-19T00:00:00+05:30', 152.8, 154.55, 151.3, 152.8, 8993112], ['2021-05-20T00:00:00+05:30', 151.95, 152.3, 146.75, 147.35, 14895731], ['2021-05-21T00:00:00+05:30', 147.6, 150.45, 145.3, 145.75, 17043863], ['2021-05-24T00:00:00+05:30', 146.5, 152.6, 145.9, 151.7, 15732196], ['2021-05-25T00:00:00+05:30', 153.6, 155.75, 151.7, 155.1, 26222882], ['2021-05-26T00:00:00+05:30', 155.0, 155.8, 151.7, 152.1, 8663643], ['2021-05-27T00:00:00+05:30', 152.25, 155.0, 150.55, 152.8, 15013582], ['2021-05-28T00:00:00+05:30', 153.2, 157.4, 152.05, 153.05, 15352282], ['2021-05-31T00:00:00+05:30', 154.0, 161.4, 153.2, 159.95, 48101205], ['2021-06-01T00:00:00+05:30', 160.6, 161.75, 158.0, 160.45, 14125060], ['2021-06-02T00:00:00+05:30', 162.4, 163.45, 157.65, 158.85, 14123542], ['2021-06-03T00:00:00+05:30', 159.8, 162.25, 158.7, 161.05, 11740971], ['2021-06-04T00:00:00+05:30', 161.3, 164.75, 160.3, 162.0, 13443192], ['2021-06-07T00:00:00+05:30', 162.5, 169.9, 162.05, 168.35, 33219121], ['2021-06-08T00:00:00+05:30', 168.2, 169.8, 165.0, 167.8, 16692868], ['2021-06-09T00:00:00+05:30', 170.0, 170.3, 158.3, 162.4, 41513695], ['2021-06-10T00:00:00+05:30', 164.5, 166.0, 160.5, 163.5, 23519675], ['2021-06-11T00:00:00+05:30', 165.0, 167.75, 161.2, 163.75, 23815900], ['2021-06-14T00:00:00+05:30', 164.4, 164.6, 160.1, 163.15, 17059851], ['2021-06-15T00:00:00+05:30', 164.5, 166.2, 162.55, 163.0, 14365617], ['2021-06-16T00:00:00+05:30', 163.3, 165.75, 161.2, 161.75, 12490077], ['2021-06-17T00:00:00+05:30', 161.5, 163.65, 158.7, 160.5, 10112009], ['2021-06-18T00:00:00+05:30', 161.25, 161.95, 152.6, 155.05, 18169757], ['2021-06-21T00:00:00+05:30', 153.0, 156.15, 151.25, 155.25, 13484830], ['2021-06-22T00:00:00+05:30', 157.0, 158.7, 154.9, 155.3, 7859425], ['2021-06-23T00:00:00+05:30', 157.0, 157.0, 152.7, 153.35, 8189214], ['2021-06-24T00:00:00+05:30', 153.9, 154.95, 151.75, 152.25, 8153848], ['2021-06-25T00:00:00+05:30', 153.0, 154.7, 152.25, 153.1, 6551074], ['2021-06-28T00:00:00+05:30', 154.6, 155.15, 152.2, 153.35, 7206825], ['2021-06-29T00:00:00+05:30', 153.45, 153.75, 151.35, 152.0, 6406676], ['2021-06-30T00:00:00+05:30', 152.05, 152.55, 149.25, 149.65, 10512615], ['2021-07-01T00:00:00+05:30', 150.7, 153.75, 150.0, 153.0, 12357143], ['2021-07-02T00:00:00+05:30', 153.95, 153.95, 151.0, 151.3, 7467504], ['2021-07-05T00:00:00+05:30', 152.3, 153.65, 151.55, 152.15, 6273400], ['2021-07-06T00:00:00+05:30', 152.7, 155.1, 148.8, 150.35, 12998966], ['2021-07-07T00:00:00+05:30', 149.8, 152.1, 148.55, 149.95, 8210966], ['2021-07-08T00:00:00+05:30', 149.85, 151.1, 148.45, 148.7, 8533698], ['2021-07-09T00:00:00+05:30', 148.7, 149.05, 147.0, 147.15, 6837801], ['2021-07-12T00:00:00+05:30', 148.5, 149.2, 146.5, 146.75, 9767750], ['2021-07-13T00:00:00+05:30', 148.15, 151.4, 147.5, 148.8, 14198854], ['2021-07-14T00:00:00+05:30', 148.0, 149.95, 147.2, 147.85, 6200851], ['2021-07-15T00:00:00+05:30', 148.9, 148.9, 143.9, 144.4, 13149215], ['2021-07-16T00:00:00+05:30', 145.0, 145.2, 143.0, 144.8, 11323698], ['2021-07-19T00:00:00+05:30', 144.8, 148.0, 143.8, 144.35, 13175418], ['2021-07-20T00:00:00+05:30', 144.4, 145.0, 140.75, 142.35, 8956762], ['2021-07-22T00:00:00+05:30', 144.6, 145.2, 141.7, 143.8, 11671203], ['2021-07-23T00:00:00+05:30', 144.0, 145.0, 142.0, 142.4, 7355532], ['2021-07-26T00:00:00+05:30', 142.0, 142.1, 139.7, 141.35, 9260846], ['2021-07-27T00:00:00+05:30', 141.85, 142.0, 138.55, 139.05, 8747495], ['2021-07-28T00:00:00+05:30', 139.9, 139.9, 136.6, 137.85, 9152741], ['2021-07-29T00:00:00+05:30', 138.9, 139.6, 136.95, 137.5, 6949660], ['2021-07-30T00:00:00+05:30', 138.0, 141.8, 137.25, 139.55, 14416176], ['2021-08-02T00:00:00+05:30', 140.95, 143.3, 139.35, 142.85, 7539166], ['2021-08-03T00:00:00+05:30', 143.95, 144.15, 141.85, 143.3, 8902280], ['2021-08-04T00:00:00+05:30', 144.25, 146.2, 141.85, 142.4, 9592503], ['2021-08-05T00:00:00+05:30', 143.0, 145.6, 140.3, 142.9, 19500102], ['2021-08-06T00:00:00+05:30', 147.0, 153.95, 144.9, 148.75, 63506840], ['2021-08-09T00:00:00+05:30', 150.2, 150.5, 145.2, 145.8, 11261259], ['2021-08-10T00:00:00+05:30', 145.9, 148.15, 143.8, 144.85, 14949570], ['2021-08-11T00:00:00+05:30', 145.5, 148.5, 144.5, 144.65, 6378762]]}
    date open high low close volume
    0 2021-05-04T00:00:00+05:30 138.50 145.65 137.35 143.75 31185770
    1 2021-05-05T00:00:00+05:30 147.30 148.85 143.00 145.05 21912068
    2 2021-05-06T00:00:00+05:30 145.90 151.85 145.60 151.15 26499738
    3 2021-05-07T00:00:00+05:30 151.30 152.65 148.70 150.25 18927457
    4 2021-05-10T00:00:00+05:30 150.25 155.50 150.25 154.55 16390776
    AARTIIND
    {'status': True, 'message': 'SUCCESS', 'errorcode': '', 'data': []}
    No Historical Data for this symbol : AARTIIND


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

    @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)

    How many days data you are requesting.


  • @admin Hi trying to get one day time frame data all F&O symbol for a week.