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 ?
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.
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 :
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 ?
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.
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.
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.
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
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.
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.
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!
Especially, if you're saving instrument files as sequence of json lines, this will be helpful.
Maybe you can use this documentation for a bug free Kite Connect API.
text/csv
.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'])