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: