175 lines
4.8 KiB
TypeScript
175 lines
4.8 KiB
TypeScript
import { at } from '@stock-bot/invariants';
|
|
import {
|
|
collect,
|
|
configuration,
|
|
count,
|
|
finite,
|
|
nonnegative,
|
|
optionalNumber,
|
|
period,
|
|
snapshot,
|
|
type IndicatorSeries,
|
|
} from './contracts';
|
|
|
|
export const RSI_CONTRACT = 'rsi-wilder/v1';
|
|
export type FlatRsi = 0 | 50 | 100;
|
|
export interface RsiOptions {
|
|
flatValue?: FlatRsi;
|
|
}
|
|
export interface RsiSnapshot {
|
|
contract: typeof RSI_CONTRACT;
|
|
period: number;
|
|
flatValue: FlatRsi;
|
|
count: number;
|
|
previous: number | null;
|
|
gain: number;
|
|
loss: number;
|
|
}
|
|
|
|
function flat(value: unknown): FlatRsi {
|
|
if (value !== 0 && value !== 50 && value !== 100) {
|
|
throw new Error('Flat RSI value must be 0, 50 or 100');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function oscillator(gain: number, loss: number, flatValue: FlatRsi): number {
|
|
if (gain === 0 && loss === 0) {
|
|
return flatValue;
|
|
}
|
|
if (loss === 0) {
|
|
return 100;
|
|
}
|
|
return finite(100 - 100 / (1 + gain / loss));
|
|
}
|
|
|
|
function parse(input: unknown): RsiSnapshot {
|
|
const value = snapshot(input, [
|
|
'contract',
|
|
'period',
|
|
'flatValue',
|
|
'count',
|
|
'previous',
|
|
'gain',
|
|
'loss',
|
|
]);
|
|
if (value.contract !== RSI_CONTRACT) {
|
|
throw new Error('Unsupported RSI snapshot contract');
|
|
}
|
|
const state: RsiSnapshot = {
|
|
contract: RSI_CONTRACT,
|
|
period: period(value.period),
|
|
flatValue: flat(value.flatValue),
|
|
count: count(value.count),
|
|
previous: optionalNumber(value.previous),
|
|
gain: nonnegative(value.gain),
|
|
loss: nonnegative(value.loss),
|
|
};
|
|
if (
|
|
(state.previous !== null) !== state.count > 0 ||
|
|
(state.count <= 1 && (state.gain !== 0 || state.loss !== 0))
|
|
) {
|
|
throw new Error('Inconsistent RSI snapshot phase');
|
|
}
|
|
return state;
|
|
}
|
|
|
|
/** SMA of the first n changes, then Wilder smoothing; the seed change is consumed once. */
|
|
export class RsiIndicator {
|
|
private state: RsiSnapshot;
|
|
constructor(size = 14, options: RsiOptions = {}) {
|
|
configuration(options, ['flatValue']);
|
|
this.state = {
|
|
contract: RSI_CONTRACT,
|
|
period: period(size),
|
|
flatValue: flat(options.flatValue === undefined ? 50 : options.flatValue),
|
|
count: 0,
|
|
previous: null,
|
|
gain: 0,
|
|
loss: 0,
|
|
};
|
|
}
|
|
update(input: number): number | null {
|
|
const value = finite(input);
|
|
const prior = this.state;
|
|
const next = { ...prior, count: count(prior.count + 1), previous: value };
|
|
if (prior.previous !== null) {
|
|
const change = finite(value - prior.previous);
|
|
const gain = Math.max(0, change),
|
|
loss = Math.max(0, -change);
|
|
if (prior.count <= prior.period) {
|
|
next.gain = nonnegative(prior.gain + gain);
|
|
next.loss = nonnegative(prior.loss + loss);
|
|
if (prior.count === prior.period) {
|
|
next.gain /= prior.period;
|
|
next.loss /= prior.period;
|
|
}
|
|
} else {
|
|
next.gain = nonnegative((prior.gain * (prior.period - 1) + gain) / prior.period);
|
|
next.loss = nonnegative((prior.loss * (prior.period - 1) + loss) / prior.period);
|
|
}
|
|
}
|
|
const result =
|
|
next.count > next.period ? oscillator(next.gain, next.loss, next.flatValue) : null;
|
|
this.state = next;
|
|
return result;
|
|
}
|
|
current(): number | null {
|
|
return this.state.count > this.state.period
|
|
? oscillator(this.state.gain, this.state.loss, this.state.flatValue)
|
|
: null;
|
|
}
|
|
snapshot(): RsiSnapshot {
|
|
return { ...this.state };
|
|
}
|
|
reset(): void {
|
|
this.state = { ...this.state, count: 0, previous: null, gain: 0, loss: 0 };
|
|
}
|
|
restore(input: unknown): void {
|
|
const next = parse(input);
|
|
if (next.period !== this.state.period || next.flatValue !== this.state.flatValue) {
|
|
throw new Error('RSI snapshot configuration mismatch');
|
|
}
|
|
this.state = next;
|
|
}
|
|
static fromSnapshot(input: unknown): RsiIndicator {
|
|
const state = parse(input);
|
|
const stream = new RsiIndicator(state.period, { flatValue: state.flatValue });
|
|
stream.restore(state);
|
|
return stream;
|
|
}
|
|
}
|
|
|
|
export function rsi(
|
|
values: readonly number[],
|
|
size = 14,
|
|
options: RsiOptions = {}
|
|
): IndicatorSeries {
|
|
return collect(values, new RsiIndicator(size, options), RSI_CONTRACT, size);
|
|
}
|
|
|
|
/** Rolling simple-average RSI, NOT Wilder RSI. Reverse summation preserves mean-reversion/v2 arithmetic. */
|
|
export function trailingSimpleRsi(
|
|
values: readonly number[],
|
|
size = 14,
|
|
flatValue: FlatRsi = 50
|
|
): number | undefined {
|
|
period(size);
|
|
flat(flatValue);
|
|
if (values.length < size + 1) {
|
|
return undefined;
|
|
}
|
|
let gain = 0,
|
|
loss = 0;
|
|
for (let i = 1; i <= size; i++) {
|
|
const change = finite(
|
|
finite(at(values, values.length - i)) - finite(at(values, values.length - i - 1))
|
|
);
|
|
if (change > 0) {
|
|
gain = finite(gain + change);
|
|
} else {
|
|
loss = finite(loss + Math.abs(change));
|
|
}
|
|
}
|
|
return oscillator(gain / size, loss / size, flatValue);
|
|
}
|