Jump to content
Medved Trader Forums

While loop question


Thebattlefront

Recommended Posts

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 by Thebattlefront
Link to comment
Share on other sites

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 by Thebattlefront
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...