Once function not working!
Forums › ProRealTime English forum › ProBuilder support › Once function not working!
- This topic has 6 replies, 3 voices, and was last updated 7 years ago by Nicolas.
-
-
10/07/2017 at 4:20 PM #48564
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.
10/07/2017 at 4:37 PM #48565ONCE 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 xRoberto
10/07/2017 at 4:47 PM #48567In 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!
10/07/2017 at 9:34 PM #48579I 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!
Once Function12345678910111213141516171819tstop = 10breakout = 5If High > (High[1] + breakout) ThenOnce buyprice = HighEndifIf High > buyprice Thenbuyprice = Highx = buyprice - tstopEndifIf Low < (Low[1] - breakout) ThenOnce sellprice = LowEndifIf Low < sellprice Thensellprice = Lowy = sellprice + tstopEndifReturn x, y10/07/2017 at 11:05 PM #48580I won’t be at my desk till monday morning.
I’ll try to test it afterwards.
10/07/2017 at 11:23 PM #48581I 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!
10/08/2017 at 8:43 AM #48597ONCE 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.
-
AuthorPosts
Find exclusive trading pro-tools on