Divergence indicator using Fold not working correctly

Cre8able

New member
VIP
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



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]
 
Solution
I'm still stuck on looking back for a high on the left side. I want to find the highest high looking back.

The manual version seems to work where I list each option (It includes1 bar to the right to check the bar that causes the peak for a higher high)

But I'm assuming there must be some way to use Highest, but the way I thought should work didn't.

any clarity is appreciated. thanks


Ruby:
def hidBearMACDValueCount =
    fold i = minLookbackForDivergence to maxLookbackForDivergence + 1
    with variable = 0
    do
    if(IsNaN(MACDPeak[0]),
        Double.NaN,
        if(IsNan(MACDPeak[i]),
            variable,
            if(

#  the manual version seems to work, but not very flexible                     
                (...

Cre8able

New member
VIP
I'm still stuck on looking back for a high on the left side. I want to find the highest high looking back.

The manual version seems to work where I list each option (It includes1 bar to the right to check the bar that causes the peak for a higher high)

But I'm assuming there must be some way to use Highest, but the way I thought should work didn't.

any clarity is appreciated. thanks


Ruby:
def hidBearMACDValueCount =
    fold i = minLookbackForDivergence to maxLookbackForDivergence + 1
    with variable = 0
    do
    if(IsNaN(MACDPeak[0]),
        Double.NaN,
        if(IsNan(MACDPeak[i]),
            variable,
            if(

#  the manual version seems to work, but not very flexible                       
                (
                GetValue(high, i - 1) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 0) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 1) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 2) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 3) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 4) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 5) > Highest(high[0], maxLookbackForPrice) 
                )&&


# doesn't work-----
                #Highest(GetValue(high,i), maxLookbackForPrice) > Highest(high[0], maxLookbackForPrice) &&


# works but doesn't check the previous 5 bars on left side
                #GetValue(high,i) > Highest(high[0], maxLookbackForPrice) && 


                GetValue(MACDPeak, i) < MACDPeak[0],
                variable + 1,
                variable
               )
           )
       );
 
Last edited:

halcyonguy

Well-known member
VIP
I'm still stuck on looking back for a high on the left side. I want to find the highest high looking back.

The manual version seems to work where I list each option (It includes1 bar to the right to check the bar that causes the peak for a higher high)

But I'm assuming there must be some way to use Highest, but the way I thought should work didn't.

any clarity is appreciated. thanks


Ruby:
def hidBearMACDValueCount =
    fold i = minLookbackForDivergence to maxLookbackForDivergence + 1
    with variable = 0
    do
    if(IsNaN(MACDPeak[0]),
        Double.NaN,
        if(IsNan(MACDPeak[i]),
            variable,
            if(

#  the manual version seems to work, but not very flexible                     
                (
                GetValue(high, i - 1) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 0) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 1) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 2) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 3) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 4) > Highest(high[0], maxLookbackForPrice) or
                GetValue(high, i + 5) > Highest(high[0], maxLookbackForPrice)
                )&&


# doesn't work-----
                #Highest(GetValue(high,i), maxLookbackForPrice) > Highest(high[0], maxLookbackForPrice) &&


# works but doesn't check the previous 5 bars on left side
                #GetValue(high,i) > Highest(high[0], maxLookbackForPrice) &&


                GetValue(MACDPeak, i) < MACDPeak[0],
                variable + 1,
                variable
               )
           )
       );

EDIT -----
to > p


EDIT 2
change range from 1 to 10, to 0 to 10


to find the highest value, with a loop, in the past 10 bars, including the current bar and 9 previous bars. it will count 0 to 9, then stop . it won't process 10 , the end range number.

the counter starts with 0, so the 1st value compared is the current bar, high[0].

def x = fold i = 1 to 10
with p
do if ( getvalue(high, i) > p then getvalue(high, i) else p;
 
Last edited:
Solution

Similar threads

Top