#//@crypto_rife
#study("RSI Div at VWAP StDev", overlay=true, resolution="")
# converted by Sam4Cok@Samer800 - 01/2023
#// RSI Inputs
input useChartTimeframe = yes;
input Aggregation = AggregationPeriod.FIFTEEN_MIN;
input Length = 14; # "Length"
input Oversold = 30; # "Oversold"
input Overbought = 70; # "Overbought"
input BearResetLevel = 50; # "Bear Div Reset level"
input BullResetLevel = 50; # "Bull Div Reset level"
input lookbackPeriod = 14; # "Div lookback period (bars)?"
input UseStdDev = yes; # "Use Std Dev"
input StdevUp = 1.51; # "Stdev above"
input StdevDn = 1.51; # "Stdev below"
def na = Double.NaN;
def ctf;def htf; def ltf; def vtf; def hltf;
if useChartTimeframe {
ctf = close;
htf = high;
ltf = low;
vtf = volume;
hltf = hl2;
} else {
ctf = close(Period=Aggregation);
htf = high(Period=Aggregation);
ltf = low(Period=Aggregation);
vtf = volume(Period=Aggregation);
hltf = hl2(Period=Aggregation);
}
#barssince(Condition) =>
script barssince {
input Condition = 0;
def barssince = if Condition then 0 else barssince[1] + 1;
plot return = barssince;
}
#// DIVS code
def nRSI = RSI(Price = ctf, Length = Length);
def highestbars = barssince(nRSI==Highest(nRSI, lookbackPeriod));
def lowestbars = barssince(nRSI==Lowest(nRSI, lookbackPeriod));
def hb = AbsValue(highestbars);# // Finds bar with highest value in last X bars
def lb = AbsValue(lowestbars);# // Finds bar with lowest value in last X bars
def max;
def max_rsi;
def min;
def min_rsi;
def divbear;
def divbull;
#// If bar with lowest / highest is current bar and rsi is oversold/overbought, use it's value
def max1 = if(hb==0 and nRSI > Overbought,ctf, if(IsNaN(max[1]),ctf,max[1]));
def max_rsi1 = if (nRSI < BearResetLevel) then na else
if(hb==0 and nRSI > Overbought,nRSI, if(IsNaN(max_rsi[1]),nRSI,max_rsi[1]));
def min1 = if(lb==0 and nRSI < Oversold,ctf, if(IsNaN(min[1]), ctf, min[1]));
def min_rsi1 = if (nRSI > BullResetLevel) then na else
if(lb==0 and nRSI < Oversold,nRSI , if(IsNaN(min_rsi[1]) ,nRSI, min_rsi[1]));
#// Compare high of current bar being examined with previous bar's high
#// If curr bar high is higher than the max bar high in the lookback window range
#// we have a new high
max = if ctf > max1 then ctf else max1;
max_rsi = if nRSI > max_rsi1 and nRSI > Overbought then nRSI else max_rsi1;
min = if ctf < min1 then ctf else min1;
min_rsi = if nRSI < min_rsi1 and nRSI < Oversold then nRSI else min_rsi1;
#// Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
if (max[1] > max[2]) and (nRSI[1] < max_rsi) and (nRSI <= nRSI[1]) {
divbear = yes;
divbull = no;
} else
if (min[1] < min[2]) and (nRSI[1] > min_rsi) and (nRSI >= nRSI[1]) {
divbull = yes;
divbear = no;
} else {
divbear = na;
divbull = na;
}
def volumesum;
def myvwap;
def dev;
def pos;
def yyyyMmDd = getYyyyMmDd();
def periodIndx = yyyyMmDd;
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeVwapSum;
def volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = vtf;
volumeVwapSum = vtf * hltf;
volumeVwap2Sum = vtf * Sqr(hltf);
} else {
volumeSum = compoundValue(1, volumeSum[1] + vtf, vtf);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + vtf * hltf, vtf * hltf);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + vtf * Sqr(hltf), vtf * Sqr(hltf));
}
myvwap = volumeVwapSum / volumeSum;
dev = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(myvwap), 0));
def U2 = myvwap + StdevUp * dev;
def D2 = myvwap - StdevDn * dev;
if (UseStdDev) {
if (htf > U2 and divbear) {
pos = 1;
} else
if (ltf < D2 and divbull) {
pos = -1;
} else {
pos = 0;}
} else
if (divbear) {
pos = 1;
} else
if (divbull) {
pos = -1;
} else {
pos = 0;
}
plot long = if pos ==-1 then low else na;
plot short= if pos == 1 then high else na;
long.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
short.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
long.SetDefaultColor(Color.GREEN);
short.SetDefaultColor(Color.RED);
long.SetLineWeight(2);
short.SetLineWeight(2);
#--- END CODE