#//@version=4
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// © tvjvzl
#study("PVT RSI")
# Converted and mod by Sam4Cok@Samer800 - 11/2022
Declare lower;
input src = close;#, "Source")
input smmaLength = 1;#, "SMMA length")
input rsiLength = 14;
input UpperLimit = 70;
input LowerLimit = 30;
def na = Double.NaN;
def h = high;
def l = low;
def v = volume;
def c = close;
def deltap = (src[0] - src[1])/src[1];
def cratio = TotalSum(deltap*v);
def smma;
smma = if isNaN(smma[1]) then SimpleMovingAvg(cratio, smmaLength) else
(smma[1] * (smmaLength - 1) + cratio) / smmaLength;
plot nRSI = rsi(Price=smma, Length=rsiLength);#, "Length")), color = color.new(#ec407a,0))
nRSI.AssignValueColor(if nRSI>UpperLimit then GetColor(5) else
if nRSI<LowerLimit then GetColor(1) else CreateColor(236,64,122));
nRSI.SetLineWeight(2);
plot midLine = if isNaN(c) then na else 50; # "Middle Band"
plot OB = if isNaN(c) then na else UpperLimit; # "Upper limit"
plot OS = if isNaN(c) then na else LowerLimit; # "Lower limit"
midLine.SetStyle(Curve.SHORT_DASH);
midLine.SetDefaultColor(Color.DARK_GRAY);
OB.SetStyle(Curve.MEDIUM_DASH);
OB.SetDefaultColor(Color.DARK_GRAY);
OS.SetStyle(Curve.MEDIUM_DASH);
OS.SetDefaultColor(Color.DARK_GRAY);
AddCloud(OB, OS, CreateColor(63, 6, 26));
#----Div-----------
input lbR = 5; # "Pivot Lookback Right"
input lbL = 5; # "Pivot Lookback Left"
input MaxLookback = 60; # "Max of Lookback Range"
input MinLookback = 5; # "Min of Lookback Range"
input DivBull = yes; # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = yes; # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"
def divSrc = nRSI;
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, lbL, lbR);
def ph = findpivots(divSrc, 1, lbL, lbR);
def plFound = if !isNaN(pl) then 1 else 0;
def phFound = if !isNaN(ph) then 1 else 0;
def vlFound = valuewhen(plFound, divSrc, 0, 1);
def vhFound = valuewhen(phFound, divSrc, 0, 1);
def plPrice = valuewhen(plFound, l, 0, 1);
def phPrice = valuewhen(phFound, h, 0, 1);
#// Regular Bullish
def oscHL = divSrc > vlFound and _inRange(plFound[1],MaxLookback,MinLookback);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc[lbr] < vlFound and _inRange(plFound[1],MaxLookback,MinLookback);
def priceHL = l[lbr] > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;
#// Regular Bearish
def oscLH = divSrc < vhFound and _inRange(phFound[1],MaxLookback,MinLookback);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and _inRange(phFound[1],MaxLookback,MinLookback);
def priceLH = h < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;
#------ Bubbles
addchartbubble(bullCond, divSrc, "R", color.GREEN, no);
addchartbubble(bearCond, divSrc, "R", Color.RED, yes);
addchartbubble(hiddenBullCond, divSrc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, divSrc, "H", color.DARK_red, yes);
#---- END