• Get $40 off VIP by signing up for a free account! Sign Up

Divergence indicator using Fold not working correctly

Cre8able

New member
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                     
                (...

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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:
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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
299 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top