Repaints RSI, OBV, MACD, STOCH, CCI, or MFI Divergence For ThinkOrSwim

Repaints

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
mod note:
This indicator uses future bars to find the low.
The bubble, scan, alert and candle painting lag is because it has to wait for the future before it can alert or signal.
It waits to see what the futures brings and THEN goes back in time to repaint bubbles, arrows, candles and / or other signals.
Further, this indicator has to wait until the divergence forms, which exacerbates the problem.

The lag is built-in into the essence of this script.

Getting alerts and scans can be problematic with future bars. Getting them on this script is more so.
You can try adding "within 3 bars" to the alert or scan. That way, when it goes back to write the bubble or paint the candle, you may catch it.

To create alerts that goes back in time to find repainted signals:
Ruby:
#---- Alerts
Alert(alerts and (bullCond within 3 bars), "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and (hiddenBullCond within 3 bars), "Hidden Bull Div", Alert.BAR, sound);

To create a scan that goes back in time to find repainted signals:
Reference the study and make your filter
Ruby:
bullCond within 3 bars or hiddenBullCond within 3 bars

@Chris77, @DoubleD, @zeek, @FOTM_8888
 
Last edited:
I changed the code to show on upper chart with multiple divergence options. i.e RSI, CCI, MACD, OBV...etc

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#study("Multiple  Divergence Indicator w/Alerts")
# Created by Sam4Cok@Samer800 - 10/2022 as requested from https://usethinkscript.com memeber

input src = hl2;        # "RSI Source"
input Indicator = {Default RSI, OBV, MACD, STOCH, CCI, MFI};
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input lbR  = 5;           # "Pivot Lookback Right"
input lbL  = 5;           # "Pivot Lookback Left"
input MaxLookback = 60;   # "Max of Lookback Range"
input MinLookback = 5;    # "Min of Lookback Range"
input DivBull = yes;      # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes;      # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"

def na = double.NaN;
#mfi(src, length) =>
script mfi {
input src = close;
input length = 14;
    def upper = sum(volume * (if(src-src[1]) <= 0.0 then 0.0 else src), length);
    def lower = sum(volume * (if(src-src[1]) >= 0.0 then 0.0 else src), length);
    def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  plot return = mfi;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
def RSI = RSI(Price = src, length= 14);
def obv = TotalSum(sign((close-close[1])) * volume);
def MACD = macd().Diff;
def stochastic = stoch(close, high, low, 14);
def CCI = CCI(Length=20);
def mfi = MFI(src,14);
#----Div-----------
def divSrc;
switch (Indicator) {
case RSI:
    divSrc = RSI;
case OBV:
    divSrc = OBV;
case MACD:
    divSrc = MACD;
case STOCH:
    divSrc = Stochastic;
case CCI:
    divSrc = CCI;
case MFI:
    divSrc = MFI;
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
  input cond = 0;
  input src = close;
  input lbr = 0;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to 200 with p=1 while p < n + 1
    do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
  plot price = GetValue(src[lbr], offset-1);
}
#_inRange(cond) =>
script _inRange {
    input cond = yes;
    input rangeUpper = 60;
    input rangeLower = 5;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}

#Div(source, lbl, lbr, MaxLookback, MinLookback, DivBull = yes, DivBear = yes, DivHiddenBull=no, DivHiddenBear=no),
script Div {
input divSrc = close;
input lbl = 5;
input lbr = 5;
input MaxLookback = 60;
input MinLookback = 5;
input DivBull = yes;
input DivBear = yes;
input DivHiddenBull=no;
input DivHiddenBear=no;
    def h = high;
    def l = low;
    def pl = findpivots(divSrc,-1, lbL, lbR);
    def ph = findpivots(divSrc, 1, lbL, lbR);

    def plFound = if !isNaN(pl) then 1 else 0;
    def phFound = if !isNaN(ph) then 1 else 0;

    def vlFound = valuewhen(plFound, divSrc, 0, 1);
    def vhFound = valuewhen(phFound, divSrc, 0, 1);

    def plPrice = valuewhen(plFound, l, 0, 1);
    def phPrice = valuewhen(phFound, h, 0, 1);

#// Regular Bullish
    def oscHL = divSrc > vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceLL = l < plPrice;
    def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
    def oscLL = divSrc[lbr] < vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceHL = l[lbr] > plPrice;
    def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;

#// Regular Bearish
    def oscLH   = divSrc < vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceHH = h > phPrice;
    def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
    def oscHH = divSrc > vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceLH = h < phPrice;
    def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;

    plot BullDiv = if bullCond then 1 else 0;
    plot BearDiv = if bearCond then 1 else 0;
    plot hidBullDiv = if hiddenBullCond then 1 else 0;
    plot HidBearDiv = if hiddenBearCond then 1 else 0;
}

#---- Div

def bullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback,DivBull, DivBear, DivHiddenBull, DivHiddenBear).BullDiv;
def bearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).BearDiv;
def hiddenBullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).hidBullDiv;
def hiddenBearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).HidBearDiv;

