nodejs-order-book 10.0.0 → 10.0.1

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.
package/README.md CHANGED
@@ -63,19 +63,19 @@ Ultra-fast Node.js Order Book written in TypeScript </br> for high-frequency tra
63
63
 
64
64
  Install with npm:
65
65
 
66
- ```sh
66
+ ```
67
67
  npm install nodejs-order-book
68
68
  ```
69
69
 
70
70
  Install with yarn:
71
71
 
72
- ```sh
72
+ ```
73
73
  yarn add nodejs-order-book
74
74
  ```
75
75
 
76
76
  Install with pnpm:
77
77
 
78
- ```sh
78
+ ```
79
79
  pnpm add nodejs-order-book
80
80
  ```
81
81
 
@@ -83,7 +83,7 @@ pnpm add nodejs-order-book
83
83
 
84
84
  To start using order book you need to import `OrderBook` and create new instance:
85
85
 
86
- ```js
86
+ ```ts
87
87
  import { OrderBook } from 'nodejs-order-book'
88
88
 
89
89
  const ob = new OrderBook()
@@ -91,20 +91,39 @@ const ob = new OrderBook()
91
91
 
92
92
  Then you'll be able to use next primary functions:
93
93
 
94
- ```js
95
- ob.createOrder({ type: 'limit' | 'market', side: 'buy' | 'sell', size: number, price?: number, id?: string, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
94
+ ```ts
95
+ ob.createOrder({
96
+ type: 'limit' | 'market',
97
+ side: 'buy' | 'sell',
98
+ size: number,
99
+ price?: number,
100
+ id?: string,
101
+ postOnly?: boolean,
102
+ timeInForce?: 'GTC' | 'FOK' | 'IOC'
103
+ })
96
104
 
97
- ob.limit({ id: string, side: 'buy' | 'sell', size: number, price: number, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
105
+ ob.limit({
106
+ id: string,
107
+ side: 'buy' | 'sell',
108
+ size: number,
109
+ price: number,
110
+ postOnly?: boolean,
111
+ timeInForce?: 'GTC' | 'FOK' | 'IOC'
112
+ })
98
113
 
99
114
  ob.market({ side: 'buy' | 'sell', size: number })
100
115
 
101
- ob.modify(orderID: string, { side: 'buy' | 'sell', size: number, price: number })
116
+ ob.modify(orderID: string, {
117
+ side: 'buy' | 'sell',
118
+ size: number,
119
+ price: number
120
+ })
102
121
 
103
122
  ob.cancel(orderID: string)
104
123
  ```
105
124
  ### Conditional Orders
106
125
  Currently `Stop Market`, `Stop Limit` and `OCO` orders are supported.
107
- ```js
126
+ ```ts
108
127
  import { OrderBook } from 'nodejs-order-book'
109
128
 
110
129
  const ob = new OrderBook()
@@ -153,26 +172,59 @@ To add an order to the order book you can call the general `createOrder()` funct
153
172
 
154
173
  ### Create Order
155
174
 
156
- ```js
175
+ ```ts
157
176
  // Create limit order
158
- ob.createOrder({ type: 'limit', side: 'buy' | 'sell', size: number, price: number, id: string, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
177
+ ob.createOrder({
178
+ type: 'limit',
179
+ side: 'buy' | 'sell',
180
+ size: number,
181
+ price: number,
182
+ id: string,
183
+ postOnly?: boolean,
184
+ timeInForce?: 'GTC' | 'FOK' | 'IOC'
185
+ })
159
186
 
160
187
  // Create market order
161
- ob.createOrder({ type: 'market', side: 'buy' | 'sell', size: number })
188
+ ob.createOrder({
189
+ type: 'market',
190
+ side: 'buy' | 'sell',
191
+ size: number
192
+ })
162
193
 
163
194
  // Create stop limit order
164
- ob.createOrder({ type: 'stop_limit', side: 'buy' | 'sell', size: number, price: number, id: string, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
195
+ ob.createOrder({
196
+ type: 'stop_limit',
197
+ side: 'buy' | 'sell',
198
+ size: number,
199
+ price: number,
200
+ id: string,
201
+ stopPrice: number,
202
+ timeInForce?: 'GTC' | 'FOK' | 'IOC'
203
+ })
165
204
 
166
205
  // Create stop market order
167
- ob.createOrder({ type: 'stop_market', side: 'buy' | 'sell', size: number, stopPrice: number })
206
+ ob.createOrder({
207
+ type: 'stop_market',
208
+ side: 'buy' | 'sell',
209
+ size: number,
210
+ stopPrice: number
211
+ })
168
212
 
169
213
  // Create OCO order
170
- ob.createOrder({ type: 'oco', side: 'buy' | 'sell', size: number, stopPrice: number, stopLimitPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC', stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC' })
214
+ ob.createOrder({
215
+ type: 'oco',
216
+ side: 'buy' | 'sell',
217
+ size: number,
218
+ stopPrice: number,
219
+ stopLimitPrice: number,
220
+ timeInForce?: 'GTC' | 'FOK' | 'IOC',
221
+ stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC'
222
+ })
171
223
  ```
172
224
 
173
225
  ### Create Limit Order
174
226
 
175
- ```js
227
+ ```ts
176
228
  /**
177
229
  * Create a limit order. See {@link LimitOrderOptions} for details.
178
230
  *
@@ -185,12 +237,19 @@ ob.createOrder({ type: 'oco', side: 'buy' | 'sell', size: number, stopPrice: num
185
237
  * @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
186
238
  * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
187
239
  */
188
- ob.limit({ side: 'buy' | 'sell', id: string, size: number, price: number, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
240
+ ob.limit({
241
+ side: 'buy' | 'sell',
242
+ id: string,
243
+ size: number,
244
+ price: number,
245
+ postOnly?: boolean,
246
+ timeInForce?: 'GTC' | 'FOK' | 'IOC'
247
+ })
189
248
  ```
190
249
 
191
250
  For example:
192
251
 
193
- ```
252
+ ```ts
194
253
  ob.limit({ side: "sell", id: "uniqueID", size: 55, price: 100 })
195
254
 
196
255
  asks: 110 -> 5 110 -> 5
@@ -203,7 +262,7 @@ done - null
203
262
  partial - null
204
263
  ```
205
264
 
206
- ```
265
+ ```ts
207
266
  ob.limit({ side: "buy", id: "uniqueID", size: 7, price: 120 })
208
267
 
209
268
  asks: 110 -> 5
@@ -217,7 +276,7 @@ done - 2 (or more orders)
217
276
  partial - uniqueID order
218
277
  ```
219
278
 
220
- ```
279
+ ```ts
221
280
  ob.limit({ side: "buy", id: "uniqueID", size: 3, price: 120 })
222
281
 
223
282
  asks: 110 -> 5
@@ -232,7 +291,7 @@ partial - 1 order with price 110
232
291
 
233
292
  ### Create Market Order
234
293
 
235
- ```js
294
+ ```ts
236
295
  /**
237
296
  * Create a market order. See {@link MarketOrderOptions} for details.
238
297
  *
@@ -246,7 +305,7 @@ ob.market({ side: 'buy' | 'sell', size: number })
246
305
 
247
306
  For example:
248
307
 
249
- ```
308
+ ```ts
250
309
  ob.market({ side: 'sell', size: 6 })
251
310
 
252
311
  asks: 110 -> 5 110 -> 5
@@ -260,7 +319,7 @@ partial - 1 order with price 80
260
319
  quantityLeft - 0
261
320
  ```
262
321
 
263
- ```
322
+ ```ts
264
323
  ob.market({ side: 'buy', size: 10 })
265
324
 
266
325
  asks: 110 -> 5
@@ -276,7 +335,7 @@ quantityLeft - 4
276
335
 
277
336
  ### Create Stop Limit Order
278
337
 
279
- ```js
338
+ ```ts
280
339
  /**
281
340
  * Create a stop limit order. See {@link StopLimitOrderOptions} for details.
282
341
  *
@@ -289,12 +348,19 @@ quantityLeft - 4
289
348
  * @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
290
349
  * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
291
350
  */
292
- ob.stopLimit({ side: 'buy' | 'sell', id: string, size: number, price: number, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
351
+ ob.stopLimit({
352
+ side: 'buy' | 'sell',
353
+ id: string,
354
+ size: number,
355
+ price: number,
356
+ stopPrice: number,
357
+ timeInForce?: 'GTC' | 'FOK' | 'IOC'
358
+ })
293
359
  ```
294
360
 
295
361
  ### Create Stop Market Order
296
362
 
297
- ```js
363
+ ```ts
298
364
  /**
299
365
  * Create a stop market order. See {@link StopMarketOrderOptions} for details.
300
366
  *
@@ -304,12 +370,16 @@ ob.stopLimit({ side: 'buy' | 'sell', id: string, size: number, price: number, st
304
370
  * @param options.stopPrice - The price at which the order will be triggered.
305
371
  * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
306
372
  */
307
- ob.stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })
373
+ ob.stopMarket({
374
+ side: 'buy' | 'sell',
375
+ size: number,
376
+ stopPrice: number
377
+ })
308
378
  ```
309
379
 
310
380
  ### Create OCO (One-Cancels-the-Other) Order
311
381
 
312
- ```js
382
+ ```ts
313
383
  /**
314
384
  * Create an OCO (One-Cancels-the-Other) order.
315
385
  * OCO order combines a `stop_limit` order and a `limit` order, where if stop price
@@ -333,12 +403,21 @@ ob.stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })
333
403
  * @param options.stopLimitTimeInForce - Time-in-force of the `stop_limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
334
404
  * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
335
405
  */
336
- ob.oco({ side: 'buy' | 'sell', id: string, size: number, price: number, stopPrice: number, stopLimitPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC', stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC' })
406
+ ob.oco({
407
+ side: 'buy' | 'sell',
408
+ id: string,
409
+ size: number,
410
+ price: number,
411
+ stopPrice: number,
412
+ stopLimitPrice: number,
413
+ timeInForce?: 'GTC' | 'FOK' | 'IOC',
414
+ stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC'
415
+ })
337
416
  ```
338
417
 
339
418
  ### Modify an existing order
340
419
 
341
- ```js
420
+ ```ts
342
421
  /**
343
422
  * Modify an existing order with given ID. When an order is modified by price or quantity,
344
423
  * it will be deemed as a new entry. Under the price-time-priority algorithm, orders are
@@ -354,7 +433,7 @@ ob.modify(orderID: string, { size: number, price: number })
354
433
 
355
434
  For example:
356
435
 
357
- ```
436
+ ```ts
358
437
  ob.limit({ side: "sell", id: "uniqueID", size: 55, price: 100 })
359
438
 
360
439
  asks: 110 -> 5 110 -> 5
@@ -385,7 +464,7 @@ bids: 90 -> 5 90 -> 5
385
464
 
386
465
  ### Cancel Order
387
466
 
388
- ```js
467
+ ```ts
389
468
  /**
390
469
  * Remove an existing order with given ID from the order book
391
470
  *
@@ -397,7 +476,7 @@ ob.cancel(orderID: string)
397
476
 
398
477
  For example:
399
478
 
400
- ```
479
+ ```ts
401
480
  ob.cancel("myUniqueID-Sell-1-with-100")
402
481
 
403
482
  asks: 110 -> 5
@@ -425,7 +504,7 @@ After taking the snapshot, you can safely remove all logs preceding the `lastOp`
425
504
 
426
505
  **Note**: The snapshot of the order book returns an object containing an `array` of `bids` and `asks`, which in turn are arrays of order objects. If the snapshot is saved to the database as a `string`, make sure to pass the snapshot in its original format when initializing the order book. For example, you can achieve this by using `JSON.parse` to convert the string back into its original object form.
427
506
 
428
- ```js
507
+ ```ts
429
508
  const ob = new OrderBook({ enableJournaling: true})
430
509
 
431
510
  // after every order save the log to the database
@@ -449,7 +528,7 @@ const ob = new OrderBook({ snapshot: JSON.parse(snapshot), journal: log, enableJ
449
528
 
450
529
  ### Journal Logs
451
530
  The `journal` option expects an array of journal logs that you can get by setting `enableJournaling` to true. When the journal is provided, the order book will replay all the operations, bringing the order book to the same state as the last log.
452
- ```js
531
+ ```ts
453
532
  // Assuming 'logs' is an array of log entries retrieved from the database
454
533
 
455
534
  const logs = await getLogs()
@@ -459,7 +538,7 @@ By combining snapshots with journaling, you can effectively restore and audit th
459
538
 
460
539
  ### Enable Journaling
461
540
  `enabledJournaling` is a configuration setting that determines whether journaling is enabled or disabled. When enabled, the property `log` will be added to the body of the response for each operation. The logs must be saved to the database and can then be used when a new instance of the order book is instantiated.
462
- ```js
541
+ ```ts
463
542
  const ob = new OrderBook({ enableJournaling: true }) // false by default
464
543
 
465
544
  // after every order save the log to the database
@@ -473,7 +552,7 @@ await saveLog(order.log)
473
552
 
474
553
  Build production (distribution) files in your dist folder:
475
554
 
476
- ```sh
555
+ ```
477
556
  npm run build
478
557
  ```
479
558
 
@@ -481,7 +560,7 @@ npm run build
481
560
 
482
561
  To run all the unit-test
483
562
 
484
- ```sh
563
+ ```
485
564
  npm run test
486
565
  ```
487
566
 
@@ -489,7 +568,7 @@ npm run test
489
568
 
490
569
  Run testing coverage
491
570
 
492
- ```sh
571
+ ```
493
572
  npm run test:cov
494
573
  ```
495
574
 
@@ -497,7 +576,7 @@ npm run test:cov
497
576
 
498
577
  Before running benchmark, make sure to have built the source code with `npm run build` first
499
578
 
500
- ```sh
579
+ ```
501
580
  npm run bench
502
581
  ```
503
582
 
@@ -704,6 +704,7 @@ var OrderBook = /** @class */ (function () {
704
704
  return false;
705
705
  }
706
706
  var cumulativeSize = 0;
707
+ // biome-ignore lint/suspicious/useIterableCallbackReturn: the forEach of the priceTree must return true to break the loop
707
708
  orderSide.priceTree().forEach(function (_, level) {
708
709
  if (price >= level.price() && cumulativeSize < size) {
709
710
  cumulativeSize += level.volume();
@@ -719,6 +720,7 @@ var OrderBook = /** @class */ (function () {
719
720
  return false;
720
721
  }
721
722
  var cumulativeSize = 0;
723
+ // biome-ignore lint/suspicious/useIterableCallbackReturn: the forEach of the priceTree must return true to break the loop
722
724
  orderSide.priceTree().forEach(function (_, level) {
723
725
  if (price <= level.price() && cumulativeSize < size) {
724
726
  cumulativeSize += level.volume();
package/dist/cjs/utils.js CHANGED
@@ -2,12 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.safeParse = exports.safeStringify = void 0;
4
4
  /* node:coverage ignore next - Don't know why this line is uncovered */
5
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
6
5
  var safeStringify = function (value) {
7
6
  try {
8
7
  return JSON.stringify(value);
9
8
  }
10
- catch (error) {
9
+ catch (_error) {
11
10
  return null;
12
11
  }
13
12
  };
@@ -17,7 +16,7 @@ var safeParse = function (value) {
17
16
  try {
18
17
  return JSON.parse(value);
19
18
  }
20
- catch (error) {
19
+ catch (_error) {
21
20
  return null;
22
21
  }
23
22
  };
@@ -701,6 +701,7 @@ var OrderBook = /** @class */ (function () {
701
701
  return false;
702
702
  }
703
703
  var cumulativeSize = 0;
704
+ // biome-ignore lint/suspicious/useIterableCallbackReturn: the forEach of the priceTree must return true to break the loop
704
705
  orderSide.priceTree().forEach(function (_, level) {
705
706
  if (price >= level.price() && cumulativeSize < size) {
706
707
  cumulativeSize += level.volume();
@@ -716,6 +717,7 @@ var OrderBook = /** @class */ (function () {
716
717
  return false;
717
718
  }
718
719
  var cumulativeSize = 0;
720
+ // biome-ignore lint/suspicious/useIterableCallbackReturn: the forEach of the priceTree must return true to break the loop
719
721
  orderSide.priceTree().forEach(function (_, level) {
720
722
  if (price <= level.price() && cumulativeSize < size) {
721
723
  cumulativeSize += level.volume();
package/dist/esm/utils.js CHANGED
@@ -1,10 +1,9 @@
1
1
  /* node:coverage ignore next - Don't know why this line is uncovered */
2
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
3
2
  export var safeStringify = function (value) {
4
3
  try {
5
4
  return JSON.stringify(value);
6
5
  }
7
- catch (error) {
6
+ catch (_error) {
8
7
  return null;
9
8
  }
10
9
  };
@@ -13,7 +12,7 @@ export var safeParse = function (value) {
13
12
  try {
14
13
  return JSON.parse(value);
15
14
  }
16
- catch (error) {
15
+ catch (_error) {
17
16
  return null;
18
17
  }
19
18
  };
@@ -1,4 +1,4 @@
1
1
  import { OrderBook } from "./orderbook";
2
+ import type { CreateOrderOptions, ICancelOrder, IOrder, IProcessOrder, LimitOrderOptions, MarketOrderOptions, OCOOrderOptions, OrderBookOptions, OrderUpdatePrice, OrderUpdateSize, StopLimitOrderOptions, StopMarketOrderOptions } from "./types";
2
3
  import { OrderType, Side } from "./types";
3
- import type { CreateOrderOptions, ICancelOrder, IProcessOrder, LimitOrderOptions, MarketOrderOptions, OCOOrderOptions, OrderBookOptions, OrderUpdatePrice, OrderUpdateSize, StopLimitOrderOptions, StopMarketOrderOptions } from "./types";
4
- export { type CreateOrderOptions, type ICancelOrder, type IProcessOrder, type LimitOrderOptions, type MarketOrderOptions, type OCOOrderOptions, OrderBook, type OrderBookOptions, OrderType, type OrderUpdatePrice, type OrderUpdateSize, Side, type StopLimitOrderOptions, type StopMarketOrderOptions, };
4
+ export { type CreateOrderOptions, type ICancelOrder, type IOrder, type IProcessOrder, type LimitOrderOptions, type MarketOrderOptions, type OCOOrderOptions, OrderBook, type OrderBookOptions, OrderType, type OrderUpdatePrice, type OrderUpdateSize, Side, type StopLimitOrderOptions, type StopMarketOrderOptions, };
@@ -1,4 +1,4 @@
1
- import { type ILimitOrder, type IStopLimitOrder, type IStopMarketOrder, type InternalLimitOrderOptions, type InternalStopLimitOrderOptions, type InternalStopMarketOrderOptions, type OrderOptions, OrderType, type Side, type TimeInForce } from "./types";
1
+ import { type ILimitOrder, type InternalLimitOrderOptions, type InternalStopLimitOrderOptions, type InternalStopMarketOrderOptions, type IStopLimitOrder, type IStopMarketOrder, type OrderOptions, OrderType, type Side, type TimeInForce } from "./types";
2
2
  declare abstract class BaseOrder {
3
3
  readonly _id: string;
4
4
  readonly _side: Side;
@@ -1,2 +1,2 @@
1
- export declare const safeStringify: (value: any) => string | null;
1
+ export declare const safeStringify: (value: unknown) => string | null;
2
2
  export declare const safeParse: <T>(value: string) => T | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs-order-book",
3
- "version": "10.0.0",
3
+ "version": "10.0.1",
4
4
  "description": "Node.js Lmit Order Book for high-frequency trading (HFT).",
5
5
  "author": "Andrea Fassina <fasenderos@gmail.com>",
6
6
  "license": "MIT",