import logging from kiteconnect import KiteConnect
logging.basicConfig(level=logging.DEBUG) global kite try: kite = KiteConnect(api_key="api_key") kite.set_access_token("access_token") kite.profile() print("logged in") except ke.TokenException as e: print('Sorry, there was an error [%s]. Let''s start over\n\n' % e) # Place an order ltp = kite.ltp('MCX:CRUDEOIL21DEC5100PE')['MCX:CRUDEOIL21DEC5100PE']['last_price'] print("LTP of CRUDEOIL21DEC5100PE is : " + str(ltp)) try: orderid = kite.place_order(tradingsymbol='CRUDEOIL21DEC5100PE', exchange=kite.EXCHANGE_MCX, variety=kite.VARIETY_REGULAR, transaction_type=kite.TRANSACTION_TYPE_BUY, quantity=1, order_type=kite.ORDER_TYPE_LIMIT, product=kite.PRODUCT_NRML, price=ltp) logging.info("Order placed. ID is: {}".format(order_id)) except Exception as e: logging.info("Order placement failed: {}".format(e.message))
Response as below
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443 DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /user/profile HTTP/1.1" 200 None DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443 logged in LTP of CRUDEOIL21DEC5100PE is : 51.8 DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /quote/ltp?i=MCX%3ACRUDEOIL21DEC5100PE HTTP/1.1" 200 None DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443 DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /orders/regular HTTP/1.1" 400 97 Traceback (most recent call last): File "C:\autoTrade\zerodha_mcx_final - order - Copy.py", line 17, in orderid = kite.place_order(tradingsymbol='CRUDEOIL21DEC5100PE', exchange=kite.EXCHANGE_MCX, File "C:\Users\prary\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 355, in place_order return self._post("order.place", File "C:\Users\prary\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 841, in _post return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params) File "C:\Users\prary\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 913, in _request raise exp(data["message"], code=r.status_code) kiteconnect.exceptions.InputException: Unknown `tradingsymbol`.
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\autoTrade\zerodha_mcx_final - order - Copy.py", line 24, in logging.info("Order placement failed: {}".format(e.message)) AttributeError: 'InputException' object has no attribute 'message'
Error comes in MCX exchange instrument only, NFO order placed successfully. getting LTP works fine, Please guide to solve this
Can you let us know, which python client version you are on? Paste the output of pip show kiteconnect. Also, change exchange="MCX"(string) instead of using kite constant and check.
if I place NFO order it placed successfully,
but in MCX error comes Unknown `tradingsymbol`
PS: Make sure to remove app and client specific tokens.
getting LTP works fine, Please guide to solve this
Paste the output of
pip show kiteconnect
.Also, change
exchange="MCX"
(string) instead of using kite constant and check.pip show kiteconnect
Name: kiteconnect
Version: 4.0.0
Summary: The official Python client for the Kite Connect trading API
Home-page: https://kite.trade
Author: Zerodha Technology Pvt. Ltd. (India)
Author-email: [email protected]
License: MIT
Location: c:\users\prary\appdata\local\programs\python\python39\lib\site-packages
Requires: service-identity, pyOpenSSL, requests, six, autobahn, pywin32, enum34, python-dateutil
Required-by:
exchange="MCX" worked and order placed successfully..
Thanks a lot....