# Moving Average Indicators

Most strategies rely on one or two moving average patterns; the examples below illustrate common building blocks rather than prescriptive templates.

**What is a Moving Average?**\
A trend-following indicator. Displayed in a value or visual line calculated based on average price over time.

**Range:** It calculates the average of past prices to filter out short-term noise.\
If the price is consistently trading above the moving average, the trend is considered up.\
If the price is consistently trading below the moving average, the trend is considered down.

### Supported technical indicators feature:

1. #### Simple Moving Average (SMA)

**Formula**: SMA = (A1 + A2 + … + An) / n

SMA factores the sum of closing prices over a specific period and dividing the total by the number of periods. Closing prices have equal weights.&#x20;

In other words, it calculates arithmetic mean of closing prices over a time window. Results are cached for performance (1-minute TTL).

#### Parameters:

* window [string](https://www.lua.org/manual/5.3/manual.html#6.4) or number Time window as duration string ("15m", "1h") or minutes (REQUIRED)
* resolution [string](https://www.lua.org/manual/5.3/manual.html#6.4) or number Bar resolution as duration string ("1m", "5m") or minutes (*default* "1m")

#### Returns:

1. number SMA value (returns 0 if no bars found)

#### Usage:

```lua
-- Using primary asset (window REQUIRED)
local sma_val = sma("15m")           -- 15-minute window, default 1-minute resolution
local sma_val = sma("1h", "5m")      -- 1-hour window, 5-minute resolution
local sma_val = sma(60, 5)           -- 60-minute window, 5-minute resolution
-- Using specific asset (asset + window REQUIRED)
local sma_val = sma("ETH", "15m")         -- ETH, 15m window, default 1m resolution
local sma_val = sma("ETH", "15m", "1m")   -- ETH, 15m window, 1m resolution
```

Common representation in LUA code as **SMA(n)**, where n is the number of periods.

2. ### Exponential Moving Average (EMA)

**Formula**: EMA = (Current Price × (2 / (N + 1))) + (Previous EMA × (1 – (2 / (N + 1)))

Is calculated using the Simple Moving Average (SMA) but assigning higher weights to recent prices.

In other words, to calculate the EMA one must be using a multiplier-based approach: multiplier = 2 / (periods + 1). Applies weighted multipliers to SMA values at each period.

#### Parameters:

* periods number Number of periods (REQUIRED)
* window [string](https://www.lua.org/manual/5.3/manual.html#6.4) or number Time window as duration string ("15m", "1h") or minutes (*default* "1m")

#### Returns:

1. number EMA value

#### Usage:

```lua
-- Using primary asset
local ema_val = ema(14)          -- 14 periods, default 1-minute window
local ema_val = ema(14, "15m")   -- 14 periods, 15-minute window
local ema_val = ema(20, 60)      -- 20 periods, 60-minute window
-- Using specific asset
local ema_val = ema("BTC", 14)         -- BTC, 14 periods, default 1m window
local ema_val = ema("BTC", 14, "15m")  -- BTC, 14 periods, 15m window
```

Representation in LUA code as **EMA(x)**, where x is the number of periods.

3. ### Weighted Moving Average (WMA)

**Formula**: WMA = \[(Price₁ × n) + (Price₂ × (n – 1)) +… + Priceₙ] / \[n × (n + 1) / 2]

Similar to EMA, places greater emphasis on recent prices by assigning them higher importance,. the formula features a linear weighting method, making it less sensitive to recent price changes.

In other words, for calculating WMA one must use descending weights: denominator = (weights + 1) \* weights / 2. More recent periods receive higher weights in descending order.

#### Parameters:

* periods number Number of periods (REQUIRED)
* window [string](https://www.lua.org/manual/5.3/manual.html#6.4) or number Time window as duration string ("15m", "1h") or minutes (REQUIRED)
* weights number Number of descending weights to apply (REQUIRED)

#### Returns:

1. number WMA value

#### Usage:

```lua
-- Using primary asset (all 3 parameters REQUIRED)
local wma_val = wma(14, "15m", 14)  -- 14 periods, 15-min window, 14 weights
local wma_val = wma(10, 30, 10)     -- 10 periods, 30-min window, 10 weights
-- Using specific asset (all 4 parameters REQUIRED)
local wma_val = wma("BTC", 14, "15m", 14)  -- BTC, 14 periods, 15m window, 14 weights
```

Representation in LUA code as **EMA(x)**, where x is the number of periods.

4. ### MACD - Moving Average Convergence Divergence

Moving Average Convergence Divergence (MACD) Calculates MACD as the difference between fast and slow EMAs. Formula: fast\_ema - slow\_ema Note: Does NOT support asset name parameter (always uses primary asset).

#### Parameters:

* small\_periods number Fast EMA periods (default: 12) (*default* 12)
* large\_periods number Slow EMA periods (default: 26, must be > small\_periods) (*default* 26)
* window [string](https://www.lua.org/manual/5.3/manual.html#6.4) or number Time window as duration string ("15m", "1h") or minutes (*default* "1m")

#### Returns:

1. number MACD value (fast EMA - slow EMA)

#### Usage:

```lua
-- Using defaults
local macd_val = macd()              -- 12, 26, "1m"
-- Custom parameters
local macd_val = macd(12, 26, "15m") -- 15-minute window
local macd_val = macd(9, 21, 60)     -- 9, 21 periods, 60-minute window
```

### Fields

**periods**

* periods number Number of periods (REQUIRED)
* window ? string|number Time window as duration string ("15m", "1h") or minutes (default: "1m")

**window**

* window string|number Time window as duration string or minutes (REQUIRED)
* resolution ? string|number Bar resolution as duration string or minutes (default: "1m")

**periods**

* periods number Number of periods (REQUIRED)
* window string|number Time window as duration string or minutes (REQUIRED)
* weights number Number of descending weights to apply (REQUIRED)

**small\_periods**

* small\_periods ? number Fast EMA periods (default: 12)
* large\_periods ? number Slow EMA periods (default: 26, must be > small\_periods)
* window ? string|number Time window as duration string ("15m", "1h") or minutes (default: "1m")


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.memejob.fun/memejob/introducing-ai-agents/agentic-and-data-layer/technical-indicators/moving-average-indicators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
