JFD-Adaptive, GKYZ-Filtered KAMA [Loxx] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
2lFEHVo.png

Creator Message:
JFD-Adaptive, GKYZ-Filtered KAMA is a Kaufman Adaptive Moving Average with the option to make it Jurik Fractal Dimension Adaptive. This also includes a Garman-Klass-Yang-Zhang Historical Volatility Filter to reduce noise.

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
#https://www.tradingview.com/v/KJQp4GrB/
#indicator("JFD-Adaptive, GKYZ-Filtered KAMA [Loxx]", shorttitle = "JFDAGKYZFKAMA [Loxx]"
# converted by Sam4Cok@Samer800    - 03/2023

script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}
#fdRange(int size)=>
script fdRange {
    input size = 10;
    def closeSize = if close[size] == 0 or IsNaN(close[size]) then close[2] else close[size];
    def out = Max(closeSize, Highest(high, size)) - Min(closeSize, Lowest(low, size));
    plot return = out;
}
#jurikFractalDimension(int size, int count)=>
script jurikFractalDimension {
    input size = 10;
    input count = 2;
    def window1 = size * (count - 1);
    def window2 = size * count;
    def smlRange;
    def smlRange1;
    def smlSumm1;
    def smlSumm;
    smlRange1 = fdRange(window2);
    smlRange  = fdRange(size);
    smlSumm1 = smlSumm[1] + smlRange;
    smlSumm = CompoundValue(1, smlSumm[1] - smlRange[window1], smlSumm1);
    def out = 2.0 - Log(smlRange1 / (smlSumm / window1)) / Log(count);
    plot return = out;
}
#kama(float src, int period, float fast, float slow, float power, bool JurikFD)=>
script kama {
    input src = close;
    input period = 10;
    input fast = 2;
    input slow = 20;
    input power = 2;
    input JurikFD = no;
    def fastend = (2.0 / (fast + 1));
    def slowend = (2.0 / (slow + 1));
    def efratio;# = 0
    def diff;# = 0
    def signal;# = 0
    def noise;# = 0
    def fract = jurikFractalDimension(period, 2);
    def JurikFDValue = Min(2.0 - fract, 1.0);
        signal = AbsValue(src - nz(src[period]));
        diff = AbsValue(src - src[1]);
        noise = Sum(diff, period);
    def efrat = if (noise != 0) then signal / noise else 1;
        efratio = if JurikFD then JurikFDValue else efrat;
    def smooth = Power(efratio * (fastend - slowend) + slowend, power);
    def kama;
    def kama1 = if kama[1] == 0 or IsNaN(kama[1]) then src else kama[1];
        kama = CompoundValue(1, nz(kama[1], src) + smooth * (src - nz(kama[1], src)), src);
    plot return = kama ;
}
#gkyzvol(int per)=>
script gkyzvol {
    input per = 10;
    def gzkylog = Log(open / close[1]);
    def pklog = Log(high / low);
    def gklog = Log(close / open);
    def garmult = (2 * Log(2) - 1);
    def gkyzsum = 1 / per * Sum(Power(gzkylog, 2), per);
    def parkinsonsum = 1 / (2 * per) * Sum(Power(pklog, 2), per);
    def garmansum = garmult / per * Sum(Power(gklog, 2), per);
    def sum = gkyzsum + parkinsonsum - garmansum;
    def devpercent = Sqrt(sum);
    plot return = devpercent;
}
#gkyzFilter(float src, int len, float filter)=>
script gkyzFilter {
    input src = close;
    input len = 10;
    input filter = 2;
    def price;
#    def price1 = price[1];
    ;# = src;
    def filtdev = filter * gkyzvol(len) * src;
    price = if AbsValue(src - price[1]) < filtdev then price[1] else src;
    plot return = price;
}
input colorbars = yes;
input showSigs = yes;
input source = close;
input AmaPeriod = 10;    # "AMA Period"
input FastEnd = 2;       # "Fast-End"
input SlowEnd = 20;      # "Slow-End"
input SmoothPower = 2;   # "Smooth Power"
input JurikFDAdaptive = no;    # "Jurik Fractal Dimension Adaptive?"
input filterop = {"Price", "KAMA", default "Both", "None"};  # "Filter Options"
input filter = 0.5;      # "Filter Multiple"
input filterperiod = 15; # "Filter Period"

def na = Double.NaN;

def src = if filterop == filterop."Both" or filterop == filterop."Price" and filter > 0 then
             gkyzFilter(source, filterperiod, filter) else source;
def out1 = kama(src, AmaPeriod, FastEnd, SlowEnd, SmoothPower, JurikFDAdaptive);
def out = if filterop == filterop."Both" or filterop == filterop."KAMA" and filter > 0 then
            gkyzFilter(out1, filterperiod, filter) else out1;

def sig = out[1];

def goLong_pre  = Crosses(out, sig, CrossingDirection.ABOVE);
def goShort_pre = Crosses(out, sig, CrossingDirection.BELOW);

def contSwitch;
def contSwitch1 = nz(contSwitch[1]);
contSwitch = if goLong_pre then 1 else if goShort_pre then -1 else contSwitch1;

def goLong = goLong_pre and (contSwitch - contSwitch[1]);
def goShort = goShort_pre and (contSwitch - contSwitch[1]);


AssignPriceColor(if !colorbars then Color.CURRENT else
                 if contSwitch == 1 then Color.GREEN else Color.RED);

plot JFDAGKYZFKAMA = out; # "JFDAGKYZFKAMA"
JFDAGKYZFKAMA.SetLineWeight(2);
JFDAGKYZFKAMA.AssignValueColor(if contSwitch == 1 then Color.CYAN else Color.MAGENTA);

AddChartBubble(showSigs and goLong, low, "Long", Color.CYAN, no);
AddChartBubble(showSigs and goShort, high, "Short", Color.MAGENTA, yes);

#--- END CODE
 
Last edited by a moderator:

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