Skip to content

Commit 176fa5e

Browse files
committed
feat: add stopbook on snapshot
1 parent 2e905e3 commit 176fa5e

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

src/orderbook.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ export class OrderBook {
421421
this.asks.priceTree().forEach((price: number, orders: OrderQueue) => {
422422
asks.push({ price, orders: orders.toArray().map((o) => o.toObject()) });
423423
});
424-
return { bids, asks, ts: Date.now(), lastOp: this._lastOp };
424+
const stopBook = this.stopBook.snapshot();
425+
return { bids, asks, stopBook, ts: Date.now(), lastOp: this._lastOp };
425426
};
426427

427428
private readonly _market = (
@@ -572,6 +573,28 @@ export class OrderBook {
572573
this.asks.append(newOrder);
573574
}
574575
}
576+
577+
if (snapshot.stopBook?.bids?.length > 0) {
578+
for (const level of snapshot.stopBook.bids) {
579+
for (const order of level.orders) {
580+
// @ts-expect-error // TODO fix types
581+
const newOrder = OrderFactory.createOrder(order);
582+
// @ts-expect-error // TODO fix types
583+
this.stopBook.add(newOrder);
584+
}
585+
}
586+
}
587+
588+
if (snapshot.stopBook?.asks?.length > 0) {
589+
for (const level of snapshot.stopBook.asks) {
590+
for (const order of level.orders) {
591+
// @ts-expect-error // TODO fix types
592+
const newOrder = OrderFactory.createOrder(order);
593+
// @ts-expect-error // TODO fix types
594+
this.stopBook.add(newOrder);
595+
}
596+
}
597+
}
575598
};
576599

577600
/**

src/stopbook.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
22
import type { StopQueue } from "./stopqueue";
33
import { StopSide } from "./stopside";
4-
import { OrderType, Side, type StopOrder } from "./types";
4+
import { type IStopOrder, OrderType, Side, type StopOrder } from "./types";
55

66
export class StopBook {
77
private readonly bids: StopSide;
@@ -76,5 +76,17 @@ export class StopBook {
7676
}
7777
return response;
7878
};
79+
80+
snapshot = () => {
81+
const bids: Array<{ price: number; orders: IStopOrder[] }> = [];
82+
const asks: Array<{ price: number; orders: IStopOrder[] }> = [];
83+
this.bids.priceTree().forEach((price: number, orders: StopQueue) => {
84+
bids.push({ price, orders: orders.toArray().map((o) => o.toObject()) });
85+
});
86+
this.asks.priceTree().forEach((price: number, orders: StopQueue) => {
87+
asks.push({ price, orders: orders.toArray().map((o) => o.toObject()) });
88+
});
89+
return { bids, asks };
90+
};
7991
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
8092
}

src/stopqueue.ts

+4
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@ export class StopQueue {
5252
}
5353
return deletedOrder;
5454
};
55+
56+
toArray = (): StopOrder[] => {
57+
return this._orders.toArray();
58+
};
5559
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
5660
}

src/stopside.ts

+4
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,9 @@ export class StopSide {
7777
);
7878
return queues;
7979
};
80+
81+
priceTree = (): createRBTree.Tree<number, StopQueue> => {
82+
return this._priceTree;
83+
};
8084
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
8185
}

src/types.ts

+10
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,16 @@ export interface Snapshot {
399399
/** List of orders associated with this price */
400400
orders: ILimitOrder[];
401401
}>;
402+
stopBook: {
403+
asks: Array<{
404+
price: number;
405+
orders: IStopOrder[];
406+
}>;
407+
bids: Array<{
408+
price: number;
409+
orders: IStopOrder[];
410+
}>;
411+
};
402412
/** Unix timestamp representing when the snapshot was taken */
403413
ts: number;
404414
/** The id of the last operation inserted in the orderbook */

0 commit comments

Comments
 (0)