RSI (or MACD) with VWAP & MA & div for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
CXGWJyg.png

Author Message:
This indicator is the default RSI with smoothing and an RSI based VWAP . I've also added the RSI based VWAP to the Moving Average options list. By default, the RSI based VWAP is turned on with the WMA selected as the Moving Average. The RSI changes colors when it is above the 55 level, VWAP , and Moving Average or below the 45 level, VWAP , and Moving Average. There is also an option for barcoloring based on the RSI colors.
[Sam4cok] - I added divergence and signal line.

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © DDMyke
#//@version=5
#indicator(title="Smoothed RSI w/ VWAP & Moving Average", shorttitle="RSIv"
# Converted and mod by Sam4Cok@Samer800 - 10/2022
declare lower;
input showBarcolor = no;      # 'Barcolor'
input showSignalLine = yes;   # 'Signal Line'
input showBackground = yes;
input rsiSourceInput = close; # 'Source'
input showRSI = yes;
input showRSIvwap = yes;
input VwapTimeFrame = {default DAY, WEEK, MONTH};
input rsiLengthInput = 14;
input rsiOverbought = 75;
input rsiOversold = 25;
input rsiSmoothing = 2;       # 'smoothing'
input showMA = yes;
input maTypeInput = {"SMA", "Bollinger Bands", "EMA", "SMMA (RMA)",default "WMA", "VWMA"};
input maLengthInput = 20;
input bbMultInput = 2.0;      # 'BB StdDev'

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
# rsiVWAP
script rsiVWAP {
    input src = close;
    input timeFrame = {default DAY, WEEK, MONTH};
    def v = Volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case DAY:
        periodIndx = yyyyMmDd;
    case WEEK:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case MONTH:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    def volumeVwap2Sum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
        volumeVwap2Sum = v * Sqr(src);
    } else {
        volumeSum = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
        volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(src), v * Sqr(src));
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#ma(smoorsi, maLengthInput, maTypeInput) =>
script ma {
    input smoorsi = close;
    input maLengthInput = 34;
    input maTypeInput = "WMA";
    def ma = if maTypeInput == "SMA" then SimpleMovingAvg(smoorsi, maLengthInput) else
         if maTypeInput == "Bollinger Bands" then SimpleMovingAvg(smoorsi, maLengthInput) else
         if maTypeInput == "EMA" then ExpAverage(smoorsi, maLengthInput) else
         if maTypeInput == "SMMA (RMA)" then WildersSmoothing(smoorsi, maLengthInput) else
         if maTypeInput == "WMA" then WMA(smoorsi, maLengthInput) else
         if maTypeInput == "VWMA" then vwma(smoorsi, maLengthInput) else Double.NaN;
    plot return = ma;
}
def nRSI = RSI(Length = rsiLengthInput, Price = rsiSourceInput);
def smoorsi = WildersSmoothing(nRSI, rsiSmoothing);
def rsiMA = ma(smoorsi, maLengthInput, maTypeInput);
def isBB = maTypeInput == maTypeInput."Bollinger Bands";

#//- VWAP
def RSIvwap = rsiVWAP(smoorsi, VwapTimeFrame);

#//----Plot---------------------------
def StrongBull = smoorsi > 55 and smoorsi > rsiMA and smoorsi > RSIvwap;
def Bullish = smoorsi > 55 and smoorsi < rsiMA and smoorsi > RSIvwap;
def StrongBear = smoorsi < 45 and smoorsi < rsiMA and smoorsi < RSIvwap;
def Bearish = smoorsi > 45 and smoorsi < rsiMA and smoorsi < RSIvwap;
def Neutral = smoorsi > 45 or smoorsi < 55;
def colorchange = if StrongBull then 2 else
                  if Bullish then 1 else
                  if Bearish then -1 else
                  if StrongBear then -2 else 0;

plot rsiPlot = if showRSI then smoorsi else na;    # "Smoothed RSI"
rsiPlot.AssignValueColor(if colorchange == 2 then Color.GREEN else
                         if colorchange == 1 then Color.DARK_GREEN else
                         if colorchange == -2 then Color.RED else
                         if colorchange == -1 then Color.DARK_RED else Color.WHITE);
rsiPlot.SetLineWeight(2);
plot vwapPlot = if showRSIvwap then RSIvwap else na;  # "RSI-based VWAP"
vwapPlot.SetDefaultColor(Color.MAGENTA);

plot maPlot = if showMA then rsiMA else na;    # "RSI-based MA"
maPlot.SetDefaultColor(Color.GRAY);

plot bbUpperBand = if isBB then rsiMA + StDev(smoorsi, maLengthInput) * bbMultInput else na;
bbUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot bbLowerBand = if isBB then rsiMA - StDev(smoorsi, maLengthInput) * bbMultInput else na;
bbLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if isBB then bbUpperBand else na, bbLowerBand, CreateColor(76, 10, 127));

#//-------- Hlines
plot rsiUpperBand = rsiOverbought;
rsiUpperBand.SetStyle(Curve.FIRM);
rsiUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot midBand = (rsiOverbought + rsiOversold) / 2 ;
midBand.SetStyle(Curve.SHORT_DASH);
midBand.SetDefaultColor(Color.DARK_GRAY);

plot rsiLowerBand = rsiOversold;
rsiLowerBand.SetStyle(Curve.FIRM);
rsiLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if showBackground then rsiUpperBand else na, midBand, CreateColor(6, 45, 3));
AddCloud(if showBackground then midBand else na, rsiLowerBand, CreateColor(66, 0, 0));
#--- Signal Line
def CrossUp = smooRSI crosses above RSIvwap;
def CrossDn = smooRSI crosses below RSIvwap;

def SigUp = if CrossUp and rsiMA > RSIvwap then 1 else 0;
def SigDn = if CrossDn and rsiMA < RSIvwap then 1 else 0;

plot Signal = if (SigUp or SigDn) then 75 else na;
signal.SetHiding(!showSignalLine);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
Signal.SetLineWeight(2);
Signal.AssignValueColor(if SigUp and smooRSI>=rsiMA then CreateColor(1,255,0) else
                        if SigUp and smooRSI<rsiMA then CreateColor(4,181,4) else
                        if SigDn and smooRSI>rsiMA then Color.DARK_RED else
                        if SigDn and smooRSI<=rsiMA then Color.RED else Color.WHITE);
plot cross = if CrossUp or CrossDn then 75 else na;
cross.SetHiding(!showSignalLine);
cross.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross.AssignValueColor( if CrossUp then CreateColor(2,117,2) else CreateColor(117,2,2));
# ---- BarColor
AssignPriceColor(if !showbarcolor then Color.CURRENT else
                 if colorchange == 2 then Color.GREEN else
                 if colorchange == 1 then Color.DARK_GREEN else
                 if colorchange == -2 then Color.RED else
                 if colorchange == -1 then Color.DARK_RED else Color.GRAY);

