HELP!! How to make EA stop and Starts

 

I'm trying to make an EA to

  • only trigger when there's a Sell signal and will trade for the amount of candle sticks required.
  • stops trading after the number of candle stick has been reached
  • Wait till there's a buy signal
  • Triggers once there's a new sell signal

I was able to get the EA trigger on sell signal and stops once the number of candle sticks are met, but I can't get it to trigger again after the new sell signal. 


#include <Trade\Trade.mqh>

CTrade trade;

input double duration                        =10; //Number of Candle sticks 
input double Lot_Size                       = 0.2; // Lot size
input int Stop_Loss                         = 20;  //Stop Loss
input int Take_Profit                       = 80; //Take Profit

int sellCheck = 0;
int check = 0;
int strp =0;
int candleCounter =0;

int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      //double Ask= NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      
      double Bid= NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

      
      string signal ="";
      
      MqlRates PriceArray[];
      
      ArraySetAsSeries(PriceArray,true);
      
      int Data = CopyRates(Symbol(),Period(),0,3,PriceArray); 
      
      double mySARArray[];
      
      int SARDefinition =iSAR(Symbol(),Period(),0.02,0.2);
      
      ArraySetAsSeries(mySARArray,true);
      
      CopyBuffer(SARDefinition,0,0,3,mySARArray);
      
      double LastSARValue = NormalizeDouble(mySARArray[1],5);
      
      
      if(LastSARValue <PriceArray[1].low)
      {
         signal="buy";
         
         sellCheck= 0;
         
      }
      

      // check = check + 1; //Increment the check control
      
      if(LastSARValue > PriceArray[1].high)
      {
      signal ="sell";
      
      }
      
   
       if(signal=="sell" && PositionsTotal() <5 )
         
         if(sellCheck==0)
           {
            trade.Sell(Lot_Size,NULL,Bid,(Bid+(Stop_Loss*1000 ) *_Point),(Bid-(Take_Profit*1000 ) *_Point),NULL);
            CalculateCandle();
           }
         
    
         sellCheck = sellCheck + 1;
         
       
       
     
     
     
      
      
      
      Comment("The Signal is :", signal);  
       
   
  }
//+------------------------------------------------------------------+

 void CalculateCandle()
 {
   MqlRates priceData[]; //Create Price Array
      
      //Sort the array from the current candle downwards
      ArraySetAsSeries(priceData,true);
      
      //copy candle price for 3 candles into array
      CopyRates(_Symbol, _Period,0,3,priceData);
      
      //Create a counter for the candle
      static int candleCounter;
      
      //Create Datetime Variable for the last time stamp
      static datetime timeStampLastCheck;
      
      //Variable for current candle
      datetime timeStampCurrentCandle;
      
       //Read the time stamp of current candle in array 
       timeStampCurrentCandle = priceData[0].time;
       
       //if the current time stamp is different from the last time 
       if(timeStampCurrentCandle!= timeStampLastCheck)
         {
            //Remember the Candle time stamp for later
            timeStampLastCheck = timeStampCurrentCandle;
            
            //Increment the candleCounter
            candleCounter = candleCounter +1;
            
          
         }
      
      
   
      if(candleCounter == duration )
        {
          ClosePosition();
          candleCounter = 1;
          
        }
     
 }
 
 
 void ClosePosition()
{
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
     
     int ticket= PositionGetTicket(i);
     
     //Get Position Direction
     int PositionDirection=PositionGetInteger(POSITION_TYPE);
     
          
     if(PositionDirection==POSITION_TYPE_SELL)
     trade.PositionClose(ticket);
      
     }

}
Reason: