nodejs-order-book
Version:
Node.js Lmit Order Book for high-frequency trading (HFT).
306 lines (305 loc) • 11.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderFactory = exports.StopLimitOrder = exports.StopMarketOrder = exports.LimitOrder = void 0;
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
var node_crypto_1 = require("node:crypto");
var errors_1 = require("./errors");
var types_1 = require("./types");
var utils_1 = require("./utils");
var BaseOrder = /** @class */ (function () {
function BaseOrder(options) {
var _a, _b;
this._id = (_a = options.id) !== null && _a !== void 0 ? _a : (0, node_crypto_1.randomUUID)();
this._side = options.side;
this._size = options.size;
this._time = (_b = options.time) !== null && _b !== void 0 ? _b : Date.now();
}
Object.defineProperty(BaseOrder.prototype, "id", {
// Getter for order ID
get: function () {
return this._id;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseOrder.prototype, "side", {
// Getter for order side
get: function () {
return this._side;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseOrder.prototype, "size", {
// Getter for order size
get: function () {
return this._size;
},
// Setter for order size
set: function (size) {
this._size = size;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseOrder.prototype, "time", {
// Getter for order timestamp
get: function () {
return this._time;
},
// Setter for order timestamp
set: function (time) {
this._time = time;
},
enumerable: false,
configurable: true
});
return BaseOrder;
}());
var LimitOrder = /** @class */ (function (_super) {
__extends(LimitOrder, _super);
function LimitOrder(options) {
var _a;
var _this = _super.call(this, options) || this;
_this.toString = function () {
return "".concat(_this._id, ":\n type: ").concat(_this.type, "\n side: ").concat(_this._side, "\n size: ").concat(_this._size, "\n origSize: ").concat(_this._origSize, "\n price: ").concat(_this._price, "\n time: ").concat(_this._time, "\n timeInForce: ").concat(_this._timeInForce, "\n makerQty: ").concat(_this._makerQty, "\n takerQty: ").concat(_this._takerQty);
};
_this.toJSON = function () { return (0, utils_1.safeStringify)(_this.toObject()); };
_this.toObject = function () { return ({
id: _this._id,
type: _this.type,
side: _this._side,
size: _this._size,
origSize: _this._origSize,
price: _this._price,
time: _this._time,
timeInForce: _this._timeInForce,
makerQty: _this._makerQty,
takerQty: _this._takerQty,
}); };
_this._type = options.type;
_this._origSize = options.origSize;
_this._price = options.price;
_this._timeInForce = options.timeInForce;
_this._makerQty = options.makerQty;
_this._takerQty = options.takerQty;
_this._postOnly = (_a = options.postOnly) !== null && _a !== void 0 ? _a : false;
_this._ocoStopPrice = options.ocoStopPrice;
return _this;
}
Object.defineProperty(LimitOrder.prototype, "type", {
// Getter for order type
get: function () {
return this._type;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "price", {
// Getter for order price
get: function () {
return this._price;
},
// Getter for order price
set: function (price) {
this._price = price;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "timeInForce", {
// Getter for timeInForce price
get: function () {
return this._timeInForce;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "postOnly", {
// Getter for order postOnly
get: function () {
return this._postOnly;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "origSize", {
// Getter for the original size of the order
get: function () {
return this._origSize;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "makerQty", {
// Getter for order makerQty
get: function () {
return this._makerQty;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "takerQty", {
// Getter for order takerQty
get: function () {
return this._takerQty;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LimitOrder.prototype, "ocoStopPrice", {
get: function () {
return this._ocoStopPrice;
},
enumerable: false,
configurable: true
});
return LimitOrder;
}(BaseOrder));
exports.LimitOrder = LimitOrder;
var StopMarketOrder = /** @class */ (function (_super) {
__extends(StopMarketOrder, _super);
function StopMarketOrder(options) {
var _this = _super.call(this, options) || this;
_this.toString = function () {
return "".concat(_this._id, ":\n type: ").concat(_this.type, "\n side: ").concat(_this._side, "\n size: ").concat(_this._size, "\n stopPrice: ").concat(_this._stopPrice, "\n time: ").concat(_this._time);
};
_this.toJSON = function () { return (0, utils_1.safeStringify)(_this.toObject()); };
_this.toObject = function () { return ({
id: _this._id,
type: _this.type,
side: _this._side,
size: _this._size,
stopPrice: _this._stopPrice,
time: _this._time,
}); };
_this._type = options.type;
_this._stopPrice = options.stopPrice;
return _this;
}
Object.defineProperty(StopMarketOrder.prototype, "type", {
// Getter for order type
get: function () {
return this._type;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StopMarketOrder.prototype, "stopPrice", {
// Getter for order stopPrice
get: function () {
return this._stopPrice;
},
enumerable: false,
configurable: true
});
return StopMarketOrder;
}(BaseOrder));
exports.StopMarketOrder = StopMarketOrder;
var StopLimitOrder = /** @class */ (function (_super) {
__extends(StopLimitOrder, _super);
function StopLimitOrder(options) {
var _a;
var _this = _super.call(this, options) || this;
_this.toString = function () {
return "".concat(_this._id, ":\n type: ").concat(_this.type, "\n side: ").concat(_this._side, "\n size: ").concat(_this._size, "\n price: ").concat(_this._price, "\n stopPrice: ").concat(_this._stopPrice, "\n\tisOCO: ").concat(_this.isOCO, "\n timeInForce: ").concat(_this._timeInForce, "\n time: ").concat(_this._time);
};
_this.toJSON = function () { return (0, utils_1.safeStringify)(_this.toObject()); };
_this.toObject = function () { return ({
id: _this._id,
type: _this.type,
side: _this._side,
size: _this._size,
price: _this._price,
stopPrice: _this._stopPrice,
isOCO: _this.isOCO,
timeInForce: _this._timeInForce,
time: _this._time,
}); };
_this._type = options.type;
_this._price = options.price;
_this._stopPrice = options.stopPrice;
_this._timeInForce = options.timeInForce;
_this._isOCO = (_a = options.isOCO) !== null && _a !== void 0 ? _a : false;
return _this;
}
Object.defineProperty(StopLimitOrder.prototype, "type", {
// Getter for order type
get: function () {
return this._type;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StopLimitOrder.prototype, "price", {
// Getter for order price
get: function () {
return this._price;
},
// Getter for order price
set: function (price) {
this._price = price;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StopLimitOrder.prototype, "stopPrice", {
// Getter for order stopPrice
get: function () {
return this._stopPrice;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StopLimitOrder.prototype, "timeInForce", {
// Getter for timeInForce price
get: function () {
return this._timeInForce;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StopLimitOrder.prototype, "isOCO", {
// Getter for order isOCO
get: function () {
return this._isOCO;
},
enumerable: false,
configurable: true
});
return StopLimitOrder;
}(BaseOrder));
exports.StopLimitOrder = StopLimitOrder;
exports.OrderFactory = {
createOrder: function (options) {
switch (options.type) {
case types_1.OrderType.LIMIT:
// biome-ignore lint: don't know how to typing this return
return new LimitOrder(options);
case types_1.OrderType.STOP_LIMIT:
// biome-ignore lint: don't know how to typing this return
return new StopLimitOrder(options);
case types_1.OrderType.STOP_MARKET:
// biome-ignore lint: don't know how to typing this return
return new StopMarketOrder(options);
default:
throw (0, errors_1.CustomError)(errors_1.ERROR.INVALID_ORDER_TYPE);
}
},
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
};