RSI Overbought/Oversold + Divergence For ThinkOrSwim

Find below. Also you can check this post.
https://usethinkscript.com/threads/rsi-obv-macd-stoch-cci-or-mfi-divergence-for-thinkorswim.13106/

CSS:
#/@seoco
#indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator',
# Converted by Sam4Cok@Samer800 -02/2023

declare lower;
input rsiShowInput          = yes;    # 'Show RSI', group='Show/Hide Settings')
input maShowInput           = no;     # 'Show MA', group='Show/Hide Settings')
input showRSIMAInput        = yes;    # 'Show RSIMA Cloud'
input rsiBandShowInput      = yes;    # 'Show Oversold/Overbought Lines'
input rsiBandExtShowInput   = yes;    # 'Show Oversold/Overbought Extended Lines'
input rsiHighlightShowInput = yes;    # 'Show Oversold/Overbought Highlight Lines'
input DivergenceShowInput   = yes;    # 'Show RSI Divergence Labels'
#//--- RSI Input Settings
input rsiSourceInput  = close;        # 'Source'
input rsiLengthInput  = 14;           # 'RSI Length'
input rsiUpperBandExtInput = 80;      # 'RSI Overbought Extended Line'
input rsiUpperBandInput   = 70;       # 'RSI Overbought Line'
input rsiLowerBandInput   = 30;       # 'RSI Oversold Line'
input rsiLowerBandExtInput = 20;      # 'RSI Oversold Extended Line'
#//--- MA Input Settings
input maTypeInput = {"SMA", "Bollinger Bands", default "EMA", "SMMA (RMA)", "WMA", "VWMA"};    # "MA Type"
input maLengthInput = 14;             # "MA Length"
#//--- Divergence Input Settings
input lbrInput        = 2;            # "Pivot Lookback Right"
input lblInput        = 2;            # "Pivot Lookback Left"
input lbRangeMaxInput = 10;           # "Max of Lookback Range"
input lbRangeMinInput = 2;            # "Min of Lookback Range"
input plotBullInput       = yes;      # "Plot Bullish"
input plotHiddenBullInput = yes;      # "Plot Hidden Bullish"
input plotBearInput       = yes;      # "Plot Bearish"
input plotHiddenBearInput = yes;      # "Plot Hidden Bearish"
#//--- Stoch RSI Settings + Calculation
input showStochRSI = no;              # "Show Stochastic RSI"
input smoothK      = 3;               # "Stochastic K"
input smoothD      = 4;               # "Stochastic D"
input lengthRSI    = 14;              # "Stochastic RSI Lenght"
input lengthStoch  = 14;              # "Stochastic Lenght"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
#--- Color
DefineGlobalColor("Green"     , CreateColor(38,166,154));
DefineGlobalColor("Red"        , CreateColor(239,83,79));
DefineGlobalColor("Gray"       , CreateColor(42,42,42));
#//--- RSI Calculation
def close15 = close(Period = AggregationPeriod.FIFTEEN_MIN);
def close1h = close(Period = AggregationPeriod.HOUR);
def close4h = close(Period = AggregationPeriod.FOUR_HOURS);
def close1d = close(Period = AggregationPeriod.DAY);

def rsi         = RSI(PRICE = rsiSourceInput, LENGTH = rsiLengthInput);

def up15   = WildersAverage(Max((close15 - close15[1]), 0), rsiLengthInput);
def down15 = WildersAverage(-Min((close15 - close15[1]), 0), rsiLengthInput);
def rsi15 = if down15 == 0 then 100 else if up15 == 0 then 0 else 100 - (100 / (1 + up15 / down15));

def up1h   = WildersAverage(Max((close1h - close1h[1]), 0), rsiLengthInput);
def down1h = WildersAverage(-Min((close1h - close1h[1]), 0), rsiLengthInput);
def rsi1h = if down1h == 0 then 100 else if up1h == 0 then 0 else 100 - (100 / (1 + up1h / down1h));

def up4h   = WildersAverage(Max((close4h - close4h[1]), 0), rsiLengthInput);
def down4h = WildersAverage(-Min((close4h - close4h[1]), 0), rsiLengthInput);
def rsi4h = if down4h == 0 then 100 else if up4h == 0 then 0 else 100 - (100 / (1 + up4h / down4h));

def up1d   = WildersAverage(Max((close1d - close1d[1]), 0), rsiLengthInput);
def down1d = WildersAverage(-Min((close1d - close1d[1]), 0), rsiLengthInput);
def rsi1d = if down1d == 0 then 100 else if up1d == 0 then 0 else 100 - (100 / (1 + up1d / down1d));

#//--- MA Calculation
# 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;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 14;
    input type = "EMA";
    def ma =
        if type == "SMA" then SimpleMovingAvg(source, length) else
        if type == "Bollinger Bands" then SimpleMovingAvg(source, length) else
        if type == "EMA" then ExpAverage(source, length) else
        if type == "SMMA (RMA)" then WildersAverage(source, length) else
        if type == "WMA" then WMA(source, length) else
        if type == "VWMA" then SimpleMovingAvg(source * volume, length) / SimpleMovingAvg(volume, length) else source;
    plot return = ma;
}

def rsiMA = ma(rsi, maLengthInput, maTypeInput);
def rsi1  = RSI(Price=close15, LENGTH=lengthRSI);
def k     = SimpleMovingAvg(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK);
def d     = SimpleMovingAvg(k, smoothD);
def osc = rsi;
def rsiColor = rsi >= rsiMA;

#//--- Plot Lines
plot maPlot     = if maShowInput then rsiMA else na;
maPlot.SetDefaultColor(Color.YELLOW);
def rsiMAPlot  = if showRSIMAInput then rsiMA else na;
plot rsiPlot    = if rsiShowInput then rsi else na;
rsiPlot.AssignValueColor(if rsiColor then GlobalColor("Green") else GlobalColor("Red"));
AddCloud(rsiPlot, rsiMAPlot, GlobalColor("Green"), GlobalColor("Red"));

plot StochK = if showStochRSI then k else na;
StochK.SetDefaultColor(Color.WHITE);
plot StockD = if showStochRSI then d else na;
StockD.SetDefaultColor(Color.MAGENTA);


plot bbUpperBand = if isBB then rsiMA + stdev(rsi, maLengthInput) * 2 else na;    # "Upper Bollinger Band"
bbUpperBand.SetDefaultColor(Color.GRAY);
plot bbLowerBand = if isBB then rsiMA - stdev(rsi, maLengthInput) * 2 else na;    # "Lower Bollinger Band"
bbLowerBand.SetDefaultColor(Color.GRAY);

AddCloud(bbUpperBand, bbLowerBand, GlobalColor("Gray"));

plot midLine = 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot rsiUpBandExt = if rsiBandExtShowInput then rsiUpperBandExtInput else na;
rsiUpBandExt.SetStyle(Curve.SHORT_DASH);
rsiUpBandExt.SetDefaultColor(Color.RED);
plot rsiUpBand = if rsiBandShowInput then rsiUpperBandInput else na;
rsiUpBand.SetStyle(Curve.SHORT_DASH);
rsiUpBand.SetDefaultColor(Color.DARK_RED);
plot rsiLoBand = if rsiBandShowInput then rsiLowerBandInput else na;
rsiLoBand.SetStyle(Curve.SHORT_DASH);
rsiLoBand.SetDefaultColor(Color.DARK_GREEN);
plot rsiLoBandExt = if rsiBandExtShowInput then rsiLowerBandExtInput else na;
rsiLoBandExt.SetStyle(Curve.SHORT_DASH);
rsiLoBandExt.SetDefaultColor(Color.GREEN);

AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandExtInput then pos else na else na, neg, Color.GREEN);
AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandInput then if rsi < rsiUpperBandExtInput then pos else na else na else na, neg, Color.DARK_GREEN);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandExtInput then pos else na else na, neg, Color.RED);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandInput then if rsi > rsiLowerBandExtInput then pos else na else na else na, neg, Color.DARK_RED);

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 = 2;
  input MaxLookback = 10;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to MinLookback + 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 = 10;
    input rangeLower = 2;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
