Volume-Weighted RSI [wbburgin] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
3B0Fg1F.png


Author Message :
The Volume-Weighted RSI takes a new approach to the traditional calculation of the RSI in using a price::volume calculation. As some traders consider volume to be a leading indicator for price, the volume-weighted RSI can come in handy if you want to visualize volume easier.
More Details : https://www.tradingview.com/v/cM6CWwGG/

CODE:

CSS:
# https://www.tradingview.com/v/cM6CWwGG/
#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// © wbburgin
#indicator("Volume-Weighted RSI [wbburgin]",overlay=false)
# converted by Sam4Cok@Samer800    - 10 / 2023
declare lower;

input barColors = yes;             # "Bar Colors"
input length = 14;        # "Length"
input src = close;        # "Source"
input showVolumeWeightedRsi = yes; # "Plot Volume-weighted RSI"
input ShowSignalLine = yes;        # "Use Signal"
input SignalLineLength = 25;       # " | Signal Length"
input showAbnormalActivity = yes;  # "Plot Abnormal Activity"
input Multi = 2.5;                 # " | SD Multiple"
input stylesheet = {default "Dark Style", "Light Style"};
input overbought_level = 70;       # 'Overbought'
input oversold_level = 30;         # 'Oversold'

def na = Double.NaN;
def last = isNaN(Close);
def plotBase = showVolumeWeightedRsi;
def useabn = showAbnormalActivity;
# //  CORE LOGIC
#minimax(ds, p, min, max) =>
script minimax {
    input ds = close;
    input p  = 10;
    input min = 0;
    input max = 100;
    def hi = max;#Highest(ds, p);
    def lo = min;#Lowest(ds, p);
    def minimax = (max - min) * (ds - lo) / (hi - lo) + min;
    plot out = minimax;
}
#pricevolume_rsi(x, y) =>
script pricevolume_rsi {
    input x = close;
    input y = 14;
    def u_x = Max(x - x[1], 0) * Max(volume - volume[1], 0);
    def d_x = Max(x[1] - x, 0) * Max(volume[1] - volume, 0);
    def u = if u_x > 0 then Sqrt(u_x) else - Sqrt(u_x);
    def d = if d_x > 0 then Sqrt(d_x) else - Sqrt(d_x);
    def rs = WildersAverage(u, y) / WildersAverage(d, y);
    def res = 100 - 100 / (1 + rs);
    plot out = res;
}
def pvrRSI1 = pricevolume_rsi(src, length);
def pvrsi = Average(pvrRSI1, 1);
def pvrsi_ma = WildersAverage(pvrsi, SignalLineLength);

#// Abnormal Volume Calculations
def SignSrc = Sign(src - src[1]);
def volDif = AbsValue(volume - volume[1]);
def obv = Sum(SignSrc * volume, 500);
def volume_difference = volDif / volume[1];
def avgVolDif = Average(volume_difference, length);
def sDevVolDif = StDev(volume_difference, length) * Multi;
def abnormal_volume = volume_difference >= (avgVolDif + sDevVolDif);
def abv_pos = abnormal_volume and obv > obv[1];
def abv_neg = abnormal_volume and obv < obv[1];
def vd_placement = if abv_neg then 90 else if abv_pos then 10 else na;

#// Abnormal Price Calculations
def SignVol = Sign(volume - volume[1]);
def srcDif = AbsValue(src - src[1]);
def obp = Sum(SignVol * src, 500);
def price_difference = srcDif / src[1];
def avgSrcDif = Average(price_difference, length);
def sDevSrcDif = StDev(price_difference, length) * Multi;
def abnormal_price =  price_difference >= (avgSrcDif + sDevSrcDif);
def abp_pos = abnormal_price and obp > obp[1];
def abp_neg = abnormal_price and obp < obp[1];
def pd_placement = if abp_neg then 90 else if abp_pos then 10 else na;

# // STYLE & DESIGN
# // RSI Design  ---------- |
def dark = stylesheet == stylesheet."Dark Style";
def colCond = pvrsi > overbought_level or pvrsi < oversold_level;
plot plot_rsi = if plotBase then pvrsi else na;    # "Volume RSI"
plot_rsi.AssignValueColor(if colCond then Color.GRAY else
                          if dark then Color.WHITE else Color.BLACK);

#// Adding gradients on top of the PVRSI plot ---------- |
def pvrsi_up = if pvrsi > 50 then pvrsi else na;
def pvrsi_dn = if pvrsi < 50 then pvrsi else na;

def pvrsi_up_color = minimax(pvrsi, 50, 100);
def pvrsi_dn_color = minimax(pvrsi,  0, 50);

