Skip to main content

EMA

Source

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

Syntax Format

ema(source, period)

Overview

EMA (Exponential Moving Average) belongs to the MA family, which gives more weight to the recent price changes, making it more suitable for short-term strategies. The function returns the exponential moving average of source for the period bars.

Parameters

ParameterTypePurpose
sourceseriesInput series, taken into calculation
periodnumericMA (moving average) period

Returns: series.

Example

instrument { name = "Exponential Moving Average", short_name = "EMA", overlay = true, icon="indicators:MA" }

period = input (9, "period", input.integer, 1)
source = input (1, "source", input.string_selection, inputs.titles_overlay)

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

local sourceSeries = inputs [source]

plot (ema (sourceSeries, period), "EMA", color, width)

Formula

The formula for calculating EMA is:

image.png

The EMA function is calculated by multiplying the smoothing factor (α) by the current closing price (source) and adding it to the previous day's value. The smoothing factor, in its turn, is calculated by taking the period for the EMA+1 and dividing 2 by the result value.