This code is for various Quants and Geeks who like to algo trade.
We use following price action Strategy:
- Detetcing if price is near Support/Resistance.
- Reversal if Engulfing Patter is formed
- Breakout if Support or Resistance is penetrated.
- Confluance of Volume and Candle power is also taken into account
- The code gets all the data and saves as csv file in Data directory.
- The CSV data is ingested in Postgresql Database, it also updates the new data if found from step 1.
- The data is loaded in Pandas dataFrame for passing into Backtrader cerebro (aka brain).
- All analysis is done and a Tear Sheet is generated for the stock.
- You can use single line function to download as much data from IBKR for a stock or list of stocks. Just remember not to abuse the API or you can be banned by IBKR.
- The strtegy rely on Support and Resistance level provided by you.
- You can easly bring the drawdown as low as 2%
Our code uses very few open source project to run :
- [Backtrader] - For Backtesting.
- [Postgresql] - For storing data.
- [TWS API] - For Retriving data
Install the all the pip packages.
pip activate <your_enviorenment_name>
pip install backtrader
pip install pandas numpy matplotlib==3.2.0 plotly psycopg2 seaborn scipy SQLAlchemy statsmodels tabulate tzlocal
We require TWS API requires to run. Download IBAPI from following: (https://interactivebrokers.github.io/) And open terminal anf type following:
cd ./IBJts/source/pythonclient/
pip activate <your_enviorenment_name>
python setup.py install
Install Postgres and create database.
sudo apt-get install postgresql pgadmin
sudo su postgres
psql
>create user krh with encrypted password 'krh@123';
>create database krh;
>grant all privileges on database krh to krh;
>\q;
First Step: Click Here to Download this Repo Second Step: Open terminal and run:
pip activate <your_enviorenment_name>
python backtest.py
Third Step: Change parameters in backtest.py at function "cerebro.addstrategy". Go back to Step 2.
Most algo Traders and quants have to optimize the paramters and thus have to re run thier code many times. Thus we recommed to use VS Code Ide as by single click you can run. This will eliminate Step 2. Just by single click you can keep optimizing code.
Data Related | Usage |
---|---|
backfiller.py | It is used to obatin data. Function "backfill_now" does all magic for you. |
Strategy Related | |
strategy/ReversalAction.py | This is where our startegy reversal action is made. Skim through it |
analyzer.py | This where we can add/remove various parametrers to judge.ex sharpe ratio, yield |
For Actual Running | |
backtest.py | Final backtesting libary. You only have to run this. |
live.py | Final backtesting libary for live trading. You only have to run this for trading. |
Tutorial: | |
Ingester_baby_steps.ipynb | How to make data ingesting library step by step by using TWS API |
Ingester.ipynb | Final Ingester file for creating library |
Backtester_Step_By_Step.ipynb | How to learn Backtesting step by step using Backtarder Platform |
Output: | |
TATAMOTOR.html | Tear Sheet for Tatat Motors, which was our x during this whole time. |
Helper Files | |
broker_to_csv.py | Helper fucntions to backfill_now, do not edit unless required |
csv_to_sql.py | Helper fucntions to backfill_now, do not edit unless required |
test.py | Dummy file do as you wish with this |
Size | |||||||
---|---|---|---|---|---|---|---|
1 secs | 5 secs | 10 secs | 15 secs | 30 secs | |||
1 min | 2 mins | 3 mins | 5 mins | 10 mins | 15 mins | 20 mins | 30 mins |
1 hour | 2 hours | 3 hours | 4 hours | 8 hours | |||
1 day | |||||||
1 week | |||||||
1 month |
Unit | Description |
---|---|
S | Seconds |
D | Day |
W | Week |
M | Month |
Y | Year |
Copyright 2021 Sagar Rathi