Problem with the number of positions

 

Take a look at this photo

10

We sell about 5 candles, but Expert also sells the next 5 candles.
When the sales signal is issued, this code covers 10 candles after the sales signal.
Even when the sales signal runs out
I would even get 4 if we put 10 candles in the box (MathMax(10,Orders())) if the sales signal was 4 and didn't reach 10.

//+------------------------------------------------------------------+
string Hama()
  {
double b1_shift1=iCustom(Symbol(),240,"HamaSystem",0,1);
double b1_shift2=iCustom(Symbol(),240,"HamaSystem",1,1);
double b1_shift3=iCustom(Symbol(),240,"HamaSystem",2,1);
double b1_shift4=iCustom(Symbol(),240,"HamaSystem",3,1);
double b1_shift5=iCustom(Symbol(),240,"HamaSystem",4,1);
double b1_shift6=iCustom(Symbol(),240,"HamaSystem",5,1);

double b2_shift1=iCustom(Symbol(),240,"HamaSystem",0,2);
double b2_shift2=iCustom(Symbol(),240,"HamaSystem",1,2);
double b2_shift3=iCustom(Symbol(),240,"HamaSystem",2,2);
double b2_shift4=iCustom(Symbol(),240,"HamaSystem",3,2);
double b2_shift5=iCustom(Symbol(),240,"HamaSystem",4,2);
double b2_shift6=iCustom(Symbol(),240,"HamaSystem",5,2);


{
  if (b1_shift1<b2_shift1 && b1_shift2>b2_shift2)  
   
  {
     if (TimeOfBuySignal==0)   // SJT added
        TimeOfBuySignal = iTime(Symbol(),240,1);
     TimeOfSellSignal = 0;
  }
   if (TimeOfBuySignal>0 &&
    iBarShift(Symbol(),240,TimeOfBuySignal)<=MathMax(10,Orders()))
    return("buy");


 if (b1_shift1>b2_shift1 && b1_shift2<b2_shift2)

 {
      if (TimeOfSellSignal==0)
         TimeOfSellSignal = iTime(Symbol(),240,1);
         TimeOfBuySignal = 0;
  }
     if (TimeOfSellSignal>0 &&
     iBarShift(Symbol(),240,TimeOfSellSignal)<=MathMax(10,Orders()))
     return("sell");
     }

   return(0);
  }
//+------------------------------------------------------------------+
 
Naqibjan:

Take a look at this photo

We sell about 5 candles, but Expert also sells the next 5 candles.
When the sales signal is issued, this code covers 10 candles after the sales signal.
Even when the sales signal runs out
I would even get 4 if we put 10 candles in the box (MathMax(10,Orders())) if the sales signal was 4 and didn't reach 10.

Here's my 'guess' this time round (sorry, I just have to guess, because your questions are always difficult to understand):

  if (b1_shift1<b2_shift1 && b1_shift2>b2_shift2)  
  {
     if (TimeOfBuySignal==0)   // SJT added
        TimeOfBuySignal = iTime(Symbol(),240,1);
     TimeOfSellSignal = 0;
  } move to after 'return("buy")';
   if (TimeOfBuySignal>0 &&
    iBarShift(Symbol(),240,TimeOfBuySignal)<=MathMax(10,Orders()))
    return("buy");
  }
Do the same for sell. And change 10 to 5 if you want maximum of 5 trades. 
Reason: