hedgequantx 2.6.18 → 2.6.19
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/package.json
CHANGED
|
@@ -196,10 +196,28 @@ const createOrderHandler = (service) => {
|
|
|
196
196
|
debug('Handling ORDER_NOTIFICATION (351)');
|
|
197
197
|
handleOrderNotification(service, data);
|
|
198
198
|
break;
|
|
199
|
+
case RES.SHOW_ORDER_HISTORY:
|
|
200
|
+
debug('Handling SHOW_ORDER_HISTORY (325)');
|
|
201
|
+
handleOrderHistoryResponse(service, data);
|
|
202
|
+
break;
|
|
199
203
|
}
|
|
200
204
|
};
|
|
201
205
|
};
|
|
202
206
|
|
|
207
|
+
/**
|
|
208
|
+
* Handle order history response (325) - signals end of history snapshot
|
|
209
|
+
*/
|
|
210
|
+
const handleOrderHistoryResponse = (service, data) => {
|
|
211
|
+
try {
|
|
212
|
+
const res = proto.decode('ResponseShowOrderHistory', data);
|
|
213
|
+
debug('Order history complete:', { userMsg: res.userMsg, rpCode: res.rpCode });
|
|
214
|
+
service.emit('orderHistoryComplete', { userMsg: res.userMsg, rpCode: res.rpCode });
|
|
215
|
+
} catch (e) {
|
|
216
|
+
debug('Error decoding order history response:', e.message);
|
|
217
|
+
service.emit('orderHistoryComplete', {});
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
203
221
|
/**
|
|
204
222
|
* Create PNL_PLANT message handler
|
|
205
223
|
* @param {RithmicService} service - The Rithmic service instance
|
|
@@ -409,24 +409,201 @@ class RithmicService extends EventEmitter {
|
|
|
409
409
|
async getDailyStats() { return { success: true, stats: [] }; }
|
|
410
410
|
|
|
411
411
|
/**
|
|
412
|
-
* Get trade history from
|
|
413
|
-
*
|
|
414
|
-
* We track fills in real-time during the session
|
|
412
|
+
* Get trade history from Rithmic API
|
|
413
|
+
* Uses RequestShowOrderHistory to fetch historical fills
|
|
415
414
|
* @param {string} accountId - Optional account filter
|
|
416
|
-
* @param {number} days -
|
|
415
|
+
* @param {number} days - Number of days to fetch (default: 30)
|
|
417
416
|
* @returns {Promise<{success: boolean, trades: Array}>}
|
|
418
417
|
*/
|
|
419
|
-
async getTradeHistory(accountId, days) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
if (accountId) {
|
|
423
|
-
trades = trades.filter(t => t.accountId === accountId);
|
|
418
|
+
async getTradeHistory(accountId, days = 30) {
|
|
419
|
+
if (!this.orderConn || !this.loginInfo) {
|
|
420
|
+
return { success: true, trades: this.completedTrades || [] };
|
|
424
421
|
}
|
|
422
|
+
|
|
423
|
+
return new Promise((resolve) => {
|
|
424
|
+
const trades = [];
|
|
425
|
+
const historyOrders = [];
|
|
426
|
+
let resolved = false;
|
|
427
|
+
|
|
428
|
+
// Timeout after 5 seconds
|
|
429
|
+
const timeout = setTimeout(() => {
|
|
430
|
+
if (!resolved) {
|
|
431
|
+
resolved = true;
|
|
432
|
+
cleanup();
|
|
433
|
+
// Combine API history with session trades
|
|
434
|
+
const allTrades = [...this._processHistoryToTrades(historyOrders), ...this.completedTrades];
|
|
435
|
+
resolve({ success: true, trades: allTrades });
|
|
436
|
+
}
|
|
437
|
+
}, 5000);
|
|
438
|
+
|
|
439
|
+
// Listen for order history snapshots (RithmicOrderNotification with is_snapshot=true)
|
|
440
|
+
const onOrderNotification = (data) => {
|
|
441
|
+
try {
|
|
442
|
+
// Check if this is a historical order (snapshot)
|
|
443
|
+
if (data.isSnapshot || data.status === 'complete' || data.status === 'Complete') {
|
|
444
|
+
const order = {
|
|
445
|
+
orderId: data.orderId || data.orderTag,
|
|
446
|
+
accountId: data.accountId,
|
|
447
|
+
symbol: data.symbol,
|
|
448
|
+
exchange: data.exchange,
|
|
449
|
+
side: data.transactionType === 1 ? 0 : 1, // 1=BUY->0, 2=SELL->1
|
|
450
|
+
quantity: data.quantity || data.totalFillQuantity || 0,
|
|
451
|
+
fillPrice: data.avgFillPrice || data.lastFillPrice || 0,
|
|
452
|
+
timestamp: data.ssboe ? data.ssboe * 1000 : Date.now(),
|
|
453
|
+
status: data.status,
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
if (order.quantity > 0 && order.fillPrice > 0) {
|
|
457
|
+
historyOrders.push(order);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
} catch (e) {
|
|
461
|
+
// Ignore parse errors
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// Listen for history complete response
|
|
466
|
+
const onHistoryComplete = () => {
|
|
467
|
+
if (!resolved) {
|
|
468
|
+
resolved = true;
|
|
469
|
+
cleanup();
|
|
470
|
+
const allTrades = [...this._processHistoryToTrades(historyOrders), ...this.completedTrades];
|
|
471
|
+
resolve({ success: true, trades: allTrades });
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
const cleanup = () => {
|
|
476
|
+
clearTimeout(timeout);
|
|
477
|
+
this.removeListener('orderNotification', onOrderNotification);
|
|
478
|
+
this.removeListener('orderHistoryComplete', onHistoryComplete);
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
this.on('orderNotification', onOrderNotification);
|
|
482
|
+
this.on('orderHistoryComplete', onHistoryComplete);
|
|
483
|
+
|
|
484
|
+
// Request order history for each account
|
|
485
|
+
try {
|
|
486
|
+
const accounts = accountId
|
|
487
|
+
? this.accounts.filter(a => a.accountId === accountId)
|
|
488
|
+
: this.accounts;
|
|
489
|
+
|
|
490
|
+
for (const acc of accounts) {
|
|
491
|
+
this.orderConn.send('RequestShowOrderHistory', {
|
|
492
|
+
templateId: 324, // REQ.SHOW_ORDER_HISTORY
|
|
493
|
+
userMsg: ['HQX-HISTORY'],
|
|
494
|
+
fcmId: acc.fcmId || this.loginInfo.fcmId,
|
|
495
|
+
ibId: acc.ibId || this.loginInfo.ibId,
|
|
496
|
+
accountId: acc.accountId,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
} catch (e) {
|
|
500
|
+
if (!resolved) {
|
|
501
|
+
resolved = true;
|
|
502
|
+
cleanup();
|
|
503
|
+
resolve({ success: false, error: e.message, trades: this.completedTrades || [] });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Process historical orders into trades with P&L
|
|
511
|
+
* Matches entries and exits to calculate P&L
|
|
512
|
+
* @private
|
|
513
|
+
*/
|
|
514
|
+
_processHistoryToTrades(orders) {
|
|
515
|
+
const trades = [];
|
|
516
|
+
const openPositions = new Map(); // key: accountId:symbol
|
|
517
|
+
|
|
518
|
+
// Sort by timestamp (oldest first)
|
|
519
|
+
const sorted = [...orders].sort((a, b) => a.timestamp - b.timestamp);
|
|
425
520
|
|
|
426
|
-
|
|
427
|
-
|
|
521
|
+
for (const order of sorted) {
|
|
522
|
+
const key = `${order.accountId}:${order.symbol}`;
|
|
523
|
+
const open = openPositions.get(key);
|
|
524
|
+
|
|
525
|
+
if (open && open.side !== order.side) {
|
|
526
|
+
// Closing trade - calculate P&L
|
|
527
|
+
const closeQty = Math.min(order.quantity, open.quantity);
|
|
528
|
+
let pnl;
|
|
529
|
+
|
|
530
|
+
if (open.side === 0) {
|
|
531
|
+
// Was long (bought), now selling
|
|
532
|
+
pnl = (order.fillPrice - open.price) * closeQty;
|
|
533
|
+
} else {
|
|
534
|
+
// Was short (sold), now buying
|
|
535
|
+
pnl = (open.price - order.fillPrice) * closeQty;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// Estimate tick value (futures typically $12.50-$50 per tick)
|
|
539
|
+
// For indices like ES/NQ, multiply by contract multiplier
|
|
540
|
+
const tickMultiplier = this._getTickMultiplier(order.symbol);
|
|
541
|
+
pnl = pnl * tickMultiplier;
|
|
542
|
+
|
|
543
|
+
trades.push({
|
|
544
|
+
id: order.orderId,
|
|
545
|
+
accountId: order.accountId,
|
|
546
|
+
symbol: order.symbol,
|
|
547
|
+
exchange: order.exchange,
|
|
548
|
+
side: open.side,
|
|
549
|
+
size: closeQty,
|
|
550
|
+
entryPrice: open.price,
|
|
551
|
+
exitPrice: order.fillPrice,
|
|
552
|
+
price: order.fillPrice,
|
|
553
|
+
timestamp: order.timestamp,
|
|
554
|
+
creationTimestamp: new Date(order.timestamp).toISOString(),
|
|
555
|
+
status: 'CLOSED',
|
|
556
|
+
profitAndLoss: pnl,
|
|
557
|
+
pnl: pnl,
|
|
558
|
+
fees: 0,
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// Update or remove open position
|
|
562
|
+
const remaining = open.quantity - closeQty;
|
|
563
|
+
if (remaining <= 0) {
|
|
564
|
+
openPositions.delete(key);
|
|
565
|
+
} else {
|
|
566
|
+
open.quantity = remaining;
|
|
567
|
+
}
|
|
568
|
+
} else if (open && open.side === order.side) {
|
|
569
|
+
// Adding to position
|
|
570
|
+
const totalQty = open.quantity + order.quantity;
|
|
571
|
+
open.price = ((open.price * open.quantity) + (order.fillPrice * order.quantity)) / totalQty;
|
|
572
|
+
open.quantity = totalQty;
|
|
573
|
+
} else {
|
|
574
|
+
// Opening new position
|
|
575
|
+
openPositions.set(key, {
|
|
576
|
+
side: order.side,
|
|
577
|
+
quantity: order.quantity,
|
|
578
|
+
price: order.fillPrice,
|
|
579
|
+
timestamp: order.timestamp,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
}
|
|
428
583
|
|
|
429
|
-
return
|
|
584
|
+
return trades;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Get tick multiplier for P&L calculation
|
|
589
|
+
* @private
|
|
590
|
+
*/
|
|
591
|
+
_getTickMultiplier(symbol) {
|
|
592
|
+
const sym = (symbol || '').toUpperCase();
|
|
593
|
+
if (sym.startsWith('ES')) return 50; // E-mini S&P 500: $50 per point
|
|
594
|
+
if (sym.startsWith('NQ')) return 20; // E-mini Nasdaq: $20 per point
|
|
595
|
+
if (sym.startsWith('YM')) return 5; // E-mini Dow: $5 per point
|
|
596
|
+
if (sym.startsWith('RTY')) return 50; // E-mini Russell: $50 per point
|
|
597
|
+
if (sym.startsWith('MES')) return 5; // Micro E-mini S&P: $5 per point
|
|
598
|
+
if (sym.startsWith('MNQ')) return 2; // Micro E-mini Nasdaq: $2 per point
|
|
599
|
+
if (sym.startsWith('GC')) return 100; // Gold: $100 per point
|
|
600
|
+
if (sym.startsWith('SI')) return 5000; // Silver: $5000 per point
|
|
601
|
+
if (sym.startsWith('CL')) return 1000; // Crude Oil: $1000 per point
|
|
602
|
+
if (sym.startsWith('NG')) return 10000; // Natural Gas: $10000 per point
|
|
603
|
+
if (sym.startsWith('ZB') || sym.startsWith('ZN')) return 1000; // Bonds
|
|
604
|
+
if (sym.startsWith('6E')) return 125000; // Euro FX
|
|
605
|
+
if (sym.startsWith('6J')) return 12500000; // Japanese Yen
|
|
606
|
+
return 1; // Default
|
|
430
607
|
}
|
|
431
608
|
|
|
432
609
|
async getMarketStatus() {
|