Skip to content

Commit

Permalink
feat: add journal log for conditional orders
Browse files Browse the repository at this point in the history
  • Loading branch information
fasenderos committed Aug 31, 2024
1 parent 9fc263a commit ca140c1
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 27 deletions.
93 changes: 72 additions & 21 deletions src/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class OrderBook {
opId: ++this._lastOp,
ts: Date.now(),
op: "m",
o: { side: options.side, size: options.size },
o: options,
};
}
return response;
Expand All @@ -160,7 +160,16 @@ export class OrderBook {
throw new Error(
"In order to use conditional orders you need to instantiate the order book with the `experimentalConditionalOrders` option set to true",
);
return this._stopMarket(options);
const response = this._stopMarket(options);
if (this.enableJournaling && response.err === null) {
response.log = {
opId: ++this._lastOp,
ts: Date.now(),
op: "sm",
o: options,
};
}
return response;
};

/**
Expand All @@ -182,14 +191,7 @@ export class OrderBook {
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,
},
o: options,
};
}
return response;
Expand All @@ -213,7 +215,16 @@ export class OrderBook {
throw new Error(
"In order to use conditional orders you need to instantiate the order book with the `experimentalConditionalOrders` option set to true",
);
return this._stopLimit(options);
const response = this._stopLimit(options);
if (this.enableJournaling && response.err === null) {
response.log = {
opId: ++this._lastOp,
ts: Date.now(),
op: "sl",
o: options,
};
}
return response;
};

/**
Expand Down Expand Up @@ -245,7 +256,16 @@ export class OrderBook {
throw new Error(
"In order to use conditional orders you need to instantiate the order book with the `experimentalConditionalOrders` option set to true",
);
return this._oco(options);
const response = this._oco(options);
if (this.enableJournaling && response.err === null) {
response.log = {
opId: ++this._lastOp,
ts: Date.now(),
op: "oco",
o: options,
};
}
return response;
};

/**
Expand Down Expand Up @@ -780,21 +800,52 @@ export class OrderBook {
if (side == null || size == null) {
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
}
this.market({ side, size });
this.market(log.o);
break;
}
case "l": {
const { side, id, size, price, timeInForce } = log.o;
const { side, id, size, price } = log.o;
if (side == null || id == null || size == null || price == null) {
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
}
this.limit({
side,
id,
size,
price,
timeInForce,
});
this.limit(log.o);
break;
}
case "sm": {
const { side, size, stopPrice } = log.o;
if (side == null || size == null || stopPrice == null) {
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
}
this.stopMarket(log.o);
break;
}
case "sl": {
const { side, id, size, price, stopPrice } = log.o;
if (
side == null ||
id == null ||
size == null ||
price == null ||
stopPrice == null
) {
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
}
this.stopLimit(log.o);
break;
}
case "oco": {
const { side, id, size, price, stopPrice, stopLimitPrice } = log.o;
if (
side == null ||
id == null ||
size == null ||
price == null ||
stopPrice == null ||
stopLimitPrice == null
) {
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
}
this.oco(log.o);
break;
}
case "d":
Expand Down
46 changes: 45 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export interface InternalStopLimitOrderOptions extends ILimitOrderOptions {
* Specific options for oco order.
*/
export interface OCOOrderOptions extends StopLimitOrderOptions {
stopPrice: number;
stopLimitPrice: number;
stopLimitTimeInForce?: TimeInForce;
}
Expand Down Expand Up @@ -239,6 +238,48 @@ interface LimitOrderJournalLog {
o: LimitOrderOptions;
}

/**
* Represents a log entry for a stop_market order operation.
*/
interface StopMarketOrderJournalLog {
/** Incremental ID of the operation */
opId: number;
/** Timestamp of the operation. */
ts: number;
/** Operation type: 'sm' for stop_market order. */
op: "sm";
/** Specific options for the stop_market order. */
o: StopMarketOrderOptions;
}

/**
* Represents a log entry for a stop_limit order operation.
*/
interface StopLimitOrderJournalLog {
/** Incremental ID of the operation */
opId: number;
/** Timestamp of the operation. */
ts: number;
/** Operation type: 'l' for stop_limit order. */
op: "sl";
/** Specific options for the stop_limit order. */
o: StopLimitOrderOptions;
}

/**
* Represents a log entry for a oco order operation.
*/
interface OCOOrderJournalLog {
/** Incremental ID of the operation */
opId: number;
/** Timestamp of the operation. */
ts: number;
/** Operation type: 'l' for oco order. */
op: "oco";
/** Specific options for the oco order. */
o: OCOOrderOptions;
}

/**
* Represents a log entry for an order modification operation.
*/
Expand Down Expand Up @@ -273,6 +314,9 @@ interface CancelOrderJournalLog {
export type JournalLog =
| MarketOrderJournalLog
| LimitOrderJournalLog
| StopMarketOrderJournalLog
| StopLimitOrderJournalLog
| OCOOrderJournalLog
| ModifyOrderJournalLog
| CancelOrderJournalLog;

Expand Down
Loading

0 comments on commit ca140c1

Please sign in to comment.