In the part 2 of backtesting in Python for breakout strategy, we studied about reading csv data from file. Now, in this section we will be calculating ATR, A sudo volatility parameter and swing points.
Below is the code for calculation of all the three above.
# ATR calculation
df['ATR'] = atr(df, 7)
# volatility calculation
df['Volatility'] = round((df['High'] - df['Low']) / df['ATR'], 2)
# calculate pivot
df = pivot(df)
In the above code, I have used functions for calculation of ATR and swing high and low. For calculation of swing high and swing low, pinescript of tradingview has a builtin function known as "pivothigh" and "pivotlow". I have taken inspiration from pinescript and have developed my own function for calculation of swings and has named this function as pivot.
For learning more about ATR function in python check out my earlier post on ATR.
For learning more about Pivot function implementation in python, check out my earlier post on Calculating Swing High and swing low in python.
Explanation of the code
The first instruction line of code is for the calculation of ATR
df['ATR'] will create a new column in dataframe with the title as 'ATR'. The function will return the value of ATR after the calculation of the same. The ATR function takes dataframe and lookback period as input. For more details on ATR function and how it works you can visit my earlier post here.
For all practical purposes, when you have code of the function, just copy paste and use it. You should not get involved into the details of the code. If you have understood the above code of ATR that it takes dataframe and look back period and return only one column of dataframe as output. Just copy the code provided and use it in python. Python and all other higher level languages are all about using in-built functions effectively.
The second instruction line of code is for the calculation of a pseudo parameter for volatility.
df['Volatility'] = round((df['High'] - df['Low']) / df['ATR'], 2)
The calculated value will be stored in a new dataframe column 'Volatility'. This parameter is calculated by dividing the range of bar with ATR value calculated in the first instruction line. discussed above.
The 'ATR' is not a percentage but an absolute number, while the range is also an absolute number. The division of range with ATR thus makes it a relative number and compares current volatility with the range of bar. Note that the operation between dataframe are being done column-wise and the result is also stored in a column.
In case, the operation being done is not a column to column, you have to take help of other python packages like numpy.
The third instruction line of code is for the calculation of swing high and swing low.
We have developed our own function for the calculation of swing high and swing low in python. This takes only dataframe as input and provide result as a new dataframe with additional columns of pivothigh and pivotlow.
You can view the entire dataframe by converting dataframe into CSV file with below mention code:
df.to_csv("outputDataframe.csv")
Below is the output of the CSV file. You can notice that a few more columns have been added to the initial dataframe which was consisting of only the price data.
Datetime |
Open |
High |
Low |
Close |
Volume |
ATR |
Volatility |
recentPL |
recentPH |
|
0 |
2021-09-01T00:00:00 |
639 |
650 |
636 |
641.5 |
245555 |
14 |
1 |
||
1 |
2021-09-02T00:00:00 |
644.8 |
648 |
640.5 |
643.85 |
169259 |
13.07 |
0.57 |
||
2 |
2021-09-03T00:00:00 |
646 |
649.9 |
637.6 |
640.75 |
220883 |
12.96 |
0.95 |
636 |
650 |
3 |
2021-09-06T00:00:00 |
638.2 |
661 |
638.2 |
643.35 |
423062 |
14.37 |
1.59 |
636 |
650 |
4 |
2021-09-07T00:00:00 |
644 |
648.4 |
631 |
636.7 |
239806 |
14.8 |
1.18 |
636 |
650 |
5 |
2021-09-08T00:00:00 |
636.7 |
641.15 |
631 |
633.05 |
161029 |
14.14 |
0.72 |
636 |
661 |
6 |
2021-09-09T00:00:00 |
634.4 |
635 |
626.4 |
630.55 |
206552 |
13.34 |
0.64 |
636 |
661 |
7 |
2021-09-13T00:00:00 |
630 |
645 |
626.5 |
636.85 |
339060 |
14.08 |
1.31 |
636 |
661 |
8 |
2021-09-14T00:00:00 |
640 |
645 |
632.1 |
633.85 |
232448 |
13.91 |
0.93 |
626.4 |
661 |
9 |
2021-09-15T00:00:00 |
637 |
640.85 |
632 |
638.35 |
192844 |
13.19 |
0.67 |
626.4 |
661 |
10 |
2021-09-16T00:00:00 |
640.5 |
645.55 |
631 |
632.35 |
201783 |
13.38 |
1.09 |
626.4 |
661 |
11 |
2021-09-17T00:00:00 |
638.5 |
638.5 |
619.5 |
622.35 |
391946 |
14.19 |
1.34 |
626.4 |
661 |
12 |
2021-09-20T00:00:00 |
620.5 |
645 |
602.9 |
606.9 |
475008 |
18.17 |
2.32 |
626.4 |
645.55 |
13 |
2021-09-21T00:00:00 |
608 |
617 |
592.25 |
600.1 |
379960 |
19.11 |
1.3 |
626.4 |
645.55 |
14 |
2021-09-22T00:00:00 |
607.95 |
607.95 |
595.35 |
599.7 |
360390 |
18.18 |
0.69 |
626.4 |
645.55 |
15 |
2021-09-23T00:00:00 |
600 |
619.9 |
600 |
610.9 |
550854 |
18.47 |
1.08 |
592.25 |
645.55 |
16 |
2021-09-24T00:00:00 |
614 |
614 |
597 |
601.05 |
266065 |
18.26 |
0.93 |
592.25 |
645.55 |
17 |
2021-09-27T00:00:00 |
600.5 |
604.75 |
585.3 |
594.7 |
325472 |
18.43 |
1.06 |
592.25 |
645.55 |
18 |
2021-09-28T00:00:00 |
599 |
609 |
594.7 |
598.5 |
364345 |
17.84 |
0.8 |
592.25 |
645.55 |
19 |
2021-09-29T00:00:00 |
596 |
629.8 |
593 |
624.3 |
1020112 |
20.55 |
1.79 |
585.3 |
645.55 |
The ATR column was added with the first instruction code. While the second instruction code added the volatility column. The third instruction has added two columns to show recently formed swing highs and swing lows. The columns with other pivots were dropped in the function itself. If you want you can retain them to check whether the code is working as per your requirement or not.
Next part - BreakOut Strategy - Backtesting in Python (PART-4)