Thebattlefront Posted March 21, 2023 Report Share Posted March 21, 2023 (edited) Ok I'm a bit stumped and I think I need some help. I have this function that has two while loops: //------------------------------------------------------------------------------------------------------------------------ Sto Trend private bool StoTrend(int StoCount) { int y = 0; if (StoLine1 >= StoSignal1) { while (StoLine1[y] >= StoSignal1[y]) {y++;} } else if (StoLine1 <= StoSignal1) { while (StoLine1[y] <= StoSignal1[y]) {y++;} } if (y >= StoCount) {return true;} return false; } This function should count the number of times the stochastic line is above or below the signal line until it crosses over (or vice versa). If that number is greater than the input (StoCount) then it will return true, meaning that there is an established trend with the stochastics line. The problem is this function causes medved to freeze. I know this because if I replace the two conditions for the while loops ---- stoline1[y] >= stosignal1[y] ---- to something like ---- y <= 2 ----- medved does not freeze and works again. But I'm not sure why. It's probably something simple but I cannot figure it out. Edited March 21, 2023 by Thebattlefront Quote Link to comment Share on other sites More sharing options...
Jerry Medved Posted March 21, 2023 Report Share Posted March 21, 2023 your while loops are infinite or very large, thus causing the freeze. if you changed the while to look at last 20 candles for example, it would fix the issue: while (StoLine1[y] <= StoSignal1[y] && y < 20) Quote Link to comment Share on other sites More sharing options...
Thebattlefront Posted March 21, 2023 Author Report Share Posted March 21, 2023 (edited) Shouldn't the while loop stop as soon as the condition isn't true? Like, at some point, the stochastic line will cross the signal again, thus making the condition for the while loop not true anymore and allowing the computer to exit the while loop? Because usually the stochastics line crosses the signal like after every 5 points or so (I use 14,3,3 settings - i know the amount fluctuates a bit but i dont think its possible for the stochastics line to stay above or below the signal indefinitely). That's why i was so stumped by this. Oh ok no your solution does work. I accidentally replaced the while loops with "if" conditions to make the program stop freezing lol. You are right, it does work now. Thank you jerry! Edited March 21, 2023 by Thebattlefront Quote Link to comment Share on other sites More sharing options...
Jerry Medved Posted March 21, 2023 Report Share Posted March 21, 2023 what if the chart does not have any data on it or only a few points and the condition is never false because both values are zero and thus equal. The loop would continue to infinity. it is best not to assume certain things will happen and put in safeguards Quote Link to comment Share on other sites More sharing options...
Thebattlefront Posted March 21, 2023 Author Report Share Posted March 21, 2023 (edited) Yeah i will remember to put in safeguards in the future for while loops (or any kind of infinity-producing type of function). Edited March 21, 2023 by Thebattlefront Quote Link to comment Share on other sites More sharing options...
Mike Medved Posted March 21, 2023 Report Share Posted March 21, 2023 In general, looping in scans/paintbars is not an efficient method... You have to remember, the code in scan/paintbar is executed for each candle to start with, and then for each update subsequently. The paintbar is a state machine. So all the stuff that you do with loops can be done by, for example for your loops, keeping a counter that is increased each time the condition is met, zeroing it each time the position is not met. Have to do that with "State-keeping" code, with the counter inside the state. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.