stock-bot/libs/calculations/src/indicators/ema.ts

122 lines
3.3 KiB
TypeScript

import {
collect,
configuration,
count,
finite,
optionalNumber,
period,
snapshot,
type IndicatorSeries,
} from './contracts';
export const EMA_CONTRACT = 'ema/v1';
export type EmaSeed = 'sma' | 'first';
export interface EmaOptions {
seed?: EmaSeed;
}
export interface EmaSnapshot {
contract: typeof EMA_CONTRACT;
period: number;
seed: EmaSeed;
count: number;
value: number | null;
seedSum: number;
}
function seed(value: unknown): EmaSeed {
if (value !== 'sma' && value !== 'first') {
throw new Error('EMA seed must be sma or first');
}
return value;
}
function parse(input: unknown): EmaSnapshot {
const value = snapshot(input, ['contract', 'period', 'seed', 'count', 'value', 'seedSum']);
if (value.contract !== EMA_CONTRACT) {
throw new Error('Unsupported EMA snapshot contract');
}
const state: EmaSnapshot = {
contract: EMA_CONTRACT,
period: period(value.period),
seed: seed(value.seed),
count: count(value.count),
value: optionalNumber(value.value),
seedSum: finite(value.seedSum),
};
const ready = state.count >= (state.seed === 'first' ? 1 : state.period);
if (
(state.value !== null) !== ready ||
((ready || state.count === 0 || state.seed === 'first') && state.seedSum !== 0)
) {
throw new Error('Inconsistent EMA snapshot phase');
}
return state;
}
/** O(1) memory. Rejected inputs/restores leave all prior state intact. */
export class EmaIndicator {
private state: EmaSnapshot;
constructor(size: number, options: EmaOptions = {}) {
configuration(options, ['seed']);
this.state = {
contract: EMA_CONTRACT,
period: period(size),
seed: seed(options.seed === undefined ? 'sma' : options.seed),
count: 0,
value: null,
seedSum: 0,
};
}
update(input: number): number | null {
const value = finite(input);
const next = { ...this.state, count: count(this.state.count + 1) };
if (next.value !== null) {
const alpha = 2 / (next.period + 1);
next.value = finite(alpha * value + (1 - alpha) * next.value);
} else if (next.seed === 'first') {
next.value = value;
} else {
next.seedSum = finite(next.seedSum + value);
if (next.count === next.period) {
next.value = finite(next.seedSum / next.period);
next.seedSum = 0;
}
}
this.state = next;
return next.value;
}
current(): number | null {
return this.state.value;
}
snapshot(): EmaSnapshot {
return { ...this.state };
}
reset(): void {
this.state = { ...this.state, count: 0, value: null, seedSum: 0 };
}
restore(input: unknown): void {
const next = parse(input);
if (next.period !== this.state.period || next.seed !== this.state.seed) {
throw new Error('EMA snapshot configuration mismatch');
}
this.state = next;
}
static fromSnapshot(input: unknown): EmaIndicator {
const state = parse(input);
const stream = new EmaIndicator(state.period, { seed: state.seed });
stream.restore(state);
return stream;
}
}
export function ema(
values: readonly number[],
size: number,
options: EmaOptions = {}
): IndicatorSeries {
const stream = new EmaIndicator(size, options);
return collect(values, stream, EMA_CONTRACT, stream.snapshot().seed === 'first' ? 0 : size - 1);
}