Programmatic Trading in Indian Markets using Python
We have told you why Python is one of the preferred languages to do algo trading in this article. We have also told you how programmatic trading in India. We also had a successful webinar on Trading in Indian Markets using Python (Click here to watch the webinar), we ought to give you a prelude to the trading platform which will enable you to implement your algorithmic trading strategies in python.
To enable trading in Indian Markets using Python, we will utilize Kite Connect API, India’s first market API for retail clients. We will be covering this in detail in the webinar. We cover most of the trading platforms in EPAT™, our highly sought after course on algorithmic trading and quantitative finance. We have also covered various libraries available in Python for programmatic trading in an earlier article, in this article we will also be talking about the official python client for communicating with Kite Connect API.
What is Kite Connect?
Kite Connect is a compilation of REST-like hypertext transfer protocol based APIs that ease numerous capabilities needed to build investment and trading platforms. It is built on top of Zerodha’s online trading platform. The users should be clients of Zerodha only and they will get programmatic access to all the information on the brokerage. You can also use Kite Connect API for:
- Executing orders in real time
- managing portfolios
- streaming real time market data
- offsite order execution
- getting reliable updates on your orders
- of course, getting historical data
Now, if you remember, in our article on trading with Interactive Brokers, we had mentioned some of the advantages of using IB API for algorithmic trading. For Indian markets, Kite Connect is a great choice for implementing algorithmic trading strategies using Python, which has been further explained in our webinar.
Objectives of Kite Connect
Gone are the days when retail clients stayed away from capital markets because they did not have any option to access their trading accounts pragmatically. With aim to bring a revolutionary change in the way trading is done in India, Kite Connect promises to:
- Cutting down costs involved to perform trading in India
- Going completely brokerage free in future
- Making the process more transparent
- Offer a better platform to do trading
- Keeping a client-first attitude.
- Supporting a wide array of programming languages including Python, Java, C#, Excel VBA, etc. You can also use the command line console on Kite Connect.
Besides placing and managing orders, Kite Connect promises to enable users to build:
- Multi asset risk modelling systems
- Stock screeners
- Quant strategies
- Equity stock selection models
- Option greeks calculators
- Backtesting, machine learning
- Personal Kite or Pi.
One of the key advantages of Kite Connect is that you will own and control your trading account and the data associated with it, and you will not be restricted to the platform offered by your broker.
Python for Programmatic Trading in Kite Connect
We have already told you why Python has sufficed as the best choice for programmatic trading in our article titled: Why Python Programmatic Trading is Preferred Choice Among Traders?
Kite Connect has client libraries in multiple programming languages including PHP, JS and of course, Python. The official library for Python is available here:
Zerodha is India’s first discount broker and all the trades in Kite Connect are executed through Zerodha. So, first thing that you need to do is to create a trading account on Zerodha so that you can access the APIs. Once you have that, next thing to do is to register for a developer account. That’s it! You can get started immediately. In our webinar, Nithin Kamat & Satyajit Sarangi will be explaining all this in a step-by-step manner.
Since Zerodha has already got all the necessary approvals for users of Kite Connect API, therefore you, as a Kite Connect user, do not have to acquire any other approvals.
Installation of Python client
After downloading the resources from the github repository you will have to install the files. To install the client, you will have to use the following command:
pip install kiteconnect
You can find the following code on the github repository as well. However, for convenience’ sake, we are giving it here:
from kiteconnect import KiteConnect kite = KiteConnect(api_key="your_api_key") # Redirect the user to the login url obtained # from kite.login_url(), and receive the request_token # from the registered redirect url after the login flow. # Once you have the request_token, obtain the access_token # as follows. data = kite.request_access_token("request_token_here", secret="your_secret") kite.set_access_token(data["access_token"]) # Place an order try: order_id = kite.order_place(tradingsymbol="INFY", exchange="NSE", transaction_type="BUY", quantity=1, order_type="MARKET", product="NRML") print("Order placed. ID is", order_id) except Exception as e: print("Order placement failed", e.message) # Fetch all orders print(kite.orders())
from kiteconnect import WebSocket # Initialise. kws = WebSocket("your_api_key", "your_public_token", "logged_in_user_id") # Callback for tick reception. def on_tick(tick, ws): print tick # Callback for successful connection. def on_connect(ws): # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe([738561, 5633]) # Set RELIANCE to tick in `full` mode. ws.set_mode(ws.MODE_FULL, ) # Assign the callbacks. kws.on_tick = on_tick kws.on_connect = on_connect # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect()
You can read further about all the list of supported methods here. The top guns from Zerodha will be covering this in great detail in our webinar on Trading in Indian Markets using Python. If you didn’t attend it then watch the recording by clicking here.
Algorithmic Trading in Indian Markets using Python has become all the more interesting and easier, thanks to Zerodha’s Kite Connect API. All you have to do now is:
- Sign up with Zerodha for a trading account
- Register for a Kite Connect account
- Log in to your kite connect developer account
- Start creating your strategies
That’s it! Enjoy the programmatic trading offered by Kite Connect
Butterfly Spreads are a way of earning from an underlying asset closing in a predicted range. Broken Wing Butterfly is the same as a Butterfly Spread wherein the sold spread is typically wider spread than the purchased spread. Read all about it here.