Projects
I currently have three modules in deployment. They are currently deployed at a fraction of the planned capital and are being rejigged and escalated at every expiry.- KiteBase: Manages the KiteConnect Instance and provides robust API access features to dependent modules.
- KiteRange: Trades intraday range breakouts.
- KiteSwing: Takes positional trendfollowing trades.
Requirements
- An account with Zerodha for equity trading
- KiteConnect enabled
- An activated App
- Historical Data/Candles API subscription added-on
- Python 3 installed, preferably with Anaconda for Windows users
- KiteConnect Python API installed
- Optional: GCP Account
Download
Download the setup script to the installation directory and run it. Chose the modules you want to install. You will need a secret code to install the package onto your system. Please contact me @harshnisar on Twitter or Telegram for the code.
GCP Setup
Since the codebase is so light, start with a minimal Compute Engine Instance (f1-micro should suffice). I prefer using the latest LTS Ubuntu minimal image to boot from. Ensure the timezone is correct. Download KiteBase on the instance.Once KiteBase is installed, the user can treat the cloud instance as a local machine and carry on as if it is a local device. I rather suggest using the instance as a dumb box, with zero input required from the user. This can be done using Instance Schedule.
Create a daily repeating schedule which starts an hour before opening bell and stops a few minutes after closing bell and attach the Schedule to your VM Instance. You will need to create an
Instance-Start-Stop-Role
with ID CustomRole221
generally available with the following permissions: compute.instances.start
& compute.instances.stop
which can be searched by filtering for Compute Instance Admin (v1)
. This Role will need be added to Google Provided Role Grants in IAM of the form service-PROJECT_NUMBER@compute-system.iam.gserviceaccount.com
. This bit of setup will ensure a smooth auto start-stop schedule for your VM.
Further, since 2FA is mandatory, KiteBase performs server side gymnastics to ensure TOTP Authentication is not a barrier for automation. For this purpose, a
flask
Network Tag needs to be added to your VM Instance. A new firewall rule (in VPC Network) targeting specified tag flask
filtering source IP ranges 0.0.0.0/0
at TCP Port 5000
needs to be created. Ensure you allow the port thru the firewall sudo ufw allow 5000
-
Create a
run.py
script in the KiteBase working directory. This script should contain all the commands described in Workflow, so for example
from Instance import Instance
from Range import Range
I = Instance()
infy = Range(I,'INFY')
tcs = Range(I,'TCS',Q=5,define_time='09:35') -
Create a
startup.sh
bash script which is run by contrab at startup to executerun.sh
on a detached screen
#!/bin/bash -e
screen -d -m -S KiteBase ./run.sh -
Finally add
startup.sh
to the contrab file.
$ contrab -e
and add@reboot /KiteBase-working-directory-/startup.sh
to the file, whereKiteBase-working-directory
is the working directory whrestartup.sh
is located -
Done! Now anytime the user intends to see how the KiteBase execution is going on, simple resume the screen.
$ screen -r
Information
Setup: It will be ensured here that all requirements are met and the necessary items are installed. The setup will show any unmet requirements when it is done. Please ensure these are complete before proceeding to run the strategy.Here is a summary of what setup does:
- Dependencies: KiteBase uses a few libraries which are not a part of the python standary libraries. If your system is missing a library, setup will add it to the list of unmet requirements.
- Installation Directory: Its best to install everything in its own directory. This will be the working directory from where the strategy should be run. Any required files will also be stored here.
-
Credentials: Logging into KiteConnect everyday is tedious. So at setup, all relevant credentials, including the Kite ClientID, password, PIN, API Key and API Secret are stored in a local encrypted
cred
file whose password is set during setup. - Chrome Driver: In inclination of convenience, web-scrapping is used to log into the API every morning which automates the login procedure using a headless Chrome browser. Please save a compatible version of chromedriver with execute permissions to the working directory. It goes without saying, Google Chrome must be installed on the system.
Stages
I have found that the developement cycle is roughly constituted of the following stages:- Research
- Testing
- Deployment
Research
Research provides the basis for system trading. It is the most resourse intensive stage. It involves the following:- Data Management: Involves maintaining robust historical price, volume, volatility, open interest etc. data adjusted for coroprate actions, at necessary time resolution.
- Strategies: Involves exploring trading strategies, for example, for speculators, it involves defining entry and exit points, profit-booking and stop-loss zones.
- Backtesting: Involves testing said defined strategies on historical data to determine optimum parameters, and projecting strategy risk and returns.
Testing
Testing involves running the strategy for a probatory period on limited capital to get a measure of slippages and real world adherence to projected risk and returns. Testing at time begins with deployment of a quick and dirty script, which is gradually developed in a robust trading module. All edge cases are resolved. All user features are built in the testing stage.Deployment
With the backing of proven projected risk and returns, capital is allotted, preferably incrementally over a few expiry periods. Any bugs are ironed out before full capital is deployed. Each escalation of capital deployment includes re-backtesting with room being kept for delaying full deployment, or aborting the strategy if required as per updated re-backtest results.A strategy which has performed within the projected risk and return parameters in all three market cycles is considered successfull.