Back

John Ehlers' Sine Wave Indicator

Here is the Sine Wave indicator by John Ehlers.

Most technical analysis is quite simple but John Ehlers takes it one step further and incorporates fractals and sine waves into his analysis. The mathematics in his technical analysis indicators is really some of the most advanced that you can find. He releases most of his findings publicly on his website here. Some of his papers are really a must-read for those who want to take their technical analysis to the next level.

I wanted to backtest some of his strategies so I've converted his EasyLanguage code into C#.

In this share, I present to you the Sine Wave indicator. The Sine Wave (also called MESA) indicator is based on the assumption that markets move in cyclical patterns. He tries to quantify such cycles and then predict them (by making his indicator leading instead of lagging like most indicators). This works extremely well when the market is indeed moving in a cycle. When the market is trending, however, this system fails (and one should adjust for that). Here is an example where the indicator is working well and when it is not. Note that when the market is trending, you could recognise this and use it to your advantage (see his Empirical Mode Decomposition recognition algorithm).



So when the market is cyclical, you could use the Sine Wave Indicator. When the market is trending, you could use a moving average-based indicator. Right now, I haven't incorporated this yet. The algorithm (as is) should therefore be used on markets that are known to move in cyclical patterns. The S&P 500 (e.g. SPY) is one of such markets, but many believe that the FOREX markets are also quite cyclical. It's interesting to see that it performs quite well on SPY but fails miserably on, for example, AAPL.

I hope you like it. Happy coding.
Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



Having enjoyed toying with the Mama Frama code, I've been meaning for some time to concentrate on the sine wave indicator in conjunction with the Empirical Mode Decomposition (EMD). I've not had any problem getting the code working and have yet to notice any obvious bugs. My backtests are running through lean rather than the QC interface, so no graphs I'm afraid.

I first of all tried the Sine Wave as a standalone on a forex pair and recreated the striping behavior that was expected which is presumably alternation between cyclical and trending. I then proceeded to tweak various parameters (MESA_length, _limit, pps upper and lower and TradeBar period) to maximize compound return.

When I then integrated the EMD as a filter, the results at first significantly reduced the return. Obviously, this was due to the tweaked parameters on Sine Wave not being consistent for EMD. I then tweaked parameters on EMD (DC_delta, DC_period, DC_frac, MESA_length and window lengths) in the hope of further improving returns.

At this stage I've tried everything I can think of to use EMD as an efficient filter of Sine Wave trades, but my best result actually slightly reduces returns compared to the Sine Wave independently.

The Sine Wave algorithm by itself produces satisfactory results, but I've not been able to leverage this with EMD. It seems the EMD is hampering profitable trades more often than it is filtering losses. What do you think would be a good approach to coordinating the Sine Wave parameters with that of EMD?
0

"My backtests are running through lean rather than the QC interface, so no graphs I'm afraid." -- Give us a week we'll have some beautiful local charting for you :)
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


@James Smith

Same results here. Sine Wave Indicator works better standalone than in combination with the EMD. John Ehlers recommended to combine them but I'm not sure whether he actually tried to do that himself. Also, parameter optimalisation of the two algoroithms based on return improvements is probably not robust and subject to overfitting (so be cautious when doing this).
0

I wonder if there is a stricter definition to determine if a market exhibits the cyclical and trending alternation? It may be that this phenomena is less pronounced in some markets. I share the concern about over-fitting given the number of parameters, which is why I was hoping the optimum EMD may be derived directly from a given sine wave. These internals are probably beyond me: I just rely on trial and error.
0

@Jared: That's great. I noticed the UI branch but had no idea it was so close to complete.
0

@James Smith

Look closely at the start of the "bad" cycles in the picture in this post. Do you see how the blue line of the Sine Wave indicator goes up again without having crossed at the bottom? That is an indication that the market is not cyclical. Hence, this implies that the market is trending. As you can see, this happens twice before a cross at the bottom occurs again. Same holds for crosses at the top. This could pose as an alternative indicator of trends. If you can somehow detect this trending behaviour quickly (blue line goes up instead of down, or even better: blue line moves approximately sideways instead of down), you could use this as a signal that the stock is currently trending.
0

I have a question related to this bit of code:

 

for (int i = 0; i < MESA_length; i++) {
double temp = Prices[i];
realPart = realPart + temp*Math.Cos(2*Math.PI*i/MESA_length);
imagPart = imagPart + temp*Math.Sin(2*Math.PI*i/MESA_length);
}I know that this is how Ehlers has published it in his books, I also know that over the years his books have had many errors. I'm wondering if the code should be:
  for (int i = 0; i < MESA_length; i++) {
double temp = Prices[i];
realPart = realPart + temp*Math.Cos(2*Math.PI*(i+1)/MESA_length);
imagPart = imagPart + temp*Math.Sin(2*Math.PI*(i+1)/MESA_length); }
I would appreciate anyone's thoughts on this.

 

0

Please, develop your question a little further.

At first sight, only I can said that is '+1' will lose it impact as the period of the indicator increases.

Now, why do you think is an improvement to estimate the indicator with the '+1'? Have you tried both setups? What are the changes you note?

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thanks for responding, and sorry for my sloppy formatting in previous comment.

 

The eqaution is essentially COS(angle)*amplitude

where angle = i/MESA_length

Should the angle of the initial observation be zero (0/Mesa_length)) or 1/MESA_length?

0

The transformation should start in zero, if not the indicator will be out of phase. But again, the phase shift will be smaller as the indicator period increases.

Don’t just take my word, try with both setups with some arbitrary periods (say 5, 10, 30, 60 and 120) and check the results. 

Good luck!

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed