We have told you why Python is one of the preferred languages to do algo trading in this article. We have also told you about 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 Zerodha 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 Zerodha Kite Connect API.
What is Zerodha 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
Objectives of Zerodha Kite ConnectGone 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, Zerodha 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.
- Multi asset risk modelling systems
- Stock screeners
- Quant strategies
- Equity stock selection models
- Option greeks calculators
- Backtesting, machine learning
- Personal Kite or Pi.
Python for Programmatic Trading in Kite ConnectWe 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?
Zerodha's Kite Connect has client libraries in multiple programming languages including PHP, JS and of course, Python. The official library for Python is available here:
RequirementsZerodha 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, Founder & CEO at Zerodha, & Satyajit Sarangi, Software Developer at Zerodha, 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 clientAfter 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
API UsageYou 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")
# Place an order
order_id = kite.order_place(tradingsymbol="INFY",
<span class="pl-c1">print</span>(<span class="pl-s"><span class="pl-pds">"</span>Order placed. ID is<span class="pl-pds">"</span></span>, order_id)
except Exception as e:
print("Order placement failed", e.message)
# Fetch all orders
from kiteconnect import WebSocket
kws = WebSocket("your_api_key", "your_public_token", "logged_in_user_id")
# Callback for tick reception.
def on_tick(tick, ws):
# Callback for successful connection.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
<span class="pl-c"># Set RELIANCE to tick in `full` mode.</span> ws.set_mode(ws.<span class="pl-c1">MODE_FULL</span>, [<span class="pl-c1">738561</span>])
# 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.
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.
To ConcludeAlgorithmic 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
Next StepPython algorithmic trading has gained traction in the quant finance community as it makes it easy to build intricate statistical models with ease due to the availability of sufficient scientific libraries like Pandas, NumPy, PyAlgoTrade, Pybacktest and more.
In case you are looking to master the art of using Python to generate trading strategies, backtest, deal with time series, generate trading signals, predictive analysis and much more, you can enroll for our course on Python for Trading!Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.