Moving average with variable period (MAVP)
Summary
Moving average whose period varies per bar, driven by a companion period series. For each bar it computes an MA of the selected type over the (clamped) period given by inPeriods.
Chart
Chart metadata i
Outputs
outReal:line
Formula
p_i = clamp((int)inPeriods[startIdx+i], optInMinPeriod, optInMaxPeriod); outReal[i] = MA(inReal, p_i, optInMAType) at bar startIdx+i
Notes
- Fractional per-bar periods are truncated to whole numbers before being clamped to the minimum and maximum period.
- Period values of 1 perform no smoothing (the bar's output equals its input); the minimum allowed period is 1 since 0.6.5.
optInMinPeriodmust not exceedoptInMaxPeriod; a larger minimum is rejected.
Inputs
inReal— series to be averagedinPeriods— per-bar desired MA period
Outputs
outReal— variable-period moving average
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInMinPeriod | integer | 2 | 1–10000 | Lower clamp for the per-bar period |
optInMaxPeriod | integer | 30 | 1–10000 | Upper clamp for the per-bar period |
optInMAType | MAType | SMA (0) | any MAType | Moving-average type applied |
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
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 |
| ☐ 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 periods[252]; /* one period per bar */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_MAVP( 0, n - 1, series, periods, 2, 30, TA_MAType_SMA, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d; last out = %f\n", outNBElement, outBegIdx, out[outNBElement - 1] );use ta_lib::{Core, MAType};
let series = vec![0.0; 252]; // your data, oldest first
let periods = vec![0.0; 252]; // one period per bar
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.mavp(0, series.len() - 1, &series, &periods, 2, 30, MAType::SMA,
&mut out)?;
println!("{} values from bar {}; last out = {}", range.count, range.beg_idx, out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.MAType;
import io.github.talib.OutRange;
double[] series = new double[252]; // your data, oldest first
double[] periods = new double[252]; // one period per bar
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.mavp(0, series.length - 1, series, periods, 2, 30, MAType.SMA, out);
System.out.println(r.count() + " values from bar " + r.begIdx() + "; last out = " + out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var periods = new double[252]; // one period per bar
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Mavp(0, series.Length - 1, series, periods, 2, 30, MAType.SMA, outReal);
Console.WriteLine($"{r.Count} values from bar {r.BegIdx}; last outReal = {outReal[r.Count - 1]}");import { MAType, mavp } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const periods = new Float64Array(252); // one period per bar
const { begIdx, values } = mavp(series, periods, {
minPeriod: 2,
maxPeriod: 30,
maType: MAType.SMA,
});
console.log(values.length + ' values from bar ' + begIdx + '; last values = ' + values[values.length - 1]);Implementation
Added in 0.4.0.
TA-Lib Definition: mavp.c · mavp.yaml
| Native | File |
|---|---|
| C | ta_MAVP.c |
| Rust | mavp.rs |
| Java | Core_MAVP.java |
| C# | Core_MAVP.cs |
| TypeScript | mavp.ts |
Python: talib.MAVP, see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Moving Average Variable Period, Variable Period Moving Average