Break of moving avarages

 
Hello guys in the following code "
                 for(int i=0; i<limit; i++) 
                 {
                     //---- ma_shift set to 0 because SetIndexShift called abowe 
                     MediaVeloceBuffer[i]=iMA(NULL,0,MediaVeloceTf5Period,0,MODE_EMA,0,i); 
                     MediaLentaBuffer[i]=iMA(NULL,0,MediaLentaTf5Period,0,MODE_EMA,0,i);
                     //MediaStopBuffer[i] =iMA(NULL,0,MediaStopTf5Period,0,MODE_SMA,0,i); 
               
                     ImaVeloce=MediaVeloceBuffer[i]; 
                     ImaLenta=MediaLentaBuffer[i];
                     ImaStop=MediaStopBuffer[i];
                                                                            
                     MediaVeloceTfSup=iMA(NULL,PERIOD_M30,MediaVeloceTf30Period,0,MODE_EMA,0,i);
                     MediaLentaTfSup=iMA(NULL,PERIOD_M30,MediaLentaTf30Period,0,MODE_EMA,0,i);
                  }
                  MediaVeloceCandelaPrec=iMA(NULL,0,MediaVeloceTf5Period,0,MODE_EMA,0,1); 
                  MediaLentaCandelaPrec=iMA(NULL,0,MediaLentaTf5Period,0,MODE_EMA,0,1);
                  
                  
                  if(MediaVeloceTfSup < MediaLentaTfSup){
                     ObjectSetText("MyText","Trend M30: Short",8,"Tahoma",Yellow);
                     ObjectSetText("MyText1","\nMedia Veloce M30: " + DoubleToString(MediaVeloceTfSup,4),8,"Tahoma",clrYellowGreen);
                     ObjectSetText("MyText2","\nMedia Lenta M30: " + DoubleToString(MediaLentaTfSup,4),8,"Tahoma",clrRed);
                  //   create_label("aa",230,10,"\nMedia Lenta M30: " + DoubleToString(MediaLentaTfSup,4)+"\n Media Veloce M30: " + DoubleToString(MediaVeloceTfSup,4)                    , "tex",clrFuchsia,9);
                     }
                  else{
                     ObjectSetText("MyText","Trend M30: Long",8,"Tahoma",Yellow);
                     ObjectSetText("MyText1","\nMedia Veloce M30: " + DoubleToString(MediaVeloceTfSup,4),8,"Tahoma",clrYellowGreen);
                     ObjectSetText("MyText2","\nMedia Lenta M30: " + DoubleToString(MediaLentaTfSup,4),8,"Tahoma",clrRed);
                     }
                     
                 if(NuovaCandela())
               {
                  if((MediaLentaCandelaPrec < ImaVeloce) && (ImaLenta >ImaVeloce))
                     {
                        up++;
                     }
                  if((MediaLentaCandelaPrec > ImaVeloce) && (ImaLenta < ImaVeloce))
                     {
                        down ++;
                     }
                }
                     Comment("Down: "+ down +"\n Up: " + up);
" puts 2 moving averages, with the third if it should increase the "Up" variable when the "mediaveloce" at the candle "x-1" is < then “medialenta” of the candle “x-1” and the “mediaveloce” becomes > “medialenta” at the “x” candle (in practice only when one average cuts the other. Image in attachment), but it does not always work how it should, can someone help me to fix it. Thanks