Backtesting Heikin-Ashi Candles and EMA in Excel

So, a few weeks ago, Bridegewater posted about doing a strategy of weekly Heikin-Ashi candles closing in relation to 7 EMA, using Stockcharts and the ticker QQQ. Just recently, he posted using it on Saul’s stocks. Had previously started running some backtesting in Excel. Still working on somethings, but now seemed a good time to throw it out for general review.

This is not intended to be a “how-to” thread nor a specific endorsement of Excel backtesting. It is intended to introduce some of the uses and outcomes of such testing, as well as provide more specific information as to specifically using EMA-Wkly HA crosses. I am going to break this up into a series of posts (although keeping it all on one thread). I’m also NOT an Excel guru and may have made mistakes that I’ve not detected, so please do your own due diligence before adapting anything and please share any thoughts or issues that you see.

I’m using Excel 365 with the basic structure of the Workbook containing a worksheet of the raw data and separate worksheets for calculations. The raw data is downloaded from Yahoo Historical data as weekly data. This is the OHLC for the week’s data as tabulated by Yahoo. NOTE: I used “Max” data range so this uses typically about 20 years of action (depending upon stock) so it covers a huge variation in stock and market conditions and reaction. The Heikin-Ashi candle values are calculated in the workbook directly from the raw data, as are the moving averages. Since all these calculations refer directly to the raw data worksheet, this allows me to simply replace the raw data in the single worksheet to generate evaluation of a different ticker. Each ticker has a separate workbook. I had previously a few years ago generated the core workbook and calculations for the HA candles and EMAs but double checked them here with candle values in TOS and Stockcharts. It is interesting that those values are not always the same between TOS and Stockcharts. My excel values are typically spot on or very close to TOS. Stockcharts seems to have more variation (via smoothing or adjusted closes?) but it’s all within reason.

Calculating the buy/sell signals is from the basic formula:
=IF(AND(E4>F4,E3<=F3),“Buy”,IF(AND(E4<F4,E3>=F3),“Sell”,“”)) where E=Close value of the weekly HA Candle and F= EMA value.

Data are collated and tabulated using the FILTER command for date and values, filtered by “Buy” or “Sell” result from the above formula. Standard formulas for EMA’s, Heikin Ashi candles, etc are from Investopedia and Stockcharts. Note, I commonly include additional “IF” functions in many of the calculations so that if there is no further raw data or subsequent data then the value is blank. This eliminates subsequent Div/0 and other common results that create errors further down the chain. Here’s a couple screen shots to give you an idea of the end results. You are not suppose to be able to read detail necessarily, it’s just to get the gestalt of what is involved and shows up.

The end calculations are in conditionally formatted cells showing green cell color for positive and red for negative. You can visually get an impression of the results above. I did tabulate the results in total as well broken down by the trades per year in conditionally formatted cells.

I think the first striking thing is the number of red cells, certainly makes the point that you don’t expect all trades to be positive when strictly using just EMA/HA Close data.
There are multiple variables to work through including optimal MA, form of MA and Buy/Sell timing. One of the first issues I addressed was the Trade timing, meaning do you buy/sell just before the close on the last moments for the candle (meaning Friday dependent upon the apparent candle close relative to the MA) or do you buy the open of the next candle, meaning Monday morning. There’s also the variation of combinations. For simplicity, I first looked at the HA candle close relative to the 7 EMA for QQQ, as initially suggested by Bridgewater. Here are the results, note that SCC=Same Candle Close (Friday’s) and NCO=Next Candle Open (Monday’s) for example so that SCC/SCC is buying and selling on the apparent close on Friday for both. Again, remember the total P/L is the sum over the entire duration of the data (often 20 years or more) so Average P/L is the better comparison between stocks. For 7 EMA crossovers of the weekly HA close:

The ”Whipsaw” calculations are referring to a Buy-Sell or Sell-Buy interval of <10 days (next candle) or <15 days (two candles) for Buy-Buy. Total whipsaws are total trades of one candle duration.

It’s interesting that the NCO trades are slightly better, although there is not a huge difference. Remember, backtesting is specific for an individual ticker and you can not directly assume every ticker will respond exactly the same. It does give you an indication of how things will likely respond but it’s up to you to form your plan and approach. Personally, I will lean towards making SCC decisions, but it’s nice to know that should those decisions be delayed that I am unlikely risking a significant loss of potential profit.

The next issue is what is the optimal EMA. I used these worksheets and formulas to compare QQQ weekly Heikin-Ashi candle closes to various EMA’s. I chose 3, 5, 8, 13, 21, 34 and 55 as they are Fibonacci numbers. I’m showing data for both same candle close price and next candle open price out of curiosity.

The change in qualitative variation is interesting, but not surprising that the shorter EMA’s had more trades with more whipsaw. There is certainly a trend for longer periods to do better in overall P/L. Clearly 21 and 34 EMA’s did much better than the shorter 7 EMA, with longer duration trades, fewer negative trades, less whipsaw.

  1. Using the weekly Heikin-Ashi candle close compared to the 7 EMA is a reasonable indicator of when to buy or sell. Is it absolute? Certainly not, but it appears to provide a positive ratio of profit versus loss to make money.
  2. The data clearly does emphasize the common accepted perspective that you will always have losing trades but you can still make a good profit if you manage them. Supports the adage of cutting losers and letting winners run.
  3. Surprisingly, to me, it appears that while using HA candles (that do NOT show gaps and are calculated) it does not apparently influence the P/L much whether you work to close at the end of Friday or the beginning of Monday. The SCC compared to NCO data. It certainly makes me more comfortable knowing that I
  4. Longer length EMA’s did significantly better than the shorter EMA’s such as the suggested 7 EMA.

What does this EMA-HA Close crossover tell you? Not much more about a stock than it has moved into an uptrend or a down-trend. It gives no indication as to the strength of the move nor anticipated duration. Understand the limits. The following graphically shows you the difference.

Wkly Heikin-Ashi with 13 EMA

Wkly HA 13 EMA

21 EMA

Wkly HA 21 EMA

34 EMA

Wkly HA 34 EMA

The trend is your friend until it ends with a bend.

The interesting thing is to look at some other moving averages as well as different equities. QQQ is a relatively lower volatile equity. I have some other data, but won’t have time to post until later.

Happy hunting,
Lakedog

3 Likes

OTHER TICKERS AND MOVING AVERAGES

There are many ways to calculate moving averages such as Exponential, Hull, Weighted, TEMA, Triangular, etc. While I have run most of them and they all show variation in the P/L curves, it is often a peak shift. I have opted to not dump thousands of data points on you, but wanted to give you just four tickers using EMA and Triangular MA (since we just had a discussion on that).

Here’s QQQ again, but with the data also for Triangular MA (TriMA).

It certainly appears like there is improved P/L in the higher MA levels, but remember this is data over 20+ years so the annual average is not quite as impressive. Although, statistically, probably has a real shift. But here’s some more.

Here’s CAT, a slower moving work horse of a ticker.

And MSFT over it’s lifetime.

And now, the most recent triple trillion dollar company.

From my perspective, these data support that the gain of using moving averages is largely helping you maintain your position in a ticker until there is more evidence of a trend change. The key is selecting strong companies that have growth charts. Tickers do not trade in a vacuum. That also refers to watching the overall market environment, sector rotation and group performance.

Remember, I am dealing with thousands of data points, hundreds of formulas and technically speaking, $hit happens. Do your own due diligence. And please, if you see errors, let me know. I have some more data that I will post as I can.

Happy hunting,
Lakedog

2 Likes

so looks like ít is better to use EMA on high growth stocks or ETF. Wonder how TQQQ’s result compared to QQQ - thanks

TQQQ looks great. I have that data and am trying to get time to put it together for all. Hoping later today. There’s a couple curves to it relating to QQQ and SQQQ, trying to get time to double check the data before posting. Started a major landscaping project as well as gutting the lake house kitchen for a remodel. Not to mention, I’m out of the country for the next couple weeks. Free time isn’t free right now. Will drop what I can today and tomorrow.

Happy Fathers Day all!

Lakedog

1 Like

Wow. Thank you Lakedog! It will take some careful reading to go thru this analysis, but really appreciate your effort. Good luck with your home projects. Personally I’m taking Father’s Day off.

Mike

Time’s escaping me, so thought I would just post this not as double checked as I’d like, but still interesting. This is TQQQ and EMA’s. TriMAs were similar.

Table data in error-----removed. See below****

From the data, it suggests that pattern of longer length moving averages, such as the 34 EMA is better holds for TQQQ. I will do multiple other leveraged ETFs at some point. I also have not done any manual double-checks with this, so I do hesitate to put it out. However, you are all forewarned. I can tell you that in Excel back-testing, SQQQ does not fare well. I think mainly because the duration of events are so short (relatively short, being generally only a few weeks) and the negative trades overwhelm.

Again, the moving averages mainly are mainly informative as to a change in trend. Stochastic RSI was mentioned and it is likely a good choice as maybe a better “indicator” when matched with weekly Heikin-Ashi. I have started some initial testing but it is difficult. A simple 0.5 cross is not strong, EMA tends to be superior. However, there are multiple “trigger” levels in StochRSI such as crossing above the 0.2 line, crossing 0.5, dropping below 0.8 or combinations of those. It is going to take some fancy formula generation with IF, AND and OR statements. Not sure I’m up to it but will give it a whirl when back in country. Happy to hear from anyone else with their thoughts and trials.

Happy hunting,
Lakedog

1 Like

Just want to make sure I read the table correctly: when you say 7 EMA that means 7 WEEK EMA or 7 DAY EMA? Thanks for the excellent data. Really appreciate it

1 Like

I debated from your original post which way you were doing it. Convention from most charting programs is calculating indices from the same time interval as the graphing. So I followed suit. This is WEEKLY EMA. Calculations are from weekly OHLC values.

I must stress again and again. Backtesting is not absolute. It supports the interactions of price action and indicators, but does not guarantee or predict for other influences. I double check what I can, but a simple formula error in an early worksheet can tweak the entire workbook. Please due your own due diligence.

Lakedog

PS And thank you Bridgewater for sharing the weekly HA approach.

The comment on daily data was made. I should note that the initial major interest here was using weekly intervals for those of us who can not take the time to consistently spend hours daily reviewing charts. Because the major signal really is trend, it works on other time frames. Daily appears also very effective with TQQQ.

EMA crosses are another common approach to predict trend changes. I used to use 3-8 EMA crosses. The difference here is using Heikin-Ashi candles. But I tested variations on this a few years ago, even trying an average of the HA candles with moving averages. The key then and now is to never forget that the choice of equity is equally, actually more important. Weak, flat and choppy stocks will be weak, flat or choppy. But hopefully you’ll be out of it using whatever signal.

Happy hunting,
Lakedog

PS Don’t forget, OPEX is Friday. Max pain week is always interesting.

***************ERROR FOUND ***********

Two lines of data didn’t copy from Yahoo, messed up the sequencing. Here’s a corrected table.
So, so sorry! Doesn’t change the relative nature, just the specific. One more reason that I look at the trend of the data, not the specific number.

TQQQ with Weekly Heikin-Ashi Candles and EMA’s

Apologies to all,
Lakedog

The glitch in data transfer may have corrupted an earlier file and template. I need to do some checking. This is what I dread the most and apologize again. I try not to post anything until I’ve double-checked. I broke my own rules.

I think the positive nature of the process holds true, the numbers are probably wrong. I will go back and try to check/correct as much as I can in the next day or so, but will be out of the country and without access for several weeks. I WILL review and correct all eventually.

I stand frustrated and embarrassed. Apologies.

Lakedog

1 Like

Hey Lake no need to apologize. Mistakes are all part of the process. Thanks for sharing.

Andy

1 Like

Do you have the trade dates for 7wEMA and 34wEMA? Will help my study tremendously
Bridgewater

1 Like

For TQQQ:

The trade data for 7 wEMA (in two parts so you can read it):

Trade data for 34 wEMA:

Couple of notes. First, the very first “Buy” or last “Sell” may be artificial. The Excel formulas can’t handle and calculate an initial “Sell” nor end on a “Buy.” I added those if there was not a natural start and stop. It’s actually starting a trade late or ending it early, so is very trivial to the numbers. The alternative is to delete the initial or last action but that varies the amount of data between EMAs, so I chose the former.

Second, I have randomly checked several of the Excel values (Heikin-Ashi close, EMA value and cross point) with Thinkorswim graphs and listed values. TOS makes it real easy to check such. They are either exact matches or vary by on a few thousands. Not significant. Crossing points matched on the ones I checked. As I have mentioned before, Stockcharts has more variation in the HA values, often in the tenths. It’s unclear why and they have never answered a request to understand. Likely a smoothing calculation, but again, does not seem to change the graphics at all.

Please let me know if this makes sense and how it works out for you. And please understand if I don’t answer for the next couple weeks. I will in July.

Lakedog

2 Likes

Thank you so much, and have a good vacation

1 Like

Hey Bridgewater,

Wondering if you got anything out of the data from the excel files?

So here’s some more data to chew on if you are interested.

Here’s TQQQ but using it under daily.

The big question is of course, can it work with SQQQ. Sadly, the answer seems to be “no.” SQQQ is a viable ETF, but doesn’t have a solid enough trend to work with this approach. Need to look at further options to fit better.

SQQQ Weekly Heikin-Ashi EMA crosses:

And SQQQ Daily data:

It’s surprising that the numbers are not more variable. I’ve double checked EMA, HA and cross-over data and it all matches TOS data. Will explore more variations and combinations to see if a pattern presents itself. I am exploring Stochastic RSI but the calculations are complex. Simple 0.5 crosses does not seem viable. There are differences when the cross is a rebound from below 0.2 or above 0.2, etc. Using ChatGPT to try and help sort it out but it’s been a challenge. Will see.

Happy hunting,
Lakedog

1 Like

Welcome back from vacation
I am still in the process of marking up on the chart the trades you gave me, going kind of slow due to the grandkids summer activities. Will post the result when done
Looks like many friends on this board are interested in the 21dayEMA, could you give me the trade dates for it also
Thanks
Bridgewater

1 Like

Hear you about grandkids. Got a few weeks of duties coming up myself. I’ve been randomly picking one or two “trades” from the 1st, 2nd and third yearly portions of the data and checking that data. Haven’t been doing each trade as kinda defeats the purpose of using the spreadsheet to evaluate. Sounds like you have a group you’re working with, here’s the 21 EMA data.

Enjoy the grandkids!

Lakedog

PS Do you use Thinkorswim?

1 Like

I use stockcharts, I do have a TDTrade account and thinking about learning TOS
BTW, the list you just sent is it for 21weekEMA or 21DayEMA ? recently I saw many posts in the TechnicalTraderSantuary about 21dEMA - I do use 21dEMA, and also recently been using 2Hour50EMA chart with good result (may be just because most things are up lately - 2Hr50EMA is about the same as 13DayEMA and many folks do use it

this is the 2Hour chart for IOT - the dark blue line is 50EMA - I ignore all the Buy/Sell price label - just use the 50EMA

2 Likes