Files
2024-01-25 04:07:49 +03:30

166 lines
6.3 KiB
Plaintext

//@version=3
study(title="Ultra RSI [DW]", overlay=false)
//by Donovan Wall
//This is an RSI Variation with six different averaging methods to choose from.
//The averaging methods I've included in this script are:
//-Exponential Moving Average
//-Simple Moving Average
//-Smoothed Moving Average
//-Weighted Moving Average
//-Volume Weighted Moving Average
//-Arnaud Legoux Moving Average
//Each method produces a different, yet significant gauge of relative strength.
//Custom bar colors are included.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Updates:
// - Added two more averages, Coefficient of Variation Weighted Moving Average and Kaufman's Adaptive Moving Average, to the available averaging methods.
// - Added a Laguerre mode to the script, which calculates the RSI using the Laguerre method instead of the conventional method when enabled.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Updates:
//Updated the filter types for RSI calculation
//The available filters for RSI calculation are:
//-> Exponential Moving Average
//-> Double Exponential Moving Average
//-> Simple Moving Average
//-> Smoothed Moving Average
//-> Weighted Moving Average
//-> Volume Weighted Moving Average
//-> Arnaud Legoux Moving Average
//-> Coefficient of Variation Moving Average
//-> Kaufman's Adaptive Moving Average
//The filter type and RSI type are selectable via easy to use dropdown menus.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Source
src = input(defval=close, title="Source")
//Periods
per = input(defval=14, minval=1, title="Sampling Period")
//Filter Type
ftype = input(defval="EMA", options=["EMA", "DEMA", "SMA", "SMMA", "WMA", "VWMA", "ALMA", "COVWMA", "KAMA"], title="Filter Type")
//RSI Type
rsitype = input(defval="Standard", options=["Standard", "Laguerre"], title="RSI Type")
//Volume Type
voltype = input(defval="Default", options=["Default", "Tick"], title="Volume Type")
//ALMA Offset and Sigma
aoff = input(defval=0.85, step=0.01, minval=0, title="Offset (if ALMA)")
sigma = input(defval=6, minval=0, title="Sigma (if ALMA)")
//KAMA
fast = input(defval=0.666, step=0.001, title="Smoothing Constant Fast End (if KAMA)")
slow = input(defval=0.0645, step=0.0001, title="Smoothing Constant Slow End (if KAMA)")
//Thresholds
obt = input(defval=70, title="Overbought Threshold")
ost = input(defval=30, title="Oversold Threshold")
//Laguerre Mode
gamma = input(defval=0.75, minval=0.1, step=0.01, maxval=0.9, title="Gamma (if Laguerre Mode is Active)")
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Gains and Losses
srcu = src > src[1] ? src - src[1] : 0
srcd = src < src[1] ? abs(src - src[1]) : 0
//DEMA
dema(x, t)=>
dema = 2*ema(x, t) - ema(ema(x, t), t)
dema
//VWMA
VWMA(x, t)=>
tick = syminfo.mintick
rng = close - open
tickrng = tick
tickrng := abs(rng) < tick ? nz(tickrng[1]) : rng
tickvol = abs(tickrng)/tick
vol = voltype=="Default" ? (volume==na ? tickvol : volume) : tickvol
vmp = x*vol
VWMA = sum(vmp, t)/sum(vol, t)
VWMA
//SMMA
smma(x, t)=>
smma = x
smma := na(smma[1]) ? sma(x, t) : (nz(smma[1])*(t - 1) + x)/t
smma
//COVWMA
covwma(x, t) =>
cov = stdev(x, t)/sma(x, t)
cw = x*cov
covwma = sum(cw, t)/sum(cov, t)
covwma
//KAMA
kama(x, t)=>
dist = abs(x[0] - x[1])
signal = abs(x - x[t])
noise = sum(dist, t)
effr = noise != 0 ? signal/noise : 1
sc = pow(effr*(fast - slow) + slow, 2)
kama = x
kama := nz(kama[1]) + sc*(x - nz(kama[1]))
kama
//Filters
mau = ftype=="EMA" ? ema(srcu, per) : ftype=="DEMA" ? dema(srcu, per) : ftype=="SMA" ? sma(srcu, per) : ftype=="SMMA" ? smma(srcu, per) : ftype=="WMA" ? wma(srcu, per) : ftype=="VWMA" ? VWMA(srcu, per) : ftype=="ALMA" ? alma(srcu, per, 0.85, 6) : ftype=="COVWMA" ? covwma(srcu, per) : kama(srcu, per)
mad = ftype=="EMA" ? ema(srcd, per) : ftype=="DEMA" ? dema(srcd, per) : ftype=="SMA" ? sma(srcd, per) : ftype=="SMMA" ? smma(srcd, per) : ftype=="WMA" ? wma(srcd, per) : ftype=="VWMA" ? VWMA(srcd, per) : ftype=="ALMA" ? alma(srcd, per, 0.85, 6) : ftype=="COVWMA" ? covwma(srcd, per) : kama(srcd, per)
//Laguerre RSI
l0 = src
l0 := (1 - gamma)*src + gamma*nz(l0[1])
l1 = l0
l1 := -gamma*l0 + nz(l0[1]) + gamma*nz(l1[1])
l2 = l1
l2 := -gamma*l1 + nz(l1[1]) + gamma*nz(l2[1])
l3 = l2
l3 := -gamma*l2 + nz(l2[1]) + gamma*nz(l3[1])
cu = (l0 > l1 ? l0 - l1 : 0) + (l1 > l2 ? l1 - l2 : 0) + (l2 > l3 ? l2 - l3 : 0)
cd = (l0 < l1 ? l1 - l0 : 0) + (l1 < l2 ? l2 - l1 : 0) + (l2 < l3 ? l3 - l2 : 0)
lrsi = 100*((cu + cd)==0 ? -1 : (cu + cd)==-1 ? 0 : cu/((cu + cd)==0 ? -1 : cu + cd))
//RSI
rs = mau/mad
rsi = rsitype=="Laguerre" ? lrsi : 100 - (100/(1 + rs))
//Color
rsicolor = (rsi > 50) and (src > src[1]) ? lime : (rsi > 50) and (src <= src[1]) ? green : (rsi < 50) and (src < src[1]) ? red : (rsi < 50) and (src >= src[1]) ? maroon : orange
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Thresholds
obplot = plot(obt, color=lime, title="Overbought Threshold")
mplot = plot(50, color=orange, title="Midline")
osplot = plot(ost, color=red, title="Oversold Threshold")
//RSI
rsiplot = plot(rsi, color=rsicolor, transp=0, title="RSI")
//Fill
fill(rsiplot, mplot, color=rsicolor, transp=60, title="RSI Fill")
//Bar Color
barcolor(rsicolor, title="Bar Colors")