#//--- Plot Divergence
#// Regular Bullish
#// Osc: Higher Low
def pl = findpivots(osc,-1, lblInput, lbrInput);
def ph = findpivots(osc, 1, lblInput, lbrInput);

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

def vlFound = valuewhen(plFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);
def vhFound = valuewhen(phFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);

def plPrice = valuewhen(plFound, low, lbRangeMinInput, lbRangeMaxInput, 1);
def phPrice = valuewhen(phFound, high, lbRangeMinInput, lbRangeMaxInput, 1);

#// Regular Bullish
def oscHL = osc > vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLL = low < plPrice;
def bullCond = DivergenceShowInput and plotBullInput and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = osc < vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHL = low > plPrice;
def hiddenBullCond = DivergenceShowInput and plotHiddenBullInput and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = osc < vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHH = high > phPrice;
def bearCond = DivergenceShowInput and plotBearInput and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = osc > vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLH = high < phPrice;
def hiddenBearCond = DivergenceShowInput and plotHiddenBearInput and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, osc, "R", color.GREEN, no);
addchartbubble(bearCond, osc, "R", Color.RED, yes);
addchartbubble(hiddenBullCond, osc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, osc, "H", color.DARK_red, yes);

#--- Labels
AddLabel(rsiShowInput, "RSI Current: " + ROUND(rsi,2), if rsi<50 then Color.RED else Color.GREEN);
AddLabel(rsiShowInput, "RSI 15m: " + ROUND(rsi15,2), if rsi15<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1h: " + ROUND(rsi1h,2), if rsi1h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 4h: " + ROUND(rsi4h,2), if rsi4h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1d: " + ROUND(rsi1d,2), if rsi1d<50 then Color.PINK else Color.LIGHT_GREEN);

#--- END CODE
 
find below. Also you can check this post.
https://usethinkscript.com/threads/rsi-obv-macd-stoch-cci-or-mfi-divergence-for-thinkorswim.13106/




CSS:
#/@seoco
#indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator',
# Converted by Sam4Cok@Samer800 -02/2023

declare lower;
input rsiShowInput          = yes;    # 'Show RSI', group='Show/Hide Settings')
input maShowInput           = no;     # 'Show MA', group='Show/Hide Settings')
input showRSIMAInput        = yes;    # 'Show RSIMA Cloud'
input rsiBandShowInput      = yes;    # 'Show Oversold/Overbought Lines'
input rsiBandExtShowInput   = yes;    # 'Show Oversold/Overbought Extended Lines'
input rsiHighlightShowInput = yes;    # 'Show Oversold/Overbought Highlight Lines'
input DivergenceShowInput   = yes;    # 'Show RSI Divergence Labels'
#//--- RSI Input Settings
input rsiSourceInput  = close;        # 'Source'
input rsiLengthInput  = 14;           # 'RSI Length'
input rsiUpperBandExtInput = 80;      # 'RSI Overbought Extended Line'
input rsiUpperBandInput   = 70;       # 'RSI Overbought Line'
input rsiLowerBandInput   = 30;       # 'RSI Oversold Line'
input rsiLowerBandExtInput = 20;      # 'RSI Oversold Extended Line'
#//--- MA Input Settings
input maTypeInput = {"SMA", "Bollinger Bands", default "EMA", "SMMA (RMA)", "WMA", "VWMA"};    # "MA Type"
input maLengthInput = 14;             # "MA Length"
#//--- Divergence Input Settings
input lbrInput        = 2;            # "Pivot Lookback Right"
input lblInput        = 2;            # "Pivot Lookback Left"
input lbRangeMaxInput = 10;           # "Max of Lookback Range"
input lbRangeMinInput = 2;            # "Min of Lookback Range"
input plotBullInput       = yes;      # "Plot Bullish"
input plotHiddenBullInput = yes;      # "Plot Hidden Bullish"
input plotBearInput       = yes;      # "Plot Bearish"
input plotHiddenBearInput = yes;      # "Plot Hidden Bearish"
#//--- Stoch RSI Settings + Calculation
input showStochRSI = no;              # "Show Stochastic RSI"
input smoothK      = 3;               # "Stochastic K"
input smoothD      = 4;               # "Stochastic D"
input lengthRSI    = 14;              # "Stochastic RSI Lenght"
input lengthStoch  = 14;              # "Stochastic Lenght"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
#--- Color
DefineGlobalColor("Green"     , CreateColor(38,166,154));
DefineGlobalColor("Red"        , CreateColor(239,83,79));
DefineGlobalColor("Gray"       , CreateColor(42,42,42));
#//--- RSI Calculation
def close15 = close(Period = AggregationPeriod.FIFTEEN_MIN);
def close1h = close(Period = AggregationPeriod.HOUR);
def close4h = close(Period = AggregationPeriod.FOUR_HOURS);
def close1d = close(Period = AggregationPeriod.DAY);

def rsi         = RSI(PRICE = rsiSourceInput, LENGTH = rsiLengthInput);

def up15   = WildersAverage(Max((close15 - close15[1]), 0), rsiLengthInput);
def down15 = WildersAverage(-Min((close15 - close15[1]), 0), rsiLengthInput);
def rsi15 = if down15 == 0 then 100 else if up15 == 0 then 0 else 100 - (100 / (1 + up15 / down15));

def up1h   = WildersAverage(Max((close1h - close1h[1]), 0), rsiLengthInput);
def down1h = WildersAverage(-Min((close1h - close1h[1]), 0), rsiLengthInput);
def rsi1h = if down1h == 0 then 100 else if up1h == 0 then 0 else 100 - (100 / (1 + up1h / down1h));

def up4h   = WildersAverage(Max((close4h - close4h[1]), 0), rsiLengthInput);
def down4h = WildersAverage(-Min((close4h - close4h[1]), 0), rsiLengthInput);
def rsi4h = if down4h == 0 then 100 else if up4h == 0 then 0 else 100 - (100 / (1 + up4h / down4h));

def up1d   = WildersAverage(Max((close1d - close1d[1]), 0), rsiLengthInput);
def down1d = WildersAverage(-Min((close1d - close1d[1]), 0), rsiLengthInput);
def rsi1d = if down1d == 0 then 100 else if up1d == 0 then 0 else 100 - (100 / (1 + up1d / down1d));

#//--- MA Calculation
# 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;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 14;
    input type = "EMA";
    def ma =
        if type == "SMA" then SimpleMovingAvg(source, length) else
        if type == "Bollinger Bands" then SimpleMovingAvg(source, length) else
        if type == "EMA" then ExpAverage(source, length) else
        if type == "SMMA (RMA)" then WildersAverage(source, length) else
        if type == "WMA" then WMA(source, length) else
        if type == "VWMA" then SimpleMovingAvg(source * volume, length) / SimpleMovingAvg(volume, length) else source;
    plot return = ma;
}

def rsiMA = ma(rsi, maLengthInput, maTypeInput);
def rsi1  = RSI(Price=close15, LENGTH=lengthRSI);
def k     = SimpleMovingAvg(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK);
def d     = SimpleMovingAvg(k, smoothD);
def osc = rsi;
def rsiColor = rsi >= rsiMA;

#//--- Plot Lines
plot maPlot     = if maShowInput then rsiMA else na;
maPlot.SetDefaultColor(Color.YELLOW);
def rsiMAPlot  = if showRSIMAInput then rsiMA else na;
plot rsiPlot    = if rsiShowInput then rsi else na;
rsiPlot.AssignValueColor(if rsiColor then GlobalColor("Green") else GlobalColor("Red"));
AddCloud(rsiPlot, rsiMAPlot, GlobalColor("Green"), GlobalColor("Red"));

