EMA Variant Settings
TL;DR: An EMA needs a first value to start from. TA-Lib has always started from a simple average (SMA) over period bars, and still does by default; starting from the first element of the input array is now an option since v0.8.4 (Dec 2026).
The two options
| Value | How the EMA starts | First value published | Note |
|---|---|---|---|
SMA_SEED | From an SMA over period bars | Once period bars are available | De facto industry standard (and TA-Lib default). |
FIRST_BAR_SEED | From the first element of the input array, then one EMA step per bar | Once period bars are available | Smaller error on the first values published: see Measured difference. |
How to set it
/* Once, after TA_Initialize: */
TA_SetEMAVariation( TA_EMAVariation_FIRST_BAR_SEED );
TA_EMAVariation v = TA_GetEMAVariation(); /* TA_EMAVariation_FIRST_BAR_SEED */
/* Multithreading: the setting is process-wide. Change it only while no TA
* function is running and no stream is open. */use ta_lib::{Core, EMAVariation};
let core = Core::builder()
.ema_variation(EMAVariation::FirstBarSeed)
.build()?;
let v = core.ema_variation(); // EMAVariation::FirstBarSeed
// Multithreading: the setting belongs to this Core and is fixed once the Core
// is built, so the Core can be shared between threads. A stream reads it when
// it opens and keeps it for its life.import io.github.talib.Core;
import io.github.talib.EMAVariation;
Core core = Core.builder()
.emaVariation(EMAVariation.FIRST_BAR_SEED)
.build();
EMAVariation v = core.emaVariation(); // EMAVariation.FIRST_BAR_SEED
// Multithreading: the setting belongs to this Core and is fixed once the Core
// is built, so the Core can be shared between threads. A stream reads it when
// it opens and keeps it for its life.using TALib;
Core core = Core.Builder()
.EmaVariation(EMAVariation.FirstBarSeed)
.Build();
EMAVariation v = core.EmaVariation(); // EMAVariation.FirstBarSeed
// Multithreading: the setting belongs to this Core and is fixed once the Core
// is built, so the Core can be shared between threads. A stream reads it when
// it opens and keeps it for its life.import { CoreBuilder, EMAVariation } from 'ta-lib';
const core = new CoreBuilder()
.emaVariation(EMAVariation.FIRST_BAR_SEED)
.build();
const v = core.emaVariation(); // EMAVariation.FIRST_BAR_SEED
// Multithreading: the setting belongs to this Core and is fixed once the Core
// is built. Pass the Core as the `core` option of each call. A stream reads
// the setting when it opens and keeps it for its life.import talib "ta-lib.github.io/go"
core := talib.NewCore()
if err := core.SetEMAVariation(talib.EMAVariationFirstBarSeed); err != nil {
log.Fatal(err)
}
v := core.EMAVariation() // talib.EMAVariationFirstBarSeed
// Multithreading: call a Core's setters only while no other goroutine uses
// it and no stream opened on it is in use.The rules for changing a setting are in the specification.
Which one to pick
Mostly a matter of preference: pick the seed of the tool or application you want your values to match.
| Products | Seed | First value published | TA-Lib setting |
|---|---|---|---|
TA-Lib (default), R TTR::EMA, TradingView ta.ema | SMA over period bars | Bar period - 1 | SMA_SEED |
| MetaStock | First element | Bar period - 1 | FIRST_BAR_SEED |
Tulip Indicators, pandas ewm(adjust=False), TradeStation XAverage | First element | Bar 0 | FIRST_BAR_SEED, from bar period - 1 |
Two trade-offs beyond that:
- Speed.
SMA_SEEDreads only the bars just before the requested start, so a call that starts late in a long array, such as one for the last bar only, is faster. UnderFIRST_BAR_SEEDthat call runs from the start of the array, and its cost grows withstartIdx(rT16). A call over the whole array reads the same bars under both. - Error on early bars.
FIRST_BAR_SEEDhas the smaller error on the first values published, so its output settles sooner: see Measured difference.
Measured difference
How far the first value a single EMA publishes is from an EMA with unlimited history.
The same measure for two functions that chain EMAs.
The full method and results: EMA seeding study.
Why does the error range increase as the period increases?
- Short period, small lag. An EMA(5) follows the price closely, so the first element is already near where an EMA with unlimited history would be. The seed is a good guess, and the error of the first value published is small.
- Long period, large lag. An EMA(200) trails far behind the price, so the first element is a poor guess for where an EMA with unlimited history would be. The seed starts far off, and about 14% of that miss is still in the first value published.
How to reduce the error?
Whichever seed is used, the first values an EMA publishes still carry part of the seed's error. The proper way to get numerically accurate values is to have TA-Lib leave those bars out: see Unstable Period.
Which functions follow the setting
APO, BBANDS, BBW, CVI, DEMA, DOSC, EFI, EMA, ERI, KC, KDJ, KSTEXT, MA, MACD, MACDEXT, MACDFIX, MACDV, MAENV, MASSI, MAVP, PERCENTB, PPO, PSO, PVO, SMI, STC, STOCH, STOCHF, STOCHRSI, TEMA, TRIX, TSI, WAVETREND, ZLEMA.
A function that takes an MA type follows the setting only with an EMA, DEMA, TEMA or ZLEMA type.
See also
- Specification: the rules of the setting
- Core API: C/C++, Rust, Java, C#, TypeScript, Go
- Unstable Period
- Candlestick Settings