#----Div-----------
input PivotLookback  = 5; # "Pivot Lookback"
input DivBull = yes;      # "Plot Bullish"
input DivBear = yes;      # "Plot Bearish"

def divSrc = smoorsi;

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 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;
}
def pl = findpivots(divSrc,-1, PivotLookback, PivotLookback);
def ph = findpivots(divSrc, 1, PivotLookback, PivotLookback);

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],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);

## END CODE

Updated - Added MACD as requested by a member and modified the signal line to be trend line.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © DDMyke
#//@version=5
#indicator(title="Smoothed RSI w/ VWAP & Moving Average", shorttitle="RSIv"
# Converted and mod by Sam4Cok@Samer800 - 10/2022
# Updated - Added MACD indicator to the study
declare lower;
input showBarcolor   = no;    # 'Barcolor'
input showTrendLine  = yes;   # 'Signal Line'
input HighlightObOsArea = yes;
input Source         = close; # 'Source'
input VwapTimeFrame  = {default DAY, WEEK, MONTH};
input studySelect    = {Default RSI, MACD};
input Smoothing      = yes;
input SmoothLength   = 2;     # 'smoothing'
input showMaLine     = yes;
input showVwap       = yes;
input showStudyLine  = yes;
input maTypeInput    = {"SMA", "Bollinger Bands", "EMA", "SMMA (RMA)",default "WMA", "VWMA"};
input maLengthInput  = 28;
input macdMaType     = {"SMA",Default "EMA", "SMMA (RMA)", "WMA", "VWMA"}; # "MACD MA"
input bbMultInput    = 2.0;   # 'BB StdDev'
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 9;
input rsiLengthInput = 14;
input rsiOverbought  = 75;
input rsiOversold    = 25;

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def type = if studySelect==studySelect.RSI then 1 else 0;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
# VWAP
script _VWAP {
    input src = close;
    input timeFrame = {default DAY, WEEK, MONTH};
    def v = Volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case DAY:
        periodIndx = yyyyMmDd;
    case WEEK:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case MONTH:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    def volumeVwap2Sum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
        volumeVwap2Sum = v * Sqr(src);
    } else {
        volumeSum = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
        volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(src), v * Sqr(src));
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#ma(src, maLengthInput, maTypeInput) =>
script ma {
    input src = close;
    input maLengthInput = 34;
    input maTypeInput = "WMA";
    def ma = if maTypeInput == "SMA" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "Bollinger Bands" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "EMA" then ExpAverage(src, maLengthInput) else
         if maTypeInput == "SMMA (RMA)" then WildersAverage(src, maLengthInput) else
         if maTypeInput == "WMA" then WMA(src, maLengthInput) else
         if maTypeInput == "VWMA" then vwma(src, maLengthInput) else Double.NaN;
    plot return = ma;
}
#--- RSI
def nRSI = RSI(Length = rsiLengthInput, Price = Source);
#--- MACD
def fastMA = ma(Source, macdFastLength, macdMaType);
def slowMA = ma(Source, macdSlowLength, macdMaType);
def diff = fastMA - slowMA;
def macd = ma(diff, macdSignalLength, macdMaType);
#--- Calc
def smoothRSI = if(Smoothing, WildersSmoothing(nRsi, SmoothLength), nRsi);
def srcType   = if type then smoothRSI else macd;
def maSrcType = if type then smoothRSI else diff;
def movAvg = ma(maSrcType, maLengthInput, maTypeInput);
#--- BB
def upper = movAvg + StDev(maSrcType, maLengthInput) * bbMultInput;
def lower = movAvg - StDev(maSrcType, maLengthInput) * bbMultInput;
#//- VWAP
def studyVwap = _VWAP(srcType, VwapTimeFrame);

#//----Plot---------------------------
def StrongBull = srcType > if(type,55,-0.05) and srcType > studyVwap and srcType>srcType[1] and srcType > movAvg;
def Bullish    = srcType > if(type,55, 0.05) and srcType > studyVwap and srcType<srcType[1];
def StrongBear = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType<srcType[1] and srcType < movAvg;
def Bearish    = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType>srcType[1];

def colorchange = if StrongBull then  2 else
                  if Bullish    then  1 else
                  if Bearish    then -1 else
                  if StrongBear then -2 else 0;

plot StudyPlot = if showStudyLine then srcType else na;
StudyPlot.AssignValueColor(if colorchange == 2 then Color.GREEN else
                           if colorchange == 1 then Color.DARK_GREEN else
                           if colorchange == -2 then Color.RED else
                           if colorchange == -1 then Color.DARK_RED else Color.WHITE);
StudyPlot.SetLineWeight(2);
plot vwapPlot = if showVwap then studyVwap else na; # "Study-based VWAP"
vwapPlot.SetDefaultColor(Color.MAGENTA);

plot maPlot = if showMaLine then movAvg else na;       # "Study-based MA"
maPlot.SetDefaultColor(Color.GRAY);

plot bbUpperBand = if isBB then upper else na;
bbUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot bbLowerBand = if isBB then lower else na;
bbLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if isBB then bbUpperBand else na, bbLowerBand, CreateColor(76, 10, 127));

#//-------- Hlines
def UpLimit = if(type, rsiOverbought, 1);
def LoLimit = if(type, rsiOversold, -1);

plot midBand = if isNaN(close) then na else (UpLimit + LoLimit) / 2 ;
midBand.SetStyle(Curve.SHORT_DASH);
midBand.SetDefaultColor(Color.DARK_GRAY);

#--- Signal Line
def CUp = if (srcType > movAvg and srcType > studyVwap and srcType>srcType[1]) then CUp[1] + 1 else 0;
def CDn = if (srcType < movAvg and srcType < studyVwap and srcType<srcType[1]) then CDn[1] + 1 else 0;
def NUp = if (srcType > movAvg and movAvg > studyVwap and srcType<srcType[1]) then NUp[1] + 1 else 0;
def NDn = if (srcType < movAvg and movAvg < studyVwap and srcType>srcType[1]) then NDn[1] + 1 else 0;
def SigUp = if CUp>1 then 1 else 0;
def SigDn = if CDn>1 then 1 else 0;

plot Signal = if (SigUp or SigDn) then if(type, 80, 1.5) else na;
signal.SetHiding(!showTrendLine);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Signal.AssignValueColor(if SigUp then CreateColor(1,255,0) else
                        if SigDn then Color.RED else Color.WHITE);
plot cross = if NUp>1 or NDn>1 then if(type, 80, 1.5) else na;
cross.SetHiding(!showTrendLine);
cross.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross.AssignValueColor( if NUp then CreateColor(2,117,2) else CreateColor(117,2,2));

