Skip to content

Commit

Permalink
perf: refactor market and limit journaling
Browse files Browse the repository at this point in the history
  • Loading branch information
fasenderos committed Aug 31, 2024
1 parent 2833a0f commit 9fc263a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
65 changes: 34 additions & 31 deletions src/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};

Expand All @@ -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,
Expand All @@ -437,28 +454,14 @@ 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;
};

private readonly _stopMarket = (
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,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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. */
Expand All @@ -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. */
Expand All @@ -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. */
Expand All @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions test/orderbook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ void test("orderbook enableJournaling option", () => {
id: "first-order",
size: 50,
price: 100,
postOnly: false,
timeInForce: TimeInForce.GTC,
});
}
Expand Down

0 comments on commit 9fc263a

Please sign in to comment.