#// Signal and Bar Color ---------- |
def minmaxBar = minimax(pvrsi, oversold_level, overbought_level);
def barcolor = (if minmaxBar > overbought_level then 100 else
                if minmaxBar < oversold_level then 0 else minmaxBar) * 2.55;

plot Signal = if ShowSignalLine then pvrsi_ma else na;#, "Signal"
Signal.SetLineWeight(2);
Signal.AssignValueColor(CreateColor(255 - barcolor, barcolor, barcolor));

AssignPriceColor(if !barcolors then Color.CURRENT else CreateColor(255 - barcolor, barcolor, barcolor));


#// Overbought / Oversold  ---------- |
def obColor = (if minmaxBar > overbought_level then 80 else
               if minmaxBar < oversold_level then 20 else minmaxBar) ;

plot plot_up = if last then na else overbought_level;
plot plot_avg = if last then na else 50;
plot plot_dn = if last then na else oversold_level;

plot_avg.SetStyle(Curve.SHORT_DASH);
plot_avg.SetDefaultColor(Color.GRAY);
plot_Up.AssignValueColor(CreateColor(obColor , obColor * 2.55, obColor));
plot_Dn.AssignValueColor(CreateColor(obColor * 2.55, obColor, obColor));

AddCloud(if pvrsi > overbought_level then plot_rsi else na, plot_up, Color.GREEN);
AddCloud(if pvrsi < oversold_level then plot_dn else na, plot_rsi, Color.RED);

#/ Abnormal Activity  ---------- |

#pvs = switch
AddVerticalLine(useabn and abv_pos and abp_pos, "Spike", Color.GREEN);
AddVerticalLine(useabn and abv_neg and abp_pos, "Spike", Color.RED);

plot AbVol = if useabn then vd_placement else na;#, title="Abnormal Volume"
plot AbPri = if useabn then pd_placement else na;#, title="Abnormal Price"

AbVol.AssignValueColor(if obv < obv[1] then Color.RED else Color.GREEN);
AbPri.AssignValueColor(if obp < obp[1] then Color.MAGENTA else Color.CYAN);

AbVol.SetPaintingStrategy(PaintingStrategy.POINTS);
AbPri.SetPaintingStrategy(PaintingStrategy.TRIANGLES);

#-- END of CODE
 
3B0Fg1F.png


Author Message :
The Volume-Weighted RSI takes a new approach to the traditional calculation of the RSI in using a price::volume calculation. As some traders consider volume to be a leading indicator for price, the volume-weighted RSI can come in handy if you want to visualize volume easier.
More Details : https://www.tradingview.com/v/cM6CWwGG/

CODE:

CSS:
# https://www.tradingview.com/v/cM6CWwGG/
#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// © wbburgin
#indicator("Volume-Weighted RSI [wbburgin]",overlay=false)
# converted by Sam4Cok@Samer800    - 10 / 2023
declare lower;

input barColors = yes;             # "Bar Colors"
input length = 14;        # "Length"
input src = close;        # "Source"
input showVolumeWeightedRsi = yes; # "Plot Volume-weighted RSI"
input ShowSignalLine = yes;        # "Use Signal"
input SignalLineLength = 25;       # " | Signal Length"
input showAbnormalActivity = yes;  # "Plot Abnormal Activity"
input Multi = 2.5;                 # " | SD Multiple"
input stylesheet = {default "Dark Style", "Light Style"};
input overbought_level = 70;       # 'Overbought'
input oversold_level = 30;         # 'Oversold'

def na = Double.NaN;
def last = isNaN(Close);
def plotBase = showVolumeWeightedRsi;
def useabn = showAbnormalActivity;
# //  CORE LOGIC
#minimax(ds, p, min, max) =>
script minimax {
    input ds = close;
    input p  = 10;
    input min = 0;
    input max = 100;
    def hi = max;#Highest(ds, p);
    def lo = min;#Lowest(ds, p);
    def minimax = (max - min) * (ds - lo) / (hi - lo) + min;
    plot out = minimax;
}
#pricevolume_rsi(x, y) =>
script pricevolume_rsi {
    input x = close;
    input y = 14;
    def u_x = Max(x - x[1], 0) * Max(volume - volume[1], 0);
    def d_x = Max(x[1] - x, 0) * Max(volume[1] - volume, 0);
    def u = if u_x > 0 then Sqrt(u_x) else - Sqrt(u_x);
    def d = if d_x > 0 then Sqrt(d_x) else - Sqrt(d_x);
    def rs = WildersAverage(u, y) / WildersAverage(d, y);
    def res = 100 - 100 / (1 + rs);
    plot out = res;
}
def pvrRSI1 = pricevolume_rsi(src, length);
def pvrsi = Average(pvrRSI1, 1);
def pvrsi_ma = WildersAverage(pvrsi, SignalLineLength);