#---- Cloud--
AddCloud(if HighlightObOsArea then pos else na, UpLimit, CreateColor(66,0,0), CreateColor(66,0,0), yes);
AddCloud(if HighlightObOsArea then neg else na, LoLimit, CreateColor(6,45,3), CreateColor(6,45,3), yes);

# ---- BarColor
AssignPriceColor(if !showbarcolor then Color.CURRENT else
                 if colorchange == 2 then Color.GREEN else
                 if colorchange == 1 then Color.DARK_GREEN else
                 if colorchange == -2 then Color.RED else
                 if colorchange == -1 then Color.DARK_RED else Color.GRAY);

#----Div-----------
input PivotLookback  = 5; # "Pivot Lookback"
input DivBull = yes;      # "Plot Bullish"
input DivBear = yes;      # "Plot Bearish"

def divSrc = srcType;

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 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;
}
def pl = findpivots(divSrc,-1, PivotLookback, PivotLookback);
def ph = findpivots(divSrc, 1, PivotLookback, PivotLookback);

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],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);

## END CODE
 
Last edited by a moderator:
Hi @samer800 , Is it possible to add a second aggregation of smoothed RSI to this (say, "15 min")?
try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © DDMyke
#//@version=5
#indicator(title="Smoothed RSI w/ VWAP & Moving Average", shorttitle="RSIv"
# Converted and mod by Sam4Cok@Samer800 - 10/2022
# Updated - Added MACD indicator to the study
# Updated - added second timeframe line - 06/2023
declare lower;
input showFirstLine  = yes;
input ShowSecondLine = yes;
input SecondTimeframe = AggregationPeriod.FIFTEEN_MIN;
input showBarcolor   = no;    # 'Barcolor'
input showTrendLine  = yes;   # 'Signal Line'
input HighlightObOsArea = yes;
input Source         = close; # 'Source'
input VwapTimeFrame  = {default DAY, WEEK, MONTH};
input studySelect    = {Default RSI, MACD};
input Smoothing      = yes;
input SmoothLength   = 2;     # 'smoothing'
input showMaLine     = yes;
input showVwap       = yes;
input maTypeInput    = {"SMA", "Bollinger Bands", "EMA", "SMMA (RMA)",default "WMA", "VWMA"};
input maLengthInput  = 28;
input macdMaType     = {"SMA",Default "EMA", "SMMA (RMA)", "WMA", "VWMA"}; # "MACD MA"
input bbMultInput    = 2.0;   # 'BB StdDev'
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 9;
input rsiLengthInput = 14;
input rsiOverbought  = 75;
input rsiOversold    = 25;

def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def htClose = close(Period=SecondTimeFrame);
def htVolume = volume(Period=SecondTimeFrame);
def type = if studySelect==studySelect.RSI then 1 else 0;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
# VWAP
script _VWAP {
    input src = close;
    input timeFrame = {default DAY, WEEK, MONTH};
    input v = volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case DAY:
        periodIndx = yyyyMmDd;
    case WEEK:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case MONTH:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    def volumeVwap2Sum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
        volumeVwap2Sum = v * Sqr(src);
    } else {
        volumeSum = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
        volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(src), v * Sqr(src));
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    input v = volume;
    def VWMA = SimpleMovingAvg(x * v, y) / SimpleMovingAvg(v, y);
    plot result = VWMA;
}
#ma(src, maLengthInput, maTypeInput) =>
script ma {
    input src = close;
    input maLengthInput = 34;
    input maTypeInput = "WMA";
    input htVolume = volume;
    def ma = if maTypeInput == "SMA" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "Bollinger Bands" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "EMA" then ExpAverage(src, maLengthInput) else
         if maTypeInput == "SMMA (RMA)" then WildersAverage(src, maLengthInput) else
         if maTypeInput == "WMA" then WMA(src, maLengthInput) else
         if maTypeInput == "VWMA" then vwma(src, maLengthInput, htVolume) else Double.NaN;
    plot return = ma;
}
#--- RSI
def nRSI = RSI(Length = rsiLengthInput, Price = Source);
def RSI2 = RSI(Length = rsiLengthInput, Price = htClose);
#--- MACD
def fastMA = ma(Source, macdFastLength, macdMaType);
def slowMA = ma(Source, macdSlowLength, macdMaType);
def diff = fastMA - slowMA;
def macd = ma(diff, macdSignalLength, macdMaType);
#--- 2nd MACD
def fastMA2 = ma(htClose, macdFastLength, macdMaType, htVolume);
def slowMA2 = ma(htClose, macdSlowLength, macdMaType, htVolume);
def diff2 = fastMA2 - slowMA2;
def macd2 = ma(diff2, macdSignalLength, macdMaType, htVolume);
#--- Calc
def smoothRSI = if(Smoothing, WildersSmoothing(nRsi, SmoothLength), nRsi);
def srcType   = if type then smoothRSI else macd;
def maSrcType = if type then smoothRSI else diff;
def movAvg = ma(maSrcType, maLengthInput, maTypeInput);
#--- 2nd Calc
def smoothRSI2 = if(Smoothing, WildersSmoothing(Rsi2, SmoothLength), Rsi2);
def srcType2   = if type then smoothRSI2 else macd2;
def maSrcType2 = if type then smoothRSI2 else diff2;
def movAvg2 = ma(maSrcType2, maLengthInput, maTypeInput, htVolume);
#--- BB
def upper = movAvg + StDev(maSrcType, maLengthInput) * bbMultInput;
def lower = movAvg - StDev(maSrcType, maLengthInput) * bbMultInput;
#--- 2nd BB
def upper2 = movAvg2 + StDev(maSrcType2, maLengthInput) * bbMultInput;
def lower2 = movAvg2 - StDev(maSrcType2, maLengthInput) * bbMultInput;
#//- VWAP
def studyVwap = _VWAP(srcType, VwapTimeFrame);
#//- 2nd VWAP
def studyVwap2 = _VWAP(srcType2, VwapTimeFrame, htVolume);

#//----Plot---------------------------
def StrongBull = srcType > if(type,55,-0.05) and srcType > studyVwap and srcType>srcType[1] and srcType > movAvg;
def Bullish    = srcType > if(type,55, 0.05) and srcType > studyVwap and srcType<srcType[1];
def StrongBear = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType<srcType[1] and srcType < movAvg;
def Bearish    = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType>srcType[1];

def colorchange = if StrongBull then  2 else
                  if Bullish    then  1 else
                  if Bearish    then -1 else
                  if StrongBear then -2 else 0;

plot StudyPlot = if showFirstLine then srcType else na;
StudyPlot.AssignValueColor(if colorchange == 2 then Color.GREEN else
                           if colorchange == 1 then Color.DARK_GREEN else
                           if colorchange == -2 then Color.RED else
                           if colorchange == -1 then Color.DARK_RED else Color.WHITE);
