UNPKG

nodejs-order-book

Version:

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

74 lines (73 loc) 3.04 kB
import { StopSide } from "./stopside"; import { OrderType, Side } from "./types"; var StopBook = /** @class */ (function () { function StopBook() { var _this = this; this.add = function (order) { var stopSide = order.side === Side.BUY ? _this.bids : _this.asks; stopSide.append(order); }; this.remove = function (side, id, stopPrice) { var stopSide = side === Side.BUY ? _this.bids : _this.asks; return stopSide.remove(id, stopPrice); }; this.removePriceLevel = function (side, priceLevel) { var stopSide = side === Side.BUY ? _this.bids : _this.asks; stopSide.removePriceLevel(priceLevel); }; this.getConditionalOrders = function (side, priceBefore, marketPrice) { var stopSide = side === 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 === OrderType.STOP_LIMIT) { // Buy: marketPrice < stopPrice <= price if (side === Side.BUY && marketPrice < stopPrice && stopPrice <= order.price) { response = true; } // Sell: marketPrice > stopPrice >= price if (side === Side.SELL && marketPrice > stopPrice && stopPrice >= order.price) { response = true; } } else { // Buy: marketPrice < stopPrice if (side === Side.BUY && marketPrice < stopPrice) response = true; // Sell: marketPrice > stopPrice if (side === 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(Side.BUY); this.asks = new StopSide(Side.SELL); } return StopBook; }()); export { StopBook };