#// Abnormal Volume Calculations
def SignSrc = Sign(src - src[1]);
def volDif = AbsValue(volume - volume[1]);
def obv = Sum(SignSrc * volume, 500);
def volume_difference = volDif / volume[1];
def avgVolDif = Average(volume_difference, length);
def sDevVolDif = StDev(volume_difference, length) * Multi;
def abnormal_volume = volume_difference >= (avgVolDif + sDevVolDif);
def abv_pos = abnormal_volume and obv > obv[1];
def abv_neg = abnormal_volume and obv < obv[1];
def vd_placement = if abv_neg then 90 else if abv_pos then 10 else na;

#// Abnormal Price Calculations
def SignVol = Sign(volume - volume[1]);
def srcDif = AbsValue(src - src[1]);
def obp = Sum(SignVol * src, 500);
def price_difference = srcDif / src[1];
def avgSrcDif = Average(price_difference, length);
def sDevSrcDif = StDev(price_difference, length) * Multi;
def abnormal_price =  price_difference >= (avgSrcDif + sDevSrcDif);
def abp_pos = abnormal_price and obp > obp[1];
def abp_neg = abnormal_price and obp < obp[1];
def pd_placement = if abp_neg then 90 else if abp_pos then 10 else na;

# // STYLE & DESIGN
# // RSI Design  ---------- |
def dark = stylesheet == stylesheet."Dark Style";
def colCond = pvrsi > overbought_level or pvrsi < oversold_level;
plot plot_rsi = if plotBase then pvrsi else na;    # "Volume RSI"
plot_rsi.AssignValueColor(if colCond then Color.GRAY else
                          if dark then Color.WHITE else Color.BLACK);

#// Adding gradients on top of the PVRSI plot ---------- |
def pvrsi_up = if pvrsi > 50 then pvrsi else na;
def pvrsi_dn = if pvrsi < 50 then pvrsi else na;

def pvrsi_up_color = minimax(pvrsi, 50, 100);
def pvrsi_dn_color = minimax(pvrsi,  0, 50);

#// Signal and Bar Color ---------- |
def minmaxBar = minimax(pvrsi, oversold_level, overbought_level);
def barcolor = (if minmaxBar > overbought_level then 100 else
                if minmaxBar < oversold_level then 0 else minmaxBar) * 2.55;

plot Signal = if ShowSignalLine then pvrsi_ma else na;#, "Signal"
Signal.SetLineWeight(2);
Signal.AssignValueColor(CreateColor(255 - barcolor, barcolor, barcolor));

AssignPriceColor(if !barcolors then Color.CURRENT else CreateColor(255 - barcolor, barcolor, barcolor));


#// Overbought / Oversold  ---------- |
def obColor = (if minmaxBar > overbought_level then 80 else
               if minmaxBar < oversold_level then 20 else minmaxBar) ;

plot plot_up = if last then na else overbought_level;
plot plot_avg = if last then na else 50;
plot plot_dn = if last then na else oversold_level;

plot_avg.SetStyle(Curve.SHORT_DASH);
plot_avg.SetDefaultColor(Color.GRAY);
plot_Up.AssignValueColor(CreateColor(obColor , obColor * 2.55, obColor));
plot_Dn.AssignValueColor(CreateColor(obColor * 2.55, obColor, obColor));

AddCloud(if pvrsi > overbought_level then plot_rsi else na, plot_up, Color.GREEN);
AddCloud(if pvrsi < oversold_level then plot_dn else na, plot_rsi, Color.RED);

#/ Abnormal Activity  ---------- |

#pvs = switch
AddVerticalLine(useabn and abv_pos and abp_pos, "Spike", Color.GREEN);
AddVerticalLine(useabn and abv_neg and abp_pos, "Spike", Color.RED);

plot AbVol = if useabn then vd_placement else na;#, title="Abnormal Volume"
plot AbPri = if useabn then pd_placement else na;#, title="Abnormal Price"

AbVol.AssignValueColor(if obv < obv[1] then Color.RED else Color.GREEN);
AbPri.AssignValueColor(if obp < obp[1] then Color.MAGENTA else Color.CYAN);

AbVol.SetPaintingStrategy(PaintingStrategy.POINTS);
AbPri.SetPaintingStrategy(PaintingStrategy.TRIANGLES);

#-- END of CODE
Great study. As of now the signal line usually stays between 65 and 25 occasionally going above or below. Is there a way to expand it so it fluctuates between 100 and 0 or 90/10? I realize that moves everything up, and occasionally it will go above 100 and below 0, but it would really help me combine it with other studies. Thank you Samer800
 

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