Intelligence, Advanced Charting & Trading Platform
feat: add deterministic price spike detection
davoodmehraban89 committed
Oct 5, 2026 at 16:58 UTC
fcc58e923f513edc44209234c05e1e084794ee9c
1 file changed
+16
-4
packages/market-domain/src/data-quality.mjs
+16
-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,17 @@ 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
+ if (movePct > maxMovePct) flags.push('PRICE_SPIKE');
47
+ }
48
+
49
+ if (flags.includes('STALE_SOURCE')) return { state: 'STALE', flags, divergencePct, movePct };
50
+ if (flags.length > 0) return { state: 'DEGRADED', flags, divergencePct, movePct };
51
+ return { state: 'GOOD', flags, divergencePct, movePct };
52
}