149 lines
4.2 KiB
TypeScript
149 lines
4.2 KiB
TypeScript
import {
|
|
collect,
|
|
configuration,
|
|
count,
|
|
finite,
|
|
nonnegative,
|
|
optionalNumber,
|
|
period,
|
|
snapshot,
|
|
validateBar,
|
|
type HlcBar,
|
|
type IndicatorSeries,
|
|
} from './contracts';
|
|
|
|
export const ATR_CONTRACT = 'atr-wilder/v1';
|
|
export type AtrFirstRange = 'previous-close' | 'high-low';
|
|
export interface AtrOptions {
|
|
firstRange?: AtrFirstRange;
|
|
}
|
|
export interface AtrSnapshot {
|
|
contract: typeof ATR_CONTRACT;
|
|
period: number;
|
|
firstRange: AtrFirstRange;
|
|
count: number;
|
|
previousClose: number | null;
|
|
seedSum: number;
|
|
value: number | null;
|
|
}
|
|
|
|
function firstRange(value: unknown): AtrFirstRange {
|
|
if (value !== 'previous-close' && value !== 'high-low') {
|
|
throw new Error('ATR first range must be previous-close or high-low');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function parse(input: unknown): AtrSnapshot {
|
|
const value = snapshot(input, [
|
|
'contract',
|
|
'period',
|
|
'firstRange',
|
|
'count',
|
|
'previousClose',
|
|
'seedSum',
|
|
'value',
|
|
]);
|
|
if (value.contract !== ATR_CONTRACT) {
|
|
throw new Error('Unsupported ATR snapshot contract');
|
|
}
|
|
const state: AtrSnapshot = {
|
|
contract: ATR_CONTRACT,
|
|
period: period(value.period),
|
|
firstRange: firstRange(value.firstRange),
|
|
count: count(value.count),
|
|
previousClose: optionalNumber(value.previousClose),
|
|
seedSum: nonnegative(value.seedSum),
|
|
value: optionalNumber(value.value),
|
|
};
|
|
const ranges = state.count - (state.firstRange === 'previous-close' ? 1 : 0);
|
|
const ready = ranges >= state.period;
|
|
if (
|
|
(state.previousClose !== null) !== state.count > 0 ||
|
|
(state.value !== null) !== ready ||
|
|
(state.value !== null && state.value < 0) ||
|
|
((ready || ranges <= 0) && state.seedSum !== 0)
|
|
) {
|
|
throw new Error('Inconsistent ATR snapshot phase');
|
|
}
|
|
return state;
|
|
}
|
|
|
|
/** Wilder ATR with an explicit first-bar policy; finite HLC inputs, O(1) memory. */
|
|
export class AtrIndicator {
|
|
private state: AtrSnapshot;
|
|
constructor(size = 14, options: AtrOptions = {}) {
|
|
configuration(options, ['firstRange']);
|
|
this.state = {
|
|
contract: ATR_CONTRACT,
|
|
period: period(size),
|
|
firstRange: firstRange(
|
|
options.firstRange === undefined ? 'previous-close' : options.firstRange
|
|
),
|
|
count: 0,
|
|
previousClose: null,
|
|
seedSum: 0,
|
|
value: null,
|
|
};
|
|
}
|
|
update(bar: HlcBar): number | null {
|
|
validateBar(bar);
|
|
const prior = this.state;
|
|
const next = { ...prior, count: count(prior.count + 1), previousClose: bar.close };
|
|
if (prior.previousClose !== null || prior.firstRange === 'high-low') {
|
|
const range = finite(
|
|
prior.previousClose === null
|
|
? bar.high - bar.low
|
|
: Math.max(
|
|
bar.high - bar.low,
|
|
Math.abs(bar.high - prior.previousClose),
|
|
Math.abs(bar.low - prior.previousClose)
|
|
)
|
|
);
|
|
if (prior.value === null) {
|
|
next.seedSum = nonnegative(prior.seedSum + range);
|
|
const ranges = next.count - (prior.firstRange === 'previous-close' ? 1 : 0);
|
|
if (ranges === prior.period) {
|
|
next.value = nonnegative(next.seedSum / prior.period);
|
|
next.seedSum = 0;
|
|
}
|
|
} else {
|
|
next.value = nonnegative((prior.value * (prior.period - 1) + range) / prior.period);
|
|
}
|
|
}
|
|
this.state = next;
|
|
return next.value;
|
|
}
|
|
current(): number | null {
|
|
return this.state.value;
|
|
}
|
|
snapshot(): AtrSnapshot {
|
|
return { ...this.state };
|
|
}
|
|
reset(): void {
|
|
this.state = { ...this.state, count: 0, previousClose: null, seedSum: 0, value: null };
|
|
}
|
|
restore(input: unknown): void {
|
|
const next = parse(input);
|
|
if (next.period !== this.state.period || next.firstRange !== this.state.firstRange) {
|
|
throw new Error('ATR snapshot configuration mismatch');
|
|
}
|
|
this.state = next;
|
|
}
|
|
static fromSnapshot(input: unknown): AtrIndicator {
|
|
const state = parse(input);
|
|
const stream = new AtrIndicator(state.period, { firstRange: state.firstRange });
|
|
stream.restore(state);
|
|
return stream;
|
|
}
|
|
}
|
|
|
|
export function atr(bars: readonly HlcBar[], size = 14, options: AtrOptions = {}): IndicatorSeries {
|
|
const stream = new AtrIndicator(size, options);
|
|
return collect(
|
|
bars,
|
|
stream,
|
|
ATR_CONTRACT,
|
|
size - (stream.snapshot().firstRange === 'high-low' ? 1 : 0)
|
|
);
|
|
}
|