[Giankam] JMA (Jurik Moving Average)

stable
By StacksMooningWhale in Trend Published February 2023 👁 2,036 views 💬 0 comments

Description

Based on the original Jurik Moving Average http://jurikres.com/catalog1/ms_ama.htm this is one of the most sought after moving averages. Due to the recursive logic it the code uses Pshai’s CC_IndicatorMemory. Example:

local src = ClosePrices()
local length = 20
local phase = 0
local exp = 2
local jma= CC_IndicatorMemory(
            |i, mem| CC_JMA(src, length, phase, exp))
Plot(0, 'JMA', jma, {color=White, width=4})

HaasScript
--- Author: Giankam
DefineCommand('JMA', 'Jurik Moving Average')
 
local src = DefineParameter(ListNumberType, 'source', 'Data source to transform', true, ClosePrices())
local period = DefineParameter(NumberType, 'period', 'JMA period', true, 14)
local phase = DefineParameter(NumberType, 'phase', 'JMA phase', true, 0) --range 1-199
local exp = DefineParameter(NumberType, 'exp', 'JMA exp', true, 2) --range 1-3
 
local jma = CC_IndicatorMemory(
    function(i, mem)
        local beta = (0.45 * ( period - 1 )) / ((0.45 * ( period - 1)) +2)
        local alpha = Pow( beta, exp)
        local phaseRatio = phase < -100 and 0.5 or ( phase > 100 and 2.5 or phase / 100 + 1.5)
        if mem[1] == 0 then
            mem = ArrayUnshift( mem, ArrayGet(src, 2))
            mem = ArrayUnshift( mem, ArrayGet(src, 1))
        end
        local JMA = mem[1] or 0
        local MA1 = CC_IndicatorMemory(
            function(d, mem)
                if mem[1] == 0 then
                    mem[1] = ArrayUnshift( mem, ArrayGet(src, 1))
                end
                local _MA1 = (1-alpha) * ArrayGet( src, 1) + alpha * mem[1]
                return _MA1
            end
        )
        local det0 = CC_IndicatorMemory(
            function(j, mem)
                    local _Det0 = (ArrayGet( src, 1) - MA1[j]) * ( 1 - beta) + beta * mem[1]
                return _Det0
            end
        )
        local det1 = CC_IndicatorMemory(
            function(d1, mem)
                local MA2 = MA1 + phaseRatio * det0
                local Det1 = (MA2 - JMA[i]) * (1-alpha)*(1-alpha) + (alpha*alpha * mem[1])
                return Det1
            end
        )
    JMA = mem[1] + det1     
    return JMA
end
)
DefineOutput(ListNumberType, jma)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!