SWMA - Sine Weighted Moving Average

stable
By Bunka in Trend Published December 2021 👁 1,338 views 💬 1 comments

Description

SWMA - Sine Weighted Moving Average - ported to HaasScript by Bunka - optimized by Pshai Copyright (c) 2018-present, Alex Orekhov (everget) Sine Weighted Moving Average script may be freely distributed under the MIT license. Example of usage, similar to other moving average indicators: swma = CC_SWMA(ClosePrices(), length)
HaasScript
-- SWMA - Sine Weighted Moving Average
-- ported to HaasScript by Bunka
-- optimized by Pshai
-- Copyright (c) 2018-present, Alex Orekhov (everget)
-- Sine Weighted Moving Average script may be freely distributed under the MIT license.

DefineCommand("SWMA","Sine Weighted Moving Average")

local function sinList(length)
    local l = {}
    for i = 1, length do
        l[i] = i+1
    end
    return l
end

local src = DefineParameter(ListNumberType,"src","src",true,ClosePrices())
local length = DefineParameter(NumberType,"length","length", true, 14) --default 14
local temp = Grab(src, 0, length)
local weights = Sin(Div(Mult(sinList(length), PI), (length + 1)))
local weightSum = Sum(weights)
local sum = 0

for i = 1, length -1 do
    sum = sum + Mult(Grab(src,i-1,i+1), weights[i])
end

local swma = Div(sum, weightSum)

--Plot(0, "SWMA", swma, Yellow)

DefineOutput(ListNumberType, swma, "SWMA values")

1 Comment

Sign in to leave a comment.

B
Bunka over 4 years ago

New version of same script, shared some time ago, optimized and cleaned