nodejs-order-book 10.0.1 → 10.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +198 -0
- package/dist/cjs/errors.js +47 -45
- package/dist/cjs/index.js +4 -3
- package/dist/cjs/order.js +229 -272
- package/dist/cjs/orderbook.js +848 -784
- package/dist/cjs/orderqueue.js +68 -66
- package/dist/cjs/orderside.js +139 -147
- package/dist/cjs/stopbook.js +69 -69
- package/dist/cjs/stopqueue.js +47 -51
- package/dist/cjs/stopside.js +68 -68
- package/dist/cjs/types.js +8 -1
- package/dist/cjs/utils.js +2 -2
- package/dist/esm/errors.js +47 -46
- package/dist/esm/index.js +2 -2
- package/dist/esm/order.js +227 -273
- package/dist/esm/orderbook.js +844 -781
- package/dist/esm/orderqueue.js +67 -66
- package/dist/esm/orderside.js +134 -143
- package/dist/esm/stopbook.js +67 -68
- package/dist/esm/stopqueue.js +46 -51
- package/dist/esm/stopside.js +64 -65
- package/dist/esm/types.js +7 -0
- package/dist/esm/utils.js +2 -2
- package/dist/types/errors.d.ts +2 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/order.d.ts +5 -1
- package/dist/types/types.d.ts +24 -0
- package/package.json +13 -12
package/dist/cjs/order.js
CHANGED
|
@@ -1,292 +1,249 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.OrderFactory = exports.StopLimitOrder = exports.StopMarketOrder = exports.LimitOrder = void 0;
|
|
19
4
|
/* node:coverage ignore next - Don't know why first and last line of each file count as uncovered */
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const types_1 = require("./types");
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
class BaseOrder {
|
|
10
|
+
_id;
|
|
11
|
+
_side;
|
|
12
|
+
_size;
|
|
13
|
+
_time;
|
|
14
|
+
_accountId;
|
|
15
|
+
_stpMode;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this._id = options.id ?? (0, node_crypto_1.randomUUID)();
|
|
28
18
|
this._side = options.side;
|
|
29
19
|
this._size = options.size;
|
|
30
|
-
this._time =
|
|
20
|
+
this._time = options.time ?? Date.now();
|
|
21
|
+
this._accountId = options.accountId ?? "";
|
|
22
|
+
this._stpMode = options.stpMode ?? types_1.SelfTradePreventionMode.NONE;
|
|
31
23
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return this._id;
|
|
36
|
-
},
|
|
37
|
-
enumerable: false,
|
|
38
|
-
configurable: true
|
|
39
|
-
});
|
|
40
|
-
Object.defineProperty(BaseOrder.prototype, "side", {
|
|
41
|
-
// Getter for order side
|
|
42
|
-
get: function () {
|
|
43
|
-
return this._side;
|
|
44
|
-
},
|
|
45
|
-
enumerable: false,
|
|
46
|
-
configurable: true
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(BaseOrder.prototype, "size", {
|
|
49
|
-
// Getter for order size
|
|
50
|
-
get: function () {
|
|
51
|
-
return this._size;
|
|
52
|
-
},
|
|
53
|
-
// Setter for order size
|
|
54
|
-
set: function (size) {
|
|
55
|
-
this._size = size;
|
|
56
|
-
},
|
|
57
|
-
enumerable: false,
|
|
58
|
-
configurable: true
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(BaseOrder.prototype, "time", {
|
|
61
|
-
// Getter for order timestamp
|
|
62
|
-
get: function () {
|
|
63
|
-
return this._time;
|
|
64
|
-
},
|
|
65
|
-
// Setter for order timestamp
|
|
66
|
-
set: function (time) {
|
|
67
|
-
this._time = time;
|
|
68
|
-
},
|
|
69
|
-
enumerable: false,
|
|
70
|
-
configurable: true
|
|
71
|
-
});
|
|
72
|
-
return BaseOrder;
|
|
73
|
-
}());
|
|
74
|
-
var LimitOrder = /** @class */ (function (_super) {
|
|
75
|
-
__extends(LimitOrder, _super);
|
|
76
|
-
function LimitOrder(options) {
|
|
77
|
-
var _a;
|
|
78
|
-
var _this = _super.call(this, options) || this;
|
|
79
|
-
_this.toString = function () {
|
|
80
|
-
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);
|
|
81
|
-
};
|
|
82
|
-
_this.toJSON = function () { return (0, utils_1.safeStringify)(_this.toObject()); };
|
|
83
|
-
_this.toObject = function () { return ({
|
|
84
|
-
id: _this._id,
|
|
85
|
-
type: _this.type,
|
|
86
|
-
side: _this._side,
|
|
87
|
-
size: _this._size,
|
|
88
|
-
origSize: _this._origSize,
|
|
89
|
-
price: _this._price,
|
|
90
|
-
time: _this._time,
|
|
91
|
-
timeInForce: _this._timeInForce,
|
|
92
|
-
makerQty: _this._makerQty,
|
|
93
|
-
takerQty: _this._takerQty,
|
|
94
|
-
}); };
|
|
95
|
-
_this._type = options.type;
|
|
96
|
-
_this._origSize = options.origSize;
|
|
97
|
-
_this._price = options.price;
|
|
98
|
-
_this._timeInForce = options.timeInForce;
|
|
99
|
-
_this._makerQty = options.makerQty;
|
|
100
|
-
_this._takerQty = options.takerQty;
|
|
101
|
-
_this._postOnly = (_a = options.postOnly) !== null && _a !== void 0 ? _a : false;
|
|
102
|
-
_this._ocoStopPrice = options.ocoStopPrice;
|
|
103
|
-
return _this;
|
|
24
|
+
// Getter for order ID
|
|
25
|
+
get id() {
|
|
26
|
+
return this._id;
|
|
104
27
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
28
|
+
// Getter for order side
|
|
29
|
+
get side() {
|
|
30
|
+
return this._side;
|
|
31
|
+
}
|
|
32
|
+
// Getter for order size
|
|
33
|
+
get size() {
|
|
34
|
+
return this._size;
|
|
35
|
+
}
|
|
36
|
+
// Setter for order size
|
|
37
|
+
set size(size) {
|
|
38
|
+
this._size = size;
|
|
39
|
+
}
|
|
40
|
+
// Getter for order timestamp
|
|
41
|
+
get time() {
|
|
42
|
+
return this._time;
|
|
43
|
+
}
|
|
44
|
+
// Setter for order timestamp
|
|
45
|
+
set time(time) {
|
|
46
|
+
this._time = time;
|
|
47
|
+
}
|
|
48
|
+
// Getter for account ID (self-trade prevention)
|
|
49
|
+
get accountId() {
|
|
50
|
+
return this._accountId;
|
|
51
|
+
}
|
|
52
|
+
// Getter for self-trade prevention mode
|
|
53
|
+
get stpMode() {
|
|
54
|
+
return this._stpMode;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
class LimitOrder extends BaseOrder {
|
|
58
|
+
_type;
|
|
59
|
+
_origSize;
|
|
60
|
+
_price;
|
|
61
|
+
_timeInForce;
|
|
62
|
+
_makerQty;
|
|
63
|
+
_takerQty;
|
|
64
|
+
_postOnly;
|
|
65
|
+
// Refers to the linked Stop Limit order stopPrice
|
|
66
|
+
_ocoStopPrice;
|
|
67
|
+
constructor(options) {
|
|
68
|
+
super(options);
|
|
69
|
+
this._type = options.type;
|
|
70
|
+
this._origSize = options.origSize;
|
|
71
|
+
this._price = options.price;
|
|
72
|
+
this._timeInForce = options.timeInForce;
|
|
73
|
+
this._makerQty = options.makerQty;
|
|
74
|
+
this._takerQty = options.takerQty;
|
|
75
|
+
this._postOnly = options.postOnly ?? false;
|
|
76
|
+
this._ocoStopPrice = options.ocoStopPrice;
|
|
77
|
+
}
|
|
78
|
+
// Getter for order type
|
|
79
|
+
get type() {
|
|
80
|
+
return this._type;
|
|
81
|
+
}
|
|
82
|
+
// Getter for order price
|
|
83
|
+
get price() {
|
|
84
|
+
return this._price;
|
|
85
|
+
}
|
|
86
|
+
// Getter for order price
|
|
87
|
+
set price(price) {
|
|
88
|
+
this._price = price;
|
|
89
|
+
}
|
|
90
|
+
// Getter for timeInForce price
|
|
91
|
+
get timeInForce() {
|
|
92
|
+
return this._timeInForce;
|
|
93
|
+
}
|
|
94
|
+
// Getter for order postOnly
|
|
95
|
+
get postOnly() {
|
|
96
|
+
return this._postOnly;
|
|
97
|
+
}
|
|
98
|
+
// Getter for the original size of the order
|
|
99
|
+
get origSize() {
|
|
100
|
+
return this._origSize;
|
|
101
|
+
}
|
|
102
|
+
// Getter for order makerQty
|
|
103
|
+
get makerQty() {
|
|
104
|
+
return this._makerQty;
|
|
105
|
+
}
|
|
106
|
+
// Getter for order takerQty
|
|
107
|
+
get takerQty() {
|
|
108
|
+
return this._takerQty;
|
|
109
|
+
}
|
|
110
|
+
get ocoStopPrice() {
|
|
111
|
+
return this._ocoStopPrice;
|
|
112
|
+
}
|
|
113
|
+
toString = () => `${this._id}:
|
|
114
|
+
type: ${this.type}
|
|
115
|
+
side: ${this._side}
|
|
116
|
+
size: ${this._size}
|
|
117
|
+
origSize: ${this._origSize}
|
|
118
|
+
price: ${this._price}
|
|
119
|
+
time: ${this._time}
|
|
120
|
+
timeInForce: ${this._timeInForce}
|
|
121
|
+
makerQty: ${this._makerQty}
|
|
122
|
+
takerQty: ${this._takerQty}`;
|
|
123
|
+
toJSON = () => (0, utils_1.safeStringify)(this.toObject());
|
|
124
|
+
toObject = () => ({
|
|
125
|
+
id: this._id,
|
|
126
|
+
type: this.type,
|
|
127
|
+
side: this._side,
|
|
128
|
+
size: this._size,
|
|
129
|
+
origSize: this._origSize,
|
|
130
|
+
price: this._price,
|
|
131
|
+
time: this._time,
|
|
132
|
+
timeInForce: this._timeInForce,
|
|
133
|
+
makerQty: this._makerQty,
|
|
134
|
+
takerQty: this._takerQty,
|
|
135
|
+
...(this._accountId ? { accountId: this._accountId } : {}),
|
|
136
|
+
...(this._stpMode !== types_1.SelfTradePreventionMode.NONE
|
|
137
|
+
? { stpMode: this._stpMode }
|
|
138
|
+
: {}),
|
|
171
139
|
});
|
|
172
|
-
|
|
173
|
-
}(BaseOrder));
|
|
140
|
+
}
|
|
174
141
|
exports.LimitOrder = LimitOrder;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
_this.toJSON = function () { return (0, utils_1.safeStringify)(_this.toObject()); };
|
|
183
|
-
_this.toObject = function () { return ({
|
|
184
|
-
id: _this._id,
|
|
185
|
-
type: _this.type,
|
|
186
|
-
side: _this._side,
|
|
187
|
-
size: _this._size,
|
|
188
|
-
stopPrice: _this._stopPrice,
|
|
189
|
-
time: _this._time,
|
|
190
|
-
}); };
|
|
191
|
-
_this._type = options.type;
|
|
192
|
-
_this._stopPrice = options.stopPrice;
|
|
193
|
-
return _this;
|
|
142
|
+
class StopMarketOrder extends BaseOrder {
|
|
143
|
+
_type;
|
|
144
|
+
_stopPrice;
|
|
145
|
+
constructor(options) {
|
|
146
|
+
super(options);
|
|
147
|
+
this._type = options.type;
|
|
148
|
+
this._stopPrice = options.stopPrice;
|
|
194
149
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
150
|
+
// Getter for order type
|
|
151
|
+
get type() {
|
|
152
|
+
return this._type;
|
|
153
|
+
}
|
|
154
|
+
// Getter for order stopPrice
|
|
155
|
+
get stopPrice() {
|
|
156
|
+
return this._stopPrice;
|
|
157
|
+
}
|
|
158
|
+
toString = () => `${this._id}:
|
|
159
|
+
type: ${this.type}
|
|
160
|
+
side: ${this._side}
|
|
161
|
+
size: ${this._size}
|
|
162
|
+
stopPrice: ${this._stopPrice}
|
|
163
|
+
time: ${this._time}`;
|
|
164
|
+
toJSON = () => (0, utils_1.safeStringify)(this.toObject());
|
|
165
|
+
toObject = () => ({
|
|
166
|
+
id: this._id,
|
|
167
|
+
type: this.type,
|
|
168
|
+
side: this._side,
|
|
169
|
+
size: this._size,
|
|
170
|
+
stopPrice: this._stopPrice,
|
|
171
|
+
time: this._time,
|
|
172
|
+
...(this._accountId ? { accountId: this._accountId } : {}),
|
|
173
|
+
...(this._stpMode !== types_1.SelfTradePreventionMode.NONE
|
|
174
|
+
? { stpMode: this._stpMode }
|
|
175
|
+
: {}),
|
|
210
176
|
});
|
|
211
|
-
|
|
212
|
-
}(BaseOrder));
|
|
177
|
+
}
|
|
213
178
|
exports.StopMarketOrder = StopMarketOrder;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
price: _this._price,
|
|
229
|
-
stopPrice: _this._stopPrice,
|
|
230
|
-
isOCO: _this.isOCO,
|
|
231
|
-
timeInForce: _this._timeInForce,
|
|
232
|
-
time: _this._time,
|
|
233
|
-
}); };
|
|
234
|
-
_this._type = options.type;
|
|
235
|
-
_this._price = options.price;
|
|
236
|
-
_this._stopPrice = options.stopPrice;
|
|
237
|
-
_this._timeInForce = options.timeInForce;
|
|
238
|
-
_this._isOCO = (_a = options.isOCO) !== null && _a !== void 0 ? _a : false;
|
|
239
|
-
return _this;
|
|
179
|
+
class StopLimitOrder extends BaseOrder {
|
|
180
|
+
_type;
|
|
181
|
+
_price;
|
|
182
|
+
_stopPrice;
|
|
183
|
+
_timeInForce;
|
|
184
|
+
// It's true when there is a linked Limit Order
|
|
185
|
+
_isOCO;
|
|
186
|
+
constructor(options) {
|
|
187
|
+
super(options);
|
|
188
|
+
this._type = options.type;
|
|
189
|
+
this._price = options.price;
|
|
190
|
+
this._stopPrice = options.stopPrice;
|
|
191
|
+
this._timeInForce = options.timeInForce;
|
|
192
|
+
this._isOCO = options.isOCO ?? false;
|
|
240
193
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
194
|
+
// Getter for order type
|
|
195
|
+
get type() {
|
|
196
|
+
return this._type;
|
|
197
|
+
}
|
|
198
|
+
// Getter for order price
|
|
199
|
+
get price() {
|
|
200
|
+
return this._price;
|
|
201
|
+
}
|
|
202
|
+
// Getter for order price
|
|
203
|
+
set price(price) {
|
|
204
|
+
this._price = price;
|
|
205
|
+
}
|
|
206
|
+
// Getter for order stopPrice
|
|
207
|
+
get stopPrice() {
|
|
208
|
+
return this._stopPrice;
|
|
209
|
+
}
|
|
210
|
+
// Getter for timeInForce price
|
|
211
|
+
get timeInForce() {
|
|
212
|
+
return this._timeInForce;
|
|
213
|
+
}
|
|
214
|
+
// Getter for order isOCO
|
|
215
|
+
get isOCO() {
|
|
216
|
+
return this._isOCO;
|
|
217
|
+
}
|
|
218
|
+
toString = () => `${this._id}:
|
|
219
|
+
type: ${this.type}
|
|
220
|
+
side: ${this._side}
|
|
221
|
+
size: ${this._size}
|
|
222
|
+
price: ${this._price}
|
|
223
|
+
stopPrice: ${this._stopPrice}
|
|
224
|
+
isOCO: ${this.isOCO}
|
|
225
|
+
timeInForce: ${this._timeInForce}
|
|
226
|
+
time: ${this._time}`;
|
|
227
|
+
toJSON = () => (0, utils_1.safeStringify)(this.toObject());
|
|
228
|
+
toObject = () => ({
|
|
229
|
+
id: this._id,
|
|
230
|
+
type: this.type,
|
|
231
|
+
side: this._side,
|
|
232
|
+
size: this._size,
|
|
233
|
+
price: this._price,
|
|
234
|
+
stopPrice: this._stopPrice,
|
|
235
|
+
isOCO: this.isOCO,
|
|
236
|
+
timeInForce: this._timeInForce,
|
|
237
|
+
time: this._time,
|
|
238
|
+
...(this._accountId ? { accountId: this._accountId } : {}),
|
|
239
|
+
...(this._stpMode !== types_1.SelfTradePreventionMode.NONE
|
|
240
|
+
? { stpMode: this._stpMode }
|
|
241
|
+
: {}),
|
|
284
242
|
});
|
|
285
|
-
|
|
286
|
-
}(BaseOrder));
|
|
243
|
+
}
|
|
287
244
|
exports.StopLimitOrder = StopLimitOrder;
|
|
288
245
|
exports.OrderFactory = {
|
|
289
|
-
createOrder
|
|
246
|
+
createOrder(options) {
|
|
290
247
|
switch (options.type) {
|
|
291
248
|
case types_1.OrderType.LIMIT:
|
|
292
249
|
// biome-ignore lint: don't know how to typing this return
|