Settings and Threads
Part of TA-Lib's specification, a reference for AI-agent-driven integration.
C keeps its settings (unstable periods, candle settings and the EMA variation) process-wide; Rust, Java, C# and TypeScript keep them in an immutable Core, so cores with different settings coexist in one process, and in Rust, Java and C# any number of threads can share one with no synchronization (rT1). Go keeps them in a Core that its setters change, shared by goroutines once configured (Go settings). What each setting means: Unstable Period, Candlestick Settings (with the defaults), EMA variation. Names per language: names. How a failure reaches the caller: failures.
The caller must
- In C, initialize exactly once per process. Call
TA_Initializebefore any other TA call. CallTA_Shutdownbefore the process exits; the library must not be used after it. Rust, Java, C#, TypeScript and Go have no lifecycle call: aCoreis ready when constructed. - In C, change a setting only while no TA function is running and no stream is open. A change is a call to
TA_SetUnstablePeriod,TA_SetEMAVariation,TA_SetCandleSettings,TA_RestoreCandleDefaultSettingsorTA_Shutdown. Between changes, any number of threads may call the library (rT11); it assumes themallocandfreeit allocates with are thread-safe. - Run a writer alone on its stream handle. The writers are
Update,Advanceand C'sClose; not evenPeekorValuemay run alongside one. Separate handles, a clone included (rH8), may each be driven on their own thread. Rust enforces this at compile time: the writing calls take&mut self, and every handle isSend + Sync + Clone. A Go stream belongs to one goroutine: no call on it may run alongside another. - In Go, configure a
Corebefore sharing it. Call its setters only while no other goroutine uses it and no stream opened on it is in use. Between changes, any number of goroutines may call through it. - Confine holders, builders and sinks to one thread. A parameter holder is not thread-safe in C, Java, C# and Go; neither is a Java or C#
CoreBuildernor the sink a Java multi-output stream writes into (names). In C, theconstinTA_CallFunc's signature does not make a holder shareable: the call writes the output buffers bound to it.
Core in Rust, Java, C# and TypeScript
rT1 In Rust, Java, C# and TypeScript, settings live on an immutable Core made by a builder. In Rust, Java and C#, one Core may be shared by any number of threads with no synchronization; Rust's is Send + Sync. To change a setting, build another Core, from defaults or seeded from an existing one (to_builder(), toBuilder(), ToBuilder()). A stream keeps the settings of the Core that opened it. A Java, C# or TypeScript builder stays usable after build().
Validation
The rows hold for every setter and getter in every language, except where a note says otherwise. A Rust or Java enum cannot hold a value outside its members.
| Rule | Condition | Language notes |
|---|---|---|
| rT2 | A setter fails on a target or a value outside its enum. TA_SetCandleSettings, the builders' candle setter and Go's SetCandleSetting also fail on TA_AllCandleSettings. | In Rust and Java only the wildcard fails. |
| rT3 | An unstable period is a count in [0, TA_INDEX_MAX] or an Auto level: TA_UNSTABLE_AUTO_PREC_4 or TA_UNSTABLE_AUTO_PREC_8 (constants). | C takes an unsigned int, so a negative value arrives above every level and fails. Rust's u32 cannot hold one. |
| rT4 | Reading a setting for the set-all wildcard fails. | For the wildcard or an out-of-range id, C's TA_GetUnstablePeriod returns 0, which is also a legal period. |
| rT5 | A candle setting's range type is a TA_RangeType member. | |
| rT6 | A candle setting's avgPeriod is in [0, TA_INDEX_MAX]. | |
| rT7 | A candle setting's factor is finite and not negative. | |
| rT8 | A failed call leaves every setting as it was, a wildcard call included. | Rust's build() returns the first failure, even after a later valid setter or restore_candle_default. |
rT9 Two wildcards act on every member at once: TA_FUNC_UNST_ALL sets every unstable period, and TA_AllCandleSettings restores every candle setting's default through TA_RestoreCandleDefaultSettings, the builders' restore call (restore_candle_default, restoreCandleDefault, RestoreCandleDefault) and Go's RestoreCandleDefault.
rT12 A getter returns the value the setter stored. For an unstable period on an Auto level that is the level's constant, not the number of bars it adds (rL6).
Initial state
rT10 In C, TA_Initialize leaves every unstable period at 0, every candle setting at its default and the EMA variation at TA_EMAVariation_SMA_SEED, and so do Go's NewCore and a zero Core.
Threads in C
rT11 Between setting changes, C calls made from several threads at once, each on its own stream handles and parameter holders, return what a single thread returns. On one handle with no writer running, Peek, Value, OutRange and Clone may run from several threads at once.
EMA variation
The EMA variation selects the seed of the EMA family: SMA_SEED, the default, or FIRST_BAR_SEED. The setters per language, the functions it moves and the measured difference: EMA Variant Settings.
rT13 The setting changes only the value an EMA starts from. The EMA formula is unchanged, and so are T3, Wilder smoothing (RSI, ATR, ADX) and the adaptive averages.
rT14 Under FIRST_BAR_SEED, an EMA of the input, or of a series computed from the input bar by bar, starts from the first value of that series in the input array, whatever startIdx is. An EMA computed on the output of another average (the second EMA of DEMA, MACD's signal, STOCH's %D) or of RSI starts from the first value that output has in the call.
rT15 No lookback changes (rL5): the first output bar and the output count are the same under both (rW1).
rT16 Under FIRST_BAR_SEED, a call reads the input from its first element up to startIdx, not only the lookback bars before startIdx (rL1). Its cost grows with startIdx.
rT17 Under FIRST_BAR_SEED, a function whose EMAs all read the input (EMA, MACD's line) gives the same value for a bar whatever startIdx is, up to rounding. A function that feeds an average into an EMA (DEMA, TEMA, TRIX, MACD's signal, STOCH's %D) does not: compare its values only between calls with the same startIdx.
rT18 The setting affects exactly the functions listed.
From TA-Lib 0.7.1 and earlier. For the EMA family, a call with startIdx 0 under FIRST_BAR_SEED writes, within rounding, what the MetaStock compatibility mode wrote. That mode's RSI and CMO variant is not reproduced.