UNPKG

orderbooks

Version:

In-memory state stores and handlers for caching multiple exchange:symbol orderbook states

76 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderBooksStore = void 0; const OrderBook_1 = require("./OrderBook"); /** * Store for multi-symbol orderbooks, grouped into one book (OrderBook) per symbol * * `ExtraStateType` is optional extra state you may want to store with each orderbook level, completely optional. Inject a union type if desired. * @class OrderBooksStore */ class OrderBooksStore { constructor(options) { this.books = {}; this.books = {}; this.traceLog = (options === null || options === void 0 ? void 0 : options.traceLog) === true; this.shouldCheckTimestamp = (options === null || options === void 0 ? void 0 : options.checkTimestamps) === true; this.maxDepth = (options === null || options === void 0 ? void 0 : options.maxDepth) || 250; } /** * Get the current orderbook store for a symbol. Automatically initialised (empty), if none exists yet. * @param {string} symbol * @returns {OrderBook} created for symbol if not already tracked */ getBook(symbol) { if (this.books[symbol]) { return this.books[symbol]; } this.books[symbol] = new OrderBook_1.OrderBook(symbol, { checkTimestamps: this.shouldCheckTimestamp, maxDepth: this.maxDepth, }); return this.books[symbol]; } /** * @public Store/replace existing orderbook state in-memory * * @param {string} symbol * @param {Array} data current orderbook snapshot represented as array, where each child element is a level in the orderbook * @param {number} timestamp * @returns {OrderBook} store instance that handled this event */ handleSnapshot(symbol, data, timestamp = Date.now()) { if (this.traceLog) { console.log('handleSnapshot ', symbol, timestamp); } return this.getBook(symbol).handleSnapshot(data, timestamp); } /** * @public Update existing orderbook state in-memory * * @param {string} symbol * @param {Array} deleteLevels - array with levels to delete * @param {Array} updateLevels - array with levels to update * @param {Array} insertLevels - array with levels to insert * @param {number} timestamp * @returns {OrderBook} store instance that handled this event */ handleDelta(symbol, deleteLevels, updateLevels, insertLevels, timestamp = Date.now()) { if (this.traceLog) { console.log('handleDelta ', symbol, timestamp); } return this.getBook(symbol).handleDelta(deleteLevels, updateLevels, insertLevels, timestamp); } /** * Calculate expected slippage for a market order of a given size for a specific symbol * @param {string} symbol - The trading symbol * @param {number} orderSize - The size of the order in base units * @param {string} side - 'Buy' or 'Sell' side of the order * @returns {{ executionPrice: number, slippagePercent: number, slippageBasisPoints: number } | null} - The expected execution price and slippage */ getEstimatedSlippage(symbol, baseOrderSize, side) { return this.getBook(symbol).getEstimatedSlippage(baseOrderSize, side); } } exports.OrderBooksStore = OrderBooksStore; //# sourceMappingURL=OrderBooksStore.js.map