BreakOut Strategy - Backtesting in Python (PART-1)

I have created a simple breakout strategy based on my last pinescript code in python. For those who are new to python I strongly suggest you to have your basics brushed up. You can learn basics from my book "Python for Non-Programmers".

Basics of Python

Python have many modules that you may install and use. One of them allows to work on data like you are working on spreadsheet while other allows you to store data like a spread sheet. These useful modules are :

1. pandas : Allows storage as if it is a spread sheet and retrival of data from this virtual spread sheet known as Dataframe.

2. numpy : useful for manupulation of dataframe data

Installing the above two modules in your Python Environment

Installation of modules in python is simple, it can be installed from your  command  prompt  or  power  shell.  Below  is  an  example wherein a module named “pandas” is installed on the system. 


For installation of any module just type “pip installed” followed by the name of the module. 

pip install [name of module] 

You can do practice by installing the the other module “numpy”.  In case you copy a code from somewhere, it gives an error  like  some  function  is  not  available.  It  is  most  likely  that  a module is missing and you have not installed it. 

If a module is not included,  assuming you people are using pycharm  IDE, it will  highlight missing  functions with a red underline.

Downloading Data For BackTesting

I am using API provided by my broker (5Paisa in India) for live feed and historical data. The data feed should be reliable with no breaks. 

Here in this example either you can download data from yahoo through python or can use already downloaded file for analysis.

In case you want to learn How to download Data from Yahoo, I will explain in separate blog post.

Here in this example, I am using a csv file already available with me. It looks like :


CSV is a file extension which stands for " comma seprated file". The csv file can easily be opened in EXCEL or any other spreadsheet software, but note that this is not a excel file. Comma seperated files are written as: 

Datetime,Open,High,Low,Close,Volume
2021-10-01T09:15:00,448.05,452.1,444.05,451.35,2323271
2021-10-01T09:30:00,451.15,451.95,449.8,450.3,688008
2021-10-01T09:45:00,450.3,450.3,447.5,448.5,861238
2021-10-01T10:00:00,448.85,450.15,448.0,449.75,630605
2021-10-01T10:15:00,449.7,452.8,449.7,452.15,787878
2021-10-01T10:30:00,452.2,452.2,450.05,451.3,445230
2021-10-01T10:45:00,451.35,453.45,451.25,452.2,618452
2021-10-01T11:00:00,452.25,452.6,450.75,451.5,311143
2021-10-01T11:15:00,451.5,454.75,451.2,452.7,712678

Now, if you have a comma-separated historical data file , python installed , pycharm IDE or any other good IDE, already installed pandas and numpy, we are ready to enter into phase two of python i.e. READING DATA FROM CSV.

Resources

Highest Rated Udemy Course on PineScript - Grab your Seat Now 

Udemy Discount Coupon Code : UDEMY-JAN23 (Valid upto 30th Nov 2023)

Learn more about coding on tradingview in PineScript through Books on pinescript available on amazon and kindle.


200+ pages book100 pages book200+ pages book


Point and Figure Charts : A Time-Tested Tool for Technical Analysis

In the dynamic world of financial markets, investors and traders constantly seek tools that can provide valuable insights into market trends...