Volume Weighted RSI: Extending The Power (TradingView)

Lummox

New member
hey everyone,
long time lurker, first time poster. i was wondering if there was anyone interested in converting this indicator from Tradingview for ThinkorSwim:

https://www.tradingview.com/script/...d-RSI-Extending-the-power-of-the-classic-RSI/

iLq8jMQ.png


here's the code:

Ruby:
//@version=3
study(title="RedK Relative Strength Index", shorttitle="RedK_RSI", scale=scale.right, precision=2)

//inputs
src = close
len = input(10, minval=1, title="Length")
sentiment = input(3, minval=1, title="Sentiment Factor")
smooth = input(3, minval=1, title="Smoothing")
vw = input(defval = true, title="Volume Weighted ?"  )
step = input(title="Step", defval=5, maxval=50, minval=0, step=5)

//Calculations  -- prefer to use wma 
v = vw ? volume : 1
longlen = sentiment * len

up = wma(max(change(src)*v, 0), len)
down = wma(-min(change(src)*v, 0), len)
rsiraw= down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiraw100 = rsiraw *2 -100
rsi = wma(rsiraw100, smooth)

longup = wma(max(change(src)*v, 0), longlen)
longdown = wma(-min(change(src)*v, 0), longlen)
longrsiraw= longdown == 0 ? 100 : longup == 0 ? 0 : 100 - (100 / (1 + longup / longdown))
longrsiraw100 = longrsiraw *2 -100
longrsi = wma(longrsiraw100, smooth)
longrsis = step > 0 ? round(longrsi/step) * step : longrsi 

//Plot
//Plot levels and sentiment first, so the visual order is correct - doesn't cover the main plot
zeroline = hline(0, title = 'Zero Line', color=yellow, linestyle=line)
hline(40, title = 'Strong Up Level', color=green, linestyle = dotted)
hline(-40, title = 'Strong Down Level', color=red, linestyle = dotted)

plot(longrsis, title='Sentiment',style=area , transp=20, color=longrsis >=0 ? green : red , linewidth=2)

plot(rsiraw100, color=purple)
plot(rsi, title='K_RSI',style = line , transp=20, color= rsi >=0 ? aqua : orange , linewidth=2)


SignalDn = rsi < 0 and rsi[1] >= 0 
plotshape(SignalDn, title = "Signal Down", style=shape.triangledown, location = location.top, transp = 20, size=size.small, color = red, offset = -1 )

SignalUp = rsi >=0 and rsi[1] < 0 
plotshape(SignalUp, title = "Signal Up", style=shape.triangleup, location = location.bottom, transp = 20, size=size.small, color = green, offset = -1 )

--------------------------------------------------------------------------------------------------------------------------------------

note: i removed the "offset = -1" from the original code (lines 42 and 45) for the SignalUp and SignalDn arrows since they are the most important component of the indicator as i use it

appreciate any and all help and am curious if anyone else here has been using this indicator themselves.
 
Last edited by a moderator:

MerryDay

Administrative
Staff member
Staff
VIP
Converting this script is beyond my abilities :( but it has tweaked my interest.
This displays two RSIs w/ fast and short lengths which we do already have BUT it provides the option to make them volume-weighted!
The idea of adding a volume weighted option to my existing indicators is intriguing? If someone takes on the conversion, we could use the volume-weighted snippet to explore other potentials.

This is what the author on TradingView has to say:
https://www.tradingview.com/script/...d-RSI-Extending-the-power-of-the-classic-RSI/
a2.png

@Lummox
 
Last edited:

Lummox

New member
@MerryDay, thanks for helping spread the word.

the volume-weighted and smoothing options are what makes this indicator interesting to me. at the moment i use the FW_DPO_MOBO indicator to time my entries and after reviewing several previous trades this indicator looks very promising as an alternative.
 

netarchitech

Well-known member
@Lummox @MerryDay

Well, I gave it a shot, but it's not quite there yet...I'm hoping by posting my attempt here one of the many talented thinkscripters on uTS will take up the challenge and finish the script...

Ruby:
declare lower;

#Inputs
input src = close;
input len = 10;
input sentiment = 3;
input smooth = 3;
input vw = yes;
# Steps from 0 to 50 in increments of 5
input step = 5;


#Calculations - prefer to use wma
def v = if vw then volume else 1;
def longlen = sentiment * len;

def up = wma(Max(src * v, 0), len);
def down = wma(-Min(src * v, 0), len);
def rsiraw = if down == 0 then 100 else if up == 0 then 0 else 100 - (100 / (1 + up / down));

def longup = wma(Max(src * v, 0), longlen);
def longdown = wma(-Min(src * v, 0), longlen);
def longrsiraw = if longdown == 0 then 100 else if longup == 0 then 0 else 100 - (100 / (1 + longup / longdown));
def longrsiraw100 = longrsiraw * 2 - 100;
def longrsi = wma(longrsiraw100, smooth);


#Plots
# Note: Plot levels and sentiment first, so the visual order is correct - doesn't cover the main plot
plot zeroline = 0;
zeroline.SetDefaultColor(Color.YELLOW);

plot topline = 40;
topline.SetDefaultColor(Color.GREEN);
topline.SetStyle(Curve.SHORT_DASH);

plot bottomline = -40;
bottomline.SetDefaultColor(Color.RED);
bottomline.SetStyle(Curve.SHORT_DASH);

plot longrsis = if step > 0 then Round(longrsi / step) * step else longrsi;
longrsis.AssignValueColor(if longrsis >= 0 then Color.GREEN else Color.RED);
longrsis.SetLineWeight(2);

plot rsiraw100 = rsiraw * 2 - 100;
rsiraw100.SetDefaultColor(Color.MAGENTA);

plot rsi = wma(rsiraw100, smooth);
rsi.AssignValueColor(if rsi >= 0 then Color.CYAN else Color.ORANGE);
rsi.SetLineWeight(2);

plot signaldn = rsi < 0 and rsi[1] >= 0;
signaldn.SetDefaultColor(Color.RED);
signaldn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
signaldn.SetLineWeight(4);

plot signalup = rsi >= 0 and rsi[1] < 0;
signalup.SetDefaultColor(Color.GREEN);
signalup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
signalup.SetLineWeight(4);

addcloud(longrsis, zeroline, Color.GREEN, Color.RED);
 

samport

New member
VIP
@MerryDay @Lummox @horserider

Here is a modest attempt; I am not a coder by any means but I have reverse engineered quite a few scripts since joining this awesome community. I used the script for MyRSI with NET as the base and then applied volume weighted MA as the divisor. I then attempted to scale to fit and finally, added the Volume Weighted MACD to show some idea of volume. I'm sure that many will ask to have alert arrows. The parameters for showing a buy/sell signal can be changed to fit your trading needs. My plot shows the signals fine, but also plots useless arrows along the zero line. Any help to stop that and this indicator might be a nice first attempt.

Code:
# Volume Weighted RSI compiled by samport 08/13/2021

# idea from Lummox reference RedK Vol Weighted RSI on trading view: 
# https://www.tradingview.com/script/BaTELgMy-RedK-Vol-Weighted-RSI-Extending-the-power-of-the-classic-RSI/
# utilized TOS study myRSI with NET to create oscillator framework for RSI and then applied volume weighted MA as the divisor
# the Vol weighted MACD was then added and scaled to fit as suggested by Horserider; the MACD settings are 3/10/16 to approx LBR
# 3/10 oscillator
# Please feel free to test and make any corrections/improvements to code

declare lower;

input rsiLength = 5;
input netLength = 10;
input MACDlength = 16;
input showBreakoutSignals = no;

#def divisor = Sum(AbsValue(close - close[1]), rsiLength);
def divisor = Sum(volume * close, netLength) / Sum(volume, netLength);
plot MyRSI = if divisor != 0 then Sum(close - close[1], rsiLength) / divisor else 0;

def num = fold i = 1 to netLength with s do s - ( fold j = 0 to i with s2 do s2 + Sign(GetValue(MyRSI, i, netLength -  1) - GetValue(MyRSI, j, netLength - 2)) );
def den = 0.5 * netLength * (netLength - 1);

plot NET = num / den;
NET.AssignValueColor(if NET > 0 then Color.CYAN else if NET < 0 then Color.DARK_ORANGE else Color.CURRENT);

#Vol weighted MACD

def fastAvg = Sum(volume * close, 3) / Sum(volume, 3)/100;
def slowAvg = Sum(volume * close, 10) / Sum(volume, 10)/100;
plot Value = (fastAvg - slowAvg);
plot Avg = ExpAverage(Value, MACDLength);
plot Diff = Value - Avg;

plot ZeroLine = 0;

plot StrongUP = .40;
StrongUP.SetDefaultColor(Color.GREEN);
StrongUP.SetStyle(Curve.SHORT_DASH);

plot StrongDN = -.40;
StrongDN.SetDefaultColor(Color.RED);
StrongDN.SetStyle(Curve.SHORT_DASH);

def Buy = if Diff > 0 and Diff[1] < 0 then 1 else 0;
def Sell = If Diff < 0 and Diff[1] > 0 then 1 else 0;

plot UpSignal = Buy;
Plot DownSignal = Sell;

UpSignal.SetHiding(!showBreakoutSignals);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

DownSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

#MyRSI.SetDefaultColor(GetColor(2));
#NET.SetDefaultColor(GetColor(1));

ZeroLine.SetDefaultColor(Color.YELLOW);
Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if diff >= 0 then if diff > diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if diff < diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
 

horserider

Well-known member
VIP
@samport Nicely done. I think. Not sure I understand your study so I cannot help except change to this code for the arrows.

def Buy = if Diff > 0 and Diff[1] < 0 then 0 else double.NaN;
def Sell = If Diff < 0 and Diff[1] > 0 then 0 else double.NaN;


A simpler solution might be to just change the code in whatever RSI study you use as shown below.

input averageType = AverageType.WEIGHTED;

def NetChgAvg = MovingAverage(averageType, price * vol - price[1]* vol, length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price * vol - price[1]* vol), length);

If want 2 RSIs plug the code into the UltimateRSI. Try longer lengths as an example 34, 144.
 

samport

New member
VIP
@horserider

Thank you for the code correction regarding the arrows. Honestly, I only compiled the code for this indicator to see if I could do it. I like the idea of volume weighted indicators, but I don't see much yield from this indicator. I will take your suggestion for adding the volume weighted average into a standard RSI and see how it plots.
 

Similar threads

Top