#------ Bubbles
addchartbubble(bullCond, low, Indicator, color.GREEN, no);
addchartbubble(bearCond, high, Indicator, CreateColor(156,39,176), yes);
addchartbubble(hiddenBullCond, low, Indicator, color.DARK_green, no);
addchartbubble(hiddenBearCond, high, Indicator, color.DARK_red, yes);

#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);

#----End
 
Last edited by a moderator:
Oh Cool, I will check it out
**note - I did notice that if I had the RSI upper and lower study were both on it was plotting different signals. Just go to the setting in the upper study and change SRC to CLOSE and it will match.

It might just be my machine as I checked to code and it is set to close. So just a tip as you are backtesting and something looks off it is probably just your settings. I usually just go to the TOS Default indictor and see what it shows for settings and then match them up to the custom indicator.
 
Last edited:
I changed the code to show on upper chart with multiple divergence options. i.e RSI, CCI, MACD, OBV...etc

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#study("Multiple  Divergence Indicator w/Alerts")
# Created by Sam4Cok@Samer800 - 10/2022 as requested from https://usethinkscript.com memeber

input src = hl2;        # "RSI Source"
input Indicator = {Default RSI, OBV, MACD, STOCH, CCI, MFI};
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input lbR  = 5;           # "Pivot Lookback Right"
input lbL  = 5;           # "Pivot Lookback Left"
input MaxLookback = 60;   # "Max of Lookback Range"
input MinLookback = 5;    # "Min of Lookback Range"
input DivBull = yes;      # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes;      # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"

