From 9fc263a0828caf0221432167e9347f4d46a25887 Mon Sep 17 00:00:00 2001 From: fasenderos Date: Sat, 31 Aug 2024 20:20:19 +0200 Subject: [PATCH] perf: refactor market and limit journaling --- src/orderbook.ts | 65 ++++++++++++++++++++++-------------------- src/types.ts | 28 +++++++++--------- test/orderbook.test.ts | 1 + 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/orderbook.ts b/src/orderbook.ts index 74922b7..9c07a11 100644 --- a/src/orderbook.ts +++ b/src/orderbook.ts @@ -133,7 +133,16 @@ export class OrderBook { * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure */ public market(options: MarketOrderOptions): IProcessOrder { - return this._market(options); + const response = this._market(options); + if (this.enableJournaling && response.err === null) { + response.log = { + opId: ++this._lastOp, + ts: Date.now(), + op: "m", + o: { side: options.side, size: options.size }, + }; + } + return response; } /** @@ -167,7 +176,23 @@ export class OrderBook { * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure */ public limit(options: LimitOrderOptions): IProcessOrder { - return this._limit(options); + const response = this._limit(options); + if (this.enableJournaling && response.err === null) { + response.log = { + opId: ++this._lastOp, + ts: Date.now(), + op: "l", + o: { + side: options.side, + id: options.id, + size: options.size, + price: options.price, + postOnly: options.postOnly ?? false, + timeInForce: options.timeInForce ?? TimeInForce.GTC, + }, + }; + } + return response; } /** @@ -383,7 +408,7 @@ export class OrderBook { incomingResponse?: IProcessOrder, ): IProcessOrder => { const response = incomingResponse ?? this.validateMarketOrder(options); - if (response.err != null) return response; + if (response.err !== null) return response; let quantityToTrade = options.size; let iter: () => OrderQueue | undefined; @@ -410,14 +435,6 @@ export class OrderBook { this.executeConditionalOrder(options.side, priceBefore, response); - if (this.enableJournaling) { - response.log = { - opId: ++this._lastOp, - ts: Date.now(), - op: "m", - o: { side: options.side, size: options.size }, - }; - } return response; }; @@ -426,8 +443,8 @@ export class OrderBook { incomingResponse?: IProcessOrder, ): IProcessOrder => { const response = incomingResponse ?? this.validateLimitOrder(options); - if (response.err != null) return response; - const order = this.createLimitOrder( + if (response.err !== null) return response; + this.createLimitOrder( response, options.side, options.id, @@ -437,20 +454,6 @@ export class OrderBook { options.timeInForce ?? TimeInForce.GTC, options.ocoStopPrice, ); - if (this.enableJournaling && order != null) { - response.log = { - opId: ++this._lastOp, - ts: Date.now(), - op: "l", - o: { - side: order.side, - id: order.id, - size: order.size, - price: order.price, - timeInForce: order.timeInForce, - }, - }; - } return response; }; @@ -458,7 +461,7 @@ export class OrderBook { options: StopMarketOrderOptions, ): IProcessOrder => { const response = this.validateMarketOrder(options); - if (response.err != null) return response; + if (response.err !== null) return response; const stopMarket = OrderFactory.createOrder({ ...options, type: OrderType.STOP_MARKET, @@ -470,7 +473,7 @@ export class OrderBook { options: StopLimitOrderOptions, ): IProcessOrder => { const response = this.validateLimitOrder(options); - if (response.err != null) return response; + if (response.err !== null) return response; const stopLimit = OrderFactory.createOrder({ ...options, type: OrderType.STOP_LIMIT, @@ -482,7 +485,7 @@ export class OrderBook { private readonly _oco = (options: OCOOrderOptions): IProcessOrder => { const response = this.validateLimitOrder(options); /* node:coverage ignore next - Already validated with limit test */ - if (response.err != null) return response; + if (response.err !== null) return response; if (this.validateOCOOrder(options)) { // We use the same ID for Stop Limit and Limit Order, since // we check only on limit order for duplicated ids @@ -498,7 +501,7 @@ export class OrderBook { response, ); /* node:coverage ignore next - Already validated with limit test */ - if (response.err != null) return response; + if (response.err !== null) return response; const stopLimit = OrderFactory.createOrder({ type: OrderType.STOP_LIMIT, diff --git a/src/types.ts b/src/types.ts index c302f61..b3ad94c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -211,20 +211,10 @@ export interface CancelOrderOptions { orderID: string; } -/** - * Represents a cancel order operation. - */ -export interface ICancelOrder { - order: LimitOrder; - stopOrder?: StopOrder; - /** Optional log related to the order cancellation. */ - log?: CancelOrderJournalLog; -} - /** * Represents a log entry for a market order operation. */ -export interface MarketOrderJournalLog { +interface MarketOrderJournalLog { /** Incremental ID of the operation */ opId: number; /** Timestamp of the operation. */ @@ -238,7 +228,7 @@ export interface MarketOrderJournalLog { /** * Represents a log entry for a limit order operation. */ -export interface LimitOrderJournalLog { +interface LimitOrderJournalLog { /** Incremental ID of the operation */ opId: number; /** Timestamp of the operation. */ @@ -252,7 +242,7 @@ export interface LimitOrderJournalLog { /** * Represents a log entry for an order modification operation. */ -export interface ModifyOrderJournalLog { +interface ModifyOrderJournalLog { /** Incremental ID of the operation */ opId: number; /** Timestamp of the operation. */ @@ -266,7 +256,7 @@ export interface ModifyOrderJournalLog { /** * Represents a log entry for an order cancellation operation. */ -export interface CancelOrderJournalLog { +interface CancelOrderJournalLog { /** Incremental ID of the operation */ opId: number; /** Timestamp of the operation. */ @@ -293,6 +283,16 @@ export type CreateOrderOptions = | ({ type: OrderType.STOP_LIMIT } & StopLimitOrderOptions) | ({ type: OrderType.OCO } & OCOOrderOptions); +/** + * Represents a cancel order operation. + */ +export interface ICancelOrder { + order: LimitOrder; + stopOrder?: StopOrder; + /** Optional log related to the order cancellation. */ + log?: CancelOrderJournalLog; +} + /** * Options for configuring the order book. */ diff --git a/test/orderbook.test.ts b/test/orderbook.test.ts index 90f0372..202d4e0 100644 --- a/test/orderbook.test.ts +++ b/test/orderbook.test.ts @@ -1208,6 +1208,7 @@ void test("orderbook enableJournaling option", () => { id: "first-order", size: 50, price: 100, + postOnly: false, timeInForce: TimeInForce.GTC, }); }