From Statistics , we know that Range in Trading refers to the (High - Low) for the timeperiod/candlestick in concern. Wilder has started a Concept caled True Range which is defined as the Greater of the following:
- Current High minus the current Low
- Absolute Value of Current High minus the previous Close
- Absolute Value of Current Low minus the previous Close
Welles Wilder used the Absolute Values as he was interested in measuring the distance between two points, not the direction.
Take a look at the Google Sheet for Calculation of ATR. I have used GOOGLEFINANCE function to retreive the OHLCV of NIFTY for the first quarter of 2019. The GOOGLEFINANCE function is actually what makes Google Sheets such a compelling use. In due course I intend to publish a post to retreive OHLCV data for 1 minute candlestick from Google Sheets , on that can be used with Pandas DataFrame. In my opinion, once we create such an API, it is a better alternative to Quandl or Alpha Vantage or Quantra Blueshift or Twelve Data API for Backtesting. It also gives us the opportunity to do Live Testing of a Trading System with Live Data , but without risking actual Money. But more on that later on .... Here is the Google Sheet for now :
https://docs.google.com/spreadsheets/d/13pKpTZuDgPlb7jqH2Pyp8ypo7G97NKgV1YIgJ4j4toI/edit#gid=1308165568
Now lets copy the Values in the Excel We can copy the OHLCV data from the above google sheet and calculate the rest of the data. If you do it n a Excel spreadsheet, you will see that the ATR obtained from the excel is the same as the ATR obtained from Google Sheet. Now let us convert the excel to csv and use this csv file as the Input for our Python program.
The calculation of ATR depends on how far back we begin our calculation and in that measure, it is very similar to the calculation of the Exponential Moving Average (EMA). The standard period for the calculation of ATR is 14-days.
The Calculation of ATR from Google Sheets and Excel is by and large self-explanatory. The first 14-day ATR value was calculated by finding the average of the first 14 True Range values. Subsequent ATR values have been smoothed by using the ATR from the previous day.
Now lets calculate the value of ATR using Python.
First lets look at the imports used in the program:
Lets read the input file into pandas using the read_csv method. We pickup only the required columns and convert Date to Datetime:
Now lets see the calculations of the True Range(TR) in this program:
Now lets define a column called ATR and initialize it with nan values:
The average of the first 14 TR values is the first ATR for period=14
The ATR for the remainder periods is calculated as was done in the Google Sheet or Excel Spreadsheet.
The ATR calculated by using Python:
It is obvious that the value of ATR that we obtained on the Google Sheet varies from the value we obtained from our Python program . A read of this Stackoverflow Query should help to understand the right method to calculate in Python. Also the Pandas reference will help to understand the setting of the parameter adjust=False and the smoothing factor alpha
Now this is the ATR calculated.
Now the ATR value calculated with Python tallies with the ATR value in the Google Sheet (taken from StockCharts). Brokers like Trading View also use this methodology for calculation of ATR as their default (RMA smoothing). However the ATR calculated thus varies from the ATR used by brokers like Sharekhan in their trading platform Tradetiger. What we get using this method is different from what is used by Zerodha PI trading platform. (Again the ATR value in Zerodha PI differs from the ATR in Zerodha Kite. The latter is built on the Tradingview API). The difference is the ATR value for different brokers is largely explained by the smoothing method that they use. (generally RMA smoothing, SMA smoothing, WMA smoothing and EMA smoothing are the different types of smoothing used)
And so now you know why the ATR value varies from one Charting / Trading platform to another, even when the Data Source (Tick Data Source or minute level data or Daily Data) is exactly the same.
The Calculation of ATR from Google Sheets and Excel is by and large self-explanatory. The first 14-day ATR value was calculated by finding the average of the first 14 True Range values. Subsequent ATR values have been smoothed by using the ATR from the previous day.
Now lets calculate the value of ATR using Python.
First lets look at the imports used in the program:
# Imports and Declarations used in this program #import pdb import numpy as np import pandas as pd input_file = "test data.csv" period = 14 # the period for which ATR has to be calculated. Default is 14 atr = 'ATR' + '_' + str(period)
Lets read the input file into pandas using the read_csv method. We pickup only the required columns and convert Date to Datetime:
# Start of Main Program df_stock = pd.read_csv(input_file,header='infer', usecols=[0,1,2,3,4], infer_datetime_format=True) df_stock['Date'] = pd.to_datetime(df_stock['Date']) # convert date to datetime
Now lets see the calculations of the True Range(TR) in this program:
df_stock['PC'] = df_stock['Close'].shift(1) df_stock['H-PC'] = abs(df_stock['High'] - df_stock['PC']) df_stock['L-PC'] = abs(df_stock['Low'] - df_stock['PC']) df_stock['TR'] = df_stock[['H-L' ,'H-PC','L-PC']].max(axis=1)
Now lets define a column called ATR and initialize it with nan values:
df_stock[atr] = np.nan
The average of the first 14 TR values is the first ATR for period=14
df_stock.loc[(period -1), atr] = df_stock.TR[: period].mean()
The ATR for the remainder periods is calculated as was done in the Google Sheet or Excel Spreadsheet.
for i in range(period, len(df_stock)): df_stock.loc[i, atr] = (df_stock.loc[(i - 1), atr]*(i - 1) + df_stock.loc[i, 'TR'])/i print (period, df_stock.loc[period, atr])
The ATR calculated by using Python:
14 106.37219387755127 15 108.1373809523811 16 105.62566964285732 17 109.72121848739512 18 113.26448412698429 19 112.91898496240618 20 112.1405357142859 21 115.66955782312942 22 118.13912337662354 23 117.948291925466 24 115.95044642857158 --------------------- 66 113.46228354978356 67 113.47329424307036 68 113.3972163865546 69 112.56754658385094 70 112.08158163265308 71 111.36423541247487 72 111.49250992063494 73 111.57685909980432 74 112.35082046332045 75 111.93480952380952 76 112.63171992481203 77 113.40858070500929 78 113.50718864468865 79 113.34064195298374
It is obvious that the value of ATR that we obtained on the Google Sheet varies from the value we obtained from our Python program . A read of this Stackoverflow Query should help to understand the right method to calculate in Python. Also the Pandas reference will help to understand the setting of the parameter adjust=False and the smoothing factor alpha
con = pd.concat([df_stock[:period]['TR'].rolling(window=period).mean(), df_stock[period:]['TR']]) df_stock[atr] = con.ewm(alpha=1 / period, adjust=False).mean() print(df_stock[atr][period:len(df_stock)])
Now this is the ATR calculated.
14 106.37 15 108.26 16 105.38 17 110.37 18 114.88 75 108.03 76 112.10 77 116.41 78 116.74 79 115.57
Now the ATR value calculated with Python tallies with the ATR value in the Google Sheet (taken from StockCharts). Brokers like Trading View also use this methodology for calculation of ATR as their default (RMA smoothing). However the ATR calculated thus varies from the ATR used by brokers like Sharekhan in their trading platform Tradetiger. What we get using this method is different from what is used by Zerodha PI trading platform. (Again the ATR value in Zerodha PI differs from the ATR in Zerodha Kite. The latter is built on the Tradingview API). The difference is the ATR value for different brokers is largely explained by the smoothing method that they use. (generally RMA smoothing, SMA smoothing, WMA smoothing and EMA smoothing are the different types of smoothing used)
And so now you know why the ATR value varies from one Charting / Trading platform to another, even when the Data Source (Tick Data Source or minute level data or Daily Data) is exactly the same.
Reference: Check with user arkochhar in GitHub repo : https://github.com/arkochhar/Technical-Indicators/blob/master/indicator/indicators.py
No comments:
Post a Comment