I have a really simple code here which doesn’t work as you expect. Can anyone explain why?
If High > High[1] Then
Once x = 1
Endif
Return x
- If you delete word “Once” then it works as you would expect!
- If you change the If condition to something like “If 5 = 5” then it works as you would expect!
- If you take “Once x = 1” out of “If condition” for example you put “Once x =1” on the first line then it works as you would expect!
It’s weird! This is part of a complex code which I am writing and trying to debug.
ONCE is used to initialize a variable and cannot be used within IF…THEN blocks, it’s not what it is made for!
I suggest writing your code like that (sorry for not being able to insert PRT code from my smartphone):
ONCE x = 0
IF high > high[1] THEN
x = 1
END IF
RETURN x
Roberto
In my/your example ONCE should not be used at all, just leave x=0, because ONCE is executed only the first time, when x contains 1 it will remain 1 thereafter, thus returning wrong results!
I was using the above code to find the bug in my main code. Below is the simplified version of the code. I noticed when Once comes after If, it sometimes works as expected but sometimes not. In this code, the result for x is as expected but result for y is zero which is weird!
tstop = 10
breakout = 5
If High > (High[1] + breakout) Then
Once buyprice = High
Endif
If High > buyprice Then
buyprice = High
x = buyprice - tstop
Endif
If Low < (Low[1] - breakout) Then
Once sellprice = Low
Endif
If Low < sellprice Then
sellprice = Low
y = sellprice + tstop
Endif
Return x, y
I won’t be at my desk till monday morning.
I’ll try to test it afterwards.
I think I actually may have found the answer:
ProRealtime reads the Once function without respecting the If condition. If the If condition is met prior to ProRealtime reading the Once function then Once function would be applied otherwise Once function would never be executed. Pretty interesting huh!
ONCE is only made to give a variable a value one time, it cannot be used many times in a conditional block like you did. If you want that your line 4 conditions give only one time a value to a variable and then compare this value with another one, you should use 2 different variables names and compare them together instead, and get rid of the ONCE instruction.