@davoodmehraban89 / VELYQOR / commits / d224fed
Fork 0

Intelligence, Advanced Charting & Trading Platform

Phase 1: add deterministic spike detection (#12)

* test: define deterministic spike detection * feat: add deterministic price spike detection * fix: make spike threshold boundary floating-point safe * test: use tolerance for spike boundary percentage

davoodmehraban89 committed Oct 5, 2026 at 17:03 UTC d224fed3d6d826e4abdb3bb673277ad80efadedf
2 files changed +79 -4
packages/market-domain/src/data-quality.mjs
+17 -4
@@ -10,6 +10,8 @@ export function evaluateObservationQuality({
10 maxAgeMs,
11 referencePrice = null,
12 maxDivergencePct = null,
13 + previousPrice = null,
14 + maxMovePct = null,
15 }) {
16 finiteNonNegative(ageMs, 'ageMs');
17 finiteNonNegative(maxAgeMs, 'maxAgeMs');
@@ -17,7 +19,7 @@ export function evaluateObservationQuality({
19 const flags = [];
20
21 if (!Number.isFinite(price) || price <= 0) {
20 - return { state: 'INVALID', flags: ['NON_POSITIVE_PRICE'], divergencePct: null };
22 + return { state: 'INVALID', flags: ['NON_POSITIVE_PRICE'], divergencePct: null, movePct: null };
23 }
24
25 if (ageMs > maxAgeMs) {
@@ -34,7 +36,18 @@ export function evaluateObservationQuality({
36 if (divergencePct > maxDivergencePct) flags.push('PROVIDER_DIVERGENCE');
37 }
38
37 - if (flags.includes('STALE_SOURCE')) return { state: 'STALE', flags, divergencePct };
38 - if (flags.length > 0) return { state: 'DEGRADED', flags, divergencePct };
39 - return { state: 'GOOD', flags, divergencePct };
39 + let movePct = null;
40 + if (previousPrice !== null) {
41 + if (!Number.isFinite(previousPrice) || previousPrice <= 0) {
42 + throw new RangeError('previousPrice must be positive and finite when provided');
43 + }
44 + finiteNonNegative(maxMovePct, 'maxMovePct');
45 + movePct = Math.abs(price / previousPrice - 1) * 100;
46 + const epsilon = Math.max(1e-12, Math.abs(maxMovePct) * Number.EPSILON * 8);
47 + if (movePct - maxMovePct > epsilon) flags.push('PRICE_SPIKE');
48 + }
49 +
50 + if (flags.includes('STALE_SOURCE')) return { state: 'STALE', flags, divergencePct, movePct };
51 + if (flags.length > 0) return { state: 'DEGRADED', flags, divergencePct, movePct };
52 + return { state: 'GOOD', flags, divergencePct, movePct };
53 }
packages/market-domain/test/spike-detection.test.mjs new
+62
@@ -0,0 +1,62 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { evaluateObservationQuality } from '../src/data-quality.mjs';
4 +
5 +test('flags an excessive move from the previous observation as a price spike', () => {
6 + const result = evaluateObservationQuality({
7 + price: 110,
8 + previousPrice: 100,
9 + maxMovePct: 5,
10 + ageMs: 1_000,
11 + maxAgeMs: 60_000,
12 + });
13 +
14 + assert.equal(result.state, 'DEGRADED');
15 + assert.ok(result.flags.includes('PRICE_SPIKE'));
16 + assert.ok(Math.abs(result.movePct - 10) < 1e-9);
17 +});
18 +
19 +test('does not flag a move at the configured boundary', () => {
20 + const result = evaluateObservationQuality({
21 + price: 105,
22 + previousPrice: 100,
23 + maxMovePct: 5,
24 + ageMs: 1_000,
25 + maxAgeMs: 60_000,
26 + });
27 +
28 + assert.equal(result.state, 'GOOD');
29 + assert.ok(Math.abs(result.movePct - 5) < 1e-9);
30 +});
31 +
32 +test('staleness remains the dominant state when a stale observation also spikes', () => {
33 + const result = evaluateObservationQuality({
34 + price: 120,
35 + previousPrice: 100,
36 + maxMovePct: 5,
37 + ageMs: 120_000,
38 + maxAgeMs: 60_000,
39 + });
40 +
41 + assert.equal(result.state, 'STALE');
42 + assert.ok(result.flags.includes('STALE_SOURCE'));
43 + assert.ok(result.flags.includes('PRICE_SPIKE'));
44 +});
45 +
46 +test('rejects an invalid previous price or spike threshold', () => {
47 + assert.throws(() => evaluateObservationQuality({
48 + price: 100,
49 + previousPrice: 0,
50 + maxMovePct: 5,
51 + ageMs: 1_000,
52 + maxAgeMs: 60_000,
53 + }), /previousPrice/);
54 +
55 + assert.throws(() => evaluateObservationQuality({
56 + price: 100,
57 + previousPrice: 99,
58 + maxMovePct: -1,
59 + ageMs: 1_000,
60 + maxAgeMs: 60_000,
61 + }), /maxMovePct/);
62 +});