X happened withing y periods
Forums › ProRealTime English forum › ProBuilder support › X happened withing y periods
- This topic has 9 replies, 4 voices, and was last updated 5 years ago by robertogozzi.
-
-
12/13/2016 at 1:01 PM #18449
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
endifHow do i code “within the previous 500 bars”
Thanks alot guys!
12/13/2016 at 1:08 PM #18450If 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:
12345if summation[500](x=1 and y=1 and Q=0)=500 thena=1elsea=0endifbut 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:
1if summation[500](x=1 and y=1 and Q=0)>0 then ...1 user thanked author for this post.
12/13/2016 at 2:04 PM #18454Thanks 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.
12/13/2016 at 2:20 PM #18457“(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
1<span class="token keyword">if</span> <span class="token indicators">summation</span><span class="token punctuation">[</span><span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">and</span> y<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">and</span> Q<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token number">0</span> <span class="token keyword">then</span></code>...12/13/2016 at 2:38 PM #18460Thank 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.
12/13/2016 at 3:05 PM #18462ok, 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:
1<span class="token keyword">if</span> <span class="token indicators">summation</span><span class="token punctuation">[</span><span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token number">1)>0</span> <span class="token keyword">and </span><span class="token indicators">summation</span><span class="token punctuation">[</span><span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">(</span>y<span class="token operator">=</span><span class="token number">1)>0</span> <span class="token keyword">and s</span><span class="token indicators">ummation</span><span class="token punctuation">[</span><span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">(</span>Q<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">></span><span class="token number">0</span> <span class="token keyword">then</span></code>...1 user thanked author for this post.
12/13/2016 at 3:26 PM #18467With that code ProOrder has to sum occurrences 500 times at each bar.
This code should be less time consuming:
123456789101112131415ONCE Occurrences = 0....if x =1 and y = 1 and Q = 0 thenOccurences = Occurrences + 1 //increment by 1 at each barelseOccurrences = 0 //when false rest Occurrences to 0endifif Occurences = 500 then //after 500 bars in a row...a = 1else //else....a = 0endif12/13/2016 at 4:12 PM #18469Thanks 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.”123456789101112131415161718192021222324252627if close < average[2000][1] thena = 1elsea = 0endifif close > close[1] thenb = 1elseb = 0endifif close > average[2000] thenc = 1elsec = 0endifif summation[500](a=1)>0 and summation[500](b=1)>0 and summation[500](c=1)>0 thenx = 1elsex =0endifreturn x09/24/2019 at 7:50 PM #108385Im 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.
09/24/2019 at 8:38 PM #108388There you go, C will be true whenever one of the two occurs at a distance of 10 bars:
123x = Stochastic[5,3] CROSSES UNDER 20y = Rsi[14](close) CROSSES UNDER 20c = (x AND y[10]) OR (x[10] AND y) -
AuthorPosts
Find exclusive trading pro-tools on