TypeScript Streaming API
Not yet released
The TypeScript library is planned for official release in Q4 2026. Until then it is not on npm: build it from source.
The streaming API is built for live feeds: open a stream once, then feed it one bar at a time. The stream carries its state from bar to bar, so a new bar never costs a pass over the history.
Every value is bit-identical to what the batch function would return by recomputing over the whole array (see detailed specs for a few exceptions).
Each streamable function adds two opening functions and a stream class, e.g. SmaStream, with a handful of methods:
| Call | When | Does |
|---|---|---|
<name>Open(history, options) | once | validate params, consume warm-up history, return a stream |
stream.update(bar) | once per closed bar | commit one bar and answer the new value |
stream.peek(bar) | any time on the forming bar | evaluate a provisional bar without committing |
A single-output function answers with a number, a multi-output one with an object holding its outputs: see Multi-input / multi-output.
One more call, <name>OpenAndFill, also returns array output: see Array-Fill Open below.
Additional utility functions are available.
There is no close: an unreferenced stream is simply garbage-collected.
Example (SMA)
import { smaOpen } from 'ta-lib';
// Seed with warm-up history (>= smaLookback(options) + 1 bars).
const history = new Float64Array([ /* ...your closing prices... */ ]);
const s = smaOpen(history, { timePeriod: 30 }); // value() starts at the last history bar
// Each time a bar closes:
const v = s.update(newClose); // throws on a non-finite bar, or past INDEX_MAX
// Intra-bar, on the not-yet-closed bar (repeat as the price ticks):
const provisional = s.peek(formingClose); // state left unchangedopen returns the stream directly; its value() starts at the last history bar's value. After a successful open, what update and peek reject is invalid input such as NaN or ±Inf; update also rejects a bar past Core.INDEX_MAX, the last index the batch API addresses. A rejection changes nothing at all: no state, no value, and no range. To count a rejected bar rather than re-feed it, call advance(); value() then answers the value(s) at the last bar the stream counted (see Utility Calls).
Rules
- Warm-up.
opensucceeds only ifhistory.length >= <name>Lookback(options) + 1: with fewer bars there is no defined value yet. Too little history, an empty one included, throwsTALibErrorwithcode'TA_INSUFFICIENT_HISTORY'(see Error model). Afteropen, the history can be discarded: the stream keeps everything it needs. - Closed vs forming bar.
updatecommits state irreversibly, so use it only for closed bars.peekreturns exactly the value the nextupdatewould, without committing: call it as often as the forming bar ticks.value()re-reads the last committed value without recomputing. - The unstable period is resolved once, at
open. The lookback above is the one of theCorethe stream is opened on. Under an Auto level it is the Auto lookback, soopenneeds that many bars plus one. - Parameters are fixed at
open. Changing a parameter means a new stream. The unstable period and candle settings come from theCorepassed as the opener'scoreoption, orCore.DEFAULT. SinceCoreis immutable they cannot change underneath a live stream: to stream with different settings, build a newCoreand open with that.
Multi-input / multi-output
Inputs mirror the batch function: the same positional arrays and price objects at open, then one number per input component at update and peek, named as the opener's positional inputs are, a price input by its components (update(high, low, close)). A multi-output function returns its outputs in a <Name>Out, a plain mutable object with one field per output, named as the batch result's. Candlestick patterns return a number:
import { cdldojiOpen, macdOpen, MacdOut } from 'ta-lib';
// MACD: one input, three outputs
const m = macdOpen(history, { fastPeriod: 12, slowPeriod: 26, signalPeriod: 9 });
const { outMacd, outMacdSignal, outMacdHist } = m.update(newClose); // a new MacdOut
const out = new MacdOut(); // allocate once, reuse every bar
m.update(newClose, out); // fills and returns out
// A candlestick pattern returns a number
const c = cdldojiOpen({ open, high, low, close });
const pattern = c.update(o, h, l, cl);Without a sink, update, peek and value each return a new object. Passing one sink makes them allocation-free: they overwrite its fields and return it. The price is that its contents are only valid until the next call that writes it, so a peek into the sink update filled overwrites it. Copy the fields out if the reading has to outlive the call, or allocate one sink per slot.
Array-Fill Open
open returns the stream alone. One more call also returns the batch result over the history, the same shape the batch function would produce, while still opening the stream:
| Call | When | Does |
|---|---|---|
<name>OpenAndFill(history, options) | once, instead of open | like open, but also returns the output for every history bar |
import { smaOpenAndFill } from 'ta-lib';
const { stream, begIdx, values } = smaOpenAndFill(history, { timePeriod: 30 });
// values[i] is the SMA at history bar begIdx + i; then stream on:
const v = stream.update(newClose);It takes the batch function's options, output buffers included, and ignores startIdx and endIdx; the result is the batch result plus stream. Its range is the stream's outRange(): see Utility Calls below. An output buffer must not share memory with the input or with another buffer.
Utility Calls
| Call | When | Does |
|---|---|---|
stream.value() / stream.value(out) | any time | the value(s) at the last bar the stream counted, without recomputing |
stream.clone() | any time | an independent fork of the stream, at the same bar |
stream.outRange() | any time | the bars the stream has an output for, as an OutRange (begIdx, count, isEmpty()): the batch range over the same bars |
stream.advance() | after a bar you will not feed | advances the range without affecting any other internal state of the stream |
const s = smaOpen(history, { timePeriod: 30 });
const v = s.value(); // the value at the last bar s counted
const fork = s.clone(); // independent from here on
const r = s.outRange(); // the bars s has an output for
s.advance(); // a bar you skipped, countedError model
Every rejection is a TALibError; branch on its code.
| Call | Behaviour |
|---|---|
<name>Open / <name>OpenAndFill | Too little history, an empty one included, throws 'TA_INSUFFICIENT_HISTORY': catch it to accumulate more bars and retry. Out-of-range parameters throw 'TA_BAD_PARAM'. |
update / peek |
|
advance | 'TA_OUT_OF_RANGE_END_INDEX' once the range has reached bar Core.INDEX_MAX, the last index the batch API addresses. |
value / clone / outRange | Never throw, except value(out) on a multi-output stream: it rejects the same sink as update and peek do. |
Discovering streamable functions
When driving TA-Lib through the Abstract API, streamable functions carry the FuncFlags.STREAMING bit in FuncInfo.flags.