def na = double.NaN;
#mfi(src, length) =>
script mfi {
input src = close;
input length = 14;
    def upper = sum(volume * (if(src-src[1]) <= 0.0 then 0.0 else src), length);
    def lower = sum(volume * (if(src-src[1]) >= 0.0 then 0.0 else src), length);
    def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  plot return = mfi;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
def RSI = RSI(Price = src, length= 14);
def obv = TotalSum(sign((close-close[1])) * volume);
def MACD = macd().Diff;
def stochastic = stoch(close, high, low, 14);
def CCI = CCI(Length=20);
def mfi = MFI(src,14);
#----Div-----------
def divSrc;
switch (Indicator) {
case RSI:
    divSrc = RSI;
case OBV:
    divSrc = OBV;
case MACD:
    divSrc = MACD;
case STOCH:
    divSrc = Stochastic;
case CCI:
    divSrc = CCI;
case MFI:
    divSrc = MFI;
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
  input cond = 0;
  input src = close;
  input lbr = 0;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to 200 with p=1 while p < n + 1
    do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
  plot price = GetValue(src[lbr], offset-1);
}
#_inRange(cond) =>
script _inRange {
    input cond = yes;
    input rangeUpper = 60;
    input rangeLower = 5;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}

#Div(source, lbl, lbr, MaxLookback, MinLookback, DivBull = yes, DivBear = yes, DivHiddenBull=no, DivHiddenBear=no),
script Div {
input divSrc = close;
input lbl = 5;
input lbr = 5;
input MaxLookback = 60;
input MinLookback = 5;
input DivBull = yes;
input DivBear = yes;
input DivHiddenBull=no;
input DivHiddenBear=no;
    def h = high;
    def l = low;
    def pl = findpivots(divSrc,-1, lbL, lbR);
    def ph = findpivots(divSrc, 1, lbL, lbR);

    def plFound = if !isNaN(pl) then 1 else 0;
    def phFound = if !isNaN(ph) then 1 else 0;

    def vlFound = valuewhen(plFound, divSrc, 0, 1);
    def vhFound = valuewhen(phFound, divSrc, 0, 1);

    def plPrice = valuewhen(plFound, l, 0, 1);
    def phPrice = valuewhen(phFound, h, 0, 1);

#// Regular Bullish
    def oscHL = divSrc > vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceLL = l < plPrice;
    def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
    def oscLL = divSrc[lbr] < vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceHL = l[lbr] > plPrice;
    def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;

#// Regular Bearish
    def oscLH   = divSrc < vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceHH = h > phPrice;
    def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
    def oscHH = divSrc > vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceLH = h < phPrice;
    def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;

    plot BullDiv = if bullCond then 1 else 0;
    plot BearDiv = if bearCond then 1 else 0;
    plot hidBullDiv = if hiddenBullCond then 1 else 0;
    plot HidBearDiv = if hiddenBearCond then 1 else 0;
}

#---- Div

def bullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback,DivBull, DivBear, DivHiddenBull, DivHiddenBear).BullDiv;
def bearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).BearDiv;
def hiddenBullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).hidBullDiv;
def hiddenBearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).HidBearDiv;

#------ Bubbles
addchartbubble(bullCond, low, Indicator, color.GREEN, no);
addchartbubble(bearCond, high, Indicator, CreateColor(156,39,176), yes);
addchartbubble(hiddenBullCond, low, Indicator, color.DARK_green, no);
addchartbubble(hiddenBearCond, high, Indicator, color.DARK_red, yes);

#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);

#----End
great indicator like always @samer800 . a quick favor, do you think it's possible for color candle the signal, meaning when is divergence paint that candles with the signal bias. thank you in advance
 
great indicator like always @samer800 . a quick favor, do you think it's possible for color candle the signal, meaning when is divergence paint that candles with the signal bias. thank you in advance
add the below at the end of the script:

CSS:
AssignPriceColor(if (bullCond or hiddenBullCond) then color.BLUE else if (bearCond or hiddenBearCond)  then Color.MAGENTA else Color.CURRENT);
 
I changed the code to show on upper chart with multiple divergence options. i.e RSI, CCI, MACD, OBV...etc

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#study("Multiple  Divergence Indicator w/Alerts")
# Created by Sam4Cok@Samer800 - 10/2022 as requested from https://usethinkscript.com memeber

input src = hl2;        # "RSI Source"
input Indicator = {Default RSI, OBV, MACD, STOCH, CCI, MFI};
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input lbR  = 5;           # "Pivot Lookback Right"
input lbL  = 5;           # "Pivot Lookback Left"
input MaxLookback = 60;   # "Max of Lookback Range"
input MinLookback = 5;    # "Min of Lookback Range"
input DivBull = yes;      # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes;      # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"

