Average touch

 
int BuySell = 0; 
int ToucMA(int TimeFrame, double MaPeriod)
{
   // Per convenzione si usa
   // +1 = BUY
   //  0 = Tutto bene, ma non fa niente
   // -1 = SELL

   double Ima = iMA(NULL, TimeFrame, MaPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
   
   
       // MedIA ombra inf             Media che taglia la candela per buy  Candela verde
   if ( BuySell !=1 && Low[2] < Ima && Open[2]> Ima && Open[1] > Ima &&  Close[1] > Ima && Open[0] <  Close[0])//buy
   {
      BuySell = 1;
      return +1;
   }
    // MedIA ombra sup  Media che taglia la candela per sel  Candela rossa
  if (BuySell != - 1  && High[2] > Ima && Open[2]< Ima && Open[1] < Ima &&  Close[1] > Ima && Open[0] > Close[0])//SELL
   {
      BuySell  =-1;
      return -1;
   }
  else
   BuySell=0;
 
   return 0;
}   

Average touch 

In this function because it executes me only the Buy condition where the error is


Thanks in advance

 
int BuySell = 0; 
int ToucMA(int TimeFrame, double MaPeriod)
{
   // Per convenzione si usa
   // +1 = BUY
   //  0 = Tutto bene, ma non fa niente
   // -1 = SELL

   double Ima = iMA(NULL, TimeFrame, MaPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
   
   
       // MedIA ombra inf             Media che taglia la candela per buy  Candela verde
   if ( BuySell !=1 && Low[2] < Ima && Open[2]> Ima && Open[1] > Ima &&  Close[1] > Ima && Open[0] <  Close[0])//buy
   {
      BuySell = 1;
      return +1;
   }else //else must be here
    // MedIA ombra sup  Media che taglia la candela per sel  Candela rossa
  if (BuySell != - 1  && High[2] > Ima && Open[2]< Ima && Open[1] < Ima &&  Close[1] > Ima && Open[0] > Close[0])//SELL
   {
      BuySell  =-1;
      return -1;
   }
  else
   BuySell=0;
 
   return 0;
}