Convert RSI Div at VWAP StDev into TOS

Platform
  1. TradingView
here is the TV code:

Code:
//@version=4
study("RSI Div at VWAP StDev", overlay=true, resolution="")
// RSI Inputs
len = input(14, minval=1, title="Length")
os = input(30, minval=1, title="Oversold")
ob = input(70, minval=1, title="Overbought")
hr = input(50, minval=1, title="Bear Div Reset level")
lr = input(50, minval=1, title="Bull Div Reset level")
xbars = input(defval=14, title="Div lookback period (bars)?", type=input.integer, minval=1)
// VWAP Inputs
useVWAP = input(true, title="Use Std Dev")
devUp = input(1.51, title="Stdev above")
devDn = input(1.51, title="Stdev below")
// DIVS code
rsi = rsi(close, len)
hb = abs(highestbars(rsi, xbars)) // Finds bar with highest value in last X bars
lb = abs(lowestbars(rsi, xbars)) // Finds bar with lowest value in last X bars
max = float(na)
max_rsi = float(na)
min = float(na)
min_rsi = float(na)
divbear = bool(na)
divbull = bool(na)
// If bar with lowest / highest is current bar and rsi is oversold/overbought, use it's value
max := hb == 0 and rsi > ob ? close : na(max[1]) ? close : max[1]
max_rsi := hb == 0 and rsi > ob ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
min := lb == 0 and rsi < os ? close : na(min[1]) ? close : min[1]
min_rsi := lb == 0 and rsi < os ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]
// Reset if RSI crosses the Reset level
if (rsi < hr)
    max_rsi := float(na)
if (rsi > lr)
    min_rsi := float(na)
// 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
if close > max // we have a new high
    max := close // change variable "max" to use current bar's high value
if rsi > max_rsi and rsi > ob // we have a new high
    max_rsi := rsi // change variable "max_rsi" to use current bar's RSI value
if close < min // we have a new low
    min := close // change variable "min" to use current bar's low value
if rsi < min_rsi and rsi < os // we have a new low
    min_rsi := rsi // change variable "min_rsi" to use current bar's RSI value
  
// Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
if (max[1] > max[2]) and (rsi[1] < max_rsi) and (rsi <= rsi[1])
    divbear := true
if (min[1] < min[2]) and (rsi[1] > min_rsi) and (rsi >= rsi[1])
    divbull := true

// VWAP Code
tickerid = tickerid(syminfo.prefix, syminfo.ticker, session.regular, adjustment.splits)
start = security(tickerid, "D", time)
newSession = iff(change(start), 1, 0)
vwapsum = 0.0
volumesum = 0.0
v2sum = 0.0
myvwap = 0.0
dev = 0.0
pos = 0
possig = 1
vwapsum := iff(newSession, hl2*volume, vwapsum[1]+hl2*volume)
volumesum := iff(newSession, volume, volumesum[1]+volume)
v2sum := iff(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2)
myvwap := vwapsum/volumesum
dev := sqrt(max(v2sum/volumesum - myvwap*myvwap, 0))
U2=myvwap + devUp * dev
D2=myvwap - devDn * dev
pos := 0
if (useVWAP)
    if (high > U2 and divbear)
        pos := 1
    if (low < D2 and divbull)
        pos := -1
else
    if (divbear)
        pos := 1
    if (divbull)
        pos := -1
plotshape(pos == -1, "long", location = location.belowbar, color = color.green, style=shape.triangleup, size=size.tiny)
plotshape(pos == 1, "short", location = location.abovebar, color = color.red, style=shape.triangledown, size=size.tiny)
 
Last edited by a moderator:
hi group, no need to convert the indicator, here is the conversion from another source:

Code:
#//@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
 
Last edited by a moderator:
Top