Repository to perform backtest on CSV datasets of indian indexes
- Python version 3.6 or above is required
- Clone the repository from github.com Git is required if you want to clone the repository
git clone https://github.com/PrajwalShenoy/backtest.git
or
- Download the zip file from https://github.com/PrajwalShenoy/backtest
To download the zip file click on the greenCode
and click onDownload ZIP
- Make sure you are in the directory where
requirements.txt
is present (backtest/) - Open a terminal in that location and run the following command
pip install .
With this the code should be ready to use
- Open a python terminal and run the following command with your custom values.
- If you do not have a account on maticalgos, you can follow the following link to create an account http://historical.maticalgos.com/
index
can be given the value ofbanknifty
ornifty
- Make sure the directory mentioned in
file_path
is created before running the commands
from backtest.get_historical_data import get_historical_data
get_historical_data(index="banknifty", email="abc@xyz.com", password="password", start_date="2020-01-01", end_date="2020-01-31", file_path="/home/user/Desktop/historicalData")
- Open a python terminal and run the following command with your custom values.
from backtest.setTimeStraddleIndexSL import setTimeStraddleIndexSL
trade1 = setTimeStraddleIndexSL(index="BANKNIFTY", start_date="2020-01-01", end_date="2020-01-10", entry_time="09:20:00", exit_time="15:24:00", stop_loss_p=0.009, historical_data_path="/home/prajwal/Desktop/backtest-documentation/back/backtest/", number_of_lots=1, csv_out_file="trade1_report.csv", days_to_run=[2,3])
trade1.runBackTest()
- Open a python terminal and run the following command with your custom values.
from backtest.consolidate_reports import consolidate_reports
trade1 = setTimeStraddleIndexSL(index="BANKNIFTY", start_date="2019-01-01", end_date="2021-12-31", entry_time="09:20:00", exit_time="15:24:00", stop_loss_p=0.009, historical_data_path="/home/prajwal/Documents/Repositories/kotak/historical_data/", number_of_lots=1, csv_out_file="trade1_report.csv", days_to_run=[2,3])
trade1.runBackTest()
trade2 = setTimeStraddleIndexSL(index="BANKNIFTY", start_date="2019-01-01", end_date="2021-12-31", entry_time="09:20:00", exit_time="15:24:00", stop_loss_p=0.007, historical_data_path="/home/prajwal/Documents/Repositories/kotak/historical_data/", number_of_lots=1, csv_out_file="trade2_report.csv", days_to_run=[0,1,2,3,4])
trade2.runBackTest()
# The above commands generate trade1_report.csv and trade2_report.csv. The next command creates the consolidated report
consolidate_reports(csv_file_names=["trade1_report.csv", "trade2_report.csv"], consolidated_report="consolidated.csv")
- Open a python terminal and run the following command with your custom csv file
from backtest.analyseReport import m2mPlot, perWeekdayPieChart, lossesSplit, profitsSplit
import pandas as pd
df = pd.read_csv("Path to report file")
profitsSplit(df)
lossesSplit(df)
perWeekdayPieChart(df)
m2mPlot(df)
days_to_run
indicate the days on which the back test will run. The mapping is as follows
0 - Monday
1 - Tuesday
2 - Wednesday
3 - Thursday
4 - Friday
5 - Saturday
6 - Sunday
- Even when specified, backtest will not run on
5
and6
. (Afterall backtest also needs a holiday XD) Formula_generator.xlsm
is a community developed excel sheet to help you guys get the respectivepython
command to run the respective straddles.
Special thanks to Himanshu for helping test this new tool