test freezes before while loop

hello, i am doing a project, and it keep freezing, and finally i figured out where it freezes. and i wrote a simple code to help me solve the problem.  the logic i am using is, if the signal line cross 80 from top to bottom, and is true, wait till the signal line cross down 50 level and notify me.

can anyone help me where the problem is? and why it freeze during a back

```void OnTick()
{

double stochPrev = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA,0, MODE_SIGNAL, 1);

double   stoch1 = iStochastic(NULL,0,28,6,6,MODE_SMA,0,MODE_SIGNAL,0);

if(stochPrev > 80 && stoch1 < 80 && stochPrev > stoch1)
{
while(stoch1 >= 45)
{
stoch1 = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA, 0, MODE_SIGNAL, 0);

if(stoch1 < 50)
{
Alert("Stochastic crossed down 50 level from top");

}
}
}
}```

testing, right before it gets in while loop?

Ricca97:

hello, i am doing a project, and it keep freezing, and finally i figured out where it freezes. and i wrote a simple code to help me solve the problem.  the logic i am using is, if the signal line cross 80 from top to bottom, and is true, wait till the signal line cross down 50 level and notify me.

can anyone help me where the problem is? and why it freeze during a back

testing, right before it gets in while loop?

You need to implement a state machine logic...

Just do:

```   if(stochPrev > 80 && stoch1 < 80 && stochPrev > stoch1)
{
if(stoch1 >= 45)
stoch1 = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA, 0, MODE_SIGNAL, 0);
if(stoch1 < 50)
Alert("Stochastic crossed down 50 level from top");
}
```

Will this do it?

1. Ricca97:
```   double stochPrev = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA,0, MODE_SIGNAL, 1);
double   stoch1 = iStochastic(NULL,0,28,6,6,MODE_SMA,0,MODE_SIGNAL,0);```

Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

Ricca97:

hello, i am doing a project, and it keep freezing, and finally i figured out where it freezes. and i wrote a simple code to help me solve the problem.  the logic i am using is, if the signal line cross 80 from top to bottom, and is true, wait till the signal line cross down 50 level and notify me.

can anyone help me where the problem is? and why it freeze during a back

testing, right before it gets in while loop?

Don’t use while loops they are rarely a good idea they will just have the effect you are experiencing
Use logic and flags instead and check on new tick

Carl Schreiber #:

Just do:

Will this do it?

well, it do not freeze anymore, but it do not pass to the next if statement either. i get no result.

Yashar Seyyedin #:

You need to implement a state machine logic...

it still freezes. look how i coded in a state machine logic.

```void OnTick()
{
// Get the current stochastic values
stoch1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
stochPrev = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);

// Update the state machine based on the current state and stochastic values
switch(currentState)
{
case WAIT_FOR_STOCH_PREV:
if(stochPrev > 80)
{
currentState = WAIT_FOR_STOCH_1;
}
break;
case WAIT_FOR_STOCH_1:
if(stoch1 < 80)
{
currentState = WAIT_FOR_STOCH_CROSS_DOWN;
}
break;
case WAIT_FOR_STOCH_CROSS_DOWN:
while(stoch1 >= 50)
{
stoch1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
}
Alert("Stochastic crossed down 50 level from top");
currentState = WAIT_FOR_STOCH_1;
break;
}
}```

William Roeder #:
1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

thank you so much. i used rewrote this way and now it works fine
```bool waitingForStochCrossDown = false;
bool stochasticCrossedDown = false;

void OnTick()
{
double stochPrev = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA, 0, MODE_SIGNAL, 1);
double stoch1 = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA, 0, MODE_SIGNAL, 0);

if (waitingForStochCrossDown && stoch1 < 50 && !stochasticCrossedDown)
{
Alert("Stochastic crossed down 50 level from top");
stochasticCrossedDown = true;
waitingForStochCrossDown = false;
}

if (!waitingForStochCrossDown && stochPrev > 80 && stoch1 < 80 && stochPrev > stoch1)
{
waitingForStochCrossDown = true;
stochasticCrossedDown = false;
}
}```

Paul Anscombe #:
Don’t use while loops they are rarely a good idea they will just have the effect you are experiencing
Use logic and flags instead and check on new tick
thank you paul. now it works perfect. i dropped the while loop and coded it with bool logic. but it was easier in loop than bool though.
```bool waitingForStochCrossDown = false;
bool stochasticCrossedDown = false;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
{
double stochPrev = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA, 0, MODE_SIGNAL, 1);
double stoch1 = iStochastic(NULL, 0, 28, 6, 6, MODE_SMA, 0, MODE_SIGNAL, 0);

if(waitingForStochCrossDown && stoch1 < 50 && !stochasticCrossedDown)
{
Alert("Stochastic crossed down 50 level from top");
stochasticCrossedDown = true;
waitingForStochCrossDown = false;
}

if(!waitingForStochCrossDown && stochPrev > 80 && stoch1 < 80 && stochPrev > stoch1)
{
waitingForStochCrossDown = true;
stochasticCrossedDown = false;
}
}
```