StudyPlot.SetLineWeight(2);
#//----2 nd Plot---------------------------
def StrongBull2 = srcType2 > if(type,55,-0.05) and srcType2 > studyVwap2 and srcType2>srcType2[1] and srcType2 > movAvg2;
def Bullish2    = srcType2 > if(type,55, 0.05) and srcType2 > studyVwap2 and srcType2<srcType2[1];
def StrongBear2 = srcType2 < if(type,45,-0.05) and srcType2 < studyVwap2 and srcType2<srcType2[1] and srcType2 < movAvg2;
def Bearish2    = srcType2 < if(type,45,-0.05) and srcType2 < studyVwap2 and srcType2>srcType2[1];

def colorchange2 = if StrongBull2-StrongBull2[1] then  2 else
                   if Bullish2-Bullish2[1]    then  1 else
                   if Bearish2-Bearish2[1]    then -1 else
                   if StrongBear2-StrongBear2[1] then -2 else 0;

plot StudyPlot2 = if last or !ShowSecondLine then na else srcType2;
StudyPlot2.AssignValueColor(if colorchange2 == 2 then Color.CYAN else
                            if colorchange2 == 1 then Color.VIOLET else
                            if colorchange2 == -2 then Color.MAGENTA else
                            if colorchange2 == -1 then Color.PLUM else Color.GRAY);
StudyPlot2.SetLineWeight(2);
#----------
plot vwapPlot = if showVwap then studyVwap else na; # "Study-based VWAP"
vwapPlot.SetDefaultColor(Color.MAGENTA);

plot maPlot = if showMaLine then movAvg else na;    # "Study-based MA"
maPlot.SetDefaultColor(Color.GRAY);

plot bbUpperBand = if isBB then upper else na;
bbUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot bbLowerBand = if isBB then lower else na;
bbLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if isBB then bbUpperBand else na, bbLowerBand, CreateColor(76, 10, 127));

#//-------- Hlines
def UpLimit = if(type, rsiOverbought, 1);
def LoLimit = if(type, rsiOversold, -1);

plot midBand = if last then na else (UpLimit + LoLimit) / 2 ;
midBand.SetStyle(Curve.SHORT_DASH);
midBand.SetDefaultColor(Color.DARK_GRAY);

#--- Signal Line
def CUp = if (srcType > movAvg and srcType > studyVwap and srcType>srcType[1]) then CUp[1] + 1 else 0;
def CDn = if (srcType < movAvg and srcType < studyVwap and srcType<srcType[1]) then CDn[1] + 1 else 0;
def NUp = if (srcType > movAvg and movAvg > studyVwap and srcType<srcType[1]) then NUp[1] + 1 else 0;
def NDn = if (srcType < movAvg and movAvg < studyVwap and srcType>srcType[1]) then NDn[1] + 1 else 0;
def SigUp = if CUp>1 then 1 else 0;
def SigDn = if CDn>1 then 1 else 0;

plot Signal = if (SigUp or SigDn) then if(type, 80, 1.5) else na;
signal.SetHiding(!showTrendLine);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Signal.AssignValueColor(if SigUp then CreateColor(1,255,0) else
                        if SigDn then Color.RED else Color.WHITE);
plot cross = if NUp>1 or NDn>1 then if(type, 80, 1.5) else na;
cross.SetHiding(!showTrendLine);
cross.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross.AssignValueColor( if NUp then CreateColor(2,117,2) else CreateColor(117,2,2));

#--- 2nd Signal Line
def CUp2 = if (srcType2 > movAvg2 and srcType2 > studyVwap2 and srcType2>srcType2[1]) then CUp2[1] + 1 else 0;
def CDn2 = if (srcType2 < movAvg2 and srcType2 < studyVwap2 and srcType2<srcType2[1]) then CDn2[1] + 1 else 0;
def NUp2 = if (srcType2 > movAvg2 and movAvg2 > studyVwap2 and srcType2<srcType2[1]) then NUp2[1] + 1 else 0;
def NDn2 = if (srcType2 < movAvg2 and movAvg2 < studyVwap2 and srcType2>srcType2[1]) then NDn2[1] + 1 else 0;
def SigUp2 = if CUp2>0 then 1 else 0;
def SigDn2 = if CDn2>0 then 1 else 0;

plot Signal2 = if (SigUp2 or SigDn2) then if(type, 82, 1.7) else na;
signal2.SetHiding(!ShowSecondLine);
Signal2.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Signal2.AssignValueColor(if SigUp2 then Color.CYAN else
                         if SigDn2 then Color.MAGENTA else Color.WHITE);
plot cross2 = if (NUp2>0 or NDn2>0) then if(type, 82, 1.7) else na;
cross2.SetHiding(!ShowSecondLine);
cross2.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross2.AssignValueColor( if NUp2>0 then Color.VIOLET else Color.PLUM);

#---- Cloud--
AddCloud(if HighlightObOsArea then pos else na, UpLimit, CreateColor(66,0,0), CreateColor(66,0,0), yes);
AddCloud(if HighlightObOsArea then neg else na, LoLimit, CreateColor(6,45,3), CreateColor(6,45,3), yes);

# ---- BarColor
AssignPriceColor(if !showbarcolor then Color.CURRENT else
                 if colorchange == 2 then Color.GREEN else
                 if colorchange == 1 then Color.DARK_GREEN else
                 if colorchange == -2 then Color.RED else
                 if colorchange == -1 then Color.DARK_RED else Color.GRAY);

#----Div-----------
input PivotLookback  = 5; # "Pivot Lookback"
input DivBull = yes;      # "Plot Bullish"
input DivBear = yes;      # "Plot Bearish"

def divSrc = srcType;

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 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;
}
def pl = findpivots(divSrc,-1, PivotLookback, PivotLookback);
def ph = findpivots(divSrc, 1, PivotLookback, PivotLookback);

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],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);

## END CODE
 
try this.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © DDMyke
#//@version=5
#indicator(title="Smoothed RSI w/ VWAP & Moving Average", shorttitle="RSIv"
# Converted and mod by Sam4Cok@Samer800 - 10/2022
# Updated - Added MACD indicator to the study
# Updated - added second timeframe line - 06/2023
declare lower;
input showFirstLine  = yes;
input ShowSecondLine = yes;
input SecondTimeframe = AggregationPeriod.FIFTEEN_MIN;
input showBarcolor   = no;    # 'Barcolor'
input showTrendLine  = yes;   # 'Signal Line'
input HighlightObOsArea = yes;
input Source         = close; # 'Source'
input VwapTimeFrame  = {default DAY, WEEK, MONTH};
input studySelect    = {Default RSI, MACD};
input Smoothing      = yes;
input SmoothLength   = 2;     # 'smoothing'
input showMaLine     = yes;
input showVwap       = yes;
input maTypeInput    = {"SMA", "Bollinger Bands", "EMA", "SMMA (RMA)",default "WMA", "VWMA"};
input maLengthInput  = 28;
input macdMaType     = {"SMA",Default "EMA", "SMMA (RMA)", "WMA", "VWMA"}; # "MACD MA"
input bbMultInput    = 2.0;   # 'BB StdDev'
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 9;
input rsiLengthInput = 14;
input rsiOverbought  = 75;
input rsiOversold    = 25;

