UNPKG

nodejs-order-book

Version:

Node.js Lmit Order Book for high-frequency trading (HFT).

77 lines (76 loc) 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StopBook = void 0; var stopside_1 = require("./stopside"); var types_1 = require("./types"); var StopBook = /** @class */ (function () { function StopBook() { var _this = this; this.add = function (order) { var stopSide = order.side === types_1.Side.BUY ? _this.bids : _this.asks; stopSide.append(order); }; this.remove = function (side, id, stopPrice) { var stopSide = side === types_1.Side.BUY ? _this.bids : _this.asks; return stopSide.remove(id, stopPrice); }; this.removePriceLevel = function (side, priceLevel) { var stopSide = side === types_1.Side.BUY ? _this.bids : _this.asks; stopSide.removePriceLevel(priceLevel); }; this.getConditionalOrders = function (side, priceBefore, marketPrice) { var stopSide = side === types_1.Side.BUY ? _this.bids : _this.asks; return stopSide.between(priceBefore, marketPrice); }; /** * Stop-Limit Order: * Buy: marketPrice < stopPrice <= price * Sell: marketPrice > stopPrice >= price * Stop-Market Order: * Buy: marketPrice < stopPrice * Sell: marketPrice > stopPrice */ this.validConditionalOrder = function (marketPrice, order) { var response = false; var type = order.type, side = order.side, stopPrice = order.stopPrice; if (type === types_1.OrderType.STOP_LIMIT) { // Buy: marketPrice < stopPrice <= price if (side === types_1.Side.BUY && marketPrice < stopPrice && stopPrice <= order.price) { response = true; } // Sell: marketPrice > stopPrice >= price if (side === types_1.Side.SELL && marketPrice > stopPrice && stopPrice >= order.price) { response = true; } } else { // Buy: marketPrice < stopPrice if (side === types_1.Side.BUY && marketPrice < stopPrice) response = true; // Sell: marketPrice > stopPrice if (side === types_1.Side.SELL && marketPrice > stopPrice) response = true; } return response; }; this.snapshot = function () { var bids = []; var asks = []; _this.bids.priceTree().forEach(function (price, orders) { bids.push({ price: price, orders: orders.toArray().map(function (o) { return o.toObject(); }) }); }); _this.asks.priceTree().forEach(function (price, orders) { asks.push({ price: price, orders: orders.toArray().map(function (o) { return o.toObject(); }) }); }); return { bids: bids, asks: asks }; }; this.bids = new stopside_1.StopSide(types_1.Side.BUY); this.asks = new stopside_1.StopSide(types_1.Side.SELL); } return StopBook; }()); exports.StopBook = StopBook;