The SuperTrend Indicator in Python — Coding and Back-Testing Its Strategy

The SuperTrend Indicator in Python — Coding and Back-Testing Its Strategy

How to code it, back-test it, and evaluate it on FX Trading. We will introduce the intuition of the SuperTrend indicator, code it in Python, back-test a few strategies, and present our conclusion.

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.

Building Blocks

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Most popular Data Science and Machine Learning courses — July 2020

Most popular Data Science and Machine Learning courses — August 2020. This list was last updated in August 2020 — and will be updated regularly so as to keep it relevant

15 Machine Learning and Data Science Project Ideas with Datasets

Learning is a new fun in the field of Machine Learning and Data Science. In this article, we’ll be discussing 15 machine learning and data science projects.

Best Free Datasets for Data Science and Machine Learning Projects

This post will help you in finding different websites where you can easily get free Datasets to practice and develop projects in Data Science and Machine Learning.

Machine Learning in Finance | Data Science | Machine Learning | Python

In this article, I will take you through some highly used applications of Machine Learning in finance sectors that you should be perfect at if you have a good interest in Finance.

50 Data Science Jobs That Opened Just Last Week

Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments. Our latest survey report suggests that as the overall Data Science and Analytics market evolves to adapt to the constantly changing economic and business environments, data scientists and AI practitioners should be aware of the skills and tools that the broader community is working on. A good grip in these skills will further help data science enthusiasts to get the best jobs that various industries in their data science functions are offering.