Skip to content

Commit ca140c1

Browse files
committed
feat: add journal log for conditional orders
1 parent 9fc263a commit ca140c1

File tree

3 files changed

+269
-27
lines changed

3 files changed

+269
-27
lines changed

src/orderbook.ts

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class OrderBook {
139139
opId: ++this._lastOp,
140140
ts: Date.now(),
141141
op: "m",
142-
o: { side: options.side, size: options.size },
142+
o: options,
143143
};
144144
}
145145
return response;
@@ -160,7 +160,16 @@ export class OrderBook {
160160
throw new Error(
161161
"In order to use conditional orders you need to instantiate the order book with the `experimentalConditionalOrders` option set to true",
162162
);
163-
return this._stopMarket(options);
163+
const response = this._stopMarket(options);
164+
if (this.enableJournaling && response.err === null) {
165+
response.log = {
166+
opId: ++this._lastOp,
167+
ts: Date.now(),
168+
op: "sm",
169+
o: options,
170+
};
171+
}
172+
return response;
164173
};
165174

166175
/**
@@ -182,14 +191,7 @@ export class OrderBook {
182191
opId: ++this._lastOp,
183192
ts: Date.now(),
184193
op: "l",
185-
o: {
186-
side: options.side,
187-
id: options.id,
188-
size: options.size,
189-
price: options.price,
190-
postOnly: options.postOnly ?? false,
191-
timeInForce: options.timeInForce ?? TimeInForce.GTC,
192-
},
194+
o: options,
193195
};
194196
}
195197
return response;
@@ -213,7 +215,16 @@ export class OrderBook {
213215
throw new Error(
214216
"In order to use conditional orders you need to instantiate the order book with the `experimentalConditionalOrders` option set to true",
215217
);
216-
return this._stopLimit(options);
218+
const response = this._stopLimit(options);
219+
if (this.enableJournaling && response.err === null) {
220+
response.log = {
221+
opId: ++this._lastOp,
222+
ts: Date.now(),
223+
op: "sl",
224+
o: options,
225+
};
226+
}
227+
return response;
217228
};
218229

219230
/**
@@ -245,7 +256,16 @@ export class OrderBook {
245256
throw new Error(
246257
"In order to use conditional orders you need to instantiate the order book with the `experimentalConditionalOrders` option set to true",
247258
);
248-
return this._oco(options);
259+
const response = this._oco(options);
260+
if (this.enableJournaling && response.err === null) {
261+
response.log = {
262+
opId: ++this._lastOp,
263+
ts: Date.now(),
264+
op: "oco",
265+
o: options,
266+
};
267+
}
268+
return response;
249269
};
250270

251271
/**
@@ -780,21 +800,52 @@ export class OrderBook {
780800
if (side == null || size == null) {
781801
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
782802
}
783-
this.market({ side, size });
803+
this.market(log.o);
784804
break;
785805
}
786806
case "l": {
787-
const { side, id, size, price, timeInForce } = log.o;
807+
const { side, id, size, price } = log.o;
788808
if (side == null || id == null || size == null || price == null) {
789809
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
790810
}
791-
this.limit({
792-
side,
793-
id,
794-
size,
795-
price,
796-
timeInForce,
797-
});
811+
this.limit(log.o);
812+
break;
813+
}
814+
case "sm": {
815+
const { side, size, stopPrice } = log.o;
816+
if (side == null || size == null || stopPrice == null) {
817+
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
818+
}
819+
this.stopMarket(log.o);
820+
break;
821+
}
822+
case "sl": {
823+
const { side, id, size, price, stopPrice } = log.o;
824+
if (
825+
side == null ||
826+
id == null ||
827+
size == null ||
828+
price == null ||
829+
stopPrice == null
830+
) {
831+
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
832+
}
833+
this.stopLimit(log.o);
834+
break;
835+
}
836+
case "oco": {
837+
const { side, id, size, price, stopPrice, stopLimitPrice } = log.o;
838+
if (
839+
side == null ||
840+
id == null ||
841+
size == null ||
842+
price == null ||
843+
stopPrice == null ||
844+
stopLimitPrice == null
845+
) {
846+
throw CustomError(ERROR.INVALID_JOURNAL_LOG);
847+
}
848+
this.oco(log.o);
798849
break;
799850
}
800851
case "d":

src/types.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export interface InternalStopLimitOrderOptions extends ILimitOrderOptions {
9696
* Specific options for oco order.
9797
*/
9898
export interface OCOOrderOptions extends StopLimitOrderOptions {
99-
stopPrice: number;
10099
stopLimitPrice: number;
101100
stopLimitTimeInForce?: TimeInForce;
102101
}
@@ -239,6 +238,48 @@ interface LimitOrderJournalLog {
239238
o: LimitOrderOptions;
240239
}
241240

241+
/**
242+
* Represents a log entry for a stop_market order operation.
243+
*/
244+
interface StopMarketOrderJournalLog {
245+
/** Incremental ID of the operation */
246+
opId: number;
247+
/** Timestamp of the operation. */
248+
ts: number;
249+
/** Operation type: 'sm' for stop_market order. */
250+
op: "sm";
251+
/** Specific options for the stop_market order. */
252+
o: StopMarketOrderOptions;
253+
}
254+
255+
/**
256+
* Represents a log entry for a stop_limit order operation.
257+
*/
258+
interface StopLimitOrderJournalLog {
259+
/** Incremental ID of the operation */
260+
opId: number;
261+
/** Timestamp of the operation. */
262+
ts: number;
263+
/** Operation type: 'l' for stop_limit order. */
264+
op: "sl";
265+
/** Specific options for the stop_limit order. */
266+
o: StopLimitOrderOptions;
267+
}
268+
269+
/**
270+
* Represents a log entry for a oco order operation.
271+
*/
272+
interface OCOOrderJournalLog {
273+
/** Incremental ID of the operation */
274+
opId: number;
275+
/** Timestamp of the operation. */
276+
ts: number;
277+
/** Operation type: 'l' for oco order. */
278+
op: "oco";
279+
/** Specific options for the oco order. */
280+
o: OCOOrderOptions;
281+
}
282+
242283
/**
243284
* Represents a log entry for an order modification operation.
244285
*/
@@ -273,6 +314,9 @@ interface CancelOrderJournalLog {
273314
export type JournalLog =
274315
| MarketOrderJournalLog
275316
| LimitOrderJournalLog
317+
| StopMarketOrderJournalLog
318+
| StopLimitOrderJournalLog
319+
| OCOOrderJournalLog
276320
| ModifyOrderJournalLog
277321
| CancelOrderJournalLog;
278322

0 commit comments

Comments
 (0)