nodejs-order-book
Version:
Node.js Lmit Order Book for high-frequency trading (HFT).
57 lines (56 loc) • 2.08 kB
JavaScript
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
import Denque from "denque";
var StopQueue = /** @class */ (function () {
function StopQueue(price) {
var _this = this;
this._ordersMap = {};
// returns the number of orders in queue
this.len = function () {
return _this._orders.length;
};
// remove order from head of queue
this.removeFromHead = function () {
// We can't use the shift method here because we need
// to update index in the map, so we use the remove(id) function
var order = _this._orders.peekFront();
if (order === undefined)
return;
return _this.remove(order.id);
};
// adds order to tail of the queue
this.append = function (order) {
_this._orders.push(order);
_this._ordersMap[order.id] = _this._orders.length - 1;
return order;
};
// removes order from the queue
this.remove = function (id) {
var deletedOrderIndex = _this._ordersMap[id];
if (deletedOrderIndex === undefined)
return;
var deletedOrder = _this._orders.removeOne(deletedOrderIndex);
delete _this._ordersMap[id];
// Update all orders indexes where index is greater than the deleted one
for (var orderId in _this._ordersMap) {
if (_this._ordersMap[orderId] > deletedOrderIndex) {
_this._ordersMap[orderId] -= 1;
}
}
return deletedOrder;
};
this.toArray = function () {
return _this._orders.toArray();
};
this._price = price;
this._orders = new Denque();
}
Object.defineProperty(StopQueue.prototype, "price", {
get: function () {
return this._price;
},
enumerable: false,
configurable: true
});
return StopQueue;
}());
export { StopQueue };