plot StochK = if showStochRSI then k else na;
StochK.SetDefaultColor(Color.WHITE);
plot StockD = if showStochRSI then d else na;
StockD.SetDefaultColor(Color.MAGENTA);


plot bbUpperBand = if isBB then rsiMA + stdev(rsi, maLengthInput) * 2 else na;    # "Upper Bollinger Band"
bbUpperBand.SetDefaultColor(Color.GRAY);
plot bbLowerBand = if isBB then rsiMA - stdev(rsi, maLengthInput) * 2 else na;    # "Lower Bollinger Band"
bbLowerBand.SetDefaultColor(Color.GRAY);

AddCloud(bbUpperBand, bbLowerBand, GlobalColor("Gray"));

plot midLine = 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot rsiUpBandExt = if rsiBandExtShowInput then rsiUpperBandExtInput else na;
rsiUpBandExt.SetStyle(Curve.SHORT_DASH);
rsiUpBandExt.SetDefaultColor(Color.RED);
plot rsiUpBand = if rsiBandShowInput then rsiUpperBandInput else na;
rsiUpBand.SetStyle(Curve.SHORT_DASH);
rsiUpBand.SetDefaultColor(Color.DARK_RED);
plot rsiLoBand = if rsiBandShowInput then rsiLowerBandInput else na;
rsiLoBand.SetStyle(Curve.SHORT_DASH);
rsiLoBand.SetDefaultColor(Color.DARK_GREEN);
plot rsiLoBandExt = if rsiBandExtShowInput then rsiLowerBandExtInput else na;
rsiLoBandExt.SetStyle(Curve.SHORT_DASH);
rsiLoBandExt.SetDefaultColor(Color.GREEN);

AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandExtInput then pos else na else na, neg, Color.GREEN);
AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandInput then if rsi < rsiUpperBandExtInput then pos else na else na else na, neg, Color.DARK_GREEN);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandExtInput then pos else na else na, neg, Color.RED);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandInput then if rsi > rsiLowerBandExtInput then pos else na else na else na, neg, Color.DARK_RED);

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 = 2;
  input MaxLookback = 10;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to MinLookback + 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 = 10;
    input rangeLower = 2;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
#//--- Plot Divergence
#// Regular Bullish
#// Osc: Higher Low
def pl = findpivots(osc,-1, lblInput, lbrInput);
def ph = findpivots(osc, 1, lblInput, lbrInput);

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

def vlFound = valuewhen(plFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);
def vhFound = valuewhen(phFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);

def plPrice = valuewhen(plFound, low, lbRangeMinInput, lbRangeMaxInput, 1);
def phPrice = valuewhen(phFound, high, lbRangeMinInput, lbRangeMaxInput, 1);

#// Regular Bullish
def oscHL = osc > vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLL = low < plPrice;
def bullCond = DivergenceShowInput and plotBullInput and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = osc < vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHL = low > plPrice;
def hiddenBullCond = DivergenceShowInput and plotHiddenBullInput and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = osc < vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHH = high > phPrice;
def bearCond = DivergenceShowInput and plotBearInput and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = osc > vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLH = high < phPrice;
def hiddenBearCond = DivergenceShowInput and plotHiddenBearInput and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, osc, "R", color.GREEN, no);
addchartbubble(bearCond, osc, "R", Color.RED, yes);
addchartbubble(hiddenBullCond, osc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, osc, "H", color.DARK_red, yes);

#--- Labels
AddLabel(rsiShowInput, "RSI Current: " + ROUND(rsi,2), if rsi<50 then Color.RED else Color.GREEN);
AddLabel(rsiShowInput, "RSI 15m: " + ROUND(rsi15,2), if rsi15<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1h: " + ROUND(rsi1h,2), if rsi1h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 4h: " + ROUND(rsi4h,2), if rsi4h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1d: " + ROUND(rsi1d,2), if rsi1d<50 then Color.PINK else Color.LIGHT_GREEN);

#--- END CODE
I can't thank you enough @samer800, appreciate your help!
 
do u have any plan to trade using this ? @delasol79
Yes, but looking at it on TOS, it's not as graphically appealing as it is on TV, so I may stick with the TV one. This indicator is okay to point out divergence but it is definitely not 100%, but just looking at the RSI, it gives me very good idea when to enter and exit and if used with a trend indicator, it's actually near accurate.
 
Last edited:
It is possible to sent screen shot? When to enter and exit ?
That's like a whole other topic outside of this forum and it really depends on how you trade. Are you scalping, swinging, daytrading, momentum trading, reversal trading?
Some people like to trade at the 50 line, some like to trade the 30/70 lines.
I like to trade the reversal, so I trade 30/70 lines. Anything below the 30 line would be an entry or exit and same with above the 70 line.
But you have to be careful and understand the momentum and trend. Even if it is below or above the 30/70 lines, it doesn't mean it will reverse. There is a high chance, but definitely NOT guaranteed.
With that said, you want to look for the crossover of the lines or the color change from green to red or the other way around after it has reached the overbought or oversold territory.
Again, you want to use other indicators for confirmation.
If you are new to trading, perhaps you should watch some youtube videos about entry and exit and which indicators work for you and your trading style.
 
find below. Also you can check this post.
https://usethinkscript.com/threads/rsi-obv-macd-stoch-cci-or-mfi-divergence-for-thinkorswim.13106/




CSS:
#/@seoco
#indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator',
# Converted by Sam4Cok@Samer800 -02/2023

declare lower;
input rsiShowInput          = yes;    # 'Show RSI', group='Show/Hide Settings')
input maShowInput           = no;     # 'Show MA', group='Show/Hide Settings')
input showRSIMAInput        = yes;    # 'Show RSIMA Cloud'
input rsiBandShowInput      = yes;    # 'Show Oversold/Overbought Lines'
input rsiBandExtShowInput   = yes;    # 'Show Oversold/Overbought Extended Lines'
input rsiHighlightShowInput = yes;    # 'Show Oversold/Overbought Highlight Lines'
input DivergenceShowInput   = yes;    # 'Show RSI Divergence Labels'
#//--- RSI Input Settings
input rsiSourceInput  = close;        # 'Source'
input rsiLengthInput  = 14;           # 'RSI Length'
input rsiUpperBandExtInput = 80;      # 'RSI Overbought Extended Line'
input rsiUpperBandInput   = 70;       # 'RSI Overbought Line'
input rsiLowerBandInput   = 30;       # 'RSI Oversold Line'
input rsiLowerBandExtInput = 20;      # 'RSI Oversold Extended Line'
#//--- MA Input Settings
input maTypeInput = {"SMA", "Bollinger Bands", default "EMA", "SMMA (RMA)", "WMA", "VWMA"};    # "MA Type"
input maLengthInput = 14;             # "MA Length"
#//--- Divergence Input Settings
input lbrInput        = 2;            # "Pivot Lookback Right"
input lblInput        = 2;            # "Pivot Lookback Left"
input lbRangeMaxInput = 10;           # "Max of Lookback Range"
input lbRangeMinInput = 2;            # "Min of Lookback Range"
input plotBullInput       = yes;      # "Plot Bullish"
input plotHiddenBullInput = yes;      # "Plot Hidden Bullish"
input plotBearInput       = yes;      # "Plot Bearish"
input plotHiddenBearInput = yes;      # "Plot Hidden Bearish"
#//--- Stoch RSI Settings + Calculation
input showStochRSI = no;              # "Show Stochastic RSI"
input smoothK      = 3;               # "Stochastic K"
input smoothD      = 4;               # "Stochastic D"
input lengthRSI    = 14;              # "Stochastic RSI Lenght"
input lengthStoch  = 14;              # "Stochastic Lenght"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
#--- Color
DefineGlobalColor("Green"     , CreateColor(38,166,154));
DefineGlobalColor("Red"        , CreateColor(239,83,79));
DefineGlobalColor("Gray"       , CreateColor(42,42,42));
#//--- RSI Calculation
def close15 = close(Period = AggregationPeriod.FIFTEEN_MIN);
def close1h = close(Period = AggregationPeriod.HOUR);
def close4h = close(Period = AggregationPeriod.FOUR_HOURS);
def close1d = close(Period = AggregationPeriod.DAY);

def rsi         = RSI(PRICE = rsiSourceInput, LENGTH = rsiLengthInput);

def up15   = WildersAverage(Max((close15 - close15[1]), 0), rsiLengthInput);
def down15 = WildersAverage(-Min((close15 - close15[1]), 0), rsiLengthInput);
def rsi15 = if down15 == 0 then 100 else if up15 == 0 then 0 else 100 - (100 / (1 + up15 / down15));

def up1h   = WildersAverage(Max((close1h - close1h[1]), 0), rsiLengthInput);
def down1h = WildersAverage(-Min((close1h - close1h[1]), 0), rsiLengthInput);
def rsi1h = if down1h == 0 then 100 else if up1h == 0 then 0 else 100 - (100 / (1 + up1h / down1h));

def up4h   = WildersAverage(Max((close4h - close4h[1]), 0), rsiLengthInput);
def down4h = WildersAverage(-Min((close4h - close4h[1]), 0), rsiLengthInput);
def rsi4h = if down4h == 0 then 100 else if up4h == 0 then 0 else 100 - (100 / (1 + up4h / down4h));

def up1d   = WildersAverage(Max((close1d - close1d[1]), 0), rsiLengthInput);
def down1d = WildersAverage(-Min((close1d - close1d[1]), 0), rsiLengthInput);
def rsi1d = if down1d == 0 then 100 else if up1d == 0 then 0 else 100 - (100 / (1 + up1d / down1d));

#//--- MA Calculation
# 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;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 14;
    input type = "EMA";
    def ma =
        if type == "SMA" then SimpleMovingAvg(source, length) else
        if type == "Bollinger Bands" then SimpleMovingAvg(source, length) else
        if type == "EMA" then ExpAverage(source, length) else
        if type == "SMMA (RMA)" then WildersAverage(source, length) else
        if type == "WMA" then WMA(source, length) else
        if type == "VWMA" then SimpleMovingAvg(source * volume, length) / SimpleMovingAvg(volume, length) else source;
    plot return = ma;
}

