Author Message:
RSI-Adaptive, GKYZ-Filtered DEMA is a Garman-Klass-Yang-Zhang Historical Volatility Filtered, RSI-Adaptive Double Exponential Moving Average . This is an experimental indicator. The way this is calculated is by turning RSI into an alpha value that is then injected into a DEMA function to output price. Price is then filtered using GKYZ Historical volatility . This process of creating an alpha out of RSI is only relevant to EMA-based moving averages that use an alpha value for it's calculation.
What is Garman-Klass-Yang-Zhang Historical Volatility?
Yang and Zhang derived an extension to the Garman Klass historical volatility estimator that allows for opening jumps. It assumes Brownian motion with zero drift. This is currently the preferred version of open-high-low-close volatility estimator for zero drift and has an efficiency of 8 times the classic close-to-close estimator. Note that when the drift is nonzero, but instead relative large to the volatility , this estimator will tend to overestimate the volatility . The Garman-Klass-Yang-Zhang Historical Volatility calculation is as follows:
CODE:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
#indicator("RSI-Adaptive, GKYZ-Filtered DEMA [Loxx]",
# Converted by Sam4Cok@Samer800 - 01/2023
input source = hl2;
input rsiLength = 15; # "RSI Period"
input filterop = {Price, RSIAGKYZFDEMA, default Both, None}; # "Filter Options"
input filterDevaitions = 2; # "Filter Devaitions"
input filterPeriod = 15; # "Filter Period"
input colorbars = yes; # "Color bars?"
input showSignals = yes; # "Show Signals?"
script nz {
input data = hl2;
input repl = 0;
def ret_val = if IsNaN(data) or !data then repl else data;
plot return = ret_val;
}
#gkyzFilter(float src, int len, float filter)=>
script gkyzFilter {
input src = hl2;
input len = 14;
input filter = 0;
def price;# = src
def gzkylog = Log(open / nz(close[1], close));
def pklog = Log(high / low);
def gklog = Log(close / open);
def garmult = (2 * Log(2) - 1);
def gkyzsum = 1 / len * Sum(Power(gzkylog, 2), len);
def parkinsonsum = 1 / (2 * len) * Sum(Power(pklog, 2), len);
def garmansum = garmult / len * Sum(Power(gklog, 2), len);
def sum = gkyzsum + parkinsonsum - garmansum;
def devpercent = Sqrt(sum);
def filtdev = filter * devpercent * src;
def price1 = nz(price[1], src);
price = if AbsValue(src - price1) < filtdev then price1 else src;
plot return = price;
}
#ema(float src, float alpha)=>
script ema {
input src = hl2;
input alpha = 0;
def out;# = src
out = CompoundValue(1, out[1] + alpha * (src - out[1]), src);
plot return = out;
}
#demaAlpha(float src, float alpha)=>
script demaAlpha {
input src = hl2;
input alpha = 0;
def e1 = ema(src, alpha);
def e2 = ema(e1, alpha);
def out = 2 * e1 - e2;
plot return = out;
}
def BothPrice = filterop == filterop.Both or filterop == filterop.Price and filterDevaitions > 0;
def BothRSI = filterop == filterop.Both or filterop == filterop.RSIAGKYZFDEMA and filterDevaitions > 0;
def src = if BothPrice then gkyzFilter(source, filterperiod, filterDevaitions) else source;
def nRSI = RSI(PRICE = src, LENGTH = rsiLength);
def alpha = AbsValue(nRSI /100 - 0.5) * 2.0;
def out1 = demaAlpha(src, alpha);
def out = if BothRSI then gkyzFilter(out1, filterperiod, filterDevaitions) else out1;
def sig = out[1];
def goLong_pre = Crosses(out, sig, CrossingDirection.ABOVE);
def goShort_pre = Crosses(out, sig, CrossingDirection.BELOW);
def contSwitch;
contSwitch = if goLong_pre then 1 else if goShort_pre then -1 else contSwitch[1];
def goLong = goLong_pre and (contSwitch - contSwitch[1]);
def goShort = goShort_pre and (contSwitch - contSwitch[1]);
def colorout = if contSwitch == 1 then 1 else 0;
plot RSIAATRFT3 = out;#, "RSIAATRFT3"
RSIAATRFT3.SetLineWeight(2);
RSIAATRFT3.AssignValueColor(if colorout then Color.CYAN else Color.MAGENTA);
#--- Bar Color
AssignPriceColor(if !colorbars then Color.CURRENT else
if colorout then Color.GREEN else Color.RED);
#--- Bubbles
AddChartBubble(showSignals and goLong, low, "Long", color.CYAN, no);
AddChartBubble(showSignals and goShort,high, "Short", color.MAGENTA, yes);
#-- END CODE
Last edited by a moderator: