This rare gem is a trend-following indicator that can be used either as a trading system or as a way to place your stops. We will introduce the intuition of the SuperTrend indicator, code it in Python, back-test a few strategies, and present our conclusion. Of course, just like any other indicator, the SuperTrend can find its place within a bigger trading system but not take over the whole decision-making process.

The first concept we should understand before creating the SuperTrend indicator is volatility. We sometimes measure volatility using the Average True Range. Although the ATR is considered a lagging indicator, it gives some insights as to where volatility is right now and where has it been last period (day, week, month, etc.). But before that, we should understand how the **True Range** is calculated (the ATR is just the average of that calculation).

The true range is simply the greatest of the three price differences:

**High - Low****| High - Previous close |****| Previous close - Low |**

Once we have got the maximum out of the above three, we simply take an average of n periods of the true ranges to get the Average True Range. Generally, since in periods of panic and price depreciation we see volatility go up, the ATR will most likely trend higher during these periods, similarly in times of steady uptrends or downtrends, the ATR will tend to go lower. One should always remember that this indicator is very lagging and therefore has to be used with extreme caution. Below is the function code that calculates a form of the ATR.

```
def atr(Data, atr_lookback, high, low, close, whereTR, whereATR):
## TR
for i in range(len(Data)):
Data[i, whereTR] = max(Data[i, high] - Data[i, low],
abs(Data[i, high] - Data[i - 1, close]),
abs(Data[i, low] - Data[i - 1, close]))
Data[0, whereATR] = Data[0, high] - Data[0, low]
Data[atr_lookback - 1, whereATR] = Data[0:atr_lookback - 1, whereTR].mean()
first_atr = Data[0, whereATR]
for i in range(len(Data)):
Data[0, whereATR] = first_atr
Data[i, whereATR] = (((Data[i - 1, whereATR] * (atr_lookback - 1)) + Data[i, whereTR]) / atr_lookback)
return Data
```

#data-science #investing #machine-learning #trading #finance

57.75 GEEK