def rsiMA = ma(rsi, maLengthInput, maTypeInput);
def rsi1  = RSI(Price=close15, LENGTH=lengthRSI);
def k     = SimpleMovingAvg(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK);
def d     = SimpleMovingAvg(k, smoothD);
def osc = rsi;
def rsiColor = rsi >= rsiMA;

#//--- Plot Lines
plot maPlot     = if maShowInput then rsiMA else na;
maPlot.SetDefaultColor(Color.YELLOW);
def rsiMAPlot  = if showRSIMAInput then rsiMA else na;
plot rsiPlot    = if rsiShowInput then rsi else na;
rsiPlot.AssignValueColor(if rsiColor then GlobalColor("Green") else GlobalColor("Red"));
AddCloud(rsiPlot, rsiMAPlot, GlobalColor("Green"), GlobalColor("Red"));

plot StochK = if showStochRSI then k else na;
StochK.SetDefaultColor(Color.WHITE);
plot StockD = if showStochRSI then d else na;
StockD.SetDefaultColor(Color.MAGENTA);


plot bbUpperBand = if isBB then rsiMA + stdev(rsi, maLengthInput) * 2 else na;    # "Upper Bollinger Band"
bbUpperBand.SetDefaultColor(Color.GRAY);
plot bbLowerBand = if isBB then rsiMA - stdev(rsi, maLengthInput) * 2 else na;    # "Lower Bollinger Band"
bbLowerBand.SetDefaultColor(Color.GRAY);

AddCloud(bbUpperBand, bbLowerBand, GlobalColor("Gray"));

plot midLine = 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot rsiUpBandExt = if rsiBandExtShowInput then rsiUpperBandExtInput else na;
rsiUpBandExt.SetStyle(Curve.SHORT_DASH);
rsiUpBandExt.SetDefaultColor(Color.RED);
plot rsiUpBand = if rsiBandShowInput then rsiUpperBandInput else na;
rsiUpBand.SetStyle(Curve.SHORT_DASH);
rsiUpBand.SetDefaultColor(Color.DARK_RED);
plot rsiLoBand = if rsiBandShowInput then rsiLowerBandInput else na;
rsiLoBand.SetStyle(Curve.SHORT_DASH);
rsiLoBand.SetDefaultColor(Color.DARK_GREEN);
plot rsiLoBandExt = if rsiBandExtShowInput then rsiLowerBandExtInput else na;
rsiLoBandExt.SetStyle(Curve.SHORT_DASH);
rsiLoBandExt.SetDefaultColor(Color.GREEN);

AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandExtInput then pos else na else na, neg, Color.GREEN);
AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandInput then if rsi < rsiUpperBandExtInput then pos else na else na else na, neg, Color.DARK_GREEN);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandExtInput then pos else na else na, neg, Color.RED);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandInput then if rsi > rsiLowerBandExtInput then pos else na else na else na, neg, Color.DARK_RED);

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 = 2;
  input MaxLookback = 10;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to MinLookback + 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 = 10;
    input rangeLower = 2;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
#//--- Plot Divergence
#// Regular Bullish
#// Osc: Higher Low
def pl = findpivots(osc,-1, lblInput, lbrInput);
def ph = findpivots(osc, 1, lblInput, lbrInput);

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

def vlFound = valuewhen(plFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);
def vhFound = valuewhen(phFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);

def plPrice = valuewhen(plFound, low, lbRangeMinInput, lbRangeMaxInput, 1);
def phPrice = valuewhen(phFound, high, lbRangeMinInput, lbRangeMaxInput, 1);

#// Regular Bullish
def oscHL = osc > vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLL = low < plPrice;
def bullCond = DivergenceShowInput and plotBullInput and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = osc < vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHL = low > plPrice;
def hiddenBullCond = DivergenceShowInput and plotHiddenBullInput and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = osc < vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHH = high > phPrice;
def bearCond = DivergenceShowInput and plotBearInput and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = osc > vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLH = high < phPrice;
def hiddenBearCond = DivergenceShowInput and plotHiddenBearInput and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, osc, "R", color.GREEN, no);
addchartbubble(bearCond, osc, "R", Color.RED, yes);
addchartbubble(hiddenBullCond, osc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, osc, "H", color.DARK_red, yes);

