hedgequantx 2.6.17 → 2.6.18

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedgequantx",
3
- "version": "2.6.17",
3
+ "version": "2.6.18",
4
4
  "description": "HedgeQuantX - Prop Futures Trading CLI",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -170,8 +170,9 @@ const showStats = async (service) => {
170
170
  } catch (e) {}
171
171
  }
172
172
 
173
- // ===== TRADE HISTORY (from API - ProjectX only) =====
174
- // Rithmic doesn't have getTradeHistory - returns empty array
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);
@@ -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,27 @@ 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
- async getTradeHistory() { return { success: true, trades: [] }; }
410
+
411
+ /**
412
+ * Get trade history from completed fills
413
+ * Note: Rithmic doesn't provide historical P&L per trade via API
414
+ * We track fills in real-time during the session
415
+ * @param {string} accountId - Optional account filter
416
+ * @param {number} days - Not used for Rithmic (session-only data)
417
+ * @returns {Promise<{success: boolean, trades: Array}>}
418
+ */
419
+ async getTradeHistory(accountId, days) {
420
+ // Filter by account if specified
421
+ let trades = this.completedTrades;
422
+ if (accountId) {
423
+ trades = trades.filter(t => t.accountId === accountId);
424
+ }
425
+
426
+ // Sort by timestamp descending (newest first)
427
+ trades = [...trades].sort((a, b) => b.timestamp - a.timestamp);
428
+
429
+ return { success: true, trades };
430
+ }
336
431
 
337
432
  async getMarketStatus() {
338
433
  const status = this.checkMarketHours();
@@ -615,6 +710,8 @@ class RithmicService extends EventEmitter {
615
710
  this.accountPnL.clear();
616
711
  this.positions.clear();
617
712
  this.orders = [];
713
+ this.completedTrades = [];
714
+ this._openEntries = null;
618
715
  this.loginInfo = null;
619
716
  this.user = null;
620
717
  this.credentials = null;