Trend Regularity Adaptive Moving Average, TRAMA

stable
By Bunka in Trend Published June 2022 👁 4,738 views 💬 0 comments

Description

Trend Regularity Adaptive Moving Average, TRAMA Haasscript port by Bunka and some help of Pshai Original by LuxAlgo (TV) The moving average is calculated using exponential averaging, using as smoothing factor the squared simple moving average of the number of highest high/lowest low previously made, highest high/lowest low are calculated using rolling maximums/minimums. Using higher values of length will return fewer highest high/lowest low which explains why the moving average is smoother for higher length values. Squaring allows the moving average to penalize lower values, thus appearing more stationary during ranging markets, it also allows to have some consistency regarding the length setting.
HaasScript
-- Author: Bunka's port to HaasScript with some help of Pshai - original by LuxAlgo (TV)
-- Trend Regularity Adaptive Moving Average, TRAMA

DefineCommand("TRAMA","Trend Regularity Adaptive Moving Average indicator")

local interval = DefineParameter(NumberType,"TRAMA interval", "TRAMA interval", false, 0)
local length = DefineParameter(NumberType, "TRAMA length", "TRAMA length", false, 50)
local market = DefineParameter(StringType, "Market", "Market", false, PriceMarket())
local plot_or_not = DefineParameter(BooleanType, "plot TRAMA", "enable or disable TRAMA plot", false, true)

local src = ClosePrices(interval,true,market)
local trama = Load("trama", Grab(src,0,length))
--local trama = Load("trama", ArrayGet(src, 1))

OptimizedForInterval(
    interval,
    function()

        local hh = Load("hh", {})
        local ll = Load("ll", {})
        local tc = Load("tc", {})

        local function getHLTC(src, length, i)
            
            local src2 = Grab(src, i, length+1)
            local h = GetHighs(src2, length)
            local l = GetLows(src2, length)
            local hh = Sign(h[1] - h[2])
            local ll = Sign(l[1] - l[2])*-1

            return hh, ll, (hh > 0 or ll > 0) and 1 or 0
        end

        if Load('warmup', true) then

            for i = length+1, 1, -1 do
                local _hh, _ll, _tc = getHLTC(src, length, i)

                hh = ArrayUnshift(hh, _hh)
                ll = ArrayUnshift(ll, _ll)
                tc = ArrayUnshift(tc, _tc)
            end

            Save('warmup', false)
        else

            local _hh, _ll, _tc = getHLTC(src, length, 1)

            hh = ArrayUnshift(hh, _hh)
            ll = ArrayUnshift(ll, _ll)
            tc = ArrayUnshift(tc, _tc)

        end

        local tc2 = Pow(SMA(tc, length), 2)
        trama = trama + tc2 * (src - trama)

        Save("trama", trama)
        Save("hh",Grab(hh, 0, 200))
        Save("ll",Grab(ll, 0, 200))
        Save("tc",Grab(tc, 0, 200))

        if plot_or_not then 
             Plot(0,"TRAMA", trama, Cyan)
        end    

        local results = {
            trama = trama,
            }

        DefineOutput(ListDynamicType, results, 'TRAMA')
        DefineOutputIndex(1,ListNumberType,"TRAMA","TRAMA")

    end
)

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!