Discover more from All About Trading!
The Elder Ray Index for Trading
Coding the Elder Ray Index in Python
Trend following requires understanding the current market state in order to be on the right side. This article discusses the Elder Ray index, a popular trend following technical indicator.
I have released a new book after the success of my previous one “Trend Following Strategies in Python”. It features advanced contrarian indicators and strategies with a GitHub page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below Amazon link (which contains a sample), or if you prefer to buy the PDF version, you could check the link at the end of the article.
Intuition and Calculation of the Elder Ray Index
Developed by Dr. Alexander Elder, the Elder Ray index is a trend following system that seeks to determine buy signals in an uptrend and sell signals in a downtrend.
The intuition of the index is quite simple and its formula is even simpler. Whenever the market is in a bullish trend, its highs are more likely greater than its short-term moving average (in a strong bullish market, even the lows are greater than the short-term moving average). Whenever the market is in a bearish trend, its lows are more likely lower than its short-term moving average (in a strong bearish market, even the highs are lower than the short-term moving average).
The Elder Ray index is composed of two indicators, the Bull Power and the Bear Power. These are calculated on a separate panel from the market price. The original lookback period used for the exponential moving average is 13. The formula is therefore as follows:
Next up, we’re going to see how to code this simple indicator in Python.
Coding the Elder Ray Index in Python
The required functions for this job are the function to add and remove columns, the moving average function, and the exponential moving average function.
import numpy as np def ma(data, lookback, close, position): data = add_column(data, 1) for i in range(len(data)): try: data[i, position] = (data[i - lookback + 1:i + 1, close].mean()) except IndexError: pass data = delete_row(data, lookback) return data def ema(data, alpha, lookback, close, position): alpha = alpha / (lookback + 1.0) beta = 1 - alpha data = ma(data, lookback, close, position) data[lookback + 1, position] = (data[lookback + 1, close] * alpha) + (data[lookback, position] * beta) for i in range(lookback + 2, len(data)): try: data[i, position] = (data[i, close] * alpha) + (data[i - 1, position] * beta) except IndexError: pass return data def add_column(data, times): for i in range(1, times + 1): new = np.zeros((len(data), 1), dtype = float) data = np.append(data, new, axis = 1) return data def delete_column(data, index, times): for i in range(1, times + 1): data = np.delete(data, index, axis = 1) return data
The following Figure shows the Elder Ray index on USDCAD’s daily values. Let’s see how to calculate the values.
The code is fairly simple as we are simply differencing columns as explained below in the code:
lookback = 13 # Calculating the 13-period exponential moving average my_data = ema(my_data, 2, lookback, 3, 4) # Adding two columns to harbor the Bull and Bear Power indicators my_data = add_column(my_data, 1) # Bull Power // High column - EMA column my_data[:, 5] = my_data[:, 1] - my_data[:, 4] # Bear Power // Low column - EMA column my_data[:, 6] = my_data[:, 2] - my_data[:, 4]
The following Figure shows another example of the indicator on EURUSD’s daily values.
The interpretation of the Elder Ray (composed of the Bull Power and the Bear Power) is as follows:
A strong implied bullish trend is in progress when both indicators are above zero and rising.
A strong implied bearish trend is in progress when both indicators are below zero and falling.
If we want to emphasize the Elder Ray index signals without the need to put two indicators, we can color-code the price bars as follows:
Bars colored in green refer to the strong implied bullish trend while bars colored in red refer to the strong implied bearish trend. Unfortunately, trend following causes a lot of lag which explains why the colors seem to predict reversal than to follow the trend.
This can be optimized and fixed. We will see that in a later article.
If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Learn Algorithmic Trading with Python Lumiwealth
Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more
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.
Getting healthier means changing your lifestyle. Noom Weight helps users lose weight in a sustainable way through behavioral change psychology. It's a no-brainer.