def na = double.NaN;
#mfi(src, length) =>
script mfi {
input src = close;
input length = 14;
    def upper = sum(volume * (if(src-src[1]) <= 0.0 then 0.0 else src), length);
    def lower = sum(volume * (if(src-src[1]) >= 0.0 then 0.0 else src), length);
    def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  plot return = mfi;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
def RSI = RSI(Price = src, length= 14);
def obv = TotalSum(sign((close-close[1])) * volume);
def MACD = macd().Diff;
def stochastic = stoch(close, high, low, 14);
def CCI = CCI(Length=20);
def mfi = MFI(src,14);
#----Div-----------
def divSrc;
switch (Indicator) {
case RSI:
    divSrc = RSI;
case OBV:
    divSrc = OBV;
case MACD:
    divSrc = MACD;
case STOCH:
    divSrc = Stochastic;
case CCI:
    divSrc = CCI;
case MFI:
    divSrc = MFI;
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
  input cond = 0;
  input src = close;
  input lbr = 0;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to 200 with p=1 while p < n + 1
    do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
  plot price = GetValue(src[lbr], offset-1);
}
#_inRange(cond) =>
script _inRange {
    input cond = yes;
    input rangeUpper = 60;
    input rangeLower = 5;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}

#Div(source, lbl, lbr, MaxLookback, MinLookback, DivBull = yes, DivBear = yes, DivHiddenBull=no, DivHiddenBear=no),
script Div {
input divSrc = close;
input lbl = 5;
input lbr = 5;
input MaxLookback = 60;
input MinLookback = 5;
input DivBull = yes;
input DivBear = yes;
input DivHiddenBull=no;
input DivHiddenBear=no;
    def h = high;
    def l = low;
    def pl = findpivots(divSrc,-1, lbL, lbR);
    def ph = findpivots(divSrc, 1, lbL, lbR);

    def plFound = if !isNaN(pl) then 1 else 0;
    def phFound = if !isNaN(ph) then 1 else 0;

    def vlFound = valuewhen(plFound, divSrc, 0, 1);
    def vhFound = valuewhen(phFound, divSrc, 0, 1);

    def plPrice = valuewhen(plFound, l, 0, 1);
    def phPrice = valuewhen(phFound, h, 0, 1);

#// Regular Bullish
    def oscHL = divSrc > vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceLL = l < plPrice;
    def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
    def oscLL = divSrc[lbr] < vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceHL = l[lbr] > plPrice;
    def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;

#// Regular Bearish
    def oscLH   = divSrc < vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceHH = h > phPrice;
    def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
    def oscHH = divSrc > vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceLH = h < phPrice;
    def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;

    plot BullDiv = if bullCond then 1 else 0;
    plot BearDiv = if bearCond then 1 else 0;
    plot hidBullDiv = if hiddenBullCond then 1 else 0;
    plot HidBearDiv = if hiddenBearCond then 1 else 0;
}

#---- Div

def bullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback,DivBull, DivBear, DivHiddenBull, DivHiddenBear).BullDiv;
def bearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).BearDiv;
def hiddenBullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).hidBullDiv;
def hiddenBearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).HidBearDiv;

#------ Bubbles
addchartbubble(bullCond, low, Indicator, color.GREEN, no);
addchartbubble(bearCond, high, Indicator, CreateColor(156,39,176), yes);
addchartbubble(hiddenBullCond, low, Indicator, color.DARK_green, no);
addchartbubble(hiddenBearCond, high, Indicator, color.DARK_red, yes);

#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);

#----End
Hi @samer800. I added this on my chart and looks good to me on detecting the divergences. My problem is that the bubbles does not seem to show real time. Like today on the /ES I did not see any at all the whole time. I then had to add another indicator that refreshed my chart and then suddenly there were at least 7 bubbles that showed up ( i added three times with different indicator sources). What am I missing? Is it supposed to plot real time or at least a bit delayed?
 
Hi @samer800. I added this on my chart and looks good to me on detecting the divergences. My problem is that the bubbles does not seem to show real time. Like today on the /ES I did not see any at all the whole time. I then had to add another indicator that refreshed my chart and then suddenly there were at least 7 bubbles that showed up ( i added three times with different indicator sources). What am I missing? Is it supposed to plot real time or at least a bit delayed?
I tested a few stocks, the delay is 5 days.
 
Hi @samer800 , This is a great indicator:) Seeing some really nice results after backtesting it. I have a request if you can possible help me. Can you add an option to paint arrows on top/bottom of bars for the divergence signals instead of the chartbubbles?
 
