RSI-Adaptive, GKYZ-Filtered DEMA [Loxx] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
3V9p6zz.png

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:
@samer800 Just want to say I really look forward to you posting these indicators. Thank you for your work! Just curious, out of all the indicators you have posted which ones are your favorite? Do you currently use any in your trading strategy?
 
@samer800 Just want to say I really look forward to you posting these indicators. Thank you for your work! Just curious, out of all the indicators you have posted which ones are your favorite? Do you currently use any in your trading strategy?
no preferences. I just enjoy what I am doing and share it with this great community to benefits. Try to learn more till you find your own strategy :).
 

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