def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def htClose = close(Period=SecondTimeFrame);
def htVolume = volume(Period=SecondTimeFrame);
def type = if studySelect==studySelect.RSI then 1 else 0;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
# VWAP
script _VWAP {
    input src = close;
    input timeFrame = {default DAY, WEEK, MONTH};
    input v = volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case DAY:
        periodIndx = yyyyMmDd;
    case WEEK:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case MONTH:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    def volumeVwap2Sum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
        volumeVwap2Sum = v * Sqr(src);
    } else {
        volumeSum = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
        volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(src), v * Sqr(src));
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    input v = volume;
    def VWMA = SimpleMovingAvg(x * v, y) / SimpleMovingAvg(v, y);
    plot result = VWMA;
}
#ma(src, maLengthInput, maTypeInput) =>
script ma {
    input src = close;
    input maLengthInput = 34;
    input maTypeInput = "WMA";
    input htVolume = volume;
    def ma = if maTypeInput == "SMA" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "Bollinger Bands" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "EMA" then ExpAverage(src, maLengthInput) else
         if maTypeInput == "SMMA (RMA)" then WildersAverage(src, maLengthInput) else
         if maTypeInput == "WMA" then WMA(src, maLengthInput) else
         if maTypeInput == "VWMA" then vwma(src, maLengthInput, htVolume) else Double.NaN;
    plot return = ma;
}
#--- RSI
def nRSI = RSI(Length = rsiLengthInput, Price = Source);
def RSI2 = RSI(Length = rsiLengthInput, Price = htClose);
#--- MACD
def fastMA = ma(Source, macdFastLength, macdMaType);
def slowMA = ma(Source, macdSlowLength, macdMaType);
def diff = fastMA - slowMA;
def macd = ma(diff, macdSignalLength, macdMaType);
#--- 2nd MACD
def fastMA2 = ma(htClose, macdFastLength, macdMaType, htVolume);
def slowMA2 = ma(htClose, macdSlowLength, macdMaType, htVolume);
def diff2 = fastMA2 - slowMA2;
def macd2 = ma(diff2, macdSignalLength, macdMaType, htVolume);
#--- Calc
def smoothRSI = if(Smoothing, WildersSmoothing(nRsi, SmoothLength), nRsi);
def srcType   = if type then smoothRSI else macd;
def maSrcType = if type then smoothRSI else diff;
def movAvg = ma(maSrcType, maLengthInput, maTypeInput);
#--- 2nd Calc
def smoothRSI2 = if(Smoothing, WildersSmoothing(Rsi2, SmoothLength), Rsi2);
def srcType2   = if type then smoothRSI2 else macd2;
def maSrcType2 = if type then smoothRSI2 else diff2;
def movAvg2 = ma(maSrcType2, maLengthInput, maTypeInput, htVolume);
#--- BB
def upper = movAvg + StDev(maSrcType, maLengthInput) * bbMultInput;
def lower = movAvg - StDev(maSrcType, maLengthInput) * bbMultInput;
#--- 2nd BB
def upper2 = movAvg2 + StDev(maSrcType2, maLengthInput) * bbMultInput;
def lower2 = movAvg2 - StDev(maSrcType2, maLengthInput) * bbMultInput;
#//- VWAP
def studyVwap = _VWAP(srcType, VwapTimeFrame);
#//- 2nd VWAP
def studyVwap2 = _VWAP(srcType2, VwapTimeFrame, htVolume);

#//----Plot---------------------------
def StrongBull = srcType > if(type,55,-0.05) and srcType > studyVwap and srcType>srcType[1] and srcType > movAvg;
def Bullish    = srcType > if(type,55, 0.05) and srcType > studyVwap and srcType<srcType[1];
def StrongBear = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType<srcType[1] and srcType < movAvg;
def Bearish    = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType>srcType[1];

def colorchange = if StrongBull then  2 else
                  if Bullish    then  1 else
                  if Bearish    then -1 else
                  if StrongBear then -2 else 0;

plot StudyPlot = if showFirstLine then srcType else na;
StudyPlot.AssignValueColor(if colorchange == 2 then Color.GREEN else
                           if colorchange == 1 then Color.DARK_GREEN else
                           if colorchange == -2 then Color.RED else
                           if colorchange == -1 then Color.DARK_RED else Color.WHITE);
StudyPlot.SetLineWeight(2);
#//----2 nd Plot---------------------------
def StrongBull2 = srcType2 > if(type,55,-0.05) and srcType2 > studyVwap2 and srcType2>srcType2[1] and srcType2 > movAvg2;
def Bullish2    = srcType2 > if(type,55, 0.05) and srcType2 > studyVwap2 and srcType2<srcType2[1];
def StrongBear2 = srcType2 < if(type,45,-0.05) and srcType2 < studyVwap2 and srcType2<srcType2[1] and srcType2 < movAvg2;
def Bearish2    = srcType2 < if(type,45,-0.05) and srcType2 < studyVwap2 and srcType2>srcType2[1];

def colorchange2 = if StrongBull2-StrongBull2[1] then  2 else
                   if Bullish2-Bullish2[1]    then  1 else
                   if Bearish2-Bearish2[1]    then -1 else
                   if StrongBear2-StrongBear2[1] then -2 else 0;

plot StudyPlot2 = if last or !ShowSecondLine then na else srcType2;
StudyPlot2.AssignValueColor(if colorchange2 == 2 then Color.CYAN else
                            if colorchange2 == 1 then Color.VIOLET else
                            if colorchange2 == -2 then Color.MAGENTA else
                            if colorchange2 == -1 then Color.PLUM else Color.GRAY);
StudyPlot2.SetLineWeight(2);
#----------
plot vwapPlot = if showVwap then studyVwap else na; # "Study-based VWAP"
vwapPlot.SetDefaultColor(Color.MAGENTA);

plot maPlot = if showMaLine then movAvg else na;    # "Study-based MA"
maPlot.SetDefaultColor(Color.GRAY);

plot bbUpperBand = if isBB then upper else na;
bbUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot bbLowerBand = if isBB then lower else na;
bbLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if isBB then bbUpperBand else na, bbLowerBand, CreateColor(76, 10, 127));

#//-------- Hlines
def UpLimit = if(type, rsiOverbought, 1);
def LoLimit = if(type, rsiOversold, -1);

plot midBand = if last then na else (UpLimit + LoLimit) / 2 ;
midBand.SetStyle(Curve.SHORT_DASH);
midBand.SetDefaultColor(Color.DARK_GRAY);

#--- Signal Line
def CUp = if (srcType > movAvg and srcType > studyVwap and srcType>srcType[1]) then CUp[1] + 1 else 0;
def CDn = if (srcType < movAvg and srcType < studyVwap and srcType<srcType[1]) then CDn[1] + 1 else 0;
def NUp = if (srcType > movAvg and movAvg > studyVwap and srcType<srcType[1]) then NUp[1] + 1 else 0;
def NDn = if (srcType < movAvg and movAvg < studyVwap and srcType>srcType[1]) then NDn[1] + 1 else 0;
def SigUp = if CUp>1 then 1 else 0;
def SigDn = if CDn>1 then 1 else 0;

plot Signal = if (SigUp or SigDn) then if(type, 80, 1.5) else na;
signal.SetHiding(!showTrendLine);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Signal.AssignValueColor(if SigUp then CreateColor(1,255,0) else
                        if SigDn then Color.RED else Color.WHITE);
plot cross = if NUp>1 or NDn>1 then if(type, 80, 1.5) else na;
cross.SetHiding(!showTrendLine);
cross.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross.AssignValueColor( if NUp then CreateColor(2,117,2) else CreateColor(117,2,2));

#--- 2nd Signal Line
def CUp2 = if (srcType2 > movAvg2 and srcType2 > studyVwap2 and srcType2>srcType2[1]) then CUp2[1] + 1 else 0;
def CDn2 = if (srcType2 < movAvg2 and srcType2 < studyVwap2 and srcType2<srcType2[1]) then CDn2[1] + 1 else 0;
def NUp2 = if (srcType2 > movAvg2 and movAvg2 > studyVwap2 and srcType2<srcType2[1]) then NUp2[1] + 1 else 0;
def NDn2 = if (srcType2 < movAvg2 and movAvg2 < studyVwap2 and srcType2>srcType2[1]) then NDn2[1] + 1 else 0;
def SigUp2 = if CUp2>0 then 1 else 0;
def SigDn2 = if CDn2>0 then 1 else 0;

plot Signal2 = if (SigUp2 or SigDn2) then if(type, 82, 1.7) else na;
signal2.SetHiding(!ShowSecondLine);
Signal2.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Signal2.AssignValueColor(if SigUp2 then Color.CYAN else
                         if SigDn2 then Color.MAGENTA else Color.WHITE);
plot cross2 = if (NUp2>0 or NDn2>0) then if(type, 82, 1.7) else na;
cross2.SetHiding(!ShowSecondLine);
cross2.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross2.AssignValueColor( if NUp2>0 then Color.VIOLET else Color.PLUM);

#---- Cloud--
AddCloud(if HighlightObOsArea then pos else na, UpLimit, CreateColor(66,0,0), CreateColor(66,0,0), yes);
AddCloud(if HighlightObOsArea then neg else na, LoLimit, CreateColor(6,45,3), CreateColor(6,45,3), yes);

# ---- BarColor
AssignPriceColor(if !showbarcolor then Color.CURRENT else
                 if colorchange == 2 then Color.GREEN else
                 if colorchange == 1 then Color.DARK_GREEN else
                 if colorchange == -2 then Color.RED else
                 if colorchange == -1 then Color.DARK_RED else Color.GRAY);

#----Div-----------
input PivotLookback  = 5; # "Pivot Lookback"
input DivBull = yes;      # "Plot Bullish"
input DivBear = yes;      # "Plot Bearish"

def divSrc = srcType;

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 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;
}
def pl = findpivots(divSrc,-1, PivotLookback, PivotLookback);
def ph = findpivots(divSrc, 1, PivotLookback, PivotLookback);

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],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);

## END CODE
Thank you
 
View attachment 16121
Author Message:
This indicator is the default RSI with smoothing and an RSI based VWAP . I've also added the RSI based VWAP to the Moving Average options list. By default, the RSI based VWAP is turned on with the WMA selected as the Moving Average. The RSI changes colors when it is above the 55 level, VWAP , and Moving Average or below the 45 level, VWAP , and Moving Average. There is also an option for barcoloring based on the RSI colors.
[Sam4cok] - I added divergence and signal line.

CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © DDMyke
#//@version=5
#indicator(title="Smoothed RSI w/ VWAP & Moving Average", shorttitle="RSIv"
# Converted and mod by Sam4Cok@Samer800 - 10/2022
declare lower;
input showBarcolor = no;      # 'Barcolor'
input showSignalLine = yes;   # 'Signal Line'
input showBackground = yes;
input rsiSourceInput = close; # 'Source'
input showRSI = yes;
input showRSIvwap = yes;
input VwapTimeFrame = {default DAY, WEEK, MONTH};
input rsiLengthInput = 14;
input rsiOverbought = 75;
input rsiOversold = 25;
input rsiSmoothing = 2;       # 'smoothing'
input showMA = yes;
input maTypeInput = {"SMA", "Bollinger Bands", "EMA", "SMMA (RMA)",default "WMA", "VWMA"};
input maLengthInput = 20;
input bbMultInput = 2.0;      # 'BB StdDev'

def na = Double.NaN;
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
# rsiVWAP
script rsiVWAP {
    input src = close;
    input timeFrame = {default DAY, WEEK, MONTH};
    def v = Volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case DAY:
        periodIndx = yyyyMmDd;
    case WEEK:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case MONTH:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    def volumeVwap2Sum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
        volumeVwap2Sum = v * Sqr(src);
    } else {
        volumeSum = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
        volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(src), v * Sqr(src));
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#ma(smoorsi, maLengthInput, maTypeInput) =>
script ma {
    input smoorsi = close;
    input maLengthInput = 34;
    input maTypeInput = "WMA";
    def ma = if maTypeInput == "SMA" then SimpleMovingAvg(smoorsi, maLengthInput) else
         if maTypeInput == "Bollinger Bands" then SimpleMovingAvg(smoorsi, maLengthInput) else
         if maTypeInput == "EMA" then ExpAverage(smoorsi, maLengthInput) else
         if maTypeInput == "SMMA (RMA)" then WildersSmoothing(smoorsi, maLengthInput) else
         if maTypeInput == "WMA" then WMA(smoorsi, maLengthInput) else
         if maTypeInput == "VWMA" then vwma(smoorsi, maLengthInput) else Double.NaN;
    plot return = ma;
}
def nRSI = RSI(Length = rsiLengthInput, Price = rsiSourceInput);
def smoorsi = WildersSmoothing(nRSI, rsiSmoothing);
def rsiMA = ma(smoorsi, maLengthInput, maTypeInput);
def isBB = maTypeInput == maTypeInput."Bollinger Bands";

#//- VWAP
def RSIvwap = rsiVWAP(smoorsi, VwapTimeFrame);

#//----Plot---------------------------
def StrongBull = smoorsi > 55 and smoorsi > rsiMA and smoorsi > RSIvwap;
def Bullish = smoorsi > 55 and smoorsi < rsiMA and smoorsi > RSIvwap;
def StrongBear = smoorsi < 45 and smoorsi < rsiMA and smoorsi < RSIvwap;
def Bearish = smoorsi > 45 and smoorsi < rsiMA and smoorsi < RSIvwap;
def Neutral = smoorsi > 45 or smoorsi < 55;
def colorchange = if StrongBull then 2 else
                  if Bullish then 1 else
                  if Bearish then -1 else
                  if StrongBear then -2 else 0;

plot rsiPlot = if showRSI then smoorsi else na;    # "Smoothed RSI"
rsiPlot.AssignValueColor(if colorchange == 2 then Color.GREEN else
                         if colorchange == 1 then Color.DARK_GREEN else
                         if colorchange == -2 then Color.RED else
                         if colorchange == -1 then Color.DARK_RED else Color.WHITE);
rsiPlot.SetLineWeight(2);
plot vwapPlot = if showRSIvwap then RSIvwap else na;  # "RSI-based VWAP"
vwapPlot.SetDefaultColor(Color.MAGENTA);

plot maPlot = if showMA then rsiMA else na;    # "RSI-based MA"
maPlot.SetDefaultColor(Color.GRAY);

plot bbUpperBand = if isBB then rsiMA + StDev(smoorsi, maLengthInput) * bbMultInput else na;
bbUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot bbLowerBand = if isBB then rsiMA - StDev(smoorsi, maLengthInput) * bbMultInput else na;
bbLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if isBB then bbUpperBand else na, bbLowerBand, CreateColor(76, 10, 127));

#//-------- Hlines
plot rsiUpperBand = rsiOverbought;
rsiUpperBand.SetStyle(Curve.FIRM);
rsiUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot midBand = (rsiOverbought + rsiOversold) / 2 ;
midBand.SetStyle(Curve.SHORT_DASH);
midBand.SetDefaultColor(Color.DARK_GRAY);

plot rsiLowerBand = rsiOversold;
rsiLowerBand.SetStyle(Curve.FIRM);
rsiLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if showBackground then rsiUpperBand else na, midBand, CreateColor(6, 45, 3));
AddCloud(if showBackground then midBand else na, rsiLowerBand, CreateColor(66, 0, 0));
#--- Signal Line
def CrossUp = smooRSI crosses above RSIvwap;
def CrossDn = smooRSI crosses below RSIvwap;

def SigUp = if CrossUp and rsiMA > RSIvwap then 1 else 0;
def SigDn = if CrossDn and rsiMA < RSIvwap then 1 else 0;

plot Signal = if (SigUp or SigDn) then 75 else na;
signal.SetHiding(!showSignalLine);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
Signal.SetLineWeight(2);
Signal.AssignValueColor(if SigUp and smooRSI>=rsiMA then CreateColor(1,255,0) else
                        if SigUp and smooRSI<rsiMA then CreateColor(4,181,4) else
                        if SigDn and smooRSI>rsiMA then Color.DARK_RED else
                        if SigDn and smooRSI<=rsiMA then Color.RED else Color.WHITE);
plot cross = if CrossUp or CrossDn then 75 else na;
cross.SetHiding(!showSignalLine);
cross.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross.AssignValueColor( if CrossUp then CreateColor(2,117,2) else CreateColor(117,2,2));
# ---- BarColor
AssignPriceColor(if !showbarcolor then Color.CURRENT else
                 if colorchange == 2 then Color.GREEN else
                 if colorchange == 1 then Color.DARK_GREEN else
                 if colorchange == -2 then Color.RED else
                 if colorchange == -1 then Color.DARK_RED else Color.GRAY);

#----Div-----------
input PivotLookback  = 5; # "Pivot Lookback"
input DivBull = yes;      # "Plot Bullish"
input DivBear = yes;      # "Plot Bearish"

def divSrc = smoorsi;

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 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;
}
def pl = findpivots(divSrc,-1, PivotLookback, PivotLookback);
def ph = findpivots(divSrc, 1, PivotLookback, PivotLookback);

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],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);

## END CODE

Updated - Added MACD as requested by a member and modified the signal line to be trend line.

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © DDMyke
#//@version=5
#indicator(title="Smoothed RSI w/ VWAP & Moving Average", shorttitle="RSIv"
# Converted and mod by Sam4Cok@Samer800 - 10/2022
# Updated - Added MACD indicator to the study
declare lower;
input showBarcolor   = no;    # 'Barcolor'
input showTrendLine  = yes;   # 'Signal Line'
input HighlightObOsArea = yes;
input Source         = close; # 'Source'
input VwapTimeFrame  = {default DAY, WEEK, MONTH};
input studySelect    = {Default RSI, MACD};
input Smoothing      = yes;
input SmoothLength   = 2;     # 'smoothing'
input showMaLine     = yes;
input showVwap       = yes;
input showStudyLine  = yes;
input maTypeInput    = {"SMA", "Bollinger Bands", "EMA", "SMMA (RMA)",default "WMA", "VWMA"};
input maLengthInput  = 28;
input macdMaType     = {"SMA",Default "EMA", "SMMA (RMA)", "WMA", "VWMA"}; # "MACD MA"
input bbMultInput    = 2.0;   # 'BB StdDev'
input macdFastLength = 12;
input macdSlowLength = 26;
input macdSignalLength = 9;
input rsiLengthInput = 14;
input rsiOverbought  = 75;
input rsiOversold    = 25;

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def type = if studySelect==studySelect.RSI then 1 else 0;
def isBB = maTypeInput == maTypeInput."Bollinger Bands";
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
# VWAP
script _VWAP {
    input src = close;
    input timeFrame = {default DAY, WEEK, MONTH};
    def v = Volume;
    def cap = GetAggregationPeriod();
    def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
    Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
    def yyyyMmDd = GetYYYYMMDD();
    def periodIndx;
    switch (timeFrame) {
    case DAY:
        periodIndx = yyyyMmDd;
    case WEEK:
        periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
    case MONTH:
        periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
    def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
    def volumeSum;
    def volumeVwapSum;
    def volumeVwap2Sum;
    if (isPeriodRolled) {
        volumeSum = v;
        volumeVwapSum = v * src;
        volumeVwap2Sum = v * Sqr(src);
    } else {
        volumeSum = CompoundValue(1, volumeSum[1] + v, v);
        volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + v * src, v * src);
        volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(src), v * Sqr(src));
    }
    def price = volumeVwapSum / volumeSum;
    plot return = price;
}
#vwma(source, length)
script VWMA {
    input x = close;
    input y = 15;
    def VWMA = SimpleMovingAvg(x * volume, y) / SimpleMovingAvg(volume, y);
    plot result = VWMA;
}
#ma(src, maLengthInput, maTypeInput) =>
script ma {
    input src = close;
    input maLengthInput = 34;
    input maTypeInput = "WMA";
    def ma = if maTypeInput == "SMA" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "Bollinger Bands" then SimpleMovingAvg(src, maLengthInput) else
         if maTypeInput == "EMA" then ExpAverage(src, maLengthInput) else
         if maTypeInput == "SMMA (RMA)" then WildersAverage(src, maLengthInput) else
         if maTypeInput == "WMA" then WMA(src, maLengthInput) else
         if maTypeInput == "VWMA" then vwma(src, maLengthInput) else Double.NaN;
    plot return = ma;
}
#--- RSI
def nRSI = RSI(Length = rsiLengthInput, Price = Source);
#--- MACD
def fastMA = ma(Source, macdFastLength, macdMaType);
def slowMA = ma(Source, macdSlowLength, macdMaType);
def diff = fastMA - slowMA;
def macd = ma(diff, macdSignalLength, macdMaType);
#--- Calc
def smoothRSI = if(Smoothing, WildersSmoothing(nRsi, SmoothLength), nRsi);
def srcType   = if type then smoothRSI else macd;
def maSrcType = if type then smoothRSI else diff;
def movAvg = ma(maSrcType, maLengthInput, maTypeInput);
#--- BB
def upper = movAvg + StDev(maSrcType, maLengthInput) * bbMultInput;
def lower = movAvg - StDev(maSrcType, maLengthInput) * bbMultInput;
#//- VWAP
def studyVwap = _VWAP(srcType, VwapTimeFrame);