Hi @samer800 , This is a great indicator:) Seeing some really nice results after backtesting it. I have a request if you can possible help me. Can you add an option to paint arrows on top/bottom of bars for the divergence signals instead of the chartbubbles?
add the below at the end of the code.

CSS:
AssignPriceColor(if (bullCond or hiddenBullCond) then color.BLUE else if (bearCond or hiddenBearCond)  then Color.MAGENTA else Color.CURRENT);
 
add the below at the end of the code.

CSS:
AssignPriceColor(if (bullCond or hiddenBullCond) then color.BLUE else if (bearCond or hiddenBearCond)  then Color.MAGENTA else Color.CURRENT);
Regarding the AssignPriceColor code that you provided, is there any way to display multiple divergence types at once and paint each type with a different set of colors? For example, let´s say i wanted to display both RSI, MACD and CCI all at once. So basically one candle color for each type and direction (bull or bear cond)?
I tried creating a new study where i copied the original code and then just switched the divergence type and also changed the color code for the AssignPriceColor and had both studies on the chart but it looks like it´s not working to display multiple divergence types at once, i can only see one. Just curious if there is a way to do this because it would be really great if so @samer800
 
Last edited:
Regarding the AssignPriceColor code that you provided, is there any way to display multiple divergence types at once and paint each type with a different set of colors? For example, let´s say i wanted to display both RSI, MACD and CCI all at once. So basically one candle color for each type and direction (bull or bear cond)?
I tried creating a new study where i copied the original code and then just switched the divergence type and also changed the color code for the AssignPriceColor and had both studies on the chart but it looks like it´s not working to display multiple divergence types at once, i can only see one. Just curious if there is a way to do this because it would be really great if so @samer800
the code written in this way. What you are asking can be another code.
 
the code written in this way. What you are asking can be another code.

Ok, i understand. If you could possibly help make such a study i would be very grateful. This is by far the best divergence indicator i have come across.
 
Ok, i understand. If you could possibly help make such a study i would be very grateful. This is by far the best divergence indicator i have come across.
Hi Zeek, how do you use this? when i tested it a few weeks ago the issue I encountered is that the bubbles does not seem to show real time and only shows multiple bars after. Is there a way you use the divergence detected after the reversal happened?
 
Hi Zeek, how do you use this? when i tested it a few weeks ago the issue I encountered is that the bubbles does not seem to show real time and only shows multiple bars after. Is there a way you use the divergence detected after the reversal happened?
Yes, i am gessing it has to do with how many lookback bars you have it set to. By deafult, it´s set to 5 both right and left. I lowered these and still get solid signals so right now, i am using 2 bars right. The only problem i´ve encountered so far is the alerts, they don´t trigger for me and i have it set to play sound and show alert in message center but nothing there..Not sure why this isn´t working.

Is anyone else having any luck with alerts triggering?
 
Yes, i am gessing it has to do with how many lookback bars you have it set to. By deafult, it´s set to 5 both right and left. I lowered these and still get solid signals so right now, i am using 2 bars right. The only problem i´ve encountered so far is the alerts, they don´t trigger for me and i have it set to play sound and show alert in message center but nothing there..Not sure why this isn´t working.

Is anyone else having any luck with alerts triggering?
So you changed lb r to 2 and kept lb l to 5. With that change, how many bars after does the signal bubble show up for you? I will be testing of course but wanted to get feedback on what you are seeing
 
So you changed lb r to 2 and kept lb l to 5. With that change, how many bars after does the signal bubble show up for you? I will be testing of course but wanted to get feedback on what you are seeing

Did some more testing today and i can confirm what you described earlier, the painting of bars & chartbubbles will sometimes miss completely and will not show up unless i update chart or change the symbol and then return back to same symbol, then the signal will be there so there´s something fishy going on with the plotting of signals. But it doesn´t always miss from what i can tell, sometimes it will plot it realtime (or x number of bars you have it set to look into the future) and then it will show up and the alert triggers as well. But it´s not consistent, very odd indeed..
 
