Moving average (MA)
Summary
Generic moving-average dispatcher that forwards the job to the MA implementation selected by optInMAType. Single uniform interface over all TA-Lib moving averages.
Chart
Chart metadata i
Outputs
outReal:line
Formula
outReal = MA_of_type(optInMAType)(inReal, optInTimePeriod); default type = SMA
Notes
- A period of 1 performs no smoothing for every MAType: the output is a copy of the input.
TA_MAType_DISABLEDbypasses smoothing explicitly, for any period: the output is a copy of the input with a lookback of 0. Every function that takes an MAType parameter accepts it.TA_MAType_SSFtakes a period up to SSF's own maximum, which is below this function's; a longer one is rejected.TA_MAType_DEFAULTselects the documented default of the parameter it is passed to — SMA here, EMA for APO, PPO and PVO. Every function that takes an MAType parameter accepts it.
Inputs
inReal— Series to average
Outputs
outReal— Selected moving average of the input
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 30 | 1–100000 | Averaging window length |
optInMAType | MAType | SMA (0) | any MAType | Which moving-average algorithm to dispatch to |
MAType values: 0 SMA · 1 EMA · 2 WMA · 3 DEMA · 4 TEMA · 5 TRIMA · 6 KAMA · 7 MAMA · 8 T3 · 9 HMA · 10 DISABLED · 11 DEFAULT · 12 ZLEMA · 13 RMA · 14 VIDYA · 15 ALMA · 16 SSF
Properties
Numerical Stability: Depends on MA Type — This function's default, SMA, is start-independent.
| ✅ Overlap Input i |
| ☐ Independent Y-Axis |
| ☐ Candlestick |
| ☐ Display Shift |
| ☐ Can Output NaN or ±Inf |
| ✅ Identity at Period 1 i |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_MA( 0, n - 1, series, 30, TA_MAType_SMA, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d: [%.4f, ..., %.4f]\n", outNBElement, outBegIdx, out[0], out[outNBElement - 1] );use ta_lib::{Core, MAType};
let series = vec![0.0; 252]; // your data, oldest first
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.ma(0, series.len() - 1, &series, 30, MAType::SMA, &mut out)?;
println!("{} values from bar {}: [{:.4}, ..., {:.4}]", range.count, range.beg_idx, out[0], out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.MAType;
import io.github.talib.OutRange;
import java.util.Locale;
double[] series = new double[252]; // your data, oldest first
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.ma(0, series.length - 1, series, 30, MAType.SMA, out);
System.out.printf(Locale.ROOT, "%d values from bar %d: [%.4f, ..., %.4f]%n", r.count(), r.begIdx(), out[0], out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Ma(0, series.Length - 1, series, 30, MAType.SMA, outReal);
Console.WriteLine(FormattableString.Invariant($"{r.Count} values from bar {r.BegIdx}: [{outReal[0]:F4}, ..., {outReal[r.Count - 1]:F4}]"));import { MAType, ma } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const { begIdx, values } = ma(series, { timePeriod: 30, maType: MAType.SMA });
console.log(values.length + ' values from bar ' + begIdx + ': [' + values[0]?.toFixed(4) + ', ..., ' + values[values.length - 1]?.toFixed(4) + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
series := make([]float64, 252) // your data, oldest first
out := make([]float64, 252)
core := talib.NewCore()
r, err := core.MA(0, len(series)-1, series, 30, talib.MATypeSMA, out)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d: [%.4f, ..., %.4f]\n", r.NBElement, r.BegIdx, out[0], out[r.NBElement-1])Implementation
Added in 0.4.0.
TA-Lib Definition: ma.c · ma.yaml
| Native | File |
|---|---|
| C | ta_MA.c |
| Rust | ma.rs |
| Java | Core_MA.java |
| C# | Core_MA.cs |
| TypeScript | ma.ts |
| Go | ma.go |
Python: talib.MA, see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Moving Average, MovingAverage
See Also
SMA · EMA · WMA · DEMA · TEMA · TRIMA · KAMA · MAMA · T3 · HMA · ZLEMA · RMA · VIDYA · ALMA · SSF