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.
It’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):
Avg = average[100,0](MyIndicator * (MyIndicator > 0))
The idea is to average all positive values, 0 otherwise.
I didn’t work. It returns 0
Maybe 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?
You must have done something wrong, as it works perfectly (applied to MACD):
MyMACD = Average[12,0](close) - Average[26,0](close)
MySignalLine = Average[9,0](MyMACD)
MyHisto = MyMACD - MySignalLine
Avg = average[100,0](MyHisto * (MyHisto > 0)) //average of the last 100 periods with a Histogram > 0
return Avg AS "Average MACD histogram when > 0"
If, instead, you want to count how many times the Histogram was > 0:
MyMACD = Average[12,0](close) - Average[26,0](close)
MySignalLine = Average[9,0](MyMACD)
MyHisto = MyMACD - MySignalLine
Count = summation[100](MyHisto > 0)
return Count AS "How many times Hitogram was > 0"