RSI Div at VWAP StDev For ThinkOrSwim

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@samer800 can help me and help us
try this

CSS:
#//@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
 
try this

CSS:
#//@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
Thankyou Samer800
 
try this

CSS:
#//@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
Thank you so much for the good work @samer800, Thanks a lot.
 
Last edited:
try this

CSS:
#//@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
thank you very much @samer800 you are amazing
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
594 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