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

Repaints
Is there a way to be able to choose between simple, exp, wilders for the RSI? I know that the regular RSI indicator is using Wilders, but I need exp for this RSIDivergence indicator.
 

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

Hi, i added a new divergence used the elliott wave oscilator (EWO):



#// 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, EWO};
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);
def priceewo = hl2;
def EWO = (Average(priceewo, 5) - Average(priceewo, 35));
#----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;
case EWO:
divSrc = EWO;
}

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
 
Apologies if I am asking too much,

https://usethinkscript.com/threads/rsi-obv-macd-stoch-cci-or-mfi-divergence-for-thinkorswim.13106/
but is it possible to assign a strength value to the divergences by adding weights to each divergence measurement?

That way, I suppose, finding bullish or bearish signals and deciding whether to buy or sell a stock would be much easier. I have less experience as to what weighting value should be put on which divergence measurements, so any help or advice is much appreciated.

Thanks
 
Apologies if I am asking too much,

https://usethinkscript.com/threads/rsi-obv-macd-stoch-cci-or-mfi-divergence-for-thinkorswim.13106/
but is it possible to assign a strength value to the divergences by adding weights to each divergence measurement?

That way, I suppose, finding bullish or bearish signals and deciding whether to buy or sell a stock would be much easier. I have less experience as to what weighting value should be put on which divergence measurements, so any help or advice is much appreciated.

Thanks
The study indicated in your post lets you choose ONE divergence to plot.
No, the script does not provide the ability to look at multiple divergences.
So it is not possible to weigh them or compare them.

There is little benefit to comparing collinear indicators:
https://usethinkscript.com/threads/...nt-to-successful-trading-in-thinkorswim.6114/
 
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
what do the colors really mean... is the purple time to go short? green is time to go long?
 
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 am seeing the same kind of behavior. I have the indicator on, day just chugs along, no bubbles appear. I then pop off to a new stock and return to the prior stock and bam there are bubbles. Plus I can actually see where the divergence is happening. But no bubble.

So seems like an issue with refreshing in real time.
 
I am seeing the same kind of behavior. I have the indicator on, day just chugs along, no bubbles appear. I then pop off to a new stock and return to the prior stock and bam there are bubbles. Plus I can actually see where the divergence is happening. But no bubble.

So seems like an issue with refreshing in real time.
Divergences do not update in real time.
They are repainted on afterward.
If you haven't changed any defaults, they will generally pop up painted on candles that are 5 bars back approx.

read more:
https://usethinkscript.com/threads/...divergence-for-thinkorswim.13106/#post-113367
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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