Once function not working!

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #48564 quote
    Mansoor
    Participant
    Average

    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.

    #48565 quote
    robertogozzi
    Moderator
    Master

    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

    #48567 quote
    robertogozzi
    Moderator
    Master

    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!

    #48579 quote
    Mansoor
    Participant
    Average

    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
    #48580 quote
    robertogozzi
    Moderator
    Master

    I won’t be at my desk till monday morning.

    I’ll try to test it afterwards.

    #48581 quote
    Mansoor
    Participant
    Average

    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!

    #48597 quote
    Nicolas
    Keymaster
    Master

    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.

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

Once function not working!


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Mansoor @mansoor Participant
Summary

This topic contains 6 replies,
has 3 voices, and was last updated by Nicolas
8 years, 4 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 10/07/2017
Status: Active
Attachments: No files
Logo Logo
Loading...