ADX Crossover

 

Hello everyone,

I am a little bit stuck on how to check for a cross over, I started writing this code.

 AVD_Minus = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);
 AVD_Plus  = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
 AVD_Main  = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);
 
 A = fmax (AVD_Plus, AVD_Minus); //Minus
 B = fmin (AVD_Plus, AVD_Minus); //Plus
 
 if(A==AVD_Plus)PlusHigh=A;  // 0.0
 if(A==AVD_Minus)MinusHigh=A;// 15.58637825361965
 if(B==AVD_Plus)PlusLow=B;   // 8.865850086266505
 if(B==AVD_Minus)MinusLow=B; // 0.0

I need a bit of guidance on how to code it better than this.

 
GrumpyDuckMan:

Hello everyone,

I am a little bit stuck on how to check for a cross over, I started writing this code.

I need a bit of guidance on how to code it better than this.

As a suggestion, look at any of the freely available MT4 EA's available in the code base that do crossovers.

For example, https://www.mql5.com/en/code/12448 (first one that came up when I Googled).

The key is:

1) Get some values, e.g.

      Fast_MA_Bar_0 = iMA(NULL, 0, Fast_MA_Period, Fast_MA_Shift, Fast_MA_Method, Fast_MA_Price, i+1);
      Fast_MA_Bar_1 = iMA(NULL, 0, Fast_MA_Period, Fast_MA_Shift, Fast_MA_Method, Fast_MA_Price, i+2);

      Slow_MA_Bar_0 = iMA(NULL, 0, Slow_MA_Period, Slow_MA_Shift, Slow_MA_Method, Slow_MA_Price, i+1);
      Slow_MA_Bar_1 = iMA(NULL, 0, Slow_MA_Period, Slow_MA_Shift, Slow_MA_Method, Slow_MA_Price, i+2);

2) Ensure that the previous value is below, and the current value is above, e.g.

if((Fast_MA_Bar_1 < Slow_MA_Bar_1) && (Fast_MA_Bar_0 > Slow_MA_Bar_0))
 

This sort of works, but not really telling me if there is a cross over.

 AVD_Minus = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);
 AVD_Plus  = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
 AVD_Main  = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);
 
 A = fmax (AVD_Plus, AVD_Minus);
 B = fmin (AVD_Plus, AVD_Minus);
 
 if(A==AVD_Plus)PlusHigh=A;  if(A==AVD_Minus)MinusHigh=A;
 if(B==AVD_Plus)PlusLow=B;   if(B==AVD_Minus)MinusLow=B;
 
 Indicator_Crossed_A = PlusHigh+PlusLow; 
 Indicator_Crossed_B = MinusHigh+MinusLow; 
 
  if(Indicator_Crossed_A>Indicator_Crossed_B)   BuyOut=Indicator_Crossed_A;
  if(Indicator_Crossed_A<Indicator_Crossed_B)   SellOut=Indicator_Crossed_A;
Reason: