Intelligence, Advanced Charting & Trading Platform

1 /**
2 * Future-paper-feed boundary only: does NOT contact a provider or execute orders.
3 * An operator must separately verify authenticity, entitlement, signature, and transport.
4 */
5 const timestamp=(s,name)=>{
6 if(typeof s!=='string'||!/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?(?:Z|[+-]\d\d:\d\d)$/.test(s)||
7 !Number.isFinite(Date.parse(s)))throw new TypeError(name+' needs ISO timezone');
8 return Date.parse(s);
9 };
10 const goodPrice=(value,name)=>{
11 const n=Number(value);
12 if(!Number.isFinite(n)||n<=0)throw new TypeError(name+' must be positive and finite');
13 return n;
14 };
15 export function validatePaperQuote(quote,{
16 symbol, side, now, maxAgeMs=15000, lastSequence=0,
17 approvedSources=[]
18 }={}){
19 if(!quote||typeof quote!=='object'||Array.isArray(quote))
20 throw new TypeError('invalid quote');
21 if(!/^[A-Z0-9_]{3,48}$/.test(symbol??'')||quote.symbol!==symbol)
22 throw new TypeError('symbol mismatch');
23 if(side!=='LONG'&&side!=='SHORT')throw new TypeError('unsupported side');
24 if(!Array.isArray(approvedSources)||!approvedSources.includes(quote.source)||
25 !/^[a-z][a-z0-9_]{2,48}$/.test(quote.source??''))
26 throw new TypeError('source not approved for paper quotes');
27 if(quote.usage!=='PAPER_ONLY_BID_ASK'||quote.entitlement!=='reviewed_simulation_display'||
28 quote.dataKind!=='bid_ask_quote')
29 throw new TypeError('quote type/usage not authorized');
30 if(quote.quality!=='GOOD'||quote.isMarketOpen!==true)
31 throw new TypeError('quote not healthy or market not open');
32 if(typeof quote.unit!=='string'||!/^[A-Z0-9_]{3,40}$/.test(quote.unit))
33 throw new TypeError('missing canonical quote unit');
34 if(!Number.isSafeInteger(quote.sequence)||quote.sequence<=lastSequence)
35 throw new RangeError('replayed or invalid quote sequence');
36 if(!Number.isInteger(maxAgeMs)||maxAgeMs<1000||maxAgeMs>60000)
37 throw new RangeError('unsupported freshness tolerance');
38 const receivedAt=timestamp(quote.receivedAt,'receivedAt');
39 const sourceAt=timestamp(quote.sourceTimestamp,'sourceTimestamp');
40 const currentAt=timestamp(now,'now');
41 if(sourceAt>receivedAt+1000||receivedAt>currentAt+1000||
42 sourceAt>currentAt+1000||currentAt-receivedAt>maxAgeMs||
43 currentAt-sourceAt>maxAgeMs)
44 throw new RangeError('stale or future quote');
45 const bid=goodPrice(quote.bid,'bid'),ask=goodPrice(quote.ask,'ask');
46 if(bid>ask)throw new RangeError('crossed bid/ask quote');
47 if((ask-bid)/ask>0.05)throw new RangeError('spread greater than 5% safety cap');
48 return Object.freeze({
49 symbol,source:quote.source,unit:quote.unit,sequence:quote.sequence,
50 // LONG exits against bid, SHORT exits against ask; NOT a guaranteed fill.
51 hypotheticalExitMark:side==='LONG'?bid:ask,
52 bid,ask,sourceTimestamp:new Date(sourceAt).toISOString(),
53 receivedAt:new Date(receivedAt).toISOString(),kind:'PAPER_QUOTE_ELIGIBLE'
54 });
55 }