X happened withing y periods

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #18449 quote
    Spekulanten
    Participant
    Average

    Hi,

    How do i code when i want something to have happened within a period? To make a restriction.

    For example,

    if x =1  and y = 1 and Q = 0 within the previous 500 bars then

    a = 1
    else
    a = 0
    endif

    How do i code “within the previous 500 bars”

    Thanks alot guys!

    #18450 quote
    JC_Bywan
    Moderator
    Master

    If you really need all 3 conditions on x y and Q to be true at the same time during each of the previous 500 bars, then you can make it a condition which is worth 1 when true, and make a summation of that during the past 500 bars, anything less than 500 means it wasn’t true during at least one bar so:

    if summation[500](x=1 and y=1 and Q=0)=500 then
      a=1
    else
      a=0
    endif

    but if you meant you wanted this to have happened just once at least within the last 500 bars, then same trick using summation but >0 rather than =500:

    if summation[500](x=1 and y=1 and Q=0)>0 then ...
    Nicolas thanked this post
    #18454 quote
    Spekulanten
    Participant
    Average

    Thanks for answer!

    What i want is to have all three to have happened atleast one time within the 500 previous bars. There would be a problem with this code, if x is true three times and not the others, i would still get the sum = 3. Maybe a Once count in this on each of the x,y,q? So that i can use summation to get the sum = 3 in the last 500 bars. Any idea to do this?

    It’s a good idea with summation, i thought it should also exist some simpler version versus this following approach; “if x or x[1] or x[2] or x[3]…..” A simplified code, can’t do this 500 times ^_^’

    PS: Tried your code above but didn’t manage to get it to work correctly.

    #18457 quote
    JC_Bywan
    Moderator
    Master

    “(x=1 and y=1 and Q>0)” all together works as one condition, so all three of them true is a condition true (=1) or untrue (=0), but not =3

    So if you want this condition to be true at least once within past 500 bars (and allow for it to be true more than once if the case may be), you are in the second case I suggested: using >0 instead of =500

    if summation[500](x=1 and y=1 and Q=0)>0 then...
    #18460 quote
    Spekulanten
    Participant
    Average

    Thank you alot, but i’m sorry if I am not clear enought, x,y and q will never be true at the same time, that’s why i want to get a value if they were true close to eachother, as within 500 bars.

    To illustrate with an example, if we had a bearish engulfing, a bullish engulfing and a doji the last 500 bars i would like to return these to a value = 1 and if not true, value 0. As happens there could be alot of doji’s in these 500 bars, but i don’t want this to influence my true value = 1. For me it seems that a ONCE code could be in place, but i’m not sure how to do this, because i want the “loop” to continue. I figure, because they should probably be taken individually-the x,y,q- we maybe should use a seperate summation for them.

    If the 3 conditions is true atleast once(each of them) in the previous 500 bars i want the function to equal 1.

    #18462 quote
    JC_Bywan
    Moderator
    Master

    ok, so, if the 3 conditions x=1, y=1, Q=0 are independant from each other and don’t have to be simultaneously true, but each one of those need to have happened at least once:

    if summation[500](x=1)>0 and summation[500](y=1)>0 and summation[500](Q=0)>0 then...
    Spekulanten thanked this post
    #18467 quote
    robertogozzi
    Moderator
    Master

    With that code ProOrder has to sum occurrences 500 times at each bar.

    This code should be less time consuming:

    ONCE Occurrences = 0
       .
       .
       .
       .
    if x =1  and y = 1 and Q = 0 then
       Occurences = Occurrences + 1    //increment by 1 at each bar
    else
       Occurrences = 0                 //when false rest Occurrences to 0
    endif
    if Occurences = 500 then           //after 500 bars in a row...
       a = 1
    else                               //else....
       a = 0
    endif
    #18469 quote
    Spekulanten
    Participant
    Average

    Thanks alot Noobywan, simple things is hard until right idea! Should work now [y]!

     

    Thanks robertogozzi for input! But i don’t see how this works for me, did you read my code goal? I’ll add a code below, i hope you can simplify that one, would be terrific! Really need to speed up the calculations…
    “If the 3 conditions is true atleast once(each of them) in the previous 500 bars i want the function to equal 1.”

    
    if close < average[2000][1] then 
    a = 1 
    else
    a = 0
    endif
    
    
    
    if close > close[1] then
    b = 1 
    else
    b = 0
    endif
    
    if close > average[2000] then
    c = 1
    else
    c = 0
    endif
    
    if summation[500](a=1)>0 and summation[500](b=1)>0 and summation[500](c=1)>0 then
    x = 1
    else
    x =0
    endif
    
    return x
    
    #108385 quote
    Dr Manhattan
    Participant
    Senior

    Im also curious about the answer to this one. I would like to know for example if a Stochastic crossed below 20 and for example an RSI did the same within for example 10 bars of each other.

    #108388 quote
    robertogozzi
    Moderator
    Master

    There you go, C will be true whenever one of the two occurs at a distance of 10 bars:

    x = Stochastic[5,3] CROSSES UNDER 20
    y = Rsi[14](close) CROSSES UNDER 20
    c = (x AND y[10]) OR (x[10] AND y)
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

X happened withing y periods


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 9 replies,
has 4 voices, and was last updated by robertogozzi
6 years, 4 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 12/13/2016
Status: Active
Attachments: No files
Logo Logo
Loading...