How to wait 20 new bars for checking for a signal

 

Hi,

I have an EA which is based on 3 steps.

Once the first 2  steps satisfied i need to wait for the 3rd step to come along.

Ideally i would wait 15/20 bars for the 3rd step to show up.

The problem that i have is: how do i check for the last 15-20 bars if the 3rd signal has been satisfied?

I am using a function:

bool mixuBuySTEP1_3(){
 
   bool mixbuy=false;
   bool steps1_2=false;
   
   if(mycriteriaforstep1and2 are satisified){Print("STEPS12DONE");steps1_2=true;};
   if (steps1_2==true && my3_criteria_true){Print("READYTOBUY"); mixbuy= true;}
   
      else if(steps1_2== true && my3_criteria_false) {Print("waitfor3rdcriteria");waitforIT=true;};
   if(steps1_2==true && waitforIT==true){
            
            for(int i=0; i<20;i++){
               
            if(my3_criteria_true){break; waitforIT=false;mixbuy=true;};

               if(i==19 && my3_criteria_false)waitforIT=false;mixbuy=false;
                }
      }
     
     
  return(mixbuy);} 

As you can see from above I m thinking of using a loop but i m not sure how to apply it to new last 15/20 bars coming in.


Thanks

 

You need a way to detect a new candle. Here is part of something I made earlier this week. I am new to MQL5, so there is probably better ways to do this.

I used OnTick() to check for a new candle (not a for loop), but you should be able to get something to work with Bars().


// # of candles since the last trade
int SinceLastTrade = 0;

// Set up int to detect new candles
int CurBar = 0;
int PrevBar = 0;     
   
void OnTick()
  {   
      //Get the Current Bar #
      CurBar = Bars(_Symbol,_Period);
           
      //Check to see if there is a new candle
      if(CurBar>PrevBar)
      {
         // Update the Previous Bar #
         PrevBar=CurBar;
         
         //Add 1 to Candles SinceLastTrade
         SinceLastTrade++;        
 
Ck101026:

You need a way to detect a new candle. Here is part of something I made earlier this week. I am new to MQL5, so there is probably better ways to do this.

I used OnTick() to check for a new candle (not a for loop), but you should be able to get something to work with Bars().


i get it. I have a similar formula for new Candle but i still can t figure out how to check a new candle every time it comes in, and repeat that 20 times, so 20 different candles.

Then if after 20 new candles the criteria is not satisfied it quits.

Can you help me?

 
int count=0;
datetime time;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(time!=iTime(Symbol(),PERIOD_CURRENT,0))
     {
      count++;
      time=iTime(Symbol(),PERIOD_CURRENT,0);
     }
   if(count==20)
     {
      Alert("20 new candles !");
      count=0;// reset counter
     }
  }
//+------------------------------------------------------------------+
 
Marco vd Heijden:

thanks for your answer Marco.


Would that work for an EA?

As far as I know onTick is an indicator function, can it be used in an EA?

 
billysballo:

thanks for your answer Marco.


Would that work for an EA?

As far as I know onTick is an indicator function, can it be used in an EA?

Don't rely on counting bars because that can go wrong in a lot of ways and won't recover should the EA encounter troubles. Instead you should be looking for the signal to have happened 15-20 bars back (from the current bar) and if the signal is found then run further validation. Here is an example of MA cross (UP) that happened with 15-20 bars of confirmation (where the fast MA stayed above the slow MA and didn't cross back down). 

bool buy_signal()
{
   for (int i=14; i<20; i++) { //look for cross 15 to 20 bars back
      if (ma20.Main(i + 1) < ma50.Main(i + 1) && ma20.Main(i) >= ma50.Main(i)) { //crossed up
         bool validated = true;
         for (int j=i-1; j>=0; --j) { //potential signal found count back to current bar
            if (ma20.Main(j) < ma50.Main(j)) { //crossed back down, signal invalid
               validated = false;
               break;
            }
         }
         return validated;
      }
   }
   return false;
}
Reason: