[pshaiCmd] CC_ALMA

1 1725 Views No Comments 2 months ago
  • The ALMA indicator as a command.

    NOTE:
    This command returns only 20 data points of ALMA values. If you need more than that, see the lines 25-27 (max_length).

    • This topic was modified 2 months, 1 week ago by pshai.
    HaasScript Code
    DefineCommand('ALMA', 'Arnaud Legoux Moving Average')
    
    local prices = DefineParameter(ListNumberType, 'series', 'Input series', true, ClosePrices(), 'ClosePrices, HLPrices, HLCPrices, OHLCPrices')
    local windowsize = DefineParameter(NumberType, 'windowsize', 'Period length of the calculation', true, 9, 'Number, Input')
    local offset = DefineParameter(NumberType, 'offset', 'Offset value', true, 0.85, 'Number, Input')
    local sigma = DefineParameter(NumberType, 'sigma', 'Sigma value', true, 6, 'Number, Input')
    local max_length = DefineParameter(NumberType, 'maxlength', 'This value controls the size of the returned array. The smaller the length, the more faster this command runs, but if you need more datapoints than 2, control this value.', false, 2, 'Number, Input')
    
    -- adjust this [max_length] value if you need more than 20 data points.
    -- but keep in mind that this command becomes very slow very quickly!!
    local ret = {}
    local len = ArrayGet(Min(max_length, #prices), 1)
    
    local alma = function(series)
        local length = windowsize
        local m = Floor(offset * (length - 1))
        local s = length / sigma
        local norm = 0.0
        local sum = 0.0
        local weight
    
        for i = length, 1, -1 do
            weight = Exp(-1 * Pow(i - m, 2) / (2 * Pow(s, 2)))
            norm = norm + weight
            sum = sum + ArrayGet(series, length - (i - 1)) * weight
        end
    
        return ArrayGet(sum / norm, 1)
    end
    
    for step = 1, len do
        local out = alma(prices[step])
        ret = ArrayAdd(ret, out)
    end
    
    DefineOutput(ListNumberType, ret, 'ALMA values', 'Plot')
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free