Combining Heiken-Ashi With Moving Averages in a Trading Strategy.
Can Heiken-Ashi Combined With Moving Averages Provide Good Signals Together?
Combinations of strategies may offer better opportunities. We can combine indicators together or patterns together, or even better, indicators with patterns so that we get an extra confirmation factor. In this article, we will see how to use Heiken-Ashi charting alongside moving averages as trend confirmation technique. And then at the end, we can look at optimization options.
I have just published a new book after the success of New Technical Indicators in Python. It features a more complete description and addition of complex trading strategies with a GitHub page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below link, or if you prefer the PDF version, you could contact me on LinkedIn.
The Heiken-Ashi Candlestick Method
Candlesticks are a quick way to understand OHLC data and detect patterns. It is very straightforward and easy to interpret. A bullish (typically green) candle is when the market closes above its opening price. A bearish (typically red) candle is when the market closes below its opening price.
Let us see a full chart of candlestick to understand more how it is shown. The trend is clearly bullish with some corrections seen around the red candles. Notice the small candles where the opening price is almost the same as the closing price. This is called a Doji and signifies indecision and shows a possible reversal or consolidation.
The Heiken-Ashi (Also called Heikin-Ashi) candlesticks seek to clean out the picture and show a clearer trend by smoothing out the OHLC data. Here is how to calculate the Heiken-Ashi candlesticks:
And to calculate the high and low price, we take the maximum and minimum prices of the following:
The above formulas will smooth out the candles to give us a more defined and clear trend.
To code a function in Python that adds 4 new columns containing OHLC Heiken-Ashi data, we can use the below code block:
def heiken_ashi(Data, opening, high, low, close, where):
# Heiken-Ashi Open
try:
for i in range(len(Data)):
Data[i, where] = (Data[i - 1, opening] + Data[i - 1, close]) / 2
except:
pass
# Heiken-Ashi Close
for i in range(len(Data)):
Data[i, where + 3] = (Data[i, opening] + Data[i, high] + Data[i, low] + Data[i, close]) / 4
# Heiken-Ashi High
for i in range(len(Data)):
Data[i, where + 1] = max(Data[i, where], Data[i, where + 3], Data[i, high])
# Heiken-Ashi Low
for i in range(len(Data)):
Data[i, where + 2] = min(Data[i, where], Data[i, where + 3], Data[i, low])
return Data# To be used on an OHLC array with a few columns to spare
my_ohlc_array = heiken_ashi(my_ohlc_array, 0, 1, 2, 3, 4)
# The numbers signify in order: Open, High, Low, Close, then the column indexed at 4 is where the first new Heiken OHLC data will be populated (Meaning that the columns 4, 5, 6, and 7 will have a brand new OHLC data)
The two charts are simple a comparison between a regular candlestick chart and a Heiken-Ashi chart. We can see the resemblance but we can also see that the latter is smoother. We should note however that since the Heiken-Ashi method is simply an averaging, the price shown may not be the true market price.
The Concept of Moving Averages
Moving averages help us confirm and ride the trend. They are the most known technical indicator and this is because of their simplicity and their proven track record of adding value to the analyses. We can use them to find support and resistance levels, stops and targets, and to understand the underlying trend. This versatility makes them an indispensable tool in our trading arsenal.
As the name suggests, this is your plain simple mean that is used everywhere in statistics and basically any other part in our lives. It is simply the total values of the observations divided by the number of observations. Mathematically speaking, it can be written down as:
We can see that the moving average is providing decent dynamic support and resistance levels from where we can place our orders in case the market goes down there. The code for the moving average can be written down as the following:
def ma(Data, lookback, what, where):
for i in range(len(Data)):
try:
Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())
except IndexError:
pass
return Data
To use it, we need to have an OHLC data array with an extra empty column. This can be done by using the following code:
# Defining the function that adds a certain number of columns
def adder(Data, times):
for i in range(1, times + 1):
z = np.zeros((len(Data), 1), dtype = float)
Data = np.append(Data, z, axis = 1)
return Data
# Adding 1 extra column
my_data = adder(my_data, 1)
# Calling the moving average function
my_data = ma(my_data, 200, 3, 4)
The above states that the moving average function will be called on the array named my_data for a lookback period of 200, on the column indexed at 3 (closing prices in an OHLC array). The moving average values will then be put in the column indexed at 4 which is the one we have added using the adder function.
Creating the Combined Strategy
As with any proper research method, the aim is to test the strategy and to be able to see for ourselves whether it is worth having as an add-on to our pre-existing trading framework or not.
The first step is creating the trading rules. When will the system buy and when will it go short? In other words, when is the signal given that tells the system that the current market will go up or down?
The framework of creating the necessary environment of the strategy can be written down as follows:
# Calculate the Heikin Ashi values on OHLC historical data with 0 as the opening price, 1 as the high, 2 as the low, 3 as the close, and 5 as where the first value (open) of the Heikin Ashi will be placed, meaning that 5, 6, 7, and 8 will be populated by Heikin OHLC
my_data = heiken_ashi(my_data, 0, 1, 2, 3, 5)
# Calculate a 100-period moving average on the Heikin close, remember that the close now is column 8 and we want to populate the next empty column 9 with the moving average
my_data = ma(my_data, lookback, 8, 9)
The trading conditions we can choose are:
Go long (Buy) whenever the the Heikin-Ashi chart is bullish while the market is crosses above its 100-period moving average.
Go short (Sell) whenever the the Heikin-Ashi chart is bearish while the market is crosses below its 100-period moving average.
The above chart shows the signals generated from the system. We have to keep in mind the frequency of the signals when we are developing a trading algorithm. The signal function used to generate the triggers based on the conditions mentioned above can be found in this snippet:
def signal(Data, open_heikin, close_heikin, ma_col, buy, sell):
Data = adder(Data, 10)
for i in range(len(Data)):
if Data[i, close_heikin] > Data[i, open_heikin] and Data[i, close_heikin] > Data[i, ma_col] and Data[i - 1, close_heikin] < Data[i - 1, ma_col]:
Data[i, buy] = 1
elif Data[i, close_heikin] < Data[i, open_heikin] and Data[i, close_heikin] < Data[i, ma_col] and Data[i - 1, close_heikin] > Data[i - 1, ma_col]:
Data[i, sell] = -1
return Data
Now, it is time to see the intuition of analyzing the strategy. Remember, no Back-testing results will be delivered anymore but the below will be much more helpful.
Obviously, a lot of optimization can be done here to regulate the frequency of signals and improve their quality:
We can choose a condition relating to the Heikin-Ashi chart where we trigger the signal after consecutive candles.
We can tweak the moving average lookback period. We can also add other moving averages and switch the strategy from a moving average cross rather than a simple cross between the market price and a single moving average.
We can include candlestick charts in the Heikin-Ashi and combine it with moving averages.
We can also include the distance of the Heikin-Ashi close to the moving average as a measure of risk or even as a contrarian trigger.
We can use the candle’s body width as a measure of the trend’s strength. We can also include the moving average’s steepness into the formula.
Conclusion & Important Disclaimer
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing Back-testing results should lead the reader to explore more herself the strategy and work on it more. That way you can share with me your better strategy and we will get rich together.
To sum up, are the strategies I provide realistic? Yes, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? No, it is to stimulate brainstorming and getting more trading ideas as we are all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.
One Last Word
I have recently started an NFT collection that aims to support different humanitarian and medical causes. The Society of Light is a set of limited collectibles which will help make the world slightly better as each sale will see a percentage of it sent directly to the charity attributed to the avatar. As I always say, nothing better than a bullet list to outline the benefits of buying these NFT’s:
High-potential gain: By concentrating the remaining sales proceedings on marketing and promoting The Society of Light, I am aiming to maximize their value as much as possible in the secondary market. Remember that trading in the secondary market also means that a portion of royalties will be donated to the same charity.
Art collection and portfolio diversification: Having a collection of avatars that symbolize good deeds is truly satisfying. Investing does not need to only have selfish needs even though there is nothing wrong with investing to make money. But what about investing to make money, help others, and collect art?
Donating to your preferred cause(s): This is a flexible way of allocating different funds to your charities.
A free copy of my book in PDF: Any buyer of any NFT will receive a free copy of my latest book shown in the link of the article.