Skip to main content

KAMA

Source

https://github.com/quadcode-tech/quadcodescript-library/blob/master/averages/kama.lua

Syntax Example

kama(length, fastMA, slowMA, source)

Overview

KAMA (Kaufman’s Adaptive Moving Average) belongs to the MA family, but is a bit different from the majority of Moving Averages. It adjusts its settings, depending on the market situation. This smoothing factor helps KAMA to be more responsive during higher volatility and smooth the price movements during significant trends.

Parameters

ParameterTypePurpose
lengthintegerSmoothing constant (SC) period
fastMAintegerFast Moving Average period
slowMAintegerSlow Moving Average period
sourceseriesInput series, taken into calculation

Example

instrument { name = "KAMA", overlay = true }

local length = input (21, "period", input.integer, 1 , 250 )
local fastMA = input (4, "fast period", input.integer, 1, 250)
local slowMA = input (30, "slow period", input.integer, 1, 250)
local source = input (1, "source", input.string_selection, inputs.titles_overlay)

input_group {
"KAMA Line",
color = input { default = "#57A1D0", type = input.color },
width = input { default = 1, type = input.line_width}
}

local source_series = inputs [source]

change = abs (source_series - source_series[length - 1])
volatility = sum (abs(source_series - source_series[1]), length)

local er = 0

if nz (volatility [0]) ~= 0 then
er = nz (change [0]) / nz (volatility [0])
end

local fastSC = 2 / (fastMA + 1)
local slowSC = 2 / (slowMA + 1)

local sc = (er * (fastSC - slowSC) + slowSC) ^ 2

kama = sc * source_series + (1 - sc) * nz(kama [1], source_series [0])

plot (kama, "KAMA", color, width)