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
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 ?
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
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?
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
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
YES, thats the solution. Thanks Vonasi and Nicolas!
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.
No, I didn’t saw it. Thanks for the info….it looks great.