Looking for a function, which can tell if there is an event happend in the last
Forums › ProRealTime English forum › ProBuilder support › Looking for a function, which can tell if there is an event happend in the last
- This topic has 13 replies, 4 voices, and was last updated 5 days ago by thomas2004ch.
-
-
12/04/2024 at 7:27 PM #241118
For example, I will check if in or within the last n days the SMA(5) crosses over the SMA(10).
12/04/2024 at 7:53 PM #241119You can ever use Barssince if you want to know when it happened last time (and then check if it is within the last n bars,
https://www.prorealcode.com/documentation/barssince/
or Summation to check if it happened at least once within the last n bars
https://www.prorealcode.com/documentation/summation/
3 users thanked author for this post.
12/05/2024 at 10:11 AM #241132Here’s an example of using BARSSINCE and SUMMATION. This may not be the only way to do this.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162defparam drawonlastbaronly = true//constantsdaysBack = 1 // number of days back or maximum bars in chart!// indicatorssma5 = average[5,0](close)sma10 = average[10,0](close)// crossover conditionc = sma5 crosses over sma10// offset to last crossoverClast = barssince(c,0)// crossover barXbar = (barindex-Clast)//drawtext("#Xbar# crossovers",0,-60)anchor(top,xshift,yshift)// threshold limit barLbar = barindex - (barssince(day<>day[1],daysBack-1)-1)//drawtext("#Lbar# threshold limit bar",0,-60)anchor(top,xshift,yshift)// drawing!// if theres at least one crossover after threshold limit then display textif Xbar > Lbar and cLast > -1 then// draw last crossover linedrawVline(Xbar) coloured("red") style(dottedline,1) // crossover line// draw textbi = barindex // as a user variabledif = Xbar-var // difference between xover offset and thresholddrawtext("Threshold at bar #var# SMA5 last crossed over SMA10 at bar #Xbar# , #dif# bars from threshold ",0,40, dialog,standard,20)anchor(bottom,xshift,yshift)drawtext("Also, last crossed over is #Clast# bars back from current bar #bi# ",0,20, dialog,standard,20)anchor(bottom,xshift,yshift)endif// draw threshold limit linevar = barindex - (barindex - Lbar) // threshold bardrawVline(var) style(dottedline,1) // threshold line// number of crossovers sine threhold barif barindex > var thensum = summation[barindex-var](c)drawtext("#sum# crossovers in range",0,-30)anchor(top,xshift,yshift)Xover = summation[barindex-var](c) > 0if Xover = 1 thendrawtext("#Xover# YES! ",0,-60)anchor(top,xshift,yshift)elsedrawtext("#Xover# NO! ",0,-60)anchor(top,xshift,yshift)endifendifreturn sma5 coloured("red") as"sma5" ,sma10 coloured("lime") as"sma10",close style(dottedline,1)2 users thanked author for this post.
12/05/2024 at 10:56 AM #24113612/05/2024 at 2:50 PM #241150Here i smy indicator:
MyMA5 = Average[5](close)
MyMA20 = Average[20](close)cond2 = MyMA5 crosses over MyMA20
testCross = BarsSince(cond2, 1)return testCross
The output looks like the saw signal. But what I want is like pulse as shown as in the attached picture. Is it possible?
12/05/2024 at 4:11 PM #241155It appears from image, that you want to highlight when crossovers occur.
1234567891011MyMA5 = Average[5](close)MyMA20 = Average[20](close)cond2 = 0if MyMA5 crosses over MyMA20 thencond2 = 1endiftestCross = BarsSince(cond2, 1)return cond2 style(histogram,1)12/05/2024 at 4:45 PM #241156wonderful, many thanks!
12/05/2024 at 9:43 PM #241162Hi, I thnik it is not correct since you used the cond2 but not the testCross.
Surely it maybe because I used the ‘1’ in the BarsSince(). In this case the Barssince() has less meaning. I should change it to any number greater than 1.
12/05/2024 at 10:34 PM #241163Not sure where you going with this, with regards to original question.
BarsSince(condition,0) with’0′ gets the bar offset to last condition that was true, from the current bar..
Using ‘1’ gets the offset to the true condition prior to the last.
The saw tooth action is plotting the current continuous offset value, since it is plotted every bar from on return line.
Additionally, the staggering level of the low of the saw tooth is because, when a new crossover happens,
the one being plotted stops being the prior one and the prior last one becomes the current prior one, and so on after each new crossover.
From your image I took it as you were highlighting the crossover points which is when you use ‘0’ in barsSince.
Regardless, of what value you put in BarsSince, each crossover will be highlighted other than crossovers below n down to ‘0’
So the crossovers never change position with respects to the averages and the bars number there on, just the offset from the current barindex value.
Why did you only highlight 3 crossovers in your image?
12/06/2024 at 9:43 AM #241174Hi,
my original question is: I wonder if there is a function which can tell me in the last or within the last n days an event or condition comes true.
I use another chart software where there is a function called EXIST(event, days). I attach a screesnhot. Maybe you can understand what I wat better?
12/06/2024 at 12:33 PM #241183This will return the number of occurrences withim the last N bars:
1234ONCE N = 5MyMA5 = Average[5](close)MyMA20 = Average[20](close)return summation[N](MyMA5 crosses over MyMA20) style(histogram,1)12/06/2024 at 1:06 PM #241190HI @roberto,
here i smy code:
ONCE N = 3
MyMA5 = Average[5](close)
MyMA10 = Average[10](close)
MyMA20 = Average[20](close)c1 = MyMA5 crosses over MyMA10
c2 = MyMA5 crosses over MyMA20return summation[n](c1 and c2)
Attached is the creenshot. One can see there is much less signals.
12/06/2024 at 2:42 PM #241198In your code, fast MA have to cross the 2 others at same barindex…
Correction1234567891011ONCE N = 3MyMA5 = Average[5](close)MyMA10 = Average[10](close)MyMA20 = Average[20](close)A1 = summation[n](MyMA5 crosses over MyMA10)>0A2 = summation[n](MyMA5 crosses over MyMA20)>0R = A1 and A2return R1 user thanked author for this post.
12/06/2024 at 2:56 PM #241199beautiful! Many thanks!
-
AuthorPosts