nodejs-order-book
Version:
Node.js Lmit Order Book for high-frequency trading (HFT).
172 lines (171 loc) • 9.67 kB
TypeScript
import { type OrderBookError } from "./errors";
import { type CreateOrderOptions, type ICancelOrder, type ILimitOrder, type IProcessOrder, type LimitOrderOptions, type MarketOrderOptions, type OCOOrderOptions, type OrderBookOptions, type OrderUpdatePrice, type OrderUpdateSize, Side, type Snapshot, type StopLimitOrderOptions, type StopMarketOrderOptions } from "./types";
export declare class OrderBook {
private orders;
private _lastOp;
private _marketPrice;
private readonly bids;
private readonly asks;
private readonly enableJournaling;
private readonly stopBook;
/**
* Creates an instance of OrderBook.
* @param {OrderBookOptions} [options={}] - Options for configuring the order book.
* @param {JournalLog} [options.snapshot] - The orderbook snapshot will be restored before processing any journal logs, if any.
* @param {JournalLog} [options.journal] - Array of journal logs (optional).
* @param {boolean} [options.enableJournaling=false] - Flag to enable journaling. Default to false
*/
constructor({ snapshot, journal, enableJournaling, }?: OrderBookOptions);
get marketPrice(): number;
get lastOp(): number;
/**
* Create new order. See {@link CreateOrderOptions} for details.
*
* @param options
* @param options.type - `limit` | `market` | 'stop_limit' | 'stop_market' | 'oco'
* @param options.side - `sell` or `buy`
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency. Param only for limit order
* @param options.orderID - Unique order ID. Param only for limit order
* @param options.postOnly - Can be used with 'limit' order and when it's `true` the order will be rejected if immediately matches and trades as a taker. Default is `false`
* @param options.stopPrice - The price at which the order will be triggered. Used with `stop_limit` and `stop_market` order.
* @param options.stopLimitPrice - The price at which the order will be triggered. Used with `stop_limit` and `stop_market` order.
* @param options.timeInForce - Time-in-force supported are: `GTC` (default), `FOK`, `IOC`. Param only for limit order
* @param options.stopLimitTimeInForce - Time-in-force supported are: `GTC` (default), `FOK`, `IOC`. Param only for limit order
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
createOrder(options: CreateOrderOptions): IProcessOrder;
/**
* Create a market order. See {@link MarketOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.size - How much of currency you want to trade in units of base currency
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
market(options: MarketOrderOptions): IProcessOrder;
/**
* Create a stop market order. See {@link StopMarketOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.stopPrice - The price at which the order will be triggered.
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
stopMarket: (options: StopMarketOrderOptions) => IProcessOrder;
/**
* Create a limit order. See {@link LimitOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.id - Unique order ID
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
* @param options.postOnly - When `true` the order will be rejected if immediately matches and trades as a taker. Default is `false`
* @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
limit(options: LimitOrderOptions): IProcessOrder;
/**
* Create a stop limit order. See {@link StopLimitOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.id - Unique order ID
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
* @param options.stopPrice - The price at which the order will be triggered.
* @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
stopLimit: (options: StopLimitOrderOptions) => IProcessOrder;
/**
* Create an OCO (One-Cancels-the-Other) order.
* OCO order combines a `stop_limit` order and a `limit` order, where if stop price
* is triggered or limit order is fully or partially fulfilled, the other is canceled.
* Both orders have the same `side` and `size`. If you cancel one of the orders, the
* entire OCO order pair will be canceled.
*
* For BUY orders the `stopPrice` must be above the current price and the `price` below the current price
* For SELL orders the `stopPrice` must be below the current price and the `price` above the current price
*
* See {@link OCOOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.id - Unique order ID
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.price - The price of the `limit` order at which the order is to be fullfilled, in units of the quote currency
* @param options.stopPrice - The price at which the `stop_limit` order will be triggered.
* @param options.stopLimitPrice - The price of the `stop_limit` order at which the order is to be fullfilled, in units of the quote currency.
* @param options.timeInForce - Time-in-force of the `limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
* @param options.stopLimitTimeInForce - Time-in-force of the `stop_limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
oco: (options: OCOOrderOptions) => IProcessOrder;
/**
* Modify an existing order with given ID. When an order is modified by price or quantity,
* it will be deemed as a new entry. Under the price-time-priority algorithm, orders are
* prioritized according to their order price and order time. Hence, the latest orders
* will be placed at the back of the matching order queue.
*
* @param orderID - The ID of the order to be modified
* @param orderUpdate - An object with the modified size and/or price of an order. The shape of the object is `{size, price}`.
* @returns An object with the result of the processed order or an error
*/
modify: (orderID: string, orderUpdate: OrderUpdatePrice | OrderUpdateSize) => IProcessOrder;
/**
* Remove an existing order with given ID from the order book
*
* @param orderID - The ID of the order to be removed
* @returns The removed order if exists or `undefined`
*/
cancel: (orderID: string) => ICancelOrder | undefined;
/**
* Get an existing order with the given ID
*
* @param orderID - The ID of the order to be returned
* @returns The order if exists or `undefined`
*/
order: (orderID: string) => ILimitOrder | undefined;
depth: () => [Array<[number, number]>, Array<[number, number]>];
toString: () => string;
calculateMarketPrice: (side: Side, size: number) => {
price: number;
err: null | OrderBookError;
};
snapshot: () => Snapshot;
private readonly _market;
private readonly _limit;
private readonly _stopMarket;
private readonly _stopLimit;
private readonly _oco;
private readonly _stopOrder;
private readonly restoreSnapshot;
/**
* Remove an existing order with given ID from the order book
* @param orderID The id of the order to be deleted
* @param internalDeletion Set to true when the delete comes from internal operations
* @returns The removed order if exists or `undefined`
*/
private readonly _cancelOrder;
private readonly getProcessOrderResponse;
private readonly createLimitOrder;
private readonly executeConditionalOrder;
private readonly replayJournal;
/**
* OCO Order:
* Buy: price < marketPrice < stopPrice
* Sell: price > marketPrice > stopPrice
*/
private readonly validateOCOOrder;
private readonly greaterThanOrEqual;
private readonly lowerThanOrEqual;
private readonly processQueue;
private readonly canFillOrder;
private readonly buyOrderCanBeFilled;
private readonly sellOrderCanBeFilled;
private readonly validateMarketOrder;
private readonly validateLimitOrder;
}