@@ -4,71 +4,112 @@ export type Quote = {
44 bid ?: number
55 ask ?: number
66 mid ?: number
7+ bidSize ?: number
8+ askSize ?: number
79 latestPrice ?: number
810 quoteProviderTimeUnixMs ?: number
911 tradeProviderTimeUnixMs ?: number
1012}
1113
1214export class InstrumentQuoteCache {
13- private readonly map = new Map < string , Quote > ( )
15+ private readonly map = new Map < string , Map < string , Quote > > ( )
1416
15- activate ( isin : string ) {
16- if ( ! this . map . has ( isin ) ) this . map . set ( isin , { } )
17+ isEmpty ( ) : boolean {
18+ return this . map . size === 0
19+ }
20+
21+ hasMarket ( market : string ) : boolean {
22+ const marketMap = this . map . get ( market )
23+ return ! ! marketMap && marketMap . size > 0
24+ }
25+
26+ getMarkets ( ) : string [ ] {
27+ return [ ...this . map ] . flatMap ( ( [ market , bucket ] ) => ( bucket . size ? [ market ] : [ ] ) )
1728 }
18- deactivate ( isin : string ) {
19- this . map . delete ( isin )
29+
30+ activate ( market : string , isin : string ) {
31+ let marketMap = this . map . get ( market )
32+ if ( ! marketMap ) {
33+ marketMap = new Map < string , Quote > ( )
34+ this . map . set ( market , marketMap )
35+ }
36+ if ( ! marketMap . has ( isin ) ) marketMap . set ( isin , { } )
37+ }
38+
39+ deactivate ( market : string , isin : string ) {
40+ const marketMap = this . map . get ( market )
41+ if ( ! marketMap ) {
42+ return
43+ }
44+ marketMap . delete ( isin )
45+ if ( marketMap . size === 0 ) {
46+ this . map . delete ( market )
47+ }
2048 }
21- has ( isin : string ) : boolean {
22- return this . map . has ( isin )
49+
50+ has ( market : string , isin : string ) : boolean {
51+ return this . map . get ( market ) ?. has ( isin ) ?? false
2352 }
24- get ( isin : string ) : Quote | undefined {
25- return this . map . get ( isin )
53+
54+ get ( market : string , isin : string ) : Quote | undefined {
55+ return this . map . get ( market ) ?. get ( isin )
2656 }
27- addQuote ( isin : string , bid : number , ask : number , providerTime : number ) {
28- const quote = this . get ( isin )
57+
58+ addQuote (
59+ market : string ,
60+ isin : string ,
61+ bid : number ,
62+ ask : number ,
63+ providerTime : number ,
64+ bidSz : number ,
65+ askSz : number ,
66+ ) {
67+ const quote = this . get ( market , isin )
2968 if ( ! quote ) {
30- throw new Error ( `Cannot add quote for inactive ISIN ${ isin } ` )
69+ throw new Error ( `Cannot add quote for inactive instrument ${ market } - ${ isin } ` )
3170 }
3271 const mid = new Decimal ( bid ) . plus ( ask ) . div ( 2 )
3372 quote . bid = bid
3473 quote . ask = ask
3574 quote . mid = mid . toNumber ( )
3675 quote . quoteProviderTimeUnixMs = providerTime
76+ quote . bidSize = bidSz
77+ quote . askSize = askSz
3778 }
38- addBid ( isin : string , bid : number , providerTime : number ) {
39- const quote = this . get ( isin )
79+
80+ addBid ( market : string , isin : string , bid : number , providerTime : number , bidSz ?: number ) {
81+ const quote = this . get ( market , isin )
4082 if ( ! quote ) {
4183 throw new Error ( `Cannot add quote for inactive ISIN ${ isin } ` )
4284 }
4385 if ( quote . ask !== undefined ) {
44- const mid = new Decimal ( bid ) . plus ( quote . ask ) . div ( 2 )
45- quote . mid = mid . toNumber ( )
86+ quote . mid = new Decimal ( bid ) . plus ( quote . ask ) . div ( 2 ) . toNumber ( )
4687 }
4788 quote . bid = bid
4889 quote . quoteProviderTimeUnixMs = providerTime
90+ quote . bidSize = bidSz
4991 }
50- addAsk ( isin : string , ask : number , providerTime : number ) {
51- const quote = this . get ( isin )
92+
93+ addAsk ( market : string , isin : string , ask : number , providerTime : number , askSz ?: number ) {
94+ const quote = this . get ( market , isin )
5295 if ( ! quote ) {
5396 throw new Error ( `Cannot add quote for inactive ISIN ${ isin } ` )
5497 }
5598
5699 if ( quote . bid !== undefined ) {
57- const mid = new Decimal ( quote . bid ) . plus ( ask ) . div ( 2 )
58- quote . mid = mid . toNumber ( )
100+ quote . mid = new Decimal ( quote . bid ) . plus ( ask ) . div ( 2 ) . toNumber ( )
59101 }
60102 quote . ask = ask
61103 quote . quoteProviderTimeUnixMs = providerTime
104+ quote . askSize = askSz
62105 }
63- addTrade ( isin : string , lastPrice : number , providerTime : number ) {
64- const quote = this . get ( isin )
106+
107+ addTrade ( market : string , isin : string , lastPrice : number , providerTime : number ) {
108+ const quote = this . get ( market , isin )
65109 if ( ! quote ) {
66- throw new Error ( `Cannot add trade for inactive ISIN ${ isin } ` )
110+ throw new Error ( `Cannot add trade for inactive instrument ${ market } - ${ isin } ` )
67111 }
68112 quote . latestPrice = lastPrice
69113 quote . tradeProviderTimeUnixMs = providerTime
70114 }
71- isEmpty ( ) : boolean {
72- return this . map . size === 0
73- }
74115}
0 commit comments