hedgequantx 2.6.17 → 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
package/src/pages/stats.js
CHANGED
|
@@ -170,8 +170,9 @@ const showStats = async (service) => {
|
|
|
170
170
|
} catch (e) {}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
// ===== TRADE HISTORY (from API
|
|
174
|
-
//
|
|
173
|
+
// ===== TRADE HISTORY (from API) =====
|
|
174
|
+
// ProjectX: Historical trades from API
|
|
175
|
+
// Rithmic: Session trades tracked locally (entry/exit matching)
|
|
175
176
|
if (typeof svc.getTradeHistory === 'function') {
|
|
176
177
|
try {
|
|
177
178
|
const tradesResult = await svc.getTradeHistory(account.accountId, 30);
|
|
@@ -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
|
|
@@ -71,6 +71,7 @@ class RithmicService extends EventEmitter {
|
|
|
71
71
|
this.accountPnL = new Map();
|
|
72
72
|
this.positions = new Map();
|
|
73
73
|
this.orders = [];
|
|
74
|
+
this.completedTrades = []; // Store filled orders for trade history
|
|
74
75
|
this.user = null;
|
|
75
76
|
this.credentials = null;
|
|
76
77
|
|
|
@@ -121,6 +122,80 @@ class RithmicService extends EventEmitter {
|
|
|
121
122
|
log.warn('Failed to fetch accounts', { error: err.message });
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
// Track open entries for P&L calculation
|
|
126
|
+
this._openEntries = new Map(); // key: accountId:symbol, value: {side, qty, price}
|
|
127
|
+
|
|
128
|
+
// Listen for filled orders to build trade history
|
|
129
|
+
this.on('orderFilled', (fillInfo) => {
|
|
130
|
+
const key = `${fillInfo.accountId}:${fillInfo.symbol}`;
|
|
131
|
+
const side = fillInfo.transactionType === 1 ? 0 : 1; // 0=BUY, 1=SELL (entry side)
|
|
132
|
+
const qty = fillInfo.fillQuantity || fillInfo.totalFillQuantity || 0;
|
|
133
|
+
const price = fillInfo.avgFillPrice || fillInfo.lastFillPrice || 0;
|
|
134
|
+
|
|
135
|
+
// Check if this is closing an existing position
|
|
136
|
+
const openEntry = this._openEntries.get(key);
|
|
137
|
+
let pnl = null;
|
|
138
|
+
let isClosingTrade = false;
|
|
139
|
+
|
|
140
|
+
if (openEntry && openEntry.side !== side) {
|
|
141
|
+
// This is a closing trade (opposite side of open position)
|
|
142
|
+
isClosingTrade = true;
|
|
143
|
+
const closeQty = Math.min(qty, openEntry.qty);
|
|
144
|
+
|
|
145
|
+
// Calculate P&L based on direction
|
|
146
|
+
// If we bought (side=0), we sell to close - profit if close price > entry price
|
|
147
|
+
// If we sold (side=1), we buy to close - profit if entry price > close price
|
|
148
|
+
if (openEntry.side === 0) {
|
|
149
|
+
// Was long, selling to close
|
|
150
|
+
pnl = (price - openEntry.price) * closeQty;
|
|
151
|
+
} else {
|
|
152
|
+
// Was short, buying to close
|
|
153
|
+
pnl = (openEntry.price - price) * closeQty;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Update or remove open entry
|
|
157
|
+
const remainingQty = openEntry.qty - closeQty;
|
|
158
|
+
if (remainingQty <= 0) {
|
|
159
|
+
this._openEntries.delete(key);
|
|
160
|
+
} else {
|
|
161
|
+
openEntry.qty = remainingQty;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Store the closing trade with P&L
|
|
165
|
+
this.completedTrades.push({
|
|
166
|
+
id: fillInfo.orderId || fillInfo.orderTag,
|
|
167
|
+
orderTag: fillInfo.orderTag,
|
|
168
|
+
accountId: fillInfo.accountId,
|
|
169
|
+
symbol: fillInfo.symbol,
|
|
170
|
+
exchange: fillInfo.exchange,
|
|
171
|
+
side: openEntry.side, // Original entry side (for stats: LONG or SHORT)
|
|
172
|
+
size: closeQty,
|
|
173
|
+
entryPrice: openEntry.price,
|
|
174
|
+
exitPrice: price,
|
|
175
|
+
price: price,
|
|
176
|
+
timestamp: fillInfo.localTimestamp || Date.now(),
|
|
177
|
+
creationTimestamp: new Date().toISOString(),
|
|
178
|
+
status: 'CLOSED',
|
|
179
|
+
profitAndLoss: pnl,
|
|
180
|
+
pnl: pnl,
|
|
181
|
+
fees: 0,
|
|
182
|
+
});
|
|
183
|
+
log.debug('Trade closed', { symbol: fillInfo.symbol, pnl, trades: this.completedTrades.length });
|
|
184
|
+
} else {
|
|
185
|
+
// This is opening a new position or adding to existing
|
|
186
|
+
if (openEntry && openEntry.side === side) {
|
|
187
|
+
// Adding to position - update average price
|
|
188
|
+
const totalQty = openEntry.qty + qty;
|
|
189
|
+
openEntry.price = ((openEntry.price * openEntry.qty) + (price * qty)) / totalQty;
|
|
190
|
+
openEntry.qty = totalQty;
|
|
191
|
+
} else {
|
|
192
|
+
// New position
|
|
193
|
+
this._openEntries.set(key, { side, qty, price, timestamp: Date.now() });
|
|
194
|
+
}
|
|
195
|
+
log.debug('Position opened/added', { symbol: fillInfo.symbol, side, qty, price });
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
124
199
|
// Store credentials for reconnection
|
|
125
200
|
this.credentials = { username, password };
|
|
126
201
|
|
|
@@ -332,7 +407,204 @@ class RithmicService extends EventEmitter {
|
|
|
332
407
|
async getUser() { return this.user; }
|
|
333
408
|
async getLifetimeStats() { return { success: true, stats: null }; }
|
|
334
409
|
async getDailyStats() { return { success: true, stats: [] }; }
|
|
335
|
-
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Get trade history from Rithmic API
|
|
413
|
+
* Uses RequestShowOrderHistory to fetch historical fills
|
|
414
|
+
* @param {string} accountId - Optional account filter
|
|
415
|
+
* @param {number} days - Number of days to fetch (default: 30)
|
|
416
|
+
* @returns {Promise<{success: boolean, trades: Array}>}
|
|
417
|
+
*/
|
|
418
|
+
async getTradeHistory(accountId, days = 30) {
|
|
419
|
+
if (!this.orderConn || !this.loginInfo) {
|
|
420
|
+
return { success: true, trades: this.completedTrades || [] };
|
|
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);
|
|
520
|
+
|
|
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
|
+
}
|
|
583
|
+
|
|
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
|
|
607
|
+
}
|
|
336
608
|
|
|
337
609
|
async getMarketStatus() {
|
|
338
610
|
const status = this.checkMarketHours();
|
|
@@ -615,6 +887,8 @@ class RithmicService extends EventEmitter {
|
|
|
615
887
|
this.accountPnL.clear();
|
|
616
888
|
this.positions.clear();
|
|
617
889
|
this.orders = [];
|
|
890
|
+
this.completedTrades = [];
|
|
891
|
+
this._openEntries = null;
|
|
618
892
|
this.loginInfo = null;
|
|
619
893
|
this.user = null;
|
|
620
894
|
this.credentials = null;
|