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

 

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

 
  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(); }
    }  


 

Hi..

cant get it working..

i tried many codes

1.

if(slpclr[0]==0)slpu=true; if(slpclr[0]==1)slpd=true;
 hmb=Cross(0, Open[0] > hma[0]);
 hms=Cross(1, Open[0] < hma[0]);
 arlwbd = Cross(2, Open[0] > tmlw[0]);
 arupbd = Cross(3, Open[0] < tmup[0]);
if(hmb && arlwbd && slpu)B=true;
if(hms && arupbd && slpd)S=true; 
if(B==true)
  {
//buy
}

if(s==true)
{

//sell
}

2.



if(Cross(2, Open[0] > tmlw[0]))
if(Cross(0, Open[0] > hma[0]))
if(slpclr[0]==0)
  {
BUY
}

3.

bool slpu=false,slpd=false,arupbd=false,arlwbd=false;
if(slpclr[0]==0)slpu=true;
if(slpclr[0]==1)slpd=true;
if(Cross(0, Open[0] > hma[0]) && Cross(2, Open[0] > tmlw[0] && slpu==true))B=true;
if(Cross(1, Open[0] < hma[0]) && Cross(3, Open[0] < tmup[0] && slpd==true))S=true; 
if(B==true)
  {
//buy
}

if(s==true)
{

//sell
}

4.

if(Cross(0, Open[0] > hma[0]) && Cross(2, Open[0] > tmlw[0] && slpu==true))
{
//buy
}
if(Cross(1, Open[0] < hma[0]) && Cross(3, Open[0] < tmup[0] && slpd==true))S=true; 
{
//sell
}

still cant get it working!!

it keeps placing order at a wrong place..!!

what am trying to write.. is

if condition 1 and condition 2 and condition 3  place order

each condition will wait for others then execute

 
  1. "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
  2. Abubakar Saidu: what am trying to write.. is

    if condition 1 and condition 2 and condition 3  place order

    each condition will wait for others then execute

    Those are mutually exclusive statements. Either you want all conditions at the same time, or you don't (and I already answered that in #1.2.)
 
//global variable

bool condition_1= false;

bool condition_2= false;

void MainFunction()

{

If(yourrules1())

{

contition_1=true;

}

If(yourrules2() && condition_1)

{

contition_2=true;

contition_1=false;//turn off condition 1

}

If( condition_2)

{

// do what you want to do

contition_2=false; // then switch it off

}

}
Reason: