kite.instruments ("NFO") - issue..

tarak
I am getting following out put. I am printing out first element...

print ("type of instruments: ", type(instruments))
print ("type of instruments: ", type(instruments[0]))
pprint.pprint(instruments[0])



"expiry" : as per the spec should be a string. However, I am seeing it is coming as code.. Can you please help ?
  • SRIJAN
    SRIJAN edited July 2022
    This isn't an issue.

    KiteConnect python client converts the string into a datetime.date object for ease.

    If you want it in string format,you can convert the datetime.date object back to string.
  • tarak


    Thanks for quick response. I do not think that is right. As can be seen above, API doc clearly specifies it to be string. So, it should be given as string.

    I think, the code should have been as below, so that it would become string.

    datetime.date(2022, 7, 28).strftime("%Y/%m/%d")

    If you think otherwise, please provide a code snippet to convert from that format to usable string format.

    Problem for me is I am unable to save these dictionaries as is.
  • SRIJAN
    SRIJAN edited July 2022
    Yes, the API documentation specifies it as a string.

    Actually,when you call the instruments API,it returns a csv file. So,'expiry' by default is a string.

    As told above,the python client converts it into datetime.date object for ease. Refer the official KiteConnect Python client GitHub documentation provided above to know more.

    And yes,you can use strftime method to convert it back into string.

    Also,if you want the instruments API response directly as a csv file(where expiry will be as string), you can go to :

    https://api.kite.trade/instruments

    If you want instruments from a specific exchange, add the exchange as suffix to the url.

    Like-https://api.kite.trade/instruments/NFO
  • tarak
    tarak edited July 2022
    Found the issue. It is a bug in the connect.py.

    In case of curl the output is as below.



    Observe the expiry date information

    Following is the modification required in connect.py (line no: 804)


    # Parse date
    if len(row["expiry"]) == 10:
    row["expiry"] = dateutil.parser.parse(row["expiry"]).date().strftime("%Y-%m-%d")


    Output is



    This will be similar to the curl.

    Please take it to your dev team and get it reviewed.

    Thanks for your support.


  • SRIJAN
    SRIJAN edited July 2022
    I told you above 2 times, it's not a bug.
    It's the design, otherwise the code to parse the expiry wouldn't be there,as expiry by default is a string.

    Read my above comment properly.
  • tarak
    tarak edited July 2022
    If it is not following API spec, it is a bug.
  • tarak
    I find that at many places, API is not providing string (especially date/time values). If Zerodha had given why they are doing this way instead of a String, it would have helped to understand / appreciate.

    Any way, for now I am using following code in the client and seems to work

    inst['expiry'] = str (inst['expiry'])


    Can you please confirm if this is the expected way to use in the client ?

    Thanks!
  • tarak
    tarak edited July 2022
    Some one may find this issue again. Adding a image to clarify the way I am using currently.

    Especially, if you're saving instrument files as sequence of json lines, this will be helpful.
  • sujith
    @tarak,
    Maybe you can use this documentation for a bug free Kite Connect API.
  • tarak
    tarak edited July 2022
    Thanks Sujit. If you could share code snippet of how the kite.instruments() out put is getting saved as csv, it would be helpful.
  • rakeshr
    If it is not following API spec, it is a bug.
    Response attributes data types in kite connect, represent the response data type made using base HTTP requests. And all our client libraries convert these timestamp fields( in orders, historical data APIs, etc) to DateTime types for ease to use.
    If you could share code snippet of how the kite.instruments() out put is getting saved as csv
    content-type of the instrument response is text/csv.
  • tarak
    tarak edited July 2022
    To save the NIFTY and BANKNIFTY instruments selectively, following snippet can be used. Sharing it so that some one might find it useful.


    def write_instruments_to_csv(list_of_inst, file_path, list_of_symbls):
    field_names = list_of_inst[0].keys()
    if ( (len(list_of_symbls) > 0) and (len (list_of_inst)>0) ):
    try :
    with open(file_path, 'w', newline='', encoding='utf-8') as csv_file_object:
    writer = csv.DictWriter(csv_file_object, fieldnames=field_names)
    writer.writeheader()
    for row_dict in list_of_inst:
    if (row_dict['name'] in list_of_symbls) :
    writer.writerow(row_dict)
    except Exception as error :
    logger.error("File I/O error"+str(error))
    return
    instruments = kite.instruments ("NFO")
    #To save only NIFTY and BANKNIFTY Instruments.
    write_instruments_to_csv(instruments, INSTRUMENTS_FILE, ['NIFTY', 'BANKNIFTY'])


Sign In or Register to comment.