How to write a ea command to meet one condition and then the next other before excute

To add comments, please log in or register
Tuck99
18
Tuck99  

Hi,


I'm having trouble writing a command for my EA that has to meet Condtion 1 and then after has to meet Condition 2 before execute, but condition one must be met first. Otherwise keeps looping.

Example: The code I was writing:


bool Condition 1

bool Condition 2

--------------------------------------------------------------------------------------------------------- Condition 1 and 2 must be met before excuting Condition 3 and 4 and then once 3 and 4 are met open trade

bool Condition 3

bool Condition 4

if (!ExistPositions()){


      if (Condition 1=Condition 2){

      if (Condition 3=Condition 4){

            OpenBuy();

             return(0);

}

 }

  }

This wasn't working or showing anything on my back testing.

Thanks

whroeder1
17949
whroeder1  
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. When multiple conditions will not occur at the same time (otherwise you would simply test both in one if,) use a state machine:
    enum State {idle, C11, C12, C2, C23, C24};
    static State state=idle;
    if(condition1 && state < C2){
      if(state == idle) state = C11; // Saw C1
      if(state == C12)  state = C2;  // Saw both;
    }
    if(condition2 && state < C2){
      if(state == idle) state = C11; // Saw C2
      if(state == C12)  state = C2;  // Saw both;
    }
    if(condition3  && state >= C2){
      if(state == C2)   state = C23; // Saw C3
      if(state == C24){ state = idle; doit(); } // Saw both C1 and C2, and then both C3 and C4;
    }
    if(condition4  && state >= C2){
      if(state == C2)   state = C24; // Saw C3
      if(state == C23){ state = idle; doit(); }
    }  


To add comments, please log in or register