#//----Plot---------------------------
def StrongBull = srcType > if(type,55,-0.05) and srcType > studyVwap and srcType>srcType[1] and srcType > movAvg;
def Bullish    = srcType > if(type,55, 0.05) and srcType > studyVwap and srcType<srcType[1];
def StrongBear = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType<srcType[1] and srcType < movAvg;
def Bearish    = srcType < if(type,45,-0.05) and srcType < studyVwap and srcType>srcType[1];

def colorchange = if StrongBull then  2 else
                  if Bullish    then  1 else
                  if Bearish    then -1 else
                  if StrongBear then -2 else 0;

plot StudyPlot = if showStudyLine then srcType else na;
StudyPlot.AssignValueColor(if colorchange == 2 then Color.GREEN else
                           if colorchange == 1 then Color.DARK_GREEN else
                           if colorchange == -2 then Color.RED else
                           if colorchange == -1 then Color.DARK_RED else Color.WHITE);
StudyPlot.SetLineWeight(2);
plot vwapPlot = if showVwap then studyVwap else na; # "Study-based VWAP"
vwapPlot.SetDefaultColor(Color.MAGENTA);

plot maPlot = if showMaLine then movAvg else na;       # "Study-based MA"
maPlot.SetDefaultColor(Color.GRAY);

plot bbUpperBand = if isBB then upper else na;
bbUpperBand.SetDefaultColor(Color.DARK_GRAY);

plot bbLowerBand = if isBB then lower else na;
bbLowerBand.SetDefaultColor(Color.DARK_GRAY);

AddCloud(if isBB then bbUpperBand else na, bbLowerBand, CreateColor(76, 10, 127));

#//-------- Hlines
def UpLimit = if(type, rsiOverbought, 1);
def LoLimit = if(type, rsiOversold, -1);

plot midBand = if isNaN(close) then na else (UpLimit + LoLimit) / 2 ;
midBand.SetStyle(Curve.SHORT_DASH);
midBand.SetDefaultColor(Color.DARK_GRAY);

#--- Signal Line
def CUp = if (srcType > movAvg and srcType > studyVwap and srcType>srcType[1]) then CUp[1] + 1 else 0;
def CDn = if (srcType < movAvg and srcType < studyVwap and srcType<srcType[1]) then CDn[1] + 1 else 0;
def NUp = if (srcType > movAvg and movAvg > studyVwap and srcType<srcType[1]) then NUp[1] + 1 else 0;
def NDn = if (srcType < movAvg and movAvg < studyVwap and srcType>srcType[1]) then NDn[1] + 1 else 0;
def SigUp = if CUp>1 then 1 else 0;
def SigDn = if CDn>1 then 1 else 0;

plot Signal = if (SigUp or SigDn) then if(type, 80, 1.5) else na;
signal.SetHiding(!showTrendLine);
Signal.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Signal.AssignValueColor(if SigUp then CreateColor(1,255,0) else
                        if SigDn then Color.RED else Color.WHITE);
plot cross = if NUp>1 or NDn>1 then if(type, 80, 1.5) else na;
cross.SetHiding(!showTrendLine);
cross.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
cross.AssignValueColor( if NUp then CreateColor(2,117,2) else CreateColor(117,2,2));

#---- Cloud--
AddCloud(if HighlightObOsArea then pos else na, UpLimit, CreateColor(66,0,0), CreateColor(66,0,0), yes);
AddCloud(if HighlightObOsArea then neg else na, LoLimit, CreateColor(6,45,3), CreateColor(6,45,3), yes);

# ---- BarColor
AssignPriceColor(if !showbarcolor then Color.CURRENT else
                 if colorchange == 2 then Color.GREEN else
                 if colorchange == 1 then Color.DARK_GREEN else
                 if colorchange == -2 then Color.RED else
                 if colorchange == -1 then Color.DARK_RED else Color.GRAY);

#----Div-----------
input PivotLookback  = 5; # "Pivot Lookback"
input DivBull = yes;      # "Plot Bullish"
input DivBear = yes;      # "Plot Bearish"

def divSrc = srcType;

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 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;
}
def pl = findpivots(divSrc,-1, PivotLookback, PivotLookback);
def ph = findpivots(divSrc, 1, PivotLookback, PivotLookback);

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],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;

#// Regular Bearish
def oscLH   = divSrc < vhFound and  _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;

#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);

## END CODE
@samer800 Hi, first of all, thank you for converting this script it is really useful. My question is if you see red and green dots on uperside are they related to rsi? if yes is there any way you can create similar dots for macd on the lower side? so that if macd and rsi both are green then the trend is strong. Thank you very much. I have been using this script and I have observed that whenever a green or red dot appears trend holds for a while.
 
@samer800 Hi, first of all, thank you for converting this script it is really useful. My question is if you see red and green dots on uperside are they related to rsi? if yes is there any way you can create similar dots for macd on the lower side? so that if macd and rsi both are green then the trend is strong. Thank you very much. I have been using this script and I have observed that whenever a green or red dot appears trend holds for a while.

the dots based on the "study select" in the indicator settings.
1711649030606.png
 

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