Cross Moving Avarege: Problem!

 

Hello everyone, I have a little problem with the moving average cross. The arrow must come out only when the Cross took place, however the code reads even the equal of moving averages.


//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Sell"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Buy"

//--- indicator buffers
double Buffer1[];
double Buffer2[];


extern int PeriodoLento       = 50;
extern int PeriodoVeloce      = 5;

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Base2 @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 242);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 241);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue;     
        
      //Indicator Buffer 1
      if(iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 2+i) <  iMA(NULL, PERIOD_CURRENT, PeriodoVeloce, 0, MODE_SMA, PRICE_CLOSE, 2+i)
      && iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 1+i) >  iMA(NULL, PERIOD_CURRENT, PeriodoVeloce, 0, MODE_SMA, PRICE_CLOSE, 1+i) 
      )
        {
         Buffer1[1+i] = High[1+i] + 1 * myPoint; 
        }
      
      //Indicator Buffer 2
      if(iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 2+i) >  iMA(NULL, PERIOD_CURRENT, PeriodoVeloce, 0, MODE_SMA, PRICE_CLOSE, 2+i)
      && iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 1+i) <  iMA(NULL, PERIOD_CURRENT, PeriodoVeloce, 0, MODE_SMA, PRICE_CLOSE, 1+i)
      )
        {
         Buffer2[1+i] = Low[1+i] - 1 * myPoint; 
        }
      
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
fly7680:

Hello everyone, I have a little problem with the moving average cross. The arrow must come out only when the Cross took place, however the code reads even the equal of moving averages.


It is probably extremely rare that MAs will be exactly equal. You may read both as 1.00001, but one could have an actual value of something like 1.000009
 
ok thanks, but in the code it is not correct the signal level because I have used the ">" operator.

Right?
 

I have a draw of the moving average 50 and 5 that according to the code should not be possible to display the arrow ... I think there is a mistake not?

MediaMobile 50= 1.12409

MediaMobile 5 = 1.12409


 
fly7680:

I have a draw of the moving average 50 and 5 that according to the code should not be possible to display the arrow ... I think there is a mistake not?

MediaMobile 50= 1.12409

MediaMobile 5 = 1.12409




I have already offered an explanation. Print the values to see the exact values to more than 5 Digits.
 

I added this code but I can not print the price with tenths more, I might have a little help, please?

 myPoint = Point();
   if(Digits() == 6 || Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }
 

Uncompiled / untested:

      double Lento2 =iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 2+i),
             Lento1 =iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 1+i),
             Veloce2=iMA(NULL, PERIOD_CURRENT, PeriodoVeloce, 0, MODE_SMA, PRICE_CLOSE, 2+i),
             Veloce1=iMA(NULL, PERIOD_CURRENT, PeriodoVeloce, 0, MODE_SMA, PRICE_CLOSE, 1+i);

      //Indicator Buffer 1
      if(Lento2 < Veloce2 && Lento1 > Veloce1)
        {
         Buffer1[1+i] = High[1+i] + 1 * myPoint; 
         printf("Lento2: %.8f Lento1: %.8f Veloce2: %.8f Veloce1: %.8f",Lento2,Lento1,Veloce2,Veloce1);
        }
      
      //Indicator Buffer 2
      if(Lento2 > Veloce2 && Lento1 < Veloce1)
        {
         Buffer2[1+i] = Low[1+i] - 1 * myPoint; 
         printf("Lento2: %.8f Lento1: %.8f Veloce2: %.8f Veloce1: %.8f",Lento2,Lento1,Veloce2,Veloce1);
        }
 
I'll make a test with this code.
thank you
 
Thanks again to everyone for the help, but so does not work. The values of the moving average 50 and 5, if they are equal in the spark plug 1, are still read as true.

Other ideas?

 
Check your Experts tab in the terminal - the code I gave you will be printing the actual values every time a cross happens to 8 decimal places
 

I also added another condition that should the code to work for as I understand it, but unfortunately all the Sell conditions disappear.

double Lento2 =iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 2+i),
             Lento1 =iMA(NULL, PERIOD_CURRENT, PeriodoLento, 0, MODE_SMA, PRICE_CLOSE, 1+i),
             Veloce2=iMA(NULL, PERIOD_CURRENT, PeriodoVeloce,0, MODE_SMA, PRICE_CLOSE, 2+i),
             Veloce1=iMA(NULL, PERIOD_CURRENT, PeriodoVeloce,0, MODE_SMA, PRICE_CLOSE, 1+i);
        
        
      //Indicator Buffer 1
      if(Lento2 < Veloce2 && Lento1 > Veloce1
      && MathAbs (Lento1 - Veloce1) > 0.1  // condition to eliminate the equal value
      )
        {
         Buffer1[1+i] = High[1+i] + DistanzaIcone * myPoint; 
         printf("Lento2: %.8f Lento1: %.8f Veloce2: %.8f Veloce1: %.8f",Lento2,Lento1,Veloce2,Veloce1);
        }
Reason: