Start and stop of action

 

Hi,

I've been learning to code in MQL for 2 weeks now and I made huge progress, however I cannot solve something that is easily done in different script languages.

How can I start certain action when one criteria is fulfilled and then stop it when different criteria becomes true.

The thing is that the criteria which triggers the action is no longer true after it starts the action.

For example:

if stochastic >= 80 start counting until stochastic <=20

Obviosly while, if and for won't work since the counter should work even after stoch<80 but >20.

I am a bit confused as this should be an easy thing to do. For example in EasyLanguage I would just use "then begin" command to solve such issues.

In MQL as far as I know ther is no equivalent of this command. I've thought about arrays, functions but that road seems to be too cumbersome for something that should be simple.

 

How about this.


CB


int iStochastic;
int iCount;

int init()
 {
  iCount = 0;
  return(0);
 }

int start()
 {
  if (iStochastic >= 80)
   iCount = fnCounter();
  return(0);
 }

int fnCounter()
 {
  if (iStochastic > 20)
   iCount ++;
  return(iCount);
 }
 

Or this.


CB


bool bShouldWeCount;

int iStochastic; int iCount; int init() { bShouldWeCount = false;

iCount = 0; return(0); } int start() { if (iStochastic >= 80) bShouldWeCount = true;

if (iStochastic <=20)

{

iCount = 0;

bShouldWeCount = false;

}

if (bShouldWeCount)

iCount++; return(0); }


 

Yes, that's it.

Thanks a lot.

My mistake was that I was assuming that the boolean would return to false once the value returned below 80.

 

Here's another way that eliminates a lot of lines of code while accomplishing the same task:

int count = 0;

if(iStochastics >= 80 || count > 0)
   count++;
if(iStochastics <= 20)
   count = 0;

Basically, having stoch >= 80 starts the counter and the fact that the counter is started will keep itself going until reset to 0 by the fact that it fell <= 20.

Jon

Reason: