I want to get an X bars back average value from the MACD histogram but only for positive or negative values respectively. With that I want to be able to search for histogram spikes and compare them with the average values.
So for the sake of (my) simplicity Im trying to achieve this with a function aimed either on the positive or the negative values. Here is an example for the histogram positive values where I try to look back over a _lookback amount of bars and if the bar has a positive value I ad that value to the previous average value and divide it by the number of positive values (ie. c_avgGreenCounter) that printed before that:
f_getAvgHistoGreen(_lookback) =>
var avgHistogramGreen = 0.0
var avgGreenCounter = 0
for i = 0 to _lookback by 1
if hist[i] >= 0
avgGreenCounter += 1
avgHistogramGreen := (avgHistogramGreen + hist[i]) / 2
else
avgHistogramGreen := avgHistogramGreen
avgGreenCounter := avgGreenCounter
[avgGreenCounter, avgHistogramGreen]
Only when I call this with this function:
[avgGreenCounter, avgHistogramGreen] = f_getAvgHistoGreen(50)
the avgGreenCounter hits super high numbers which is obviously wrong. Can someone help me at least with the positive values please? Histogram values print correctly, I've checked them. I think that I will be then able to figure it out for the negatives as well.