I changed the code to show on upper chart with multiple divergence options. i.e RSI, CCI, MACD, OBV...etc

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#study("Multiple  Divergence Indicator w/Alerts")
# Created by Sam4Cok@Samer800 - 10/2022 as requested from https://usethinkscript.com memeber

input src = hl2;        # "RSI Source"
input Indicator = {Default RSI, OBV, MACD, STOCH, CCI, MFI};
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input lbR  = 5;           # "Pivot Lookback Right"
input lbL  = 5;           # "Pivot Lookback Left"
input MaxLookback = 60;   # "Max of Lookback Range"
input MinLookback = 5;    # "Min of Lookback Range"
input DivBull = yes;      # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes;      # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"

def na = double.NaN;
#mfi(src, length) =>
script mfi {
input src = close;
input length = 14;
    def upper = sum(volume * (if(src-src[1]) <= 0.0 then 0.0 else src), length);
    def lower = sum(volume * (if(src-src[1]) >= 0.0 then 0.0 else src), length);
    def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  plot return = mfi;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
def RSI = RSI(Price = src, length= 14);
def obv = TotalSum(sign((close-close[1])) * volume);
def MACD = macd().Diff;
def stochastic = stoch(close, high, low, 14);
def CCI = CCI(Length=20);
def mfi = MFI(src,14);
#----Div-----------
def divSrc;
switch (Indicator) {
case RSI:
    divSrc = RSI;
case OBV:
    divSrc = OBV;
case MACD:
    divSrc = MACD;
case STOCH:
    divSrc = Stochastic;
case CCI:
    divSrc = CCI;
case MFI:
    divSrc = MFI;
}

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
  input cond = 0;
  input src = close;
  input lbr = 0;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to 200 with p=1 while p < n + 1
    do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
  plot price = GetValue(src[lbr], offset-1);
}
#_inRange(cond) =>
script _inRange {
    input cond = yes;
    input rangeUpper = 60;
    input rangeLower = 5;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}

#Div(source, lbl, lbr, MaxLookback, MinLookback, DivBull = yes, DivBear = yes, DivHiddenBull=no, DivHiddenBear=no),
script Div {
input divSrc = close;
input lbl = 5;
input lbr = 5;
input MaxLookback = 60;
input MinLookback = 5;
input DivBull = yes;
input DivBear = yes;
input DivHiddenBull=no;
input DivHiddenBear=no;
    def h = high;
    def l = low;
    def pl = findpivots(divSrc,-1, lbL, lbR);
    def ph = findpivots(divSrc, 1, lbL, lbR);

    def plFound = if !isNaN(pl) then 1 else 0;
    def phFound = if !isNaN(ph) then 1 else 0;

    def vlFound = valuewhen(plFound, divSrc, 0, 1);
    def vhFound = valuewhen(phFound, divSrc, 0, 1);

    def plPrice = valuewhen(plFound, l, 0, 1);
    def phPrice = valuewhen(phFound, h, 0, 1);

#// Regular Bullish
    def oscHL = divSrc > vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceLL = l < plPrice;
    def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
    def oscLL = divSrc[lbr] < vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
    def priceHL = l[lbr] > plPrice;
    def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;

#// Regular Bearish
    def oscLH   = divSrc < vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceHH = h > phPrice;
    def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
    def oscHH = divSrc > vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
    def priceLH = h < phPrice;
    def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;

    plot BullDiv = if bullCond then 1 else 0;
    plot BearDiv = if bearCond then 1 else 0;
    plot hidBullDiv = if hiddenBullCond then 1 else 0;
    plot HidBearDiv = if hiddenBearCond then 1 else 0;
}

#---- Div

def bullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback,DivBull, DivBear, DivHiddenBull, DivHiddenBear).BullDiv;
def bearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).BearDiv;
def hiddenBullCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).hidBullDiv;
def hiddenBearCond = DIV(divSrc,lbl,lbr,MaxLookback,MinLookback, DivBull, DivBear, DivHiddenBull, DivHiddenBear).HidBearDiv;

