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?
- 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. - Drop your while loop and return to wait for a new tick.
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?
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; } }
-
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. - Drop your while loop and return to wait for a new tick.
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; } }
Don’t use while loops they are rarely a good idea they will just have the effect you are experiencing
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; } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?