nodejs-order-book
Version:
Node.js Lmit Order Book for high-frequency trading (HFT).
303 lines (302 loc) • 11 kB
JavaScript
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 __());
};
})();
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
import { randomUUID } from "node:crypto";
import { CustomError, ERROR } from "./errors";
import { OrderType, } from "./types";
import { safeStringify } from "./utils";
var BaseOrder = /** @class */ (function () {
function BaseOrder(options) {
var _a, _b;
this._id = (_a = options.id) !== null && _a !== void 0 ? _a : 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 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));
export { 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 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));
export { 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 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));
export { StopLimitOrder };
export var OrderFactory = {
createOrder: function (options) {
switch (options.type) {
case OrderType.LIMIT:
// biome-ignore lint: don't know how to typing this return
return new LimitOrder(options);
case OrderType.STOP_LIMIT:
// biome-ignore lint: don't know how to typing this return
return new StopLimitOrder(options);
case OrderType.STOP_MARKET:
// biome-ignore lint: don't know how to typing this return
return new StopMarketOrder(options);
default:
throw CustomError(ERROR.INVALID_ORDER_TYPE);
}
},
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
};