#------ Bubbles
addchartbubble(bullCond, low, Indicator, color.GREEN, no);
addchartbubble(bearCond, high, Indicator, CreateColor(156,39,176), yes);
addchartbubble(hiddenBullCond, low, Indicator, color.DARK_green, no);
addchartbubble(hiddenBearCond, high, Indicator, color.DARK_red, yes);

#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);

#----End
Hi @samer800 thanks for the code and after testing a couple of times seems to work pretty well. I also want to test the code for the lower chart, where can I find that? Thanks for your help!
 
Hi @samer800 thanks for the code and after testing a couple of times seems to work pretty well. I also want to test the code for the lower chart, where can I find that? Thanks for your help!
try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#study("Multiple  Divergence Indicator w/Alerts")
# Created by Sam4Cok@Samer800 - 10/2022 as requested from https://usethinkscript.com memeber
# lower chart
declare lower;
input src = close;        # "RSI Source"
input Length = 14;
input ShowLabel = yes;
input Indicator = {Default RSI, OBV, MACD, STOCH, CCI, MFI};
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input LookBackRight  = 5; # "Pivot Lookback Right"
input LookBackLeft  = 5;  # "Pivot Lookback Left"
input MaxLookback = 60;   # "Max of Lookback Range"
input MinLookback = 5;    # "Min of Lookback Range"
input DivBull = yes;      # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes;      # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"

