Download MetaTrader 5

How to implement this Logic?

To add comments, please log in or register
Searching on MQL5.community is performed in all sections. Try it!
Arav007
412
Arav007 2014.03.25 18:07 

I have been trying a Logic to work.

After starting, at the first Tick, the EA will execute 'One Set of Logic'. Then from the 'Second' Tick, it'll keep executing another 'Set of Logic'.

To do so I tried something like this:

int main()
{
int Choose_Logic;
Choose_Logic=TRUE;

if(Choose_Logic==TRUE)
{
Execute the First Set of Logic.
}

if(Choose_Logic==FALSE)
{
Execute the Second Set of Logic.
}

Choose_Logic=FALSE;
return;
}

But on the Second Tick, 'Choose_Logic' remains 'TRUE' as it's getting it defined like that at the starting.

Tried some other things but didn't work.

A 'for' loop or 'Case-Break' can be used?

Please suggest me how can I implement this?

Thanks

Ian Venner
2584
Ian Venner 2014.03.25 18:21  
static bool logic = 0;

if(logic == 0)
{
//do logic 1
logic = 1;
}
if(logic == 1)
{
// do logic 2
}
Use static variables if you want their values to last more than one tick.
Arav007
412
Arav007 2014.03.25 19:12  
SDC:
Use static variables if you want their values to last more than one tick.


Hello,

Thanks for your suggestion.

I tested it but probably this too is not the solution of the problem.

Or I might applied it wrong?

Files:
prob.mq4 12 kb
whroeder1
14486
whroeder1 2014.03.25 19:42  
  1. // if(logic=FALSE) Don't assign false to logic and then test for it to be true.
    if(logic == FALSE) test if it is false.
  2. Why test it for if(logic == false), you know it is false because you set it just above in the if(logic == true{}.
  3. Remember statics (and globally declared) variables are not reset on chart changes
  4. You would never write if( (2+2) == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Especially use readable names for self documenting code
    static bool isFirstTick = true;
    if(isFirstTick){ isFirstTick = false;
       : // first tick only
    }
    else{
       : // not first tick
    }
    // every tick

Arav007
412
Arav007 2014.03.25 20:33  
WHRoeder:
  1. Why test it for if(logic == false), you know it is false because you set it just above in the if(logic == true{}.
  2. Remember statics (and globally declared) variables are not reset on chart changes
  3. You would never write if( (2+2) == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Especially use readable names for self documenting code


Thanks Sir.

Understood why I shouldn't use (Bool==True).

It has worked now.

Now I'd check it for the Full program.

I just Hope it wouldn't be a Mess!

Regards

Ian Venner
2584
Ian Venner 2014.03.26 01:05  

In my example, you would need to return after the first logic to prevent it doing both on the first tick:

static bool logic2 = 0;

if(!logic2)
{
//do logic 1
logic2 = true;
return;
}
if(logic2)
{
// do logic 2
}
To add comments, please log in or register