Lookback with condition
Forums › ProRealTime English forum › ProBuilder support › Lookback with condition
- This topic has 4 replies, 2 voices, and was last updated 4 years ago by
robertogozzi.
-
-
06/26/2021 at 2:26 AM #172502
Hello All,
This was posted in reply to Vonassi but didn’t look like it was posted. I am posting here. Apologies if it ended up being double posted.
I have a fairly simple question
How do I write a simple code that for example looks back at the last 100 bars and averages only positive values? For example I have an indicator that returns either a + value or – value for every bar. I want to lookback 100 bars and average only the + values (- values are skipped/ignored in average calculation)
thank you and regards.06/26/2021 at 2:47 AM #172505It’s an indicator, so its correct support forum is ProBuilder.
I moved it.
Try this (not tested, I can’t even know if it is an accepted syntax):
1Avg = average[100,0](MyIndicator * (MyIndicator > 0))The idea is to average all positive values, 0 otherwise.
06/26/2021 at 7:31 AM #172507I didn’t work. It returns 0
06/26/2021 at 7:34 AM #172508Maybe a code that goes through the 100 bars and everytime that finds a positive value it stores it in a variable and once it finishes going through the 1oo bars and storing all the positive values it finds then a code to average all positive values found and stored. Perhaps an array variable that can store up to 96 values?? perhaps a much more simpler code?
06/26/2021 at 10:11 AM #172515You must have done something wrong, as it works perfectly (applied to MACD):
12345MyMACD = Average[12,0](close) - Average[26,0](close)MySignalLine = Average[9,0](MyMACD)MyHisto = MyMACD - MySignalLineAvg = average[100,0](MyHisto * (MyHisto > 0)) //average of the last 100 periods with a Histogram > 0return Avg AS "Average MACD histogram when > 0"If, instead, you want to count how many times the Histogram was > 0:
12345MyMACD = Average[12,0](close) - Average[26,0](close)MySignalLine = Average[9,0](MyMACD)MyHisto = MyMACD - MySignalLineCount = summation[100](MyHisto > 0)return Count AS "How many times Hitogram was > 0" -
AuthorPosts
