/** * The rolling lookback and the seeded recurrence: the two pieces every length * taking function in this library is built from. * * "Lookback" rather than the other word for the same thing: that one names a * browser global, and the layering check refuses it anywhere in the text of a * file under `src/core`, which is what keeps this tier runnable in a worker and * on a server rather than only in a page. * * Both are held in a state region (`region.ts`) rather than in local variables, * so one implementation serves a library call folded over a series and an * engine advancing a chart a bar at a time. * * **The accumulation order is part of the contract.** Binary64 addition is not * associative, so a sum has a value and an order, and two engines that add the * same lookback two ways disagree in the last bits. This library fixes one * order and uses it everywhere: * * **oldest bar first, newest bar last, in index order.** * * That is the order `compiled-program.md` section 8.2 gives for iteration, and * it is the order a reader assumes when they see a lookback written down. * * **The sum is taken fresh over the lookback on every bar.** The tempting * alternative, carrying a running total and subtracting the value that leaves, * is O(1) instead of O(len) but it is a different number: the error in a * running total accumulates without bound over forty thousand bars, and it is * not bit-identical to a fresh sum, which `compiled-program.md` section 8.3 * refuses outright. A lookback costs `len` additions per bar, which is bounded by * the length the script asked for rather than by how much history is loaded, * and that is the property a live chart actually needs. */ import type { StateRecord } from './region.js'; import type { Value } from './value.js'; /** A fixed length view of the most recent bars. */ export interface Lookback { /** Advance by one bar. */ push(value: Value): void; /** True after the largest observed length's contributions, present or not. */ filled(): boolean; /** True when the lookback is filled and every bar in it has a value. */ complete(): boolean; /** The value `back` bars ago, 0 being the bar just pushed. */ at(back: number): Value; /** How many bars of the lookback have a value. */ presentCount(): number; /** The sum over the lookback, oldest first, absent unless the lookback is complete. */ sum(): Value; /** The sum over the lookback with absent bars left out, absent when it is not filled. */ sumPresent(): Value; /** The arithmetic mean over the lookback, absent unless the lookback is complete. */ mean(): Value; } /** * A lookback of `len` bars, held in a region under `key`. * * A length outside its contract yields a lookback that never fills, so every * function built on it returns absence rather than a number computed from a * nonsense length. See `isLength` for why that is absence and not a diagnostic. */ export declare function ring(record: StateRecord, key: string, len: number | null): Lookback; /** A lookback of `len` bars in a region of its own, for a caller that has none. */ export declare function makeLookback(len: number): Lookback; /** One step of a recurrence, from the previous running value and this bar's. */ export type Recurrence = (previous: number, value: number) => number; /** * Seeded smoothing: the mean of the first complete lookback, then one step per * bar. * * The seed lookback is complete only when it holds `len` present values, so an * average taken over another study's output starts counting at that study's * first value rather than at bar 0. That is what makes the warmups of * `stdlib.md` compose, and it is why `ema(sma(close, 10), 10)` is absent until * bar 18 rather than bar 9. * * A hole in the input freezes the recurrence: an absent bar after seeding * produces an absent bar out and leaves the running value untouched, so the * next present bar continues from where the last one left off. The alternatives * are worse: consuming absence as zero would drag the average toward nothing, * and re-seeding would let one missing bar restart a two hundred bar average. * Missing lengths also freeze it. Readiness tracks all contributions and the * largest observed length, independently from the running value. A valid step * still advances that value while a later, larger length hides its output. */ export declare function smoothed(record: StateRecord, key: string, len: number | null, value: Value, step: Recurrence): Value; //# sourceMappingURL=lookback.d.ts.map