Download MetaTrader 5
To add comments, please log in or register
The entire MQL5.community history on a single page!
Peter Bočan
89
Peter Bočan 2016.05.05 23:52 

Hello guys, I wonder, how would you solve this issue with MA crossing strategy when two MAs are basically laying on the same line and crossing each other alternatively? My expert advisor is making profit (in tester of course) but this is a very bad drawback that I have to solve and I'm not sure what should I do...

 

 Crossing MAs

Fernando Carreiro
1437
Fernando Carreiro 2016.05.06 00:40  

Add one or more of the following filters:

  • minimum separation between moving averages
  • minimum slope of moving average
  • minimum deviation from the last peak or valley of the moving average.
Peter Bočan
89
Peter Bočan 2016.05.06 22:10  
I think the first may work other will not, in my opinion. I'll try it.
Fernando Carreiro
1437
Fernando Carreiro 2016.05.07 02:22  
PeterBocan:
I think the first may work other will not, in my opinion. I'll try it.

Any of the 3 will filter out the condition you have described as well as as other conditions you have not yet described.

I did not just "invent" them - they are well known and used by many for this very fact.

Peter Bočan
89
Peter Bočan 2016.05.13 21:15  

So I tried to do something like this:

Which tries to detect the minimal slope, but the angleBefore and angleAfter are very small (~ 100x) than given constant g_MinAngleInRadians. Offset is MA's Offset (for example 5, for 5 bars). Not sure,  what causes the problem with those formulas. 

   double angleBefore = MathAbs(MathArctan((currentShortMA - currentLongMA) / Offset));
   double angleAfter = MathAbs(MathArctan((previousShortMA - previousLongMA) / Offset));
   
   if (angleAfter <= g_MinAngleInRadians && angleBefore <= g_MinAngleInRadians && 
       MathAbs(currentShortMA - currentLongMA) <= 15 * _Point && MathAbs(previousShortMA - previousLongMA) <= 15 * _Point)
       return -1;
Fernando Carreiro
1437
Fernando Carreiro 2016.05.14 01:25  

There is no need to use trigonometry to calculate the slope. Slope is not the same as angle. I wrote "minimum slope", not "minimum angle". For a single "bar", the slope is simply the delta difference of the individual moving average of the previous bar and the next, namely "iMA(..., i) - iMA(..., i + 1)".

As an alternative, you can also measure the Standard Deviation of each Moving average by using iStdDev(...) and check for a minimum instead of using the minimum slope.

In your code, by "LongMA" and a "ShortMA", I hope you mean "Slow" and "Fast" and in that case use a simple delta difference (namely "minimum separation") as the comparison between their values.

When considering a more robust method, over several bars, for each moving average, use the minimum deviation (delta difference) from the last peak or valley of each individual moving average.

Peter Bočan
89
Peter Bočan 2016.05.14 13:48  
Yes, you are right. I'm going for the robust solution... I wonder, how do you define peak/valley? It must be back checked to get a peak/valley, right?  
Fernando Carreiro
1437
Fernando Carreiro 2016.05.14 14:07  

First learn how to do the "minimum slope", "minimum standard deviation" and "minimum separation" as those are the easiest for you to implement.

Then, for "minimum deviation from extremes" (peak/valley), you will need to add two buffers (for Indicator) to keep track of the extreme points. When a MA value is higher than the previous MA value, you then track it in the "Peak/Upper/High" buffer and when it is lower than the previous value, you track it in the "Valley/Lower/Low" buffer. In an EA, instead of a buffers, you can simple use two variables for tracking the Peak and Valley at each successive new bar.

if( dblMACurrent > dblMAPrevious )
   dblPeak = dblMACurrent;
else
{
   if( dblMACurrent < dblMAPrevious )
      dblValley = dblMACurrent;
}
Peter Bočan
89
Peter Bočan 2016.05.20 14:13  
okay, thanks, I'll look at that.
/
To add comments, please log in or register