hedgequantx 2.7.14 → 2.7.16

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.
@@ -1,599 +0,0 @@
1
- /**
2
- * Rithmic Manual Protobuf Decoder
3
- * Required because protobufjs can't handle field IDs > 100000
4
- */
5
-
6
- // Field IDs for LastTrade (template_id = 150)
7
- // From last_trade.proto - field numbers are the proto field IDs
8
- const LAST_TRADE_FIELDS = {
9
- TEMPLATE_ID: 154467,
10
- SYMBOL: 110100,
11
- EXCHANGE: 110101,
12
- TRADE_PRICE: 100006,
13
- TRADE_SIZE: 100178,
14
- AGGRESSOR: 112003, // TransactionType enum: 1=BUY, 2=SELL
15
- SSBOE: 150100,
16
- USECS: 150101,
17
- };
18
-
19
- // Debug: log ALL fields to find aggressor
20
- let debugFieldsLogged = 0;
21
- let debugAllFieldsLogged = 0;
22
-
23
- // Field IDs for BestBidOffer (template_id = 151)
24
- const BBO_FIELDS = {
25
- TEMPLATE_ID: 154467,
26
- SYMBOL: 110100,
27
- EXCHANGE: 110101,
28
- BID_PRICE: 100022,
29
- BID_SIZE: 100030,
30
- ASK_PRICE: 100025,
31
- ASK_SIZE: 100031,
32
- SSBOE: 150100,
33
- USECS: 150101,
34
- };
35
-
36
- // Field IDs for AccountPnLPositionUpdate (template_id = 450)
37
- const ACCOUNT_PNL_FIELDS = {
38
- TEMPLATE_ID: 154467,
39
- IS_SNAPSHOT: 110121,
40
- FCM_ID: 154013,
41
- IB_ID: 154014,
42
- ACCOUNT_ID: 154008,
43
- FILL_BUY_QTY: 154041,
44
- FILL_SELL_QTY: 154042,
45
- BUY_QTY: 154260,
46
- SELL_QTY: 154261,
47
- OPEN_POSITION_PNL: 156961,
48
- OPEN_POSITION_QUANTITY: 156962,
49
- CLOSED_POSITION_PNL: 156963,
50
- CLOSED_POSITION_QUANTITY: 156964,
51
- NET_QUANTITY: 156967,
52
- MIN_ACCOUNT_BALANCE: 156968,
53
- ACCOUNT_BALANCE: 156970,
54
- CASH_ON_HAND: 156971,
55
- MIN_MARGIN_BALANCE: 156976,
56
- MARGIN_BALANCE: 156977,
57
- AVAILABLE_BUYING_POWER: 157015,
58
- USED_BUYING_POWER: 157014,
59
- DAY_OPEN_PNL: 157954,
60
- DAY_CLOSED_PNL: 157955,
61
- DAY_PNL: 157956,
62
- PERCENT_MAX_LOSS: 156965,
63
- SSBOE: 150100,
64
- USECS: 150101,
65
- };
66
-
67
- // Field IDs for InstrumentPnLPositionUpdate (template_id = 451)
68
- const INSTRUMENT_PNL_FIELDS = {
69
- TEMPLATE_ID: 154467,
70
- IS_SNAPSHOT: 110121,
71
- FCM_ID: 154013,
72
- IB_ID: 154014,
73
- ACCOUNT_ID: 154008,
74
- SYMBOL: 110100,
75
- EXCHANGE: 110101,
76
- PRODUCT_CODE: 100749,
77
- INSTRUMENT_TYPE: 110116,
78
- FILL_BUY_QTY: 154041,
79
- FILL_SELL_QTY: 154042,
80
- BUY_QTY: 154260,
81
- SELL_QTY: 154261,
82
- AVG_OPEN_FILL_PRICE: 154434,
83
- OPEN_POSITION_PNL: 156961,
84
- OPEN_POSITION_QUANTITY: 156962,
85
- CLOSED_POSITION_PNL: 156963,
86
- CLOSED_POSITION_QUANTITY: 156964,
87
- NET_QUANTITY: 156967,
88
- DAY_OPEN_PNL: 157954,
89
- DAY_CLOSED_PNL: 157955,
90
- DAY_PNL: 157956,
91
- SSBOE: 150100,
92
- USECS: 150101,
93
- };
94
-
95
- /**
96
- * Read a varint from buffer starting at offset
97
- * Uses BigInt internally to handle large field IDs correctly
98
- * Returns [value, newOffset]
99
- */
100
- function readVarint(buffer, offset) {
101
- let result = BigInt(0);
102
- let shift = BigInt(0);
103
- let pos = offset;
104
-
105
- while (pos < buffer.length) {
106
- const byte = buffer[pos++];
107
- result |= BigInt(byte & 0x7f) << shift;
108
- if ((byte & 0x80) === 0) {
109
- return [Number(result), pos];
110
- }
111
- shift += BigInt(7);
112
- if (shift > BigInt(63)) {
113
- throw new Error('Varint too large');
114
- }
115
- }
116
-
117
- throw new Error('Incomplete varint');
118
- }
119
-
120
- /**
121
- * Read a length-delimited field (string/bytes)
122
- * Returns [string, newOffset]
123
- */
124
- function readLengthDelimited(buffer, offset) {
125
- const [length, newOffset] = readVarint(buffer, offset);
126
- const value = buffer.slice(newOffset, newOffset + length).toString('utf8');
127
- return [value, newOffset + length];
128
- }
129
-
130
- /**
131
- * Skip a field based on wire type
132
- */
133
- function skipField(buffer, offset, wireType) {
134
- switch (wireType) {
135
- case 0: // Varint
136
- const [, newOffset] = readVarint(buffer, offset);
137
- return newOffset;
138
- case 1: // 64-bit fixed
139
- return offset + 8;
140
- case 2: // Length-delimited
141
- const [length, lenOffset] = readVarint(buffer, offset);
142
- return lenOffset + length;
143
- case 5: // 32-bit fixed
144
- return offset + 4;
145
- default:
146
- throw new Error(`Unknown wire type: ${wireType}`);
147
- }
148
- }
149
-
150
- /**
151
- * Get template ID from buffer (first field)
152
- */
153
- function getTemplateId(buffer) {
154
- let offset = 0;
155
- while (offset < buffer.length) {
156
- try {
157
- const [tag, newOffset] = readVarint(buffer, offset);
158
- const fieldNumber = tag >>> 3;
159
- const wireType = tag & 0x7;
160
- offset = newOffset;
161
-
162
- if (fieldNumber === 154467 && wireType === 0) {
163
- const [templateId] = readVarint(buffer, offset);
164
- return templateId;
165
- }
166
- offset = skipField(buffer, offset, wireType);
167
- } catch {
168
- return -1;
169
- }
170
- }
171
- return -1;
172
- }
173
-
174
- /**
175
- * Decode LastTrade message (template_id = 150)
176
- */
177
- function decodeLastTrade(buffer) {
178
- const result = {};
179
- let offset = 0;
180
- const allFields = []; // Debug: track all fields
181
-
182
- // Debug first few calls
183
- if (debugAllFieldsLogged < 3) {
184
- console.log(`[DECODER] decodeLastTrade called, buffer size: ${buffer.length}`);
185
- }
186
-
187
- while (offset < buffer.length) {
188
- try {
189
- const [tag, newOffset] = readVarint(buffer, offset);
190
- const fieldNumber = tag >>> 3;
191
- const wireType = tag & 0x7;
192
- offset = newOffset;
193
-
194
- // Debug: log ALL fields for first few messages
195
- if (debugAllFieldsLogged < 2) {
196
- allFields.push({ fieldNumber, wireType });
197
- }
198
-
199
- switch (fieldNumber) {
200
- case LAST_TRADE_FIELDS.TEMPLATE_ID:
201
- [result.templateId, offset] = readVarint(buffer, offset);
202
- break;
203
- case LAST_TRADE_FIELDS.SYMBOL:
204
- if (wireType === 2) {
205
- [result.symbol, offset] = readLengthDelimited(buffer, offset);
206
- } else {
207
- offset = skipField(buffer, offset, wireType);
208
- }
209
- break;
210
- case LAST_TRADE_FIELDS.EXCHANGE:
211
- if (wireType === 2) {
212
- [result.exchange, offset] = readLengthDelimited(buffer, offset);
213
- } else {
214
- offset = skipField(buffer, offset, wireType);
215
- }
216
- break;
217
- case LAST_TRADE_FIELDS.TRADE_PRICE:
218
- if (wireType === 1) {
219
- // 64-bit double little-endian
220
- result.tradePrice = buffer.readDoubleLE(offset);
221
- offset += 8;
222
- } else {
223
- offset = skipField(buffer, offset, wireType);
224
- }
225
- break;
226
- case LAST_TRADE_FIELDS.TRADE_SIZE:
227
- if (wireType === 0) {
228
- [result.tradeSize, offset] = readVarint(buffer, offset);
229
- } else {
230
- offset = skipField(buffer, offset, wireType);
231
- }
232
- break;
233
- case LAST_TRADE_FIELDS.AGGRESSOR:
234
- if (wireType === 0) {
235
- [result.aggressor, offset] = readVarint(buffer, offset);
236
- // Debug: log when aggressor is found
237
- if (debugAllFieldsLogged < 5) {
238
- console.log(`[DECODER] AGGRESSOR FOUND! value=${result.aggressor}`);
239
- }
240
- } else {
241
- offset = skipField(buffer, offset, wireType);
242
- }
243
- break;
244
- case LAST_TRADE_FIELDS.SSBOE:
245
- if (wireType === 0) {
246
- [result.ssboe, offset] = readVarint(buffer, offset);
247
- } else {
248
- offset = skipField(buffer, offset, wireType);
249
- }
250
- break;
251
- case LAST_TRADE_FIELDS.USECS:
252
- if (wireType === 0) {
253
- [result.usecs, offset] = readVarint(buffer, offset);
254
- } else {
255
- offset = skipField(buffer, offset, wireType);
256
- }
257
- break;
258
- default:
259
- offset = skipField(buffer, offset, wireType);
260
- }
261
- } catch {
262
- break;
263
- }
264
- }
265
-
266
- // Debug: log all fields found in first messages
267
- if (debugAllFieldsLogged < 2 && result.tradePrice > 0) {
268
- console.log(`[DECODER:LastTrade] ALL FIELDS: ${JSON.stringify(allFields)}`);
269
- console.log(`[DECODER:LastTrade] Result: price=${result.tradePrice}, aggressor=${result.aggressor}`);
270
- debugAllFieldsLogged++;
271
- }
272
-
273
- return result;
274
- }
275
-
276
- /**
277
- * Decode BestBidOffer message (template_id = 151)
278
- */
279
- function decodeBestBidOffer(buffer) {
280
- const result = {};
281
- let offset = 0;
282
-
283
- while (offset < buffer.length) {
284
- try {
285
- const [tag, newOffset] = readVarint(buffer, offset);
286
- const fieldNumber = tag >>> 3;
287
- const wireType = tag & 0x7;
288
- offset = newOffset;
289
-
290
- switch (fieldNumber) {
291
- case BBO_FIELDS.TEMPLATE_ID:
292
- [result.templateId, offset] = readVarint(buffer, offset);
293
- break;
294
- case BBO_FIELDS.SYMBOL:
295
- if (wireType === 2) {
296
- [result.symbol, offset] = readLengthDelimited(buffer, offset);
297
- } else {
298
- offset = skipField(buffer, offset, wireType);
299
- }
300
- break;
301
- case BBO_FIELDS.EXCHANGE:
302
- if (wireType === 2) {
303
- [result.exchange, offset] = readLengthDelimited(buffer, offset);
304
- } else {
305
- offset = skipField(buffer, offset, wireType);
306
- }
307
- break;
308
- case BBO_FIELDS.BID_PRICE:
309
- if (wireType === 1) {
310
- result.bidPrice = buffer.readDoubleLE(offset);
311
- offset += 8;
312
- } else {
313
- offset = skipField(buffer, offset, wireType);
314
- }
315
- break;
316
- case BBO_FIELDS.BID_SIZE:
317
- if (wireType === 0) {
318
- [result.bidSize, offset] = readVarint(buffer, offset);
319
- } else {
320
- offset = skipField(buffer, offset, wireType);
321
- }
322
- break;
323
- case BBO_FIELDS.ASK_PRICE:
324
- if (wireType === 1) {
325
- result.askPrice = buffer.readDoubleLE(offset);
326
- offset += 8;
327
- } else {
328
- offset = skipField(buffer, offset, wireType);
329
- }
330
- break;
331
- case BBO_FIELDS.ASK_SIZE:
332
- if (wireType === 0) {
333
- [result.askSize, offset] = readVarint(buffer, offset);
334
- } else {
335
- offset = skipField(buffer, offset, wireType);
336
- }
337
- break;
338
- case BBO_FIELDS.SSBOE:
339
- if (wireType === 0) {
340
- [result.ssboe, offset] = readVarint(buffer, offset);
341
- } else {
342
- offset = skipField(buffer, offset, wireType);
343
- }
344
- break;
345
- case BBO_FIELDS.USECS:
346
- if (wireType === 0) {
347
- [result.usecs, offset] = readVarint(buffer, offset);
348
- } else {
349
- offset = skipField(buffer, offset, wireType);
350
- }
351
- break;
352
- default:
353
- offset = skipField(buffer, offset, wireType);
354
- }
355
- } catch {
356
- break;
357
- }
358
- }
359
-
360
- return result;
361
- }
362
-
363
- /**
364
- * Decode AccountPnLPositionUpdate message (template_id = 450)
365
- */
366
- function decodeAccountPnL(buffer) {
367
- const result = {};
368
- let offset = 0;
369
-
370
- while (offset < buffer.length) {
371
- try {
372
- const [tag, newOffset] = readVarint(buffer, offset);
373
- const fieldNumber = tag >>> 3;
374
- const wireType = tag & 0x7;
375
- offset = newOffset;
376
-
377
- switch (fieldNumber) {
378
- case ACCOUNT_PNL_FIELDS.TEMPLATE_ID:
379
- [result.templateId, offset] = readVarint(buffer, offset);
380
- break;
381
- case ACCOUNT_PNL_FIELDS.IS_SNAPSHOT:
382
- const [isSnap, snapOffset] = readVarint(buffer, offset);
383
- result.isSnapshot = isSnap !== 0;
384
- offset = snapOffset;
385
- break;
386
- case ACCOUNT_PNL_FIELDS.FCM_ID:
387
- [result.fcmId, offset] = readLengthDelimited(buffer, offset);
388
- break;
389
- case ACCOUNT_PNL_FIELDS.IB_ID:
390
- [result.ibId, offset] = readLengthDelimited(buffer, offset);
391
- break;
392
- case ACCOUNT_PNL_FIELDS.ACCOUNT_ID:
393
- [result.accountId, offset] = readLengthDelimited(buffer, offset);
394
- break;
395
- case ACCOUNT_PNL_FIELDS.FILL_BUY_QTY:
396
- [result.fillBuyQty, offset] = readVarint(buffer, offset);
397
- break;
398
- case ACCOUNT_PNL_FIELDS.FILL_SELL_QTY:
399
- [result.fillSellQty, offset] = readVarint(buffer, offset);
400
- break;
401
- case ACCOUNT_PNL_FIELDS.BUY_QTY:
402
- [result.buyQty, offset] = readVarint(buffer, offset);
403
- break;
404
- case ACCOUNT_PNL_FIELDS.SELL_QTY:
405
- [result.sellQty, offset] = readVarint(buffer, offset);
406
- break;
407
- case ACCOUNT_PNL_FIELDS.ACCOUNT_BALANCE:
408
- [result.accountBalance, offset] = readLengthDelimited(buffer, offset);
409
- break;
410
- case ACCOUNT_PNL_FIELDS.CASH_ON_HAND:
411
- [result.cashOnHand, offset] = readLengthDelimited(buffer, offset);
412
- break;
413
- case ACCOUNT_PNL_FIELDS.MARGIN_BALANCE:
414
- [result.marginBalance, offset] = readLengthDelimited(buffer, offset);
415
- break;
416
- case ACCOUNT_PNL_FIELDS.MIN_MARGIN_BALANCE:
417
- [result.minMarginBalance, offset] = readLengthDelimited(buffer, offset);
418
- break;
419
- case ACCOUNT_PNL_FIELDS.AVAILABLE_BUYING_POWER:
420
- [result.availableBuyingPower, offset] = readLengthDelimited(buffer, offset);
421
- break;
422
- case ACCOUNT_PNL_FIELDS.USED_BUYING_POWER:
423
- [result.usedBuyingPower, offset] = readLengthDelimited(buffer, offset);
424
- break;
425
- case ACCOUNT_PNL_FIELDS.OPEN_POSITION_PNL:
426
- [result.openPositionPnl, offset] = readLengthDelimited(buffer, offset);
427
- break;
428
- case ACCOUNT_PNL_FIELDS.OPEN_POSITION_QUANTITY:
429
- [result.openPositionQuantity, offset] = readVarint(buffer, offset);
430
- break;
431
- case ACCOUNT_PNL_FIELDS.CLOSED_POSITION_PNL:
432
- [result.closedPositionPnl, offset] = readLengthDelimited(buffer, offset);
433
- break;
434
- case ACCOUNT_PNL_FIELDS.CLOSED_POSITION_QUANTITY:
435
- [result.closedPositionQuantity, offset] = readVarint(buffer, offset);
436
- break;
437
- case ACCOUNT_PNL_FIELDS.NET_QUANTITY:
438
- [result.netQuantity, offset] = readVarint(buffer, offset);
439
- break;
440
- case ACCOUNT_PNL_FIELDS.DAY_OPEN_PNL:
441
- [result.dayOpenPnl, offset] = readLengthDelimited(buffer, offset);
442
- break;
443
- case ACCOUNT_PNL_FIELDS.DAY_CLOSED_PNL:
444
- [result.dayClosedPnl, offset] = readLengthDelimited(buffer, offset);
445
- break;
446
- case ACCOUNT_PNL_FIELDS.DAY_PNL:
447
- [result.dayPnl, offset] = readLengthDelimited(buffer, offset);
448
- break;
449
- case ACCOUNT_PNL_FIELDS.PERCENT_MAX_LOSS:
450
- [result.percentMaxLoss, offset] = readLengthDelimited(buffer, offset);
451
- break;
452
- case ACCOUNT_PNL_FIELDS.SSBOE:
453
- [result.ssboe, offset] = readVarint(buffer, offset);
454
- break;
455
- case ACCOUNT_PNL_FIELDS.USECS:
456
- [result.usecs, offset] = readVarint(buffer, offset);
457
- break;
458
- default:
459
- offset = skipField(buffer, offset, wireType);
460
- }
461
- } catch {
462
- break;
463
- }
464
- }
465
-
466
- return result;
467
- }
468
-
469
- /**
470
- * Decode InstrumentPnLPositionUpdate message (template_id = 451)
471
- */
472
- function decodeInstrumentPnL(buffer) {
473
- const result = {};
474
- let offset = 0;
475
-
476
- while (offset < buffer.length) {
477
- try {
478
- const [tag, newOffset] = readVarint(buffer, offset);
479
- const fieldNumber = tag >>> 3;
480
- const wireType = tag & 0x7;
481
- offset = newOffset;
482
-
483
- switch (fieldNumber) {
484
- case INSTRUMENT_PNL_FIELDS.TEMPLATE_ID:
485
- [result.templateId, offset] = readVarint(buffer, offset);
486
- break;
487
- case INSTRUMENT_PNL_FIELDS.IS_SNAPSHOT:
488
- const [isSnap, snapOffset] = readVarint(buffer, offset);
489
- result.isSnapshot = isSnap !== 0;
490
- offset = snapOffset;
491
- break;
492
- case INSTRUMENT_PNL_FIELDS.FCM_ID:
493
- [result.fcmId, offset] = readLengthDelimited(buffer, offset);
494
- break;
495
- case INSTRUMENT_PNL_FIELDS.IB_ID:
496
- [result.ibId, offset] = readLengthDelimited(buffer, offset);
497
- break;
498
- case INSTRUMENT_PNL_FIELDS.ACCOUNT_ID:
499
- [result.accountId, offset] = readLengthDelimited(buffer, offset);
500
- break;
501
- case INSTRUMENT_PNL_FIELDS.SYMBOL:
502
- [result.symbol, offset] = readLengthDelimited(buffer, offset);
503
- break;
504
- case INSTRUMENT_PNL_FIELDS.EXCHANGE:
505
- [result.exchange, offset] = readLengthDelimited(buffer, offset);
506
- break;
507
- case INSTRUMENT_PNL_FIELDS.PRODUCT_CODE:
508
- [result.productCode, offset] = readLengthDelimited(buffer, offset);
509
- break;
510
- case INSTRUMENT_PNL_FIELDS.INSTRUMENT_TYPE:
511
- [result.instrumentType, offset] = readLengthDelimited(buffer, offset);
512
- break;
513
- case INSTRUMENT_PNL_FIELDS.FILL_BUY_QTY:
514
- [result.fillBuyQty, offset] = readVarint(buffer, offset);
515
- break;
516
- case INSTRUMENT_PNL_FIELDS.FILL_SELL_QTY:
517
- [result.fillSellQty, offset] = readVarint(buffer, offset);
518
- break;
519
- case INSTRUMENT_PNL_FIELDS.BUY_QTY:
520
- [result.buyQty, offset] = readVarint(buffer, offset);
521
- break;
522
- case INSTRUMENT_PNL_FIELDS.SELL_QTY:
523
- [result.sellQty, offset] = readVarint(buffer, offset);
524
- break;
525
- case INSTRUMENT_PNL_FIELDS.AVG_OPEN_FILL_PRICE:
526
- if (wireType === 1) {
527
- result.avgOpenFillPrice = buffer.readDoubleLE(offset);
528
- offset += 8;
529
- } else {
530
- offset = skipField(buffer, offset, wireType);
531
- }
532
- break;
533
- case INSTRUMENT_PNL_FIELDS.OPEN_POSITION_PNL:
534
- [result.openPositionPnl, offset] = readLengthDelimited(buffer, offset);
535
- break;
536
- case INSTRUMENT_PNL_FIELDS.OPEN_POSITION_QUANTITY:
537
- [result.openPositionQuantity, offset] = readVarint(buffer, offset);
538
- break;
539
- case INSTRUMENT_PNL_FIELDS.CLOSED_POSITION_PNL:
540
- [result.closedPositionPnl, offset] = readLengthDelimited(buffer, offset);
541
- break;
542
- case INSTRUMENT_PNL_FIELDS.CLOSED_POSITION_QUANTITY:
543
- [result.closedPositionQuantity, offset] = readVarint(buffer, offset);
544
- break;
545
- case INSTRUMENT_PNL_FIELDS.NET_QUANTITY:
546
- [result.netQuantity, offset] = readVarint(buffer, offset);
547
- break;
548
- case INSTRUMENT_PNL_FIELDS.DAY_OPEN_PNL:
549
- if (wireType === 1) {
550
- result.dayOpenPnl = buffer.readDoubleLE(offset);
551
- offset += 8;
552
- } else {
553
- offset = skipField(buffer, offset, wireType);
554
- }
555
- break;
556
- case INSTRUMENT_PNL_FIELDS.DAY_CLOSED_PNL:
557
- if (wireType === 1) {
558
- result.dayClosedPnl = buffer.readDoubleLE(offset);
559
- offset += 8;
560
- } else {
561
- offset = skipField(buffer, offset, wireType);
562
- }
563
- break;
564
- case INSTRUMENT_PNL_FIELDS.DAY_PNL:
565
- if (wireType === 1) {
566
- result.dayPnl = buffer.readDoubleLE(offset);
567
- offset += 8;
568
- } else {
569
- offset = skipField(buffer, offset, wireType);
570
- }
571
- break;
572
- case INSTRUMENT_PNL_FIELDS.SSBOE:
573
- [result.ssboe, offset] = readVarint(buffer, offset);
574
- break;
575
- case INSTRUMENT_PNL_FIELDS.USECS:
576
- [result.usecs, offset] = readVarint(buffer, offset);
577
- break;
578
- default:
579
- offset = skipField(buffer, offset, wireType);
580
- }
581
- } catch {
582
- break;
583
- }
584
- }
585
-
586
- return result;
587
- }
588
-
589
- module.exports = {
590
- getTemplateId,
591
- decodeLastTrade,
592
- decodeBestBidOffer,
593
- decodeAccountPnL,
594
- decodeInstrumentPnL,
595
- LAST_TRADE_FIELDS,
596
- BBO_FIELDS,
597
- ACCOUNT_PNL_FIELDS,
598
- INSTRUMENT_PNL_FIELDS,
599
- };
package/src/lib/m/s2.js DELETED
@@ -1,34 +0,0 @@
1
- /**
2
- * M2 Module
3
- */
4
- const EventEmitter = require('events');
5
- const DP = { et: 0.6, vrng: [0.8, 2.5], cth: 0.45, mxl: 3, stk: 6, tgt: 8, ctr: 1, roc: true, mht: 10000, cd: 30000 };
6
-
7
- class S2 extends EventEmitter {
8
- constructor(cfg = {}) { super(); this.p = { ...DP, ...cfg }; this.ts = cfg.tickSize || 0.25; this.cid = null; this.bh = []; this.cd = { v: 0, cv: 0, tpv: 0, sd: 0 }; this.of = { bd: 0, ad: 0, lt: null }; this.st = { s: 0, t: 0, w: 0, l: 0, pnl: 0 }; this.lst = 0; this.lss = 0; }
9
- // tickSize for tick calculations, P&L comes from API
10
- initialize(c, ts = 0.25) { this.cid = c; this.ts = ts; this.bh = []; this.cd = { v: 0, cv: 0, tpv: 0, sd: 0 }; this.of = { bd: 0, ad: 0, lt: null }; }
11
- processTick(t) { const { price, volume, side, timestamp } = t; this.bh.push({ p: price, v: volume || 1, s: side, t: timestamp || Date.now() }); if (this.bh.length > 500) this.bh.shift(); const tp = price; this.cd.cv += volume || 1; this.cd.tpv += tp * (volume || 1); if (this.cd.cv > 0) this.cd.v = this.cd.tpv / this.cd.cv; if (side === 'buy') this.of.bd += volume || 1; else if (side === 'sell') this.of.ad += volume || 1; this.of.lt = side; }
12
- onTick(t) { return this.processTick(t); }
13
- onTrade(t) { this.processTick({ price: t.price, volume: t.size || t.volume || 1, side: t.side, timestamp: t.timestamp }); }
14
- generateSignal(cp) { if (this.bh.length < 50) return null; if (Date.now() - this.lst < this.p.cd) return null; if (this.lss >= this.p.mxl) return null; const v = this.cd.v; if (v <= 0) return null; const sd = this.csd(); if (sd <= 0) return null; const dv = (cp - v) / sd; const ad = Math.abs(dv); if (ad < this.p.vrng[0] || ad > this.p.vrng[1]) return null; let dir = null; if (dv < -this.p.et) dir = 'long'; else if (dv > this.p.et) dir = 'short'; if (!dir) return null; if (this.p.roc) { const ofc = this.cof(dir); if (!ofc) return null; } const cf = Math.min(1.0, 0.5 + ad * 0.15); if (cf < this.p.cth) return null; this.lst = Date.now(); this.st.s++; return { direction: dir, confidence: cf, zScore: dv, stopTicks: this.p.stk, targetTicks: this.p.tgt }; }
15
- csd() { if (this.bh.length < 20) return 0; const ps = this.bh.slice(-100).map(b => b.p); const m = ps.reduce((a, b) => a + b, 0) / ps.length; const va = ps.reduce((s, p) => s + Math.pow(p - m, 2), 0) / ps.length; return Math.sqrt(va); }
16
- cof(dir) { const tb = this.of.bd + this.of.ad; if (tb < 10) return true; const br = this.of.bd / tb; if (dir === 'long' && br > 0.45) return true; if (dir === 'short' && br < 0.55) return true; if (this.of.lt === 'buy' && dir === 'long') return true; if (this.of.lt === 'sell' && dir === 'short') return true; return false; }
17
- recordTradeResult(pnl) { this.st.t++; this.st.pnl += pnl; if (pnl > 0) { this.st.w++; this.lss = 0; } else { this.st.l++; this.lss++; } }
18
- getStats() { return this.st; }
19
- reset() { this.bh = []; this.cd = { v: 0, cv: 0, tpv: 0, sd: 0 }; this.of = { bd: 0, ad: 0, lt: null }; this.lss = 0; }
20
- }
21
-
22
- class M2 extends EventEmitter {
23
- constructor(cfg = {}) { super(); this.s = new S2(cfg); }
24
- initialize(c, ts, tv) { this.s.initialize(c, ts, tv); }
25
- processTick(t) { this.s.processTick(t); }
26
- onTick(t) { this.s.onTick(t); const sig = this.s.generateSignal(t.price); if (sig) this.emit('signal', { side: sig.direction === 'long' ? 'buy' : 'sell', action: 'open', ...sig }); }
27
- onTrade(t) { this.s.onTrade(t); }
28
- generateSignal(p) { return this.s.generateSignal(p); }
29
- recordTradeResult(p) { this.s.recordTradeResult(p); }
30
- getStats() { return this.s.getStats(); }
31
- reset() { this.s.reset(); this.emit('log', { type: 'info', message: 'Reset' }); }
32
- }
33
-
34
- module.exports = { S2, M2, DP };