#--- Labels
AddLabel(rsiShowInput, "RSI Current: " + ROUND(rsi,2), if rsi<50 then Color.RED else Color.GREEN);
AddLabel(rsiShowInput, "RSI 15m: " + ROUND(rsi15,2), if rsi15<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1h: " + ROUND(rsi1h,2), if rsi1h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 4h: " + ROUND(rsi4h,2), if rsi4h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1d: " + ROUND(rsi1d,2), if rsi1d<50 then Color.PINK else Color.LIGHT_GREEN);

#--- END CODE
@samer800, could you look at this code and tell me if it can be altered to work on 1m, to 5m TF? The issue I am getting is with the STOCH plot lines. They seem to be set for higher TF's. I am not sure but when I use a 1m TF the STOCH lines are "jacked up" The other line appear to work fine.
Not a big issue. I can just use a separate stoch indicator.
 
Last edited:
@samer800, could you look at this code and tell me if it can be altered to work on 1m, to 5m TF? The issue I am getting is with the STOCH plot lines. They seem to be set for higher TF's. I am not sure but when I use a 1m TF the STOCH lines are "jacked up" The other line appear to work fine.
Not a big issue. I can just use a separate stoch indicator.
yes Stoch set to higher TF. add the below at the end of the script to get same chart Stoch

CSS:
input ShowCurrentTfStoch = no;
plot kTf     = if !ShowCurrentTfStoch then na else SimpleMovingAvg(stoch(rsi, rsi, rsi, lengthStoch), smoothK);
plot dTf     = if !ShowCurrentTfStoch then na else SimpleMovingAvg(kTf, smoothD);
 
yes Stoch set to higher TF. add the below at the end of the script to get same chart Stoch

CSS:
input ShowCurrentTfStoch = no;
plot kTf     = if !ShowCurrentTfStoch then na else SimpleMovingAvg(stoch(rsi, rsi, rsi, lengthStoch), smoothK);
plot dTf     = if !ShowCurrentTfStoch then na else SimpleMovingAvg(kTf, smoothD);

Hey @samer800, Im having the opposite problem of it not plotting properly on any time frame above 15min. is there something I'm missing or is there a fix?


gZ0D2SG.jpg
 
Last edited:
Hey samer800, Im having the opposite problem of it not plotting properly on any time frame above 15min. is there something I'm missing or is there a fix?

A common error when using MTF indicators is trying to use a time frame that is lower than the chart you are posting it on.

This indicator defaults to using an aggregation of fifteen minutes.
This requires that this indicator ONLY be utilized on timeframes less than fifteen minutes.

On the TOS platform, you can display data from a higher timeframe onto a lower timeframe but not the other way around.
https://tlc.thinkorswim.com/center/...hapter-11---Referencing-Secondary-Aggregation

If using on higher timeframes change the aggregations in the chart input settings to be no less than the aggregation of your char.t
 
A common error when using MTF indicators is trying to use a time frame that is lower than the chart you are posting it on.

This indicator defaults to using an aggregation of fifteen minutes.
This requires that this indicator ONLY be utilized on timeframes less than fifteen minutes.

On the TOS platform, you can display data from a higher timeframe onto a lower timeframe but not the other way around.
https://tlc.thinkorswim.com/center/...hapter-11---Referencing-Secondary-Aggregation

If using on higher timeframes change the aggregations in the chart input settings to be no less than the aggregation of your char.t
Thank You! @MerryDay
 
the only option I have for you is to change the aggregation period manually if you decided to go to higher time frame. use below code and make sure that the lowers aggregation period to be same current chart or more.

CSS:
#/@seoco
#indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator',
# Converted by Sam4Cok@Samer800 -02/2023

declare lower;
input rsiAgg1    = AggregationPeriod.FIFTEEN_MIN;
input rsiAgg2    = AggregationPeriod.HOUR;
input rsiAgg3    = AggregationPeriod.FOUR_HOURS;
input rsiAgg4    = AggregationPeriod.DAY;
input ShowCurrenttimeStoch = yes;
input rsiShowInput          = yes;    # 'Show RSI', group='Show/Hide Settings')
input maShowInput           = no;     # 'Show MA', group='Show/Hide Settings')
input showRSIMAInput        = yes;    # 'Show RSIMA Cloud'
input rsiBandShowInput      = yes;    # 'Show Oversold/Overbought Lines'
input rsiBandExtShowInput   = yes;    # 'Show Oversold/Overbought Extended Lines'
input rsiHighlightShowInput = yes;    # 'Show Oversold/Overbought Highlight Lines'
input DivergenceShowInput   = yes;    # 'Show RSI Divergence Labels'
#//--- RSI Input Settings
input rsiSourceInput  = close;        # 'Source'
input rsiLengthInput  = 14;           # 'RSI Length'
input rsiUpperBandExtInput = 80;      # 'RSI Overbought Extended Line'
input rsiUpperBandInput   = 70;       # 'RSI Overbought Line'
input rsiLowerBandInput   = 30;       # 'RSI Oversold Line'
input rsiLowerBandExtInput = 20;      # 'RSI Oversold Extended Line'
#//--- MA Input Settings
input maTypeInput = {"SMA", "Bollinger Bands", default "EMA", "SMMA (RMA)", "WMA", "VWMA"};    # "MA Type"
input maLengthInput = 14;             # "MA Length"
#//--- Divergence Input Settings
input lbrInput        = 2;            # "Pivot Lookback Right"
input lblInput        = 2;            # "Pivot Lookback Left"
input lbRangeMaxInput = 10;           # "Max of Lookback Range"
input lbRangeMinInput = 2;            # "Min of Lookback Range"
input plotBullInput       = yes;      # "Plot Bullish"
input plotHiddenBullInput = yes;      # "Plot Hidden Bullish"
input plotBearInput       = yes;      # "Plot Bearish"
input plotHiddenBearInput = yes;      # "Plot Hidden Bearish"
#//--- Stoch RSI Settings + Calculation
input showStochRSI = no;              # "Show Stochastic RSI"
input smoothK      = 3;               # "Stochastic K"
input smoothD      = 4;               # "Stochastic D"
input lengthRSI    = 14;              # "Stochastic RSI Lenght"
input lengthStoch  = 14;              # "Stochastic Lenght"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
#--- Color
DefineGlobalColor("Green"     , CreateColor(38,166,154));
DefineGlobalColor("Red"        , CreateColor(239,83,79));
DefineGlobalColor("Gray"       , CreateColor(42,42,42));
#//--- RSI Calculation
def close15 = close(Period = rsiAgg1);
def close1h = close(Period = rsiAgg2);
def close4h = close(Period = rsiAgg3);
def close1d = close(Period = rsiAgg4);

def rsi         = RSI(PRICE = rsiSourceInput, LENGTH = rsiLengthInput);

def up15   = WildersAverage(Max((close15 - close15[1]), 0), rsiLengthInput);
def down15 = WildersAverage(-Min((close15 - close15[1]), 0), rsiLengthInput);
def rsi15 = if down15 == 0 then 100 else if up15 == 0 then 0 else 100 - (100 / (1 + up15 / down15));

def up1h   = WildersAverage(Max((close1h - close1h[1]), 0), rsiLengthInput);
def down1h = WildersAverage(-Min((close1h - close1h[1]), 0), rsiLengthInput);
def rsi1h = if down1h == 0 then 100 else if up1h == 0 then 0 else 100 - (100 / (1 + up1h / down1h));

def up4h   = WildersAverage(Max((close4h - close4h[1]), 0), rsiLengthInput);
def down4h = WildersAverage(-Min((close4h - close4h[1]), 0), rsiLengthInput);
def rsi4h = if down4h == 0 then 100 else if up4h == 0 then 0 else 100 - (100 / (1 + up4h / down4h));

def up1d   = WildersAverage(Max((close1d - close1d[1]), 0), rsiLengthInput);
def down1d = WildersAverage(-Min((close1d - close1d[1]), 0), rsiLengthInput);
def rsi1d = if down1d == 0 then 100 else if up1d == 0 then 0 else 100 - (100 / (1 + up1d / down1d));

#//--- MA Calculation
# 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;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 14;
    input type = "EMA";
    def ma =
        if type == "SMA" then SimpleMovingAvg(source, length) else
        if type == "Bollinger Bands" then SimpleMovingAvg(source, length) else
        if type == "EMA" then ExpAverage(source, length) else
        if type == "SMMA (RMA)" then WildersAverage(source, length) else
        if type == "WMA" then WMA(source, length) else
        if type == "VWMA" then SimpleMovingAvg(source * volume, length) / SimpleMovingAvg(volume, length) else source;
    plot return = ma;
}

def rsiMA = ma(rsi, maLengthInput, maTypeInput);
def rsi1  = RSI(Price=close15, LENGTH=lengthRSI);
def k     = SimpleMovingAvg(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK);
def d     = SimpleMovingAvg(k, smoothD);
def osc = rsi;
def rsiColor = rsi >= rsiMA;

plot kTf     = if !ShowCurrenttimeStoch then na else SimpleMovingAvg(stoch(rsi, rsi, rsi, lengthStoch), smoothK);
plot dTf     = if !ShowCurrenttimeStoch then na else SimpleMovingAvg(kTf, smoothD);
kTf.SetDefaultColor(Color.CYAN);
dTf.SetDefaultColor(Color.YELLOW);
#//--- Plot Lines
plot maPlot     = if maShowInput then rsiMA else na;
maPlot.SetDefaultColor(Color.YELLOW);
def rsiMAPlot  = if showRSIMAInput then rsiMA else na;
plot rsiPlot    = if rsiShowInput then rsi else na;
rsiPlot.AssignValueColor(if rsiColor then GlobalColor("Green") else GlobalColor("Red"));
AddCloud(rsiPlot, rsiMAPlot, GlobalColor("Green"), GlobalColor("Red"));

plot StochK = if showStochRSI then k else na;
StochK.SetDefaultColor(Color.WHITE);
plot StockD = if showStochRSI then d else na;
StockD.SetDefaultColor(Color.MAGENTA);


plot bbUpperBand = if isBB then rsiMA + stdev(rsi, maLengthInput) * 2 else na;    # "Upper Bollinger Band"
bbUpperBand.SetDefaultColor(Color.GRAY);
plot bbLowerBand = if isBB then rsiMA - stdev(rsi, maLengthInput) * 2 else na;    # "Lower Bollinger Band"
bbLowerBand.SetDefaultColor(Color.GRAY);

AddCloud(bbUpperBand, bbLowerBand, GlobalColor("Gray"));

plot midLine = 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot rsiUpBandExt = if rsiBandExtShowInput then rsiUpperBandExtInput else na;
rsiUpBandExt.SetStyle(Curve.SHORT_DASH);
rsiUpBandExt.SetDefaultColor(Color.RED);
plot rsiUpBand = if rsiBandShowInput then rsiUpperBandInput else na;
rsiUpBand.SetStyle(Curve.SHORT_DASH);
rsiUpBand.SetDefaultColor(Color.DARK_RED);
plot rsiLoBand = if rsiBandShowInput then rsiLowerBandInput else na;
rsiLoBand.SetStyle(Curve.SHORT_DASH);
rsiLoBand.SetDefaultColor(Color.DARK_GREEN);
plot rsiLoBandExt = if rsiBandExtShowInput then rsiLowerBandExtInput else na;
rsiLoBandExt.SetStyle(Curve.SHORT_DASH);
rsiLoBandExt.SetDefaultColor(Color.GREEN);

AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandExtInput then pos else na else na, neg, Color.GREEN);
AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandInput then if rsi < rsiUpperBandExtInput then pos else na else na else na, neg, Color.DARK_GREEN);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandExtInput then pos else na else na, neg, Color.RED);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandInput then if rsi > rsiLowerBandExtInput then pos else na else na else na, neg, Color.DARK_RED);

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 = 2;
  input MaxLookback = 10;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to MinLookback + 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 = 10;
    input rangeLower = 2;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
#//--- Plot Divergence
#// Regular Bullish
#// Osc: Higher Low
def pl = findpivots(osc,-1, lblInput, lbrInput);
def ph = findpivots(osc, 1, lblInput, lbrInput);

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

def vlFound = valuewhen(plFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);
def vhFound = valuewhen(phFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);

def plPrice = valuewhen(plFound, low, lbRangeMinInput, lbRangeMaxInput, 1);
def phPrice = valuewhen(phFound, high, lbRangeMinInput, lbRangeMaxInput, 1);

#// Regular Bullish
def oscHL = osc > vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLL = low < plPrice;
def bullCond = DivergenceShowInput and plotBullInput and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = osc < vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHL = low > plPrice;
def hiddenBullCond = DivergenceShowInput and plotHiddenBullInput and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = osc < vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHH = high > phPrice;
def bearCond = DivergenceShowInput and plotBearInput and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = osc > vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLH = high < phPrice;
def hiddenBearCond = DivergenceShowInput and plotHiddenBearInput and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, osc, "R", color.GREEN, no);
addchartbubble(bearCond, osc, "R", Color.RED, yes);
addchartbubble(hiddenBullCond, osc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, osc, "H", color.DARK_red, yes);

#--- Labels
AddLabel(rsiShowInput, "RSI Current: " + ROUND(rsi,2), if rsi<50 then Color.RED else Color.GREEN);
AddLabel(rsiShowInput, "RSI 15m: " + ROUND(rsi15,2), if rsi15<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1h: " + ROUND(rsi1h,2), if rsi1h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 4h: " + ROUND(rsi4h,2), if rsi4h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1d: " + ROUND(rsi1d,2), if rsi1d<50 then Color.PINK else Color.LIGHT_GREEN);

#--- END CODE
 
the only option I have for you is to change the aggregation period manually if you decided to go to higher time frame. use below code and make sure that the lowers aggregation period to be same current chart or more.

CSS:
#/@seoco
#indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator',
# Converted by Sam4Cok@Samer800 -02/2023

declare lower;
input rsiAgg1    = AggregationPeriod.FIFTEEN_MIN;
input rsiAgg2    = AggregationPeriod.HOUR;
input rsiAgg3    = AggregationPeriod.FOUR_HOUR
input rsiAgg4    = AggregationPeriod.DAY;
input ShowCurrenttimeStoch = yes;
input rsiShowInput          = yes;    # 'Show RSI', group='Show/Hide Settings')
input maShowInput           = no;     # 'Show MA', group='Show/Hide Settings')
input showRSIMAInput        = yes;    # 'Show RSIMA Cloud'
input rsiBandShowInput      = yes;    # 'Show Oversold/Overbought Lines'
input rsiBandExtShowInput   = yes;    # 'Show Oversold/Overbought Extended Lines'
input rsiHighlightShowInput = yes;    # 'Show Oversold/Overbought Highlight Lines'
input DivergenceShowInput   = yes;    # 'Show RSI Divergence Labels'
#//--- RSI Input Settings
input rsiSourceInput  = close;        # 'Source'
input rsiLengthInput  = 14;           # 'RSI Length'
input rsiUpperBandExtInput = 80;      # 'RSI Overbought Extended Line'
input rsiUpperBandInput   = 70;       # 'RSI Overbought Line'
input rsiLowerBandInput   = 30;       # 'RSI Oversold Line'
input rsiLowerBandExtInput = 20;      # 'RSI Oversold Extended Line'
#//--- MA Input Settings
input maTypeInput = {"SMA", "Bollinger Bands", default "EMA", "SMMA (RMA)", "WMA", "VWMA"};    # "MA Type"
input maLengthInput = 14;             # "MA Length"
#//--- Divergence Input Settings
input lbrInput        = 2;            # "Pivot Lookback Right"
input lblInput        = 2;            # "Pivot Lookback Left"
input lbRangeMaxInput = 10;           # "Max of Lookback Range"
input lbRangeMinInput = 2;            # "Min of Lookback Range"
input plotBullInput       = yes;      # "Plot Bullish"
input plotHiddenBullInput = yes;      # "Plot Hidden Bullish"
input plotBearInput       = yes;      # "Plot Bearish"
input plotHiddenBearInput = yes;      # "Plot Hidden Bearish"
#//--- Stoch RSI Settings + Calculation
input showStochRSI = no;              # "Show Stochastic RSI"
input smoothK      = 3;               # "Stochastic K"
input smoothD      = 4;               # "Stochastic D"
input lengthRSI    = 14;              # "Stochastic RSI Lenght"
input lengthStoch  = 14;              # "Stochastic Lenght"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
#--- Color
DefineGlobalColor("Green"     , CreateColor(38,166,154));
DefineGlobalColor("Red"        , CreateColor(239,83,79));
DefineGlobalColor("Gray"       , CreateColor(42,42,42));
#//--- RSI Calculation
def close15 = close(Period = rsiAgg1);
def close1h = close(Period = rsiAgg2);
def close4h = close(Period = rsiAgg3);
def close1d = close(Period = rsiAgg4);

def rsi         = RSI(PRICE = rsiSourceInput, LENGTH = rsiLengthInput);

def up15   = WildersAverage(Max((close15 - close15[1]), 0), rsiLengthInput);
def down15 = WildersAverage(-Min((close15 - close15[1]), 0), rsiLengthInput);
def rsi15 = if down15 == 0 then 100 else if up15 == 0 then 0 else 100 - (100 / (1 + up15 / down15));

def up1h   = WildersAverage(Max((close1h - close1h[1]), 0), rsiLengthInput);
def down1h = WildersAverage(-Min((close1h - close1h[1]), 0), rsiLengthInput);
def rsi1h = if down1h == 0 then 100 else if up1h == 0 then 0 else 100 - (100 / (1 + up1h / down1h));

def up4h   = WildersAverage(Max((close4h - close4h[1]), 0), rsiLengthInput);
def down4h = WildersAverage(-Min((close4h - close4h[1]), 0), rsiLengthInput);
def rsi4h = if down4h == 0 then 100 else if up4h == 0 then 0 else 100 - (100 / (1 + up4h / down4h));

def up1d   = WildersAverage(Max((close1d - close1d[1]), 0), rsiLengthInput);
def down1d = WildersAverage(-Min((close1d - close1d[1]), 0), rsiLengthInput);
def rsi1d = if down1d == 0 then 100 else if up1d == 0 then 0 else 100 - (100 / (1 + up1d / down1d));

#//--- MA Calculation
# 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;
}
#ma(source, length, type) =>
script ma {
    input source = close;
    input length = 14;
    input type = "EMA";
    def ma =
        if type == "SMA" then SimpleMovingAvg(source, length) else
        if type == "Bollinger Bands" then SimpleMovingAvg(source, length) else
        if type == "EMA" then ExpAverage(source, length) else
        if type == "SMMA (RMA)" then WildersAverage(source, length) else
        if type == "WMA" then WMA(source, length) else
        if type == "VWMA" then SimpleMovingAvg(source * volume, length) / SimpleMovingAvg(volume, length) else source;
    plot return = ma;
}

def rsiMA = ma(rsi, maLengthInput, maTypeInput);
def rsi1  = RSI(Price=close15, LENGTH=lengthRSI);
def k     = SimpleMovingAvg(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK);
def d     = SimpleMovingAvg(k, smoothD);
def osc = rsi;
def rsiColor = rsi >= rsiMA;

plot kTf     = if !ShowCurrenttimeStoch then na else SimpleMovingAvg(stoch(rsi, rsi, rsi, lengthStoch), smoothK);
plot dTf     = if !ShowCurrenttimeStoch then na else SimpleMovingAvg(kTf, smoothD);
kTf.SetDefaultColor(Color.CYAN);
dTf.SetDefaultColor(Color.YELLOW);
#//--- Plot Lines
plot maPlot     = if maShowInput then rsiMA else na;
maPlot.SetDefaultColor(Color.YELLOW);
def rsiMAPlot  = if showRSIMAInput then rsiMA else na;
plot rsiPlot    = if rsiShowInput then rsi else na;
rsiPlot.AssignValueColor(if rsiColor then GlobalColor("Green") else GlobalColor("Red"));
AddCloud(rsiPlot, rsiMAPlot, GlobalColor("Green"), GlobalColor("Red"));

plot StochK = if showStochRSI then k else na;
StochK.SetDefaultColor(Color.WHITE);
plot StockD = if showStochRSI then d else na;
StockD.SetDefaultColor(Color.MAGENTA);


plot bbUpperBand = if isBB then rsiMA + stdev(rsi, maLengthInput) * 2 else na;    # "Upper Bollinger Band"
bbUpperBand.SetDefaultColor(Color.GRAY);
plot bbLowerBand = if isBB then rsiMA - stdev(rsi, maLengthInput) * 2 else na;    # "Lower Bollinger Band"
bbLowerBand.SetDefaultColor(Color.GRAY);

AddCloud(bbUpperBand, bbLowerBand, GlobalColor("Gray"));

plot midLine = 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot rsiUpBandExt = if rsiBandExtShowInput then rsiUpperBandExtInput else na;
rsiUpBandExt.SetStyle(Curve.SHORT_DASH);
rsiUpBandExt.SetDefaultColor(Color.RED);
plot rsiUpBand = if rsiBandShowInput then rsiUpperBandInput else na;
rsiUpBand.SetStyle(Curve.SHORT_DASH);
rsiUpBand.SetDefaultColor(Color.DARK_RED);
plot rsiLoBand = if rsiBandShowInput then rsiLowerBandInput else na;
rsiLoBand.SetStyle(Curve.SHORT_DASH);
rsiLoBand.SetDefaultColor(Color.DARK_GREEN);
plot rsiLoBandExt = if rsiBandExtShowInput then rsiLowerBandExtInput else na;
rsiLoBandExt.SetStyle(Curve.SHORT_DASH);
rsiLoBandExt.SetDefaultColor(Color.GREEN);

AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandExtInput then pos else na else na, neg, Color.GREEN);
AddCloud(if rsiHighlightShowInput then if rsi >= rsiUpperBandInput then if rsi < rsiUpperBandExtInput then pos else na else na else na, neg, Color.DARK_GREEN);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandExtInput then pos else na else na, neg, Color.RED);
AddCloud(if rsiHighlightShowInput then if rsi <= rsiLowerBandInput then if rsi > rsiLowerBandExtInput then pos else na else na else na, neg, Color.DARK_RED);

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 = 2;
  input MaxLookback = 10;
  input occurrence = 0;
  def n = occurrence + 1;
  def offset = fold j = 0 to MinLookback + 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 = 10;
    input rangeLower = 2;
        def bars = if cond then 0 else bars[1] + 1;
        def inrange =  (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
#//--- Plot Divergence
#// Regular Bullish
#// Osc: Higher Low
def pl = findpivots(osc,-1, lblInput, lbrInput);
def ph = findpivots(osc, 1, lblInput, lbrInput);

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

def vlFound = valuewhen(plFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);
def vhFound = valuewhen(phFound, osc, lbRangeMinInput, lbRangeMaxInput, 1);

def plPrice = valuewhen(plFound, low, lbRangeMinInput, lbRangeMaxInput, 1);
def phPrice = valuewhen(phFound, high, lbRangeMinInput, lbRangeMaxInput, 1);

#// Regular Bullish
def oscHL = osc > vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLL = low < plPrice;
def bullCond = DivergenceShowInput and plotBullInput and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = osc < vlFound and  _inRange(plFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHL = low > plPrice;
def hiddenBullCond = DivergenceShowInput and plotHiddenBullInput and plFound and oscLL and priceHL;

#// Regular Bearish
def oscLH   = osc < vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceHH = high > phPrice;
def bearCond = DivergenceShowInput and plotBearInput and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = osc > vhFound and  _inRange(phFound[1],lbRangeMaxInput,lbRangeMinInput);
def priceLH = high < phPrice;
def hiddenBearCond = DivergenceShowInput and plotHiddenBearInput and phFound and oscHH and priceLH;

#------ Bubbles
addchartbubble(bullCond, osc, "R", color.GREEN, no);
addchartbubble(bearCond, osc, "R", Color.RED, yes);
addchartbubble(hiddenBullCond, osc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, osc, "H", color.DARK_red, yes);

#--- Labels
AddLabel(rsiShowInput, "RSI Current: " + ROUND(rsi,2), if rsi<50 then Color.RED else Color.GREEN);
AddLabel(rsiShowInput, "RSI 15m: " + ROUND(rsi15,2), if rsi15<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1h: " + ROUND(rsi1h,2), if rsi1h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 4h: " + ROUND(rsi4h,2), if rsi4h<50 then Color.PINK else Color.LIGHT_GREEN);
AddLabel(rsiShowInput, "RSI 1d: " + ROUND(rsi1d,2), if rsi1d<50 then Color.PINK else Color.LIGHT_GREEN);

#--- END CODE
Hey @samer800, thank you for your response! After @MerryDay responded, I got to thinking and went back and found the code for the default aggregation and changed it to 1D and it works in all time frames now.


O3c3PsB.jpg
 
Last edited:
Hey @samer800, thank you for your response! After @MerryDay responded, I got to thinking and went back and found the code for the default aggregation and changed it to 1D and it works in all time frames now.


O3c3PsB.jpg
Hello French82, Could you explain further into detail by what you mean when you said "changed it to 1D"? Thank you I'm having the same problem and would like for it to work on all time frames.
 
Hello French82, Could you explain further into detail by what you mean when you said "changed it to 1D"? Thank you I'm having the same problem and would like for it to work on all time frames.
Hey @Quantum33 ,

I added additional aggregation periods to make it work in all time frames.. to make it easy here is the script. Happy Trading!



#/@seoco
#indicator('CryptoSignalScanner - RSI Overbought/Oversold + Divergence Indicator',
# Converted by Sam4Cok@Samer800 -02/2023

declare lower;
input rsiShowInput = yes; # 'Show RSI', group='Show/Hide Settings')
input maShowInput = no; # 'Show MA', group='Show/Hide Settings')
input showRSIMAInput = yes; # 'Show RSIMA Cloud'
input rsiBandShowInput = yes; # 'Show Oversold/Overbought Lines'
input rsiBandExtShowInput = yes; # 'Show Oversold/Overbought Extended Lines'
input rsiHighlightShowInput = yes; # 'Show Oversold/Overbought Highlight Lines'
input DivergenceShowInput = yes; # 'Show RSI Divergence Labels'
#//--- RSI Input Settings
input rsiSourceInput = close; # 'Source'
input rsiLengthInput = 14; # 'RSI Length'
input rsiUpperBandExtInput = 80; # 'RSI Overbought Extended Line'
input rsiUpperBandInput = 70; # 'RSI Overbought Line'
input rsiLowerBandInput = 30; # 'RSI Oversold Line'
input rsiLowerBandExtInput = 20; # 'RSI Oversold Extended Line'
#//--- MA Input Settings
input maTypeInput = {"SMA", "Bollinger Bands", default "EMA", "SMMA (RMA)", "WMA", "VWMA"}; # "MA Type"
input maLengthInput = 14; # "MA Length"
#//--- Divergence Input Settings
input lbrInput = 2; # "Pivot Lookback Right"
input lblInput = 2; # "Pivot Lookback Left"
input lbRangeMaxInput = 10; # "Max of Lookback Range"
input lbRangeMinInput = 2; # "Min of Lookback Range"
input plotBullInput = yes; # "Plot Bullish"
input plotHiddenBullInput = yes; # "Plot Hidden Bullish"
input plotBearInput = yes; # "Plot Bearish"
input plotHiddenBearInput = yes; # "Plot Hidden Bearish"
#//--- Stoch RSI Settings + Calculation
input showStochRSI = no; # "Show Stochastic RSI"
input smoothK = 3; # "Stochastic K"
input smoothD = 4; # "Stochastic D"
input lengthRSI = 14; # "Stochastic RSI Lenght"
input lengthStoch = 14; # "Stochastic Lenght"
input label = yes; # "#--- Labels"

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
#--- Color
DefineGlobalColor("Green" , CreateColor(38,166,154));
DefineGlobalColor("Red" , CreateColor(239,83,79));
DefineGlobalColor("Gray" , CreateColor(42,42,42));
#//--- RSI Calculation
def close15 = close(Period = AggregationPeriod.FIFTEEN_MIN);
def close1h = close(Period = AggregationPeriod.HOUR);
def close4h = close(Period = AggregationPeriod.FOUR_HOURS);
def close1d = close(Period = AggregationPeriod.DAY);

def rsi = RSI(PRICE = rsiSourceInput, LENGTH = rsiLengthInput);

def up15 = WildersAverage(Max((close15 - close15[1]), 0), rsiLengthInput);
def down15 = WildersAverage(-Min((close15 - close15[1]), 0), rsiLengthInput);
def rsi15 = if down15 == 0 then 100 else if up15 == 0 then 0 else 100 - (100 / (1 + up15 / down15));

def up1h = WildersAverage(Max((close1h - close1h[1]), 0), rsiLengthInput);
def down1h = WildersAverage(-Min((close1h - close1h[1]), 0), rsiLengthInput);
def rsi1h = if down1h == 0 then 100 else if up1h == 0 then 0 else 100 - (100 / (1 + up1h / down1h));

def up4h = WildersAverage(Max((close4h - close4h[1]), 0), rsiLengthInput);
def down4h = WildersAverage(-Min((close4h - close4h[1]), 0), rsiLengthInput);
def rsi4h = if down4h == 0 then 100 else if up4h == 0 then 0 else 100 - (100 / (1 + up4h / down4h));

def up1d = WildersAverage(Max((close1d - close1d[1]), 0), rsiLengthInput);
def down1d = WildersAverage(-Min((close1d - close1d[1]), 0), rsiLengthInput);
def rsi1d = if down1d == 0 then 100 else if up1d == 0 then 0 else 100 - (100 / (1 + up1d / down1d));

#//--- MA Calculation
# 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;
}
#ma(source, length, type) =>
script ma {
input source = close;
input length = 14;
input type = "EMA";
def ma =
if type == "SMA" then SimpleMovingAvg(source, length) else
if type == "Bollinger Bands" then SimpleMovingAvg(source, length) else
if type == "EMA" then ExpAverage(source, length) else
if type == "SMMA (RMA)" then WildersAverage(source, length) else
if type == "WMA" then WMA(source, length) else
if type == "VWMA" then SimpleMovingAvg(source * volume, length) / SimpleMovingAvg(volume, length) else source;
plot return = ma;
}

def rsiMA = ma(rsi, maLengthInput, maTypeInput);
def rsi1 = RSI(Price=close1d, LENGTH=lengthRSI);
def k = SimpleMovingAvg(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK);
def d = SimpleMovingAvg(k, smoothD);
def osc = rsi;
def rsiColor = rsi >= rsiMA;

#//--- Plot Lines
plot maPlot = if maShowInput then rsiMA else na;
maPlot.SetDefaultColor(Color.YELLOW);
def rsiMAPlot = if showRSIMAInput then rsiMA else na;
plot rsiPlot = if rsiShowInput then rsi else na;
rsiPlot.AssignValueColor(if rsiColor then GlobalColor("Green") else GlobalColor("Red"));
AddCloud(rsiPlot, rsiMAPlot, GlobalColor("Green"), GlobalColor("Red"));

plot StochK = if showStochRSI then k else na;
StochK.SetDefaultColor(Color.WHITE);
plot StockD = if showStochRSI then d else na;
StockD.SetDefaultColor(Color.MAGENTA);


plot bbUpperBand = if isBB then rsiMA + stdev(rsi, maLengthInput) * 2 else na; # "Upper Bollinger Band"
bbUpperBand.SetDefaultColor(Color.GRAY);
plot bbLowerBand = if isBB then rsiMA - stdev(rsi, maLengthInput) * 2 else na; # "Lower Bollinger Band"
bbLowerBand.SetDefaultColor(Color.GRAY);

AddCloud(bbUpperBand, bbLowerBand, GlobalColor("Gray"));

plot midLine = 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot rsiUpBandExt = if rsiBandExtShowInput then rsiUpperBandExtInput else na;
rsiUpBandExt.SetStyle(Curve.SHORT_DASH);
rsiUpBandExt.SetDefaultColor(Color.RED);
plot rsiUpBand = if rsiBandShowInput then rsiUpperBandInput else na;
rsiUpBand.SetStyle(Curve.SHORT_DASH);
rsiUpBand.SetDefaultColor(Color.DARK_RED);
plot rsiLoBand = if rsiBandShowInput then rsiLowerBandInput else na;
rsiLoBand.SetStyle(Curve.SHORT_DASH);
rsiLoBand.SetDefaultColor(Color.DARK_GREEN);
 

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