def na = double.NaN;
addLabel(ShowLabel, Indicator, Color.WHITE);
def zeroLine = Indicator==Indicator.MACD or Indicator==Indicator.CCI;
#mfi(src, length) =>
script mfi {
input src = close;
input length = 14;
    def upper = sum(volume * (if(src-src[1]) <= 0.0 then 0.0 else src), length);
    def lower = sum(volume * (if(src-src[1]) >= 0.0 then 0.0 else src), length);
    def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  plot return = mfi;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
def nRSI = RSI(Price = src, length= Length);
def obv = TotalSum(sign((close-close[1])) * volume);
def MACD = macd().Diff;
def stochastic = stoch(close, high, low, Length);
def CCI = CCI(Length=Length);
def mfi = MFI(src,Length);
#----Div-----------
def divSrc;
switch (Indicator) {
case OBV:
    divSrc = OBV;
case MACD:
    divSrc = MACD;
case STOCH:
    divSrc = Stochastic;
case CCI:
    divSrc = CCI;
case MFI:
    divSrc = MFI;
default:
    divSrc = nRSI;
}
plot incdicator = divSrc;
incdicator.SetDefaultColor(Color.ORANGE);
incdicator.SetLineWeight(2);
plot midLine = if isNaN(close) or Indicator==Indicator.OBV then na else if zeroLine then 0 else 50;
midLine.SetDefaultColor(Color.GRAY);


#----Div-----------

def h = high;
def l = low;

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
  input cond = 0;
  input src = close;
  input MinLookback = 5;
  input MaxLookback = 60;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = MinLookback to MaxLookback + 1 with p=1 while p < n + 1
    do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
  plot price = GetValue(src, offset-1);
}
#_inRange(cond) =>
script _inRange {
    input cond = yes;
    input rangeUpper = 60;
    input rangeLower = 5;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
def pl = findpivots(divSrc,-1, LookBackLeft, LookBackRight);
def ph = findpivots(divSrc, 1, LookBackLeft, LookBackRight);

def plFound = if !isNaN(pl) then 1 else 0;
def phFound = if !isNaN(ph) then 1 else 0;

def vlFound = valuewhen(plFound, divSrc, MinLookback, MaxLookback, 1);
def vhFound = valuewhen(phFound, divSrc, MinLookback, MaxLookback, 1);

def plPrice = valuewhen(plFound, l, MinLookback, MaxLookback, 1);
def phPrice = valuewhen(phFound, h, MinLookback, MaxLookback, 1);

#// Regular Bullish
def oscHL = divSrc > vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc < vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
def priceHL = l > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
def priceLH = h < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", CreateColor(156,39,176), yes);
addchartbubble(hiddenBullCond, divSrc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, divSrc, "H", color.DARK_red, yes);

#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);

#----End
 
try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#study("Multiple  Divergence Indicator w/Alerts")
# Created by Sam4Cok@Samer800 - 10/2022 as requested from https://usethinkscript.com memeber
# lower chart
declare lower;
input src = close;        # "RSI Source"
input Length = 14;
input ShowLabel = yes;
input Indicator = {Default RSI, OBV, MACD, STOCH, CCI, MFI};
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input LookBackRight  = 5; # "Pivot Lookback Right"
input LookBackLeft  = 5;  # "Pivot Lookback Left"
input MaxLookback = 60;   # "Max of Lookback Range"
input MinLookback = 5;    # "Min of Lookback Range"
input DivBull = yes;      # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes;      # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"

def na = double.NaN;
addLabel(ShowLabel, Indicator, Color.WHITE);
def zeroLine = Indicator==Indicator.MACD or Indicator==Indicator.CCI;
#mfi(src, length) =>
script mfi {
input src = close;
input length = 14;
    def upper = sum(volume * (if(src-src[1]) <= 0.0 then 0.0 else src), length);
    def lower = sum(volume * (if(src-src[1]) >= 0.0 then 0.0 else src), length);
    def mfi = 100.0 - (100.0 / (1.0 + upper / lower));
  plot return = mfi;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 0;
    def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
    plot return = stoch;
}
def nRSI = RSI(Price = src, length= Length);
def obv = TotalSum(sign((close-close[1])) * volume);
def MACD = macd().Diff;
def stochastic = stoch(close, high, low, Length);
def CCI = CCI(Length=Length);
def mfi = MFI(src,Length);
#----Div-----------
def divSrc;
switch (Indicator) {
case OBV:
    divSrc = OBV;
case MACD:
    divSrc = MACD;
case STOCH:
    divSrc = Stochastic;
case CCI:
    divSrc = CCI;
case MFI:
    divSrc = MFI;
default:
    divSrc = nRSI;
}
plot incdicator = divSrc;
incdicator.SetDefaultColor(Color.ORANGE);
incdicator.SetLineWeight(2);
plot midLine = if isNaN(close) or Indicator==Indicator.OBV then na else if zeroLine then 0 else 50;
midLine.SetDefaultColor(Color.GRAY);


#----Div-----------

def h = high;
def l = low;

script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
  input cond = 0;
  input src = close;
  input MinLookback = 5;
  input MaxLookback = 60;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = MinLookback to MaxLookback + 1 with p=1 while p < n + 1
    do p + ( if p == n then j - n else if cond[j]==yes then 1 else 0 );
  plot price = GetValue(src, offset-1);
}
#_inRange(cond) =>
script _inRange {
    input cond = yes;
    input rangeUpper = 60;
    input rangeLower = 5;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
def pl = findpivots(divSrc,-1, LookBackLeft, LookBackRight);
def ph = findpivots(divSrc, 1, LookBackLeft, LookBackRight);

def plFound = if !isNaN(pl) then 1 else 0;
def phFound = if !isNaN(ph) then 1 else 0;

def vlFound = valuewhen(plFound, divSrc, MinLookback, MaxLookback, 1);
def vhFound = valuewhen(phFound, divSrc, MinLookback, MaxLookback, 1);

def plPrice = valuewhen(plFound, l, MinLookback, MaxLookback, 1);
def phPrice = valuewhen(phFound, h, MinLookback, MaxLookback, 1);

#// Regular Bullish
def oscHL = divSrc > vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc < vlFound and  _inRange(plFound[1],MaxLookback,MinLookback);
def priceHL = l > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and  _inRange(phFound[1],MaxLookback,MinLookback);
def priceLH = h < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", CreateColor(156,39,176), yes);
addchartbubble(hiddenBullCond, divSrc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, divSrc, "H", color.DARK_red, yes);

#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);

#----End
Thank you, I'll check it out.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
384 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