Stepped Heiken Ashi Moving Average w/ Jurik Filtering [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Stepped Heiken Ashi Moving Average w/ Jurik Filtering [Loxx]", 
     shorttitle = "SHAMAJF [Loxx]", 
     overlay = true, 
     timeframe="", 
     timeframe_gaps = true)
     
import loxx/loxxexpandedsourcetypes/3
import loxx/loxxjuriktools/1

calcBaseUnit() =>
    bool  isForexSymbol = syminfo.type     == "forex"
    bool  isYenPair     = syminfo.currency == "JPY"
    float result = isForexSymbol ? isYenPair ? 0.01 : 0.0001 : syminfo.mintick

_declen()=>
    mtckstr = str.tostring(syminfo.mintick)
    da = str.split(mtckstr, ".")
    temp = array.size(da) 
    dlen = 0.
    if syminfo.mintick < 1
        dstr = array.get(da, 1) 
        dlen := str.length(dstr)
    dlen
    
greencolor = #2DD204
redcolor = #D2042D 


per= input.int(13, "Period", group = "Basic Settings")   
step = input.int(1, "Step", group = "Basic Settings", minval = 1)
better = input.bool(false, "Better HA?", group = "Basic Settings") 
jurik = input.bool(true, "Juirk Filtered?", group = "Basic Settings") 
jper= input.int(34, "Juirk Length", group = "Basic Settings")   
jphs= input.int(0, "Juirk Phase", group = "Basic Settings")   

colorbars = input.bool(false, "Color bars?", group= "UI Options")
showcandles = input.bool(false, "Show candles?", group= "UI Options")
showfill = input.bool(false, "Show fill?", group= "UI Options")

maOpen = 0.
maClose = 0.
maLow = 0.
maHigh =0.

if jurik
    maOpen := loxxjuriktools.jurik_filt(open, jper, jphs)
    maClose := loxxjuriktools.jurik_filt(close, jper, jphs)
    maLow := loxxjuriktools.jurik_filt(low, jper, jphs)
    maHigh := loxxjuriktools.jurik_filt(high, jper, jphs)

else 
    maOpen := ta.sma(open, per)
    maClose := ta.sma(close, per)
    maHigh := ta.sma(high, per)
    maLow := ta.sma(low, per)

haClose = 0.

if better
    if (maHigh != maLow)
        haClose := (maOpen + maClose) / 2 + (((maClose - maOpen) / (maHigh - maLow)) * math.abs((maClose - maOpen) / 2))
    else
        haClose := (maOpen + maClose) / 2
else 
    haClose := (maOpen + maHigh + maLow + maClose) / 4

haOpen = 0.
haOpen := (nz(haOpen[1]) + nz(haClose[1]))/2
haHigh = math.max(maHigh, math.max(haOpen, haClose))
haLow = math.min(maOpen, math.min(haOpen, haClose))

color colorhigh = na
color colorlow = na

if (haOpen < haClose) 
    colorlow := color.fuchsia
    colorhigh := color.yellow
else 
    colorlow := color.fuchsia
    colorhigh := color.yellow

outLow = haLow
outHigh = haHigh

outOpen = haOpen
outClose = haClose


if (math.abs(outLow - nz(outLow[1])) < step * _declen() * calcBaseUnit())
    outLow := nz(outLow[1])
if (math.abs(outHigh - nz(outHigh[1])) < step * _declen() * calcBaseUnit())
    outHigh := nz(outHigh[1])
if (math.abs(outOpen - nz(outOpen[1])) < step * _declen() * calcBaseUnit())
    outOpen := nz(outOpen[1])
if (math.abs(outClose - nz(outClose[1])) < step * _declen() * calcBaseUnit())
    outClose := nz(outClose[1])
    
colorout = outClose > outOpen ? greencolor : redcolor
coloroutf = outClose > outOpen ? color.yellow : color.fuchsia

plotcandle(outOpen, outHigh, outLow, outClose, color = showcandles ? coloroutf : na, wickcolor = showcandles ? coloroutf : na)

l = plot(outLow, color = colorlow)
h = plot(outHigh, color = colorhigh)

fill(h, l, color = showfill ? color.new(coloroutf, 80) : na)

barcolor(colorbars ? colorout : na)










































