[pshaiCmd] Super Smoother

stable
By pshai in Trend Published May 2022 👁 1,397 views 💬 0 comments

Description

SuperSmoother ported from a TradingView script. Usage:

local hlc = HLCPrices()
local mean_line = OptimizedForInterval(0, || CC_SuperSmoother(hlc, 200, 50))

Plot(0, 'Mean', mean_line)
Usage (loop):

local markets = {'BTC', 'ETH', 'XRP'}
 
for i = 1, Count(markets) do
    local mkt = CreateMarket({baseCurrency = markets[i]})
    local hlc = HLCPrices(0, true, mkt)
    local mean_line = OptimizedForInterval(0, || CC_SuperSmoother(hlc, 200, 50, mkt))
 
    Plot(i, 'Mean - ' .. mkt, mean_line)
end
HaasScript
-- [pshaiCmd] SuperSmoother - A command to super-smooth any series
-- Author: pshai

DefineCommand('SuperSmoother', 'A command to super-smooth any series')

local src = DefineParameter(ListNumberType, 'source', 'Source values to be super-smoothed', true, ClosePrices())
local len = DefineParameter(NumberType, 'length', 'Length of the super-smoothness', true, 200)
local mem_wup = DefineParameter(NumberType, 'mem_wup', 'Length of IndicatorMemory warmup period', false, 50)
local id = DefineParameter(StringType, 'mem_id', 'Custom memory identifier if using for multiple data sets in a loop', false, 'default')

local memory = Load('mem'..id, Grab(src, Count(src)-mem_wup, mem_wup))

local function ss(i, mem)
    local s_a1 = Exp(-Sqrt(2) * PI / len)
    local s_b1 = 2 * s_a1 * Cos(Sqrt(2) * PI / len)
    local s_c3 = -Pow(s_a1, 2)
    local s_c2 = s_b1
    local s_c1 = 1 - s_c2 - s_c3

    return s_c1 * src[i]
            + s_c2 * mem[i]
            + s_c3 * mem[i+1]
end

local function parse(v)
    local t = GetType(v)

    if (t == ArrayDataType
    or t == UserDataDataType)
    and Count(v) > 0
    then
        return ArrayGet(v, 1)
    
    else
        return v
    end
end

local func = ss

if Count(memory) == 0 then
    LogWarning('warmup indicator...')
    for i = mem_wup-1, 1, -1
    do
        local v = func(i, memory)
        
        memory[i] = parse(v)
    end
else
    local v = func(1, memory)
    memory = Grab(ArrayUnshift(memory, parse(v)), 0, 1000)
end

Save('mem', memory..id)

local ss = memory

DefineOutput(ListNumberType, ss, 'SuperSmoothed values')

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!