nodejs-order-book 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,218 @@
1
+ import { type OrderBookError } from "./errors";
2
+ import { type LimitOrder } from "./order";
3
+ import { type CreateOrderOptions, type ICancelOrder, type IProcessOrder, type LimitOrderOptions, type MarketOrderOptions, type OCOOrderOptions, type OrderBookOptions, OrderType, type OrderUpdatePrice, type OrderUpdateSize, Side, type Snapshot, type StopLimitOrderOptions, type StopMarketOrderOptions, TimeInForce } from "./types";
4
+ export declare class OrderBook {
5
+ private orders;
6
+ private _lastOp;
7
+ private _marketPrice;
8
+ private readonly bids;
9
+ private readonly asks;
10
+ private readonly enableJournaling;
11
+ private readonly stopBook;
12
+ private readonly experimentalConditionalOrders;
13
+ /**
14
+ * Creates an instance of OrderBook.
15
+ * @param {OrderBookOptions} [options={}] - Options for configuring the order book.
16
+ * @param {JournalLog} [options.snapshot] - The orderbook snapshot will be restored before processing any journal logs, if any.
17
+ * @param {JournalLog} [options.journal] - Array of journal logs (optional).
18
+ * @param {boolean} [options.enableJournaling=false] - Flag to enable journaling. Default to false
19
+ * @param {boolean} [options.experimentalConditionalOrders=false] - Flag to enable experimental Conditional Order (Stop Market, Stop Limit and OCO orders). Default to false
20
+ */
21
+ constructor({ snapshot, journal, enableJournaling, experimentalConditionalOrders, }?: OrderBookOptions);
22
+ get marketPrice(): number;
23
+ get lastOp(): number;
24
+ /**
25
+ * Create new order. See {@link CreateOrderOptions} for details.
26
+ *
27
+ * @param options
28
+ * @param options.type - `limit` | `market` | 'stop_limit' | 'stop_market' | 'oco'
29
+ * @param options.side - `sell` or `buy`
30
+ * @param options.size - How much of currency you want to trade in units of base currency
31
+ * @param options.price - The price at which the order is to be fullfilled, in units of the quote currency. Param only for limit order
32
+ * @param options.orderID - Unique order ID. Param only for limit order
33
+ * @param options.postOnly - Can be used with 'limit' order and when it's `true` the order will be rejected if immediately matches and trades as a taker. Default is `false`
34
+ * @param options.stopPrice - The price at which the order will be triggered. Used with `stop_limit` and `stop_market` order.
35
+ * @param options.stopLimitPrice - The price at which the order will be triggered. Used with `stop_limit` and `stop_market` order.
36
+ * @param options.timeInForce - Time-in-force supported are: `GTC` (default), `FOK`, `IOC`. Param only for limit order
37
+ * @param options.stopLimitTimeInForce - Time-in-force supported are: `GTC` (default), `FOK`, `IOC`. Param only for limit order
38
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
39
+ */
40
+ createOrder(options: CreateOrderOptions): IProcessOrder;
41
+ /**
42
+ * @deprecated This implementation has been deprecated and will be removed on v7.0.0.
43
+ * Use createOrder({ type, side, size, price, id, timeInForce }) instead.
44
+ *
45
+ * Create a trade order
46
+ *
47
+ * @param type - `limit` | `market` | 'stop_limit' | 'stop_market' | 'oco'
48
+ * @param side - `sell` or `buy`
49
+ * @param size - How much of currency you want to trade in units of base currency
50
+ * @param price - The price at which the order is to be fullfilled, in units of the quote currency. Param only for limit order
51
+ * @param orderID - Unique order ID. Param only for limit order
52
+ * @param timeInForce - Time-in-force supported are: `GTC` (default), `FOK`, `IOC`. Param only for limit order
53
+ * @param stopPrice - The price at which the order will be triggered. Used with `stop_limit` and `stop_market` order.
54
+ * @param stopLimitPrice - The price at which the order will be triggered. Used with `stop_limit` and `stop_market` order.
55
+ * @param stopLimitTimeInForce - Time-in-force supported are: `GTC` (default), `FOK`, `IOC`. Param only for limit order
56
+ * @param postOnly - Can be used with 'limit' order and when it's `true` the order will be rejected if immediately matches and trades as a taker. Default is `false`
57
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
58
+ */
59
+ createOrder(type: OrderType, side: Side, size: number, price?: number, orderID?: string, timeInForce?: TimeInForce, stopPrice?: number, stopLimitPrice?: number, stopLimitTimeInForce?: TimeInForce, postOnly?: boolean): IProcessOrder;
60
+ /**
61
+ * Create a market order. See {@link MarketOrderOptions} for details.
62
+ *
63
+ * @param options
64
+ * @param options.side - `sell` or `buy`
65
+ * @param options.size - How much of currency you want to trade in units of base currency
66
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
67
+ */
68
+ market(options: MarketOrderOptions): IProcessOrder;
69
+ /**
70
+ * @deprecated This implementation has been deprecated and will be removed on v7.0.0.
71
+ * Use market({ side, size }) instead.
72
+ *
73
+ * Create a market order
74
+ *
75
+ * @param side - `sell` or `buy`
76
+ * @param size - How much of currency you want to trade in units of base currency
77
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
78
+ */
79
+ market(side: Side, size: number): IProcessOrder;
80
+ /**
81
+ * Create a stop market order. See {@link StopMarketOrderOptions} for details.
82
+ *
83
+ * @param options
84
+ * @param options.side - `sell` or `buy`
85
+ * @param options.size - How much of currency you want to trade in units of base currency
86
+ * @param options.stopPrice - The price at which the order will be triggered.
87
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
88
+ */
89
+ stopMarket: (options: StopMarketOrderOptions) => IProcessOrder;
90
+ /**
91
+ * Create a limit order. See {@link LimitOrderOptions} for details.
92
+ *
93
+ * @param options
94
+ * @param options.side - `sell` or `buy`
95
+ * @param options.id - Unique order ID
96
+ * @param options.size - How much of currency you want to trade in units of base currency
97
+ * @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
98
+ * @param options.postOnly - When `true` the order will be rejected if immediately matches and trades as a taker. Default is `false`
99
+ * @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
100
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
101
+ */
102
+ limit(options: LimitOrderOptions): IProcessOrder;
103
+ /**
104
+ * @deprecated This implementation has been deprecated and will be removed on v7.0.0.
105
+ * Use limit({ id, side, size, price, timeInForce }) instead.
106
+ *
107
+ * Create a limit order
108
+ *
109
+ * @param side - `sell` or `buy`
110
+ * @param orderID - Unique order ID
111
+ * @param size - How much of currency you want to trade in units of base currency
112
+ * @param price - The price at which the order is to be fullfilled, in units of the quote currency
113
+ * @param timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
114
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
115
+ */
116
+ limit(side: Side, orderID: string, size: number, price: number, timeInForce?: TimeInForce): IProcessOrder;
117
+ /**
118
+ * Create a stop limit order. See {@link StopLimitOrderOptions} for details.
119
+ *
120
+ * @param options
121
+ * @param options.side - `sell` or `buy`
122
+ * @param options.id - Unique order ID
123
+ * @param options.size - How much of currency you want to trade in units of base currency
124
+ * @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
125
+ * @param options.stopPrice - The price at which the order will be triggered.
126
+ * @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
127
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
128
+ */
129
+ stopLimit: (options: StopLimitOrderOptions) => IProcessOrder;
130
+ /**
131
+ * Create an OCO (One-Cancels-the-Other) order.
132
+ * OCO order combines a `stop_limit` order and a `limit` order, where if stop price
133
+ * is triggered or limit order is fully or partially fulfilled, the other is canceled.
134
+ * Both orders have the same `side` and `size`. If you cancel one of the orders, the
135
+ * entire OCO order pair will be canceled.
136
+ *
137
+ * For BUY orders the `stopPrice` must be above the current price and the `price` below the current price
138
+ * For SELL orders the `stopPrice` must be below the current price and the `price` above the current price
139
+ *
140
+ * See {@link OCOOrderOptions} for details.
141
+ *
142
+ * @param options
143
+ * @param options.side - `sell` or `buy`
144
+ * @param options.id - Unique order ID
145
+ * @param options.size - How much of currency you want to trade in units of base currency
146
+ * @param options.price - The price of the `limit` order at which the order is to be fullfilled, in units of the quote currency
147
+ * @param options.stopPrice - The price at which the `stop_limit` order will be triggered.
148
+ * @param options.stopLimitPrice - The price of the `stop_limit` order at which the order is to be fullfilled, in units of the quote currency.
149
+ * @param options.timeInForce - Time-in-force of the `limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
150
+ * @param options.stopLimitTimeInForce - Time-in-force of the `stop_limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
151
+ * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
152
+ */
153
+ oco: (options: OCOOrderOptions) => IProcessOrder;
154
+ /**
155
+ * Modify an existing order with given ID. When an order is modified by price or quantity,
156
+ * it will be deemed as a new entry. Under the price-time-priority algorithm, orders are
157
+ * prioritized according to their order price and order time. Hence, the latest orders
158
+ * will be placed at the back of the matching order queue.
159
+ *
160
+ * @param orderID - The ID of the order to be modified
161
+ * @param orderUpdate - An object with the modified size and/or price of an order. The shape of the object is `{size, price}`.
162
+ * @returns An object with the result of the processed order or an error
163
+ */
164
+ modify: (orderID: string, orderUpdate: OrderUpdatePrice | OrderUpdateSize) => IProcessOrder;
165
+ /**
166
+ * Remove an existing order with given ID from the order book
167
+ *
168
+ * @param orderID - The ID of the order to be removed
169
+ * @returns The removed order if exists or `undefined`
170
+ */
171
+ cancel: (orderID: string) => ICancelOrder | undefined;
172
+ /**
173
+ * Get an existing order with the given ID
174
+ *
175
+ * @param orderID - The ID of the order to be returned
176
+ * @returns The order if exists or `undefined`
177
+ */
178
+ order: (orderID: string) => LimitOrder | undefined;
179
+ depth: () => [Array<[number, number]>, Array<[number, number]>];
180
+ toString: () => string;
181
+ calculateMarketPrice: (side: Side, size: number) => {
182
+ price: number;
183
+ err: null | OrderBookError;
184
+ };
185
+ snapshot: () => Snapshot;
186
+ private readonly _market;
187
+ private readonly _limit;
188
+ private readonly _stopMarket;
189
+ private readonly _stopLimit;
190
+ private readonly _oco;
191
+ private readonly _stopOrder;
192
+ private readonly restoreSnapshot;
193
+ /**
194
+ * Remove an existing order with given ID from the order book
195
+ * @param orderID The id of the order to be deleted
196
+ * @param internalDeletion Set to true when the delete comes from internal operations
197
+ * @returns The removed order if exists or `undefined`
198
+ */
199
+ private readonly _cancelOrder;
200
+ private readonly getProcessOrderResponse;
201
+ private readonly createLimitOrder;
202
+ private readonly executeConditionalOrder;
203
+ private readonly replayJournal;
204
+ /**
205
+ * OCO Order:
206
+ * Buy: price < marketPrice < stopPrice
207
+ * Sell: price > marketPrice > stopPrice
208
+ */
209
+ private readonly validateOCOOrder;
210
+ private readonly greaterThanOrEqual;
211
+ private readonly lowerThanOrEqual;
212
+ private readonly processQueue;
213
+ private readonly canFillOrder;
214
+ private readonly buyOrderCanBeFilled;
215
+ private readonly sellOrderCanBeFilled;
216
+ private readonly validateMarketOrder;
217
+ private readonly validateLimitOrder;
218
+ }
@@ -0,0 +1,18 @@
1
+ import type { LimitOrder } from "./order";
2
+ export declare class OrderQueue {
3
+ private readonly _price;
4
+ private _volume;
5
+ private readonly _orders;
6
+ private _ordersMap;
7
+ constructor(price: number);
8
+ len: () => number;
9
+ toArray: () => LimitOrder[];
10
+ price: () => number;
11
+ volume: () => number;
12
+ head: () => LimitOrder | undefined;
13
+ tail: () => LimitOrder | undefined;
14
+ append: (order: LimitOrder) => LimitOrder;
15
+ update: (oldOrder: LimitOrder, newOrder: LimitOrder) => void;
16
+ remove: (order: LimitOrder) => void;
17
+ updateOrderSize: (order: LimitOrder, size: number) => void;
18
+ }
@@ -0,0 +1,29 @@
1
+ import createRBTree from "functional-red-black-tree";
2
+ import { type LimitOrder } from "./order";
3
+ import { OrderQueue } from "./orderqueue";
4
+ import { type OrderUpdatePrice, type OrderUpdateSize, Side } from "./types";
5
+ export declare class OrderSide {
6
+ private _priceTree;
7
+ private _prices;
8
+ private _volume;
9
+ private _total;
10
+ private _numOrders;
11
+ private _depthSide;
12
+ private readonly _side;
13
+ constructor(side: Side);
14
+ len: () => number;
15
+ depth: () => number;
16
+ volume: () => number;
17
+ total: () => number;
18
+ priceTree: () => createRBTree.Tree<number, OrderQueue>;
19
+ append: (order: LimitOrder) => LimitOrder;
20
+ remove: (order: LimitOrder) => LimitOrder;
21
+ updateOrderPrice: (oldOrder: LimitOrder, orderUpdate: OrderUpdatePrice) => LimitOrder;
22
+ updateOrderSize: (oldOrder: LimitOrder, orderUpdate: OrderUpdateSize) => LimitOrder;
23
+ maxPriceQueue: () => OrderQueue | undefined;
24
+ minPriceQueue: () => OrderQueue | undefined;
25
+ lowerThan: (price: number) => OrderQueue | undefined;
26
+ greaterThan: (price: number) => OrderQueue | undefined;
27
+ orders: () => LimitOrder[];
28
+ toString: () => string;
29
+ }
@@ -0,0 +1,20 @@
1
+ import type { StopQueue } from "./stopqueue";
2
+ import { Side, type StopOrder } from "./types";
3
+ export declare class StopBook {
4
+ private readonly bids;
5
+ private readonly asks;
6
+ constructor();
7
+ add: (order: StopOrder) => void;
8
+ remove: (side: Side, id: string, stopPrice: number) => StopOrder | undefined;
9
+ removePriceLevel: (side: Side, priceLevel: number) => void;
10
+ getConditionalOrders: (side: Side, priceBefore: number, marketPrice: number) => StopQueue[];
11
+ /**
12
+ * Stop-Limit Order:
13
+ * Buy: marketPrice < stopPrice <= price
14
+ * Sell: marketPrice > stopPrice >= price
15
+ * Stop-Market Order:
16
+ * Buy: marketPrice < stopPrice
17
+ * Sell: marketPrice > stopPrice
18
+ */
19
+ validConditionalOrder: (marketPrice: number, order: StopOrder) => boolean;
20
+ }
@@ -0,0 +1,12 @@
1
+ import type { StopOrder } from "./types";
2
+ export declare class StopQueue {
3
+ private readonly _price;
4
+ private readonly _orders;
5
+ private _ordersMap;
6
+ constructor(price: number);
7
+ get price(): number;
8
+ len: () => number;
9
+ removeFromHead: () => StopOrder | undefined;
10
+ append: (order: StopOrder) => StopOrder;
11
+ remove: (id: string) => StopOrder | undefined;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { StopQueue } from "./stopqueue";
2
+ import { Side, type StopOrder } from "./types";
3
+ export declare class StopSide {
4
+ private _priceTree;
5
+ private _prices;
6
+ private readonly _side;
7
+ constructor(side: Side);
8
+ append: (order: StopOrder) => StopOrder;
9
+ remove: (id: string, stopPrice: number) => StopOrder | undefined;
10
+ removePriceLevel: (priceLevel: number) => void;
11
+ between: (priceBefore: number, marketPrice: number) => StopQueue[];
12
+ }
@@ -0,0 +1,324 @@
1
+ import type { OrderBookError } from "./errors";
2
+ import type { LimitOrder, StopLimitOrder, StopMarketOrder } from "./order";
3
+ export declare enum Side {
4
+ BUY = "buy",
5
+ SELL = "sell"
6
+ }
7
+ export declare enum OrderType {
8
+ LIMIT = "limit",
9
+ MARKET = "market",
10
+ OCO = "oco",
11
+ STOP_LIMIT = "stop_limit",
12
+ STOP_MARKET = "stop_market"
13
+ }
14
+ export declare enum TimeInForce {
15
+ GTC = "GTC",
16
+ IOC = "IOC",
17
+ FOK = "FOK"
18
+ }
19
+ export interface IError {
20
+ code: number;
21
+ message: string;
22
+ }
23
+ export type StopOrder = StopLimitOrder | StopMarketOrder;
24
+ export type Order = LimitOrder | StopOrder;
25
+ interface BaseOrderOptions {
26
+ id?: string;
27
+ side: Side;
28
+ size: number;
29
+ }
30
+ interface InternalBaseOrderOptions extends BaseOrderOptions {
31
+ type: OrderType;
32
+ time?: number;
33
+ }
34
+ /**
35
+ * Specific options for a market order.
36
+ */
37
+ export interface MarketOrderOptions extends BaseOrderOptions {
38
+ }
39
+ interface IMarketOrderOptions extends InternalBaseOrderOptions {
40
+ }
41
+ export interface InternalMarketOrderOptions extends IMarketOrderOptions {
42
+ type: OrderType.MARKET;
43
+ }
44
+ /**
45
+ * Specific options for a limit order.
46
+ */
47
+ export interface LimitOrderOptions extends MarketOrderOptions {
48
+ id: string;
49
+ price: number;
50
+ timeInForce?: TimeInForce;
51
+ postOnly?: boolean;
52
+ }
53
+ interface ILimitOrderOptions extends InternalBaseOrderOptions {
54
+ id: string;
55
+ price: number;
56
+ timeInForce: TimeInForce;
57
+ }
58
+ export interface InternalLimitOrderOptions extends ILimitOrderOptions {
59
+ type: OrderType.LIMIT;
60
+ origSize: number;
61
+ makerQty: number;
62
+ takerQty: number;
63
+ postOnly?: boolean;
64
+ ocoStopPrice?: number;
65
+ }
66
+ /**
67
+ * Specific options for a stop market order.
68
+ */
69
+ export interface StopMarketOrderOptions extends MarketOrderOptions {
70
+ stopPrice: number;
71
+ }
72
+ export interface InternalStopMarketOrderOptions extends IMarketOrderOptions {
73
+ type: OrderType.STOP_MARKET;
74
+ stopPrice: number;
75
+ }
76
+ /**
77
+ * Specific options for a stop limit order.
78
+ */
79
+ export interface StopLimitOrderOptions extends LimitOrderOptions {
80
+ stopPrice: number;
81
+ }
82
+ export interface InternalStopLimitOrderOptions extends ILimitOrderOptions {
83
+ type: OrderType.STOP_LIMIT;
84
+ stopPrice: number;
85
+ isOCO?: boolean;
86
+ }
87
+ /**
88
+ * Specific options for oco order.
89
+ */
90
+ export interface OCOOrderOptions extends StopLimitOrderOptions {
91
+ stopPrice: number;
92
+ stopLimitPrice: number;
93
+ stopLimitTimeInForce?: TimeInForce;
94
+ }
95
+ /**
96
+ * Object object representation of a market order returned by the toObject() method.
97
+ */
98
+ export interface IMarketOrder {
99
+ id: string;
100
+ type: OrderType;
101
+ side: Side;
102
+ size: number;
103
+ origSize: number;
104
+ time: number;
105
+ }
106
+ /**
107
+ * Object object representation of a limit order returned by the toObject() method.
108
+ */
109
+ export interface ILimitOrder {
110
+ id: string;
111
+ type: OrderType.LIMIT;
112
+ side: Side;
113
+ size: number;
114
+ origSize: number;
115
+ price: number;
116
+ time: number;
117
+ timeInForce: TimeInForce;
118
+ takerQty: number;
119
+ makerQty: number;
120
+ }
121
+ /**
122
+ * Object object representation of a stop market order returned by the toObject() method.
123
+ */
124
+ export interface IStopMarketOrder {
125
+ id: string;
126
+ type: OrderType;
127
+ side: Side;
128
+ size: number;
129
+ stopPrice: number;
130
+ time: number;
131
+ }
132
+ /**
133
+ * Object object representation of a stop limit order returned by the toObject() method.
134
+ */
135
+ export interface IStopLimitOrder {
136
+ id: string;
137
+ type: OrderType;
138
+ side: Side;
139
+ size: number;
140
+ price: number;
141
+ stopPrice: number;
142
+ timeInForce: TimeInForce;
143
+ time: number;
144
+ }
145
+ /**
146
+ * Represents an order in the order book.
147
+ */
148
+ export type OrderOptions = InternalMarketOrderOptions | InternalLimitOrderOptions | InternalStopLimitOrderOptions | InternalStopMarketOrderOptions;
149
+ export type StopOrderOptions = StopMarketOrderOptions | StopLimitOrderOptions | OCOOrderOptions;
150
+ /**
151
+ * Represents the result of processing an order.
152
+ */
153
+ export interface IProcessOrder {
154
+ /** Array of fully processed orders. */
155
+ done: Order[];
156
+ /** Array of activated (stop limit or stop market) orders */
157
+ activated: StopOrder[];
158
+ /** The partially processed order, if any. */
159
+ partial: LimitOrder | null;
160
+ /** The quantity that has been processed in the partial order. */
161
+ partialQuantityProcessed: number;
162
+ /** The remaining quantity that needs to be processed. */
163
+ quantityLeft: number;
164
+ /** The error encountered during order processing, if any. */
165
+ err: OrderBookError | null;
166
+ /** Optional journal log entry related to the order processing. */
167
+ log?: JournalLog;
168
+ }
169
+ export interface ConditionOrderOptions {
170
+ stopPrice: number;
171
+ }
172
+ /**
173
+ * Specific options for modifying an order.
174
+ */
175
+ export interface ModifyOrderOptions {
176
+ /** Unique identifier of the order. */
177
+ orderID: string;
178
+ /** Details of the order update (price or size). */
179
+ orderUpdate: OrderUpdatePrice | OrderUpdateSize;
180
+ }
181
+ /**
182
+ * Specific options for canceling an order.
183
+ */
184
+ export interface CancelOrderOptions {
185
+ /** Unique identifier of the order. */
186
+ orderID: string;
187
+ }
188
+ /**
189
+ * Represents a cancel order operation.
190
+ */
191
+ export interface ICancelOrder {
192
+ order: LimitOrder;
193
+ stopOrder?: StopOrder;
194
+ /** Optional log related to the order cancellation. */
195
+ log?: CancelOrderJournalLog;
196
+ }
197
+ /**
198
+ * Represents a log entry for a market order operation.
199
+ */
200
+ export interface MarketOrderJournalLog {
201
+ /** Incremental ID of the operation */
202
+ opId: number;
203
+ /** Timestamp of the operation. */
204
+ ts: number;
205
+ /** Operation type: 'm' for market order. */
206
+ op: "m";
207
+ /** Specific options for the market order. */
208
+ o: MarketOrderOptions;
209
+ }
210
+ /**
211
+ * Represents a log entry for a limit order operation.
212
+ */
213
+ export interface LimitOrderJournalLog {
214
+ /** Incremental ID of the operation */
215
+ opId: number;
216
+ /** Timestamp of the operation. */
217
+ ts: number;
218
+ /** Operation type: 'l' for limit order. */
219
+ op: "l";
220
+ /** Specific options for the limit order. */
221
+ o: LimitOrderOptions;
222
+ }
223
+ /**
224
+ * Represents a log entry for an order modification operation.
225
+ */
226
+ export interface ModifyOrderJournalLog {
227
+ /** Incremental ID of the operation */
228
+ opId: number;
229
+ /** Timestamp of the operation. */
230
+ ts: number;
231
+ /** Operation type: 'u' for update order. */
232
+ op: "u";
233
+ /** Specific options for modifying the order. */
234
+ o: ModifyOrderOptions;
235
+ }
236
+ /**
237
+ * Represents a log entry for an order cancellation operation.
238
+ */
239
+ export interface CancelOrderJournalLog {
240
+ /** Incremental ID of the operation */
241
+ opId: number;
242
+ /** Timestamp of the operation. */
243
+ ts: number;
244
+ /** Operation type: 'd' for delete order. */
245
+ op: "d";
246
+ /** Specific options for canceling the order. */
247
+ o: CancelOrderOptions;
248
+ }
249
+ /**
250
+ * Discriminated union of all journaling log types.
251
+ */
252
+ export type JournalLog = MarketOrderJournalLog | LimitOrderJournalLog | ModifyOrderJournalLog | CancelOrderJournalLog;
253
+ export type CreateOrderOptions = ({
254
+ type: OrderType.MARKET;
255
+ } & MarketOrderOptions) | ({
256
+ type: OrderType.LIMIT;
257
+ } & LimitOrderOptions) | ({
258
+ type: OrderType.STOP_MARKET;
259
+ } & StopMarketOrderOptions) | ({
260
+ type: OrderType.STOP_LIMIT;
261
+ } & StopLimitOrderOptions) | ({
262
+ type: OrderType.OCO;
263
+ } & OCOOrderOptions);
264
+ /**
265
+ * Options for configuring the order book.
266
+ */
267
+ export interface OrderBookOptions {
268
+ /**
269
+ * Orderbook snapshot to restore from. The restoration
270
+ * will be executed before processing any journal logs, if any.
271
+ */
272
+ snapshot?: Snapshot;
273
+ /** Flag to enable journaling. */
274
+ enableJournaling?: boolean;
275
+ /** Array of journal logs. */
276
+ journal?: JournalLog[];
277
+ /**
278
+ * Flag to enable experimental Conditional Order (Stop Market, Stop Limit and OCO orders).
279
+ * Default to false
280
+ */
281
+ experimentalConditionalOrders?: boolean;
282
+ }
283
+ /**
284
+ * Represents an update to the price of an order.
285
+ */
286
+ export interface OrderUpdatePrice {
287
+ /** New price of the order. */
288
+ price: number;
289
+ /** New size of the order (optional). */
290
+ size?: number;
291
+ }
292
+ /**
293
+ * Represents an update to the size of an order.
294
+ */
295
+ export interface OrderUpdateSize {
296
+ /** New price of the order (optional). */
297
+ price?: number;
298
+ /** New size of the order. */
299
+ size: number;
300
+ }
301
+ /**
302
+ * Interface to represent a snapshot of the order book
303
+ */
304
+ export interface Snapshot {
305
+ /** List of ask orders, each with a price and a list of associated orders */
306
+ asks: Array<{
307
+ /** Price of the ask order */
308
+ price: number;
309
+ /** List of orders associated with this price */
310
+ orders: LimitOrder[];
311
+ }>;
312
+ /** List of bid orders, each with a price and a list of associated orders */
313
+ bids: Array<{
314
+ /** Price of the bid order */
315
+ price: number;
316
+ /** List of orders associated with this price */
317
+ orders: LimitOrder[];
318
+ }>;
319
+ /** Unix timestamp representing when the snapshot was taken */
320
+ ts: number;
321
+ /** The id of the last operation inserted in the orderbook */
322
+ lastOp: number;
323
+ }
324
+ export {};