I tried to create a fairly simple MACD divergence check indicator.  Below is the code for Hidden Bull.  I must be missing something about how the Fold function works.
First I find the peaks on the MACD. Then it looks back 5 - 40 bars from Bar[0] (current bar) looking for a peak. If it finds one, it checks to see if MACDPeak value is lower and if price high is higher (hidden divergence). I also use a price lookback of 5 bars to find the local high. If it finds divergence, then it increments the total divergences found for bar[0] and adds a bubble to the MACD line.
But it's not finding divergence I can see with my eye. I linked an image where there is clear hidden divergence today but the indicator didn't find it.
hidden divergence on ZNGA daily
I'd appreciate any help in where I am missing how the Fold function should process what I am trying to do.
Or should I be doing this some other way than Fold. I've programmed this easily in MT4 using for loops, but I assume Fold is the way to do it in TS
	
	
	
	
	
		
	
		
			
		
		
	
				
			First I find the peaks on the MACD. Then it looks back 5 - 40 bars from Bar[0] (current bar) looking for a peak. If it finds one, it checks to see if MACDPeak value is lower and if price high is higher (hidden divergence). I also use a price lookback of 5 bars to find the local high. If it finds divergence, then it increments the total divergences found for bar[0] and adds a bubble to the MACD line.
But it's not finding divergence I can see with my eye. I linked an image where there is clear hidden divergence today but the indicator didn't find it.
hidden divergence on ZNGA daily
I'd appreciate any help in where I am missing how the Fold function should process what I am trying to do.
Or should I be doing this some other way than Fold. I've programmed this easily in MT4 using for loops, but I assume Fold is the way to do it in TS
		Ruby:
	
	declare lower;
input minLookbackForDivergence = 5;    #this is the min amount we would look back for a divergence
input maxLookbackForDivergence = 40;   # the most bars we would look back for divergence
input maxLookbackForPrice = 5;         #this is how many bars we would look back from bar[0] to find a price high/low
def MACDfastLength = 12;
def MACDslowLength = 26;
def MACDaverageType = AverageType.EXPONENTIAL;
plot MACDvalue = MovingAverage(MACDaverageType, close, MACDfastLength) - MovingAverage(MACDaverageType, close, MACDslowLength);
MACDvalue.SetDefaultColor(Color.CYAN);
plot zeroLine = 0;
zeroLine.SetDefaultColor(Color.WHITE);
plot MACDPeak = If(((MACDvalue[0] > MACDvalue[1]) && (MACDvalue[0] > MACDvalue[-1])), MACDvalue, Double.NaN);
MACDPeak.SetPaintingStrategy(PaintingStrategy.POINTS);
MACDPeak.SetDefaultColor(Color.MAGENTA);
def hidBearMACDValueCount =
    fold i = minLookbackForDivergence to maxLookbackForDivergence + 1
    with variable = 0
    do
    if(IsNaN(MACDPeak[0]), 
        Double.NaN,
        if(IsNan(MACDPeak[i]),
            variable,
            if(MACDPeak[i] < MACDPeak[0] &&
               Highest(high[i], maxLookbackForPrice) > Highest(high[0], maxLookbackForPrice),
                variable + 1,
                variable
               )
           )
       );
 
AddChartBubble(hidBearMACDValueCount > 0, MACDPeak[0], "Hid Bear: " + hidBearMACDValueCount, Color.WHITE, yes);
# Divergence Definitions  ===============================================================
# regBull     indValley[i] < indValley[0] && low[i] > low[0]
# hidBull     indValley[i] > indValley[0] && low[i] < low[0]
# regBear     indPeak[i] > indPeak[0] && high[i] < high[0]
# hidBear     indPeak[i] < indPeak[0] && high[i] > high[0] 
				 
						 
 
		 
 
		