[bunkaCmd] Holt's Linear Trend Forecasting method - indicator

stable
By Bunka in Other Published August 2022 👁 1,096 views 💬 0 comments

Description

Holt's Linear Trend Forecasting method - indicator Haasscript recreation by bunka Optimization and timeframe-check by pshai orig TV author: capissimo Holt (1957) extended simple exponential smoothing to allow the forecasting of data with a trend. This method involves a forecast equation and two smoothing equations (one for the level and one for the trend): For details pls refer to the book - https://otexts.com/fpp2/holt.html
HaasScript
-- Holt's Linear Trend Forecasting method - indicator  - CustomCommand

-- Haasscript recreation by bunka
-- Optimization and timeframe-check by pshai

-- orig TV author: capissimo

-- Holt (1957) extended simple exponential smoothing to allow the forecasting of data with a trend. 
-- This method involves a forecast equation and two smoothing equations (one for the level and one for the trend): 
-- For details pls refer to the book - https://otexts.com/fpp2/holt.html


DefineCommand("Holts_Linear_Trend_Forecasting_method","Holts_Linear_Trend_Forecasting_method")


local function get(x, y)
    return ArrayGet(x, y or 1)
end

local function holt(y, al, bt, l, b, f)
    local nl = al * y + (1 - al) * (l + b)
    local nb = bt * (nl - l) + (1 - bt) * b
    local nf = nl + nb

    return nl, nb, nf
end

-- Inputs

local interval = DefineParameter(NumberType,"Holt`s forcasting interval", "Holt`s forcasting interval", false, 0)
local alpha = DefineParameter(NumberType, "Holt`s forcasting Slope (alpha)", "Holt`s forcasting Slope (alpha)", false, 0.3)
local beta = DefineParameter(NumberType, "Holt`s forcasting Intercept (beta)", "Holt`s forcasting Intercept (beta)", false, 0.000001)
local market = DefineParameter(StringType, "Holt`s forcasting Market", "Holt`s forcasting Market", false, PriceMarket())

local plot_or_not = DefineParameter(BooleanType, "plot Holt`s forcasting", "enable or disable Holt`s forcasting plot", false, true)


local price = ClosePrices(interval, true, market)
--local alpha = Input("Slope (alpha)", 0.3)
--local beta  = Input("Intercept (beta)", 0.000001)

local fx = OptimizedForInterval(
    interval,
    function()

        -- Main
        local l = Load('l', {0})
        local b = Load('b', {0})
        local f = Load('f', {0})

        if Load('warmup', true) then
            for i = 50, 1, -1 do
                local nl, nb, nf = holt(get(price), alpha, beta, get(l), get(b), get(f))

                l = ArrayUnshift(l, nl)
                b = ArrayUnshift(b, nb)
                f = ArrayUnshift(f, nf)
            end

            Save('warmup', false)
        else
            local nl, nb, nf = holt(get(price), alpha, beta, get(l), get(b), get(f))
            local updated = Load('u', false)

            if MinutesTillCandleClose(CurrentInterval(price)) == 0 and not updated then
                l = Grab(ArrayUnshift(l, nl), 0, 200)
                b = Grab(ArrayUnshift(b, nb), 0, 200)
                f = Grab(ArrayUnshift(f, nf), 0, 200)
                updated = true
            else
                l[1] = nl
                b[1] = nb
                f[1] = nf
                updated = false
            end

            Save('u', updated)
        end

        Save('l', l)
        Save('b', b)
        Save('f', f)

        --local forcast = l + b 
        --local forcast = f


        if plot_or_not then 
            local line_forcast = Plot(0, "line_forcast", f, {color = Cyan, offset = 1})
            PlotShapes(line_forcast,ShapeDash,Cyan)
        end

        return f  

    end
)

DefineOutput(ListNumberType, fx, "Holts_Linear_Trend")

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!