Infinite Loop – While in If function

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #95901 quote
    vetradervetrader
    Participant
    Average

    I have a problem to find a solution for an infinite loop problem. The code below works fine, but with a while command in the if function, I receive the infinite loop message. The code should do the following:

    • The if function is the trigger to set pb = 1 and calculate krange/kprice/kbuy
    • Now pb should remain at 1 and krange/kprice/kbuy should be calculated on every new bar UNTIL close < lowerband
    • I supposed after the if line: while close < lowerband do

    Any idea?

    if dir = 1 and count <= maxbars and count >= minbars and close < upperband then
    pb = 1
    krange = upperband-lowerband
    kprice = lowerband-close
    kbuy = abs(kprice/krange)*100
    else
    pb= 0
    endif
    #96025 quote
    NicolasNicolas
    Keymaster
    Legend

    I don’t know the entire code, so it is difficult to make a precise answer, but I think that there should be another way to make the calculation avoiding the while/wend loop. I do not see any count variable increasing?

    Why not just using: if close<lowerband then .. endif ?

    #96052 quote
    vetradervetrader
    Participant
    Average

    Count simply counts bars outside of a band in relation to a defined bar range – this works fine. The if function is now the trigger for the first close below the upperband – works also fine – but only for one Bar. But now, if this trigger is 1 it should remain at 1 until close < lowerband or close > upperband…In other words: If the trigger is at 1, I want to screen until close is between upperband/lowerband and if close is outside of the bands trigger should 0. Do you know what I mean?

     

    if dir = 1 then
    pbuy = close[1] > upperband[1]
    count = 0
    while pullbuy[count] do
    count = count + 1
    wend
    endif
    
    if dir = 1 and count <= maxbars and count >= minbars and close < upperband then
    pb = 1
    krange = upperband-lowerband
    kprice = lowerband-close
    kbuy = abs(kprice/krange)*100
    else
    pb= 0
    endif
    #96070 quote
    NicolasNicolas
    Keymaster
    Legend

    Not really sure of what you want to achieved, it is still part of a complete code and some variables are missing for a better understanding, but:

    while pullbuy[count] do
    count = count + 1
    wend

    it has no start and no end, that’s why you fell into an infinite loop.
    BTW, is pullbuy the same variable as pbuy?

    #96088 quote
    vetradervetrader
    Participant
    Average

    Sorry…it’s only “pullbuy” – pullbuy/pbuy was a copy/paste error. BUT I don’t need a solution for the first if function – This works fine, like described above. I need a solution for the second if function – In this function pb is triggerd to 1 and should remain at 1 UNTIL close is within upperband/lowerband. I don’t have more code – consider upperband/lowerband just as Bollinger Bands.

     

    minbars = 2
    maxbars = 10
    
    if dir = 1 then
    pullbuy = close[1] > upperband[1]
    count = 0
    while pullbuy[count] do
    count = count + 1
    wend
    endif
    
    if dir = 1 and count <= maxbars and count >= minbars and close < upperband then
    pb = 1
    krange = upperband-lowerband
    kprice = lowerband-close
    kbuy = abs(kprice/krange)*100
    else
    pb= 0
    endif
    #96090 quote
    VonasiVonasi
    Moderator
    Master

    Something like this perhaps. Not tested.

    minbars = 2
    maxbars = 10
     
    if dir = 1 then
    pullbuy = close[1] > upperband[1]
    count = 0
    while pullbuy[count] do
    count = count + 1
    wend
    endif
    
    if count <= maxbars and count >= minbars and close < upperband then
    countok = 1
    endif
     
    if dir = 1 and countok and close < upperband then
    pb = 1
    krange = upperband-lowerband
    kprice = lowerband-close
    kbuy = abs(kprice/krange)*100
    else
    pb= 0
    countok = 0
    endif
    vetrader thanked this post
    #96102 quote
    vetradervetrader
    Participant
    Average

    YES, thats the solution. Thanks Vonasi and Nicolas!

    #96104 quote
    VonasiVonasi
    Moderator
    Master

    No problem – glad we finally worked out what it was that you wanted! Don’t forget that you can click ‘Thanks’ on any helpful replies.

    On a separate note – did you spot that the indicator you requested elsewhere is now in the library:

    Yearly Quarterly Monthly Weekly Daily H4 H1 OHLC Lines

    I only ask because you have not replied to the topic.

    vetrader thanked this post
    #96137 quote
    vetradervetrader
    Participant
    Average

    No, I didn’t saw it. Thanks for the info….it looks great.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Infinite Loop – While in If function


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
vetrader @vetrader Participant
Summary

This topic contains 8 replies,
has 3 voices, and was last updated by vetradervetrader
7 years, 5 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 04/10/2019
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...