Discover more from All About Trading!
Understanding the Aroon Oscillator in Python
How to Code and Trade the Aroon Oscillator
Trend-following also deserves to be studied thoroughly as many known indicators do a pretty well job in tracking trends. It is known that trend-following strategies has some structural lags in them due to the confirmation of the new trend. They are more about time in the market than timing the market. In this article, a very known indicator is presented and coded, the Aroon oscillator.
I have released a new book called “Contrarian Trading Strategies in Python”. It features a lot of advanced contrarian indicators and strategies with a GitHub page dedicated to the continuously updated code. If you are interested, you could buy the PDF version directly through a PayPal payment of 9.99 EUR.
Please include your email in the note before paying so that you receive it on the right address. Also, once you receive it, make sure to download it through google drive.
Creating the Aroon Oscillator
The Aroon Oscillator is created from two sub-indicators called Aroon Up and Aroon Down. They measure the trend’s strength using the highs and lows in a way that is unique and unlike other regular indicators.
The first step is to calculate the Aroon Up and Down using the following formula:
Let us look at an example on what is meant by Aroon Up and Aroon Down. Consider an asset where it has reached a new low last time period. For a 25-period Aroon Down, the value will be 96 because we will be subtracting 1 from 25, dividing it by 25 and multiplying by 100. Similarly, for a 13-period Aroon Up, where an asset has reached a new high one period ago, the Aroon Up will show a reading of 92.30.
It is understood that the higher the value of the Aroon, the stronger the trend. Now, let us code the Aroon in Python before moving on to the Aroon Oscillator.
def aroon(Data, period, close, where): # Adding Columns Data = adder(Data, 10)
# Max Highs for i in range(len(Data)): try: Data[i, where] = max(Data[i - period + 1:i + 1, 1]) except ValueError: pass
# Max Lows for i in range(len(Data)): try: Data[i, where + 1] = min(Data[i - period + 1:i + 1, 2]) except ValueError: pass # Where the High Equals the Highest High in the period for i in range(len(Data)): if Data[i, 1] == Data[i, where]: Data[i, where + 2] = 1 # Where the Low Equals the Lowest Low in the period for i in range(len(Data)): if Data[i, 2] == Data[i, where + 1]: Data[i, where + 3] = 1
# Jumping Rows Data = jump(Data, period)
# Calculating Aroon Up for i in range(len(Data)): try: try: x = max(Data[i - period:i, 1]) y = np.where(Data[i - period:i, 1] == x) y = np.array(y) distance = period - y Data[i - 1, where + 4] = 100 *((period - distance) / period)
except ValueError: pass except IndexError: pass
# Calculating Aroon Down for i in range(len(Data)): try: try: x = min(Data[i - period:i, 2]) y = np.where(Data[i - period:i, 2] == x) y = np.array(y) distance = period - y Data[i - 1, where + 5] = 100 *((period - distance) / period)
except ValueError: pass except IndexError: pass # Cleaning Data = deleter(Data, 5, 4) return Data
The above chart shows the EURUSD hourly values with the Aroon Up in blue and Aroon Down in orange. The last step is to calculate the Aroon Oscillator which is the difference between Aroon Up and Aroon Down:
my_data[:, where_aroon_osc] = my_data[:, where_aroon_up] - my_data[:, where_aroon_down]
Created by Tushar Chande, the Aroon Oscillator is a popular trend-following indicator which is calculated as seen in the above code snippet. It is worth knowing that the word Aroon most likely means dawn’s early light in Sanskrit.
Trading the Aroon Oscillator
The way to trade the Aroon Oscillator is to use the flip technique where the value flips from positive to negative or from negative to positive. In our case, the below will be the trading conditions:
Buy (Long position) whenever the Aroon Oscillator turns positive by surpassing the zero line.
Sell (Short position) whenever the Aroon Oscillator turns negative by breaking the zero line.
def signal(Data, aroon_osc_col, buy, sell): Data = adder(Data, 10) for i in range(len(Data)): if Data[i, aroon_osc_col] > 0 and Data[i - 1, aroon_osc_col] < 0: Data[i, buy] = 1 elif Data[i, aroon_osc_col] < 0 and Data[i - 1, aroon_osc_col] > 0: Data[i, sell] = -1 return Data
As can be seen from the signal chart, sometimes, lag is the biggest enemy of trend-following strategies. What is the solution to this? We can try to tweak the lookback period, the conditions, add filters, and even change the strategy itself.
The V strategy is a unique contrarian confirmation technique with a configuration that resembles the letter V. Basically, to have a V signal, we need the following conditions:
For a bullish (Buy) signal, the Aroon Oscillator must shape a V formation, meaning the current reading is above -75, the previous reading is less than -75, and the one prior to it greater than -75.
For a bullish (Sell) signal, the Aroon Oscillator must shape an inverted V formation, meaning the current reading is less than 75, the previous reading is above 75, and the one prior to it less than 75.
def signal(Data, aroon_osc_colr, buy, sell): Data = adder(Data, 10) for i in range(len(Data)): if Data[i, aroon_osc_colr] > -75 and Data[i - 1, aroon_osc_colr] < -75 and Data[i - 2, aroon_osc_colr] > -75 : Data[i, buy] = 1 elif Data[i, aroon_osc_colr] < 75 and Data[i - 1, aroon_osc_colr] > 75 and Data[i - 2, aroon_osc_colr] < 75:
Data[i, sell] = -1 return Data
Make sure to focus on the concepts and not the code. The most important thing is to comprehend the techniques and strategies. The code is a matter of execution but the idea is something you cook in your head for a while.
Check out my weekly market sentiment report to understand the current positioning and to estimate the future direction of several major markets through complex and simple models working side by side. Find out more about the report through this link:
To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.