hedgequantx 1.8.32 → 1.8.33

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": "1.8.32",
3
+ "version": "1.8.33",
4
4
  "description": "Prop Futures Algo Trading CLI - Connect to Topstep, Alpha Futures, and other prop firms",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -58,6 +58,8 @@ const REQ = {
58
58
  SYSTEM_INFO: 16,
59
59
  HEARTBEAT: 18,
60
60
  MARKET_DATA: 100,
61
+ PRODUCT_CODES: 111,
62
+ FRONT_MONTH_CONTRACT: 113,
61
63
  LOGIN_INFO: 300,
62
64
  ACCOUNT_LIST: 302,
63
65
  ACCOUNT_RMS: 304,
@@ -84,6 +86,8 @@ const RES = {
84
86
  SYSTEM_INFO: 17,
85
87
  HEARTBEAT: 19,
86
88
  MARKET_DATA: 101,
89
+ PRODUCT_CODES: 112,
90
+ FRONT_MONTH_CONTRACT: 114,
87
91
  LOGIN_INFO: 301,
88
92
  ACCOUNT_LIST: 303,
89
93
  ACCOUNT_RMS: 305,
@@ -155,6 +159,10 @@ const PROTO_FILES = [
155
159
  'response_pnl_position_updates.proto',
156
160
  'account_pnl_position_update.proto',
157
161
  'instrument_pnl_position_update.proto',
162
+ 'request_product_codes.proto',
163
+ 'response_product_codes.proto',
164
+ 'request_front_month_contract.proto',
165
+ 'response_front_month_contract.proto',
158
166
  ];
159
167
 
160
168
  module.exports = {
@@ -9,11 +9,49 @@ const { RITHMIC_ENDPOINTS, RITHMIC_SYSTEMS } = require('./constants');
9
9
  const { createOrderHandler, createPnLHandler } = require('./handlers');
10
10
  const { fetchAccounts, getTradingAccounts, requestPnLSnapshot, subscribePnLUpdates, getPositions, hashAccountId } = require('./accounts');
11
11
  const { placeOrder, cancelOrder, getOrders, getOrderHistory, closePosition } = require('./orders');
12
+ const { decodeFrontMonthContract } = require('./protobuf');
12
13
 
13
14
  // Debug mode
14
15
  const DEBUG = process.env.HQX_DEBUG === '1';
15
16
  const debug = (...args) => DEBUG && console.log('[Rithmic:Service]', ...args);
16
17
 
18
+ // Base symbols for futures contracts
19
+ const BASE_SYMBOLS = {
20
+ // Equity Index (Quarterly: H, M, U, Z)
21
+ quarterly: [
22
+ { base: 'ES', name: 'E-mini S&P 500', exchange: 'CME', category: 'Index' },
23
+ { base: 'NQ', name: 'E-mini NASDAQ-100', exchange: 'CME', category: 'Index' },
24
+ { base: 'YM', name: 'E-mini Dow Jones', exchange: 'CBOT', category: 'Index' },
25
+ { base: 'RTY', name: 'E-mini Russell 2000', exchange: 'CME', category: 'Index' },
26
+ { base: 'MES', name: 'Micro E-mini S&P 500', exchange: 'CME', category: 'Micro Index' },
27
+ { base: 'MNQ', name: 'Micro E-mini NASDAQ-100', exchange: 'CME', category: 'Micro Index' },
28
+ { base: 'MYM', name: 'Micro E-mini Dow Jones', exchange: 'CBOT', category: 'Micro Index' },
29
+ { base: 'M2K', name: 'Micro E-mini Russell 2000', exchange: 'CME', category: 'Micro Index' },
30
+ // Currencies (Quarterly)
31
+ { base: '6E', name: 'Euro FX', exchange: 'CME', category: 'Currency' },
32
+ { base: 'M6E', name: 'Micro Euro FX', exchange: 'CME', category: 'Currency' },
33
+ { base: '6B', name: 'British Pound', exchange: 'CME', category: 'Currency' },
34
+ { base: '6J', name: 'Japanese Yen', exchange: 'CME', category: 'Currency' },
35
+ { base: '6A', name: 'Australian Dollar', exchange: 'CME', category: 'Currency' },
36
+ { base: '6C', name: 'Canadian Dollar', exchange: 'CME', category: 'Currency' },
37
+ // Bonds (Quarterly)
38
+ { base: 'ZB', name: '30-Year T-Bond', exchange: 'CBOT', category: 'Bonds' },
39
+ { base: 'ZN', name: '10-Year T-Note', exchange: 'CBOT', category: 'Bonds' },
40
+ { base: 'ZF', name: '5-Year T-Note', exchange: 'CBOT', category: 'Bonds' },
41
+ { base: 'ZT', name: '2-Year T-Note', exchange: 'CBOT', category: 'Bonds' },
42
+ ],
43
+ // Energy & Metals (Monthly)
44
+ monthly: [
45
+ { base: 'CL', name: 'Crude Oil WTI', exchange: 'NYMEX', category: 'Energy' },
46
+ { base: 'MCL', name: 'Micro Crude Oil', exchange: 'NYMEX', category: 'Energy' },
47
+ { base: 'NG', name: 'Natural Gas', exchange: 'NYMEX', category: 'Energy' },
48
+ { base: 'GC', name: 'Gold', exchange: 'COMEX', category: 'Metals' },
49
+ { base: 'MGC', name: 'Micro Gold', exchange: 'COMEX', category: 'Metals' },
50
+ { base: 'SI', name: 'Silver', exchange: 'COMEX', category: 'Metals' },
51
+ { base: 'HG', name: 'Copper', exchange: 'COMEX', category: 'Metals' },
52
+ ],
53
+ };
54
+
17
55
  // PropFirm configurations - NO FAKE DATA
18
56
  const PROPFIRM_CONFIGS = {
19
57
  'apex': { name: 'Apex Trader Funding', systemName: 'Apex', gateway: RITHMIC_ENDPOINTS.CHICAGO },
@@ -45,6 +83,7 @@ class RithmicService extends EventEmitter {
45
83
  };
46
84
  this.orderConn = null;
47
85
  this.pnlConn = null;
86
+ this.tickerConn = null; // TICKER_PLANT for symbol lookup
48
87
  this.loginInfo = null;
49
88
  this.accounts = [];
50
89
  this.accountPnL = new Map();
@@ -52,6 +91,7 @@ class RithmicService extends EventEmitter {
52
91
  this.orders = [];
53
92
  this.user = null;
54
93
  this.credentials = null;
94
+ this.cachedContracts = null; // Cache contracts to avoid repeated API calls
55
95
  }
56
96
 
57
97
  /**
@@ -208,53 +248,165 @@ class RithmicService extends EventEmitter {
208
248
  }
209
249
 
210
250
  /**
211
- * Get contracts from Rithmic
212
- * TODO: Implement TICKER_PLANT connection to fetch real contracts
213
- * For now, returns common futures contracts that are available on Rithmic
251
+ * Connect to TICKER_PLANT for symbol lookup
252
+ */
253
+ async connectTicker(username, password) {
254
+ try {
255
+ this.tickerConn = new RithmicConnection();
256
+ const gateway = this.propfirm.gateway || RITHMIC_ENDPOINTS.CHICAGO;
257
+
258
+ const config = {
259
+ uri: gateway,
260
+ systemName: this.propfirm.systemName,
261
+ userId: username,
262
+ password: password,
263
+ appName: 'HQX-CLI',
264
+ appVersion: '1.0.0',
265
+ };
266
+
267
+ await this.tickerConn.connect(config);
268
+
269
+ return new Promise((resolve) => {
270
+ const timeout = setTimeout(() => {
271
+ debug('TICKER_PLANT login timeout');
272
+ resolve(false);
273
+ }, 10000);
274
+
275
+ this.tickerConn.once('loggedIn', () => {
276
+ clearTimeout(timeout);
277
+ debug('TICKER_PLANT connected');
278
+ resolve(true);
279
+ });
280
+
281
+ this.tickerConn.once('loginFailed', () => {
282
+ clearTimeout(timeout);
283
+ debug('TICKER_PLANT login failed');
284
+ resolve(false);
285
+ });
286
+
287
+ this.tickerConn.login('TICKER_PLANT');
288
+ });
289
+ } catch (e) {
290
+ debug('TICKER_PLANT connection error:', e.message);
291
+ return false;
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Get front month contract from Rithmic API
297
+ */
298
+ async getFrontMonth(baseSymbol, exchange) {
299
+ if (!this.tickerConn) {
300
+ if (!this.credentials) {
301
+ throw new Error('Not logged in - cannot fetch front month');
302
+ }
303
+ const connected = await this.connectTicker(this.credentials.username, this.credentials.password);
304
+ if (!connected) {
305
+ throw new Error('Failed to connect to TICKER_PLANT');
306
+ }
307
+ }
308
+
309
+ return new Promise((resolve, reject) => {
310
+ const timeout = setTimeout(() => {
311
+ reject(new Error(`Timeout getting front month for ${baseSymbol}`));
312
+ }, 10000);
313
+
314
+ const handler = (msg) => {
315
+ if (msg.templateId === 114) { // ResponseFrontMonthContract
316
+ const decoded = decodeFrontMonthContract(msg.data);
317
+ if (decoded.userMsg === baseSymbol) {
318
+ clearTimeout(timeout);
319
+ this.tickerConn.removeListener('message', handler);
320
+
321
+ if (decoded.rpCode[0] === '0') {
322
+ resolve({
323
+ baseSymbol: baseSymbol,
324
+ symbol: decoded.tradingSymbol || decoded.symbol,
325
+ exchange: decoded.exchange || exchange,
326
+ });
327
+ } else {
328
+ reject(new Error(`API error for ${baseSymbol}: ${decoded.rpCode.join(' ')}`));
329
+ }
330
+ }
331
+ }
332
+ };
333
+
334
+ this.tickerConn.on('message', handler);
335
+
336
+ // Send RequestFrontMonthContract (template 113)
337
+ this.tickerConn.send('RequestFrontMonthContract', {
338
+ templateId: 113,
339
+ userMsg: [baseSymbol],
340
+ symbol: baseSymbol,
341
+ exchange: exchange,
342
+ });
343
+ });
344
+ }
345
+
346
+ /**
347
+ * Get all available contracts - REAL DATA from Rithmic API
214
348
  */
215
349
  async getContracts() {
216
- // Calculate current front month dynamically
217
- const now = new Date();
218
- const month = now.getMonth();
219
- const year = now.getFullYear() % 100;
220
-
221
- // Quarterly months for index futures: H=Mar, M=Jun, U=Sep, Z=Dec
222
- const quarterlyMonths = ['H', 'M', 'U', 'Z'];
223
- const monthNames = ['Mar', 'Jun', 'Sep', 'Dec'];
224
- const quarterIndex = Math.floor(month / 3);
225
-
226
- // If past 15th of expiry month, use next quarter
227
- let frontMonth = quarterlyMonths[quarterIndex];
228
- let frontMonthName = monthNames[quarterIndex];
229
- let frontYear = year;
230
-
231
- if (now.getDate() > 15 && [2, 5, 8, 11].includes(month)) {
232
- const nextIdx = (quarterIndex + 1) % 4;
233
- frontMonth = quarterlyMonths[nextIdx];
234
- frontMonthName = monthNames[nextIdx];
235
- if (nextIdx === 0) frontYear++;
350
+ // Return cached if available and fresh (5 min cache)
351
+ if (this.cachedContracts && this.cachedContracts.timestamp > Date.now() - 300000) {
352
+ return { success: true, contracts: this.cachedContracts.data, source: 'cache' };
353
+ }
354
+
355
+ // Need credentials to fetch real data
356
+ if (!this.credentials) {
357
+ return { success: false, error: 'Not logged in - cannot fetch contracts from API' };
358
+ }
359
+
360
+ try {
361
+ // Connect to TICKER_PLANT if not connected
362
+ if (!this.tickerConn) {
363
+ const connected = await this.connectTicker(this.credentials.username, this.credentials.password);
364
+ if (!connected) {
365
+ return { success: false, error: 'Failed to connect to TICKER_PLANT' };
366
+ }
367
+ }
368
+
369
+ const contracts = [];
370
+ const allSymbols = [...BASE_SYMBOLS.quarterly, ...BASE_SYMBOLS.monthly];
371
+
372
+ debug(`Fetching front months for ${allSymbols.length} symbols...`);
373
+
374
+ // Fetch front months in parallel batches
375
+ const batchSize = 10;
376
+ for (let i = 0; i < allSymbols.length; i += batchSize) {
377
+ const batch = allSymbols.slice(i, i + batchSize);
378
+ const promises = batch.map(async (sym) => {
379
+ try {
380
+ const result = await this.getFrontMonth(sym.base, sym.exchange);
381
+ return {
382
+ symbol: result.symbol,
383
+ name: `${sym.name} (${result.symbol})`,
384
+ exchange: result.exchange,
385
+ category: sym.category,
386
+ baseSymbol: sym.base,
387
+ };
388
+ } catch (e) {
389
+ debug(`Failed to get front month for ${sym.base}: ${e.message}`);
390
+ return null;
391
+ }
392
+ });
393
+
394
+ const results = await Promise.all(promises);
395
+ contracts.push(...results.filter(r => r !== null));
396
+ }
397
+
398
+ if (contracts.length === 0) {
399
+ return { success: false, error: 'No contracts returned from API' };
400
+ }
401
+
402
+ // Cache the results
403
+ this.cachedContracts = { data: contracts, timestamp: Date.now() };
404
+ return { success: true, contracts, source: 'api' };
405
+
406
+ } catch (e) {
407
+ debug('getContracts error:', e.message);
408
+ return { success: false, error: e.message };
236
409
  }
237
-
238
- const y = frontYear; // e.g., 26 for 2026
239
- const fy = `${frontYear}`; // full year string
240
-
241
- const contracts = [
242
- { symbol: `ES${frontMonth}${y}`, name: `E-mini S&P 500 (${frontMonthName} ${fy})`, exchange: 'CME' },
243
- { symbol: `NQ${frontMonth}${y}`, name: `E-mini NASDAQ-100 (${frontMonthName} ${fy})`, exchange: 'CME' },
244
- { symbol: `MES${frontMonth}${y}`, name: `Micro E-mini S&P 500 (${frontMonthName} ${fy})`, exchange: 'CME' },
245
- { symbol: `MNQ${frontMonth}${y}`, name: `Micro E-mini NASDAQ-100 (${frontMonthName} ${fy})`, exchange: 'CME' },
246
- { symbol: `RTY${frontMonth}${y}`, name: `E-mini Russell 2000 (${frontMonthName} ${fy})`, exchange: 'CME' },
247
- { symbol: `M2K${frontMonth}${y}`, name: `Micro E-mini Russell 2000 (${frontMonthName} ${fy})`, exchange: 'CME' },
248
- { symbol: `YM${frontMonth}${y}`, name: `E-mini Dow Jones (${frontMonthName} ${fy})`, exchange: 'CBOT' },
249
- { symbol: `MYM${frontMonth}${y}`, name: `Micro E-mini Dow Jones (${frontMonthName} ${fy})`, exchange: 'CBOT' },
250
- { symbol: `CL${frontMonth}${y}`, name: `Crude Oil (${frontMonthName} ${fy})`, exchange: 'NYMEX' },
251
- { symbol: `MCL${frontMonth}${y}`, name: `Micro Crude Oil (${frontMonthName} ${fy})`, exchange: 'NYMEX' },
252
- { symbol: `GC${frontMonth}${y}`, name: `Gold (${frontMonthName} ${fy})`, exchange: 'COMEX' },
253
- { symbol: `MGC${frontMonth}${y}`, name: `Micro Gold (${frontMonthName} ${fy})`, exchange: 'COMEX' },
254
- { symbol: `SI${frontMonth}${y}`, name: `Silver (${frontMonthName} ${fy})`, exchange: 'COMEX' },
255
- { symbol: `NG${frontMonth}${y}`, name: `Natural Gas (${frontMonthName} ${fy})`, exchange: 'NYMEX' },
256
- ];
257
- return { success: true, contracts };
258
410
  }
259
411
 
260
412
  async searchContracts(searchText) {
@@ -296,6 +448,10 @@ class RithmicService extends EventEmitter {
296
448
  await this.pnlConn.disconnect();
297
449
  this.pnlConn = null;
298
450
  }
451
+ if (this.tickerConn) {
452
+ await this.tickerConn.disconnect();
453
+ this.tickerConn = null;
454
+ }
299
455
  this.accounts = [];
300
456
  this.accountPnL.clear();
301
457
  this.positions.clear();
@@ -303,6 +459,7 @@ class RithmicService extends EventEmitter {
303
459
  this.loginInfo = null;
304
460
  this.user = null;
305
461
  this.credentials = null;
462
+ this.cachedContracts = null;
306
463
  }
307
464
  }
308
465
 
@@ -0,0 +1,11 @@
1
+
2
+ package rti;
3
+
4
+ message RequestFrontMonthContract
5
+ {
6
+ required int32 template_id = 154467; // Template ID = 113
7
+ repeated string user_msg = 132760; // User message for tracking
8
+ optional string symbol = 110100; // Base symbol (e.g., "MNQ", "ES")
9
+ optional string exchange = 110101; // Exchange (e.g., "CME")
10
+ optional bool need_updates = 154352; // Request updates
11
+ }
@@ -0,0 +1,9 @@
1
+
2
+ package rti;
3
+
4
+ message RequestProductCodes
5
+ {
6
+ required int32 template_id = 154467; // Template ID = 111
7
+ repeated string user_msg = 132760; // User message for tracking
8
+ optional string exchange = 110101; // Exchange filter (optional)
9
+ }
@@ -0,0 +1,13 @@
1
+
2
+ package rti;
3
+
4
+ message ResponseFrontMonthContract
5
+ {
6
+ required int32 template_id = 154467; // Template ID = 114
7
+ repeated string rp_code = 132766; // Response code
8
+ repeated string user_msg = 132760; // Echo of user message
9
+ optional string symbol = 110100; // Full contract symbol (e.g., "MNQH5")
10
+ optional string exchange = 110101; // Exchange
11
+ optional string trading_symbol = 157095; // Trading symbol
12
+ optional string description = 110114; // Contract description
13
+ }
@@ -0,0 +1,12 @@
1
+
2
+ package rti;
3
+
4
+ message ResponseProductCodes
5
+ {
6
+ required int32 template_id = 154467; // Template ID = 112
7
+ repeated string rp_code = 132766; // Response code
8
+ repeated string user_msg = 132760; // Echo of user message
9
+ optional string exchange = 110101; // Exchange
10
+ optional string product_code = 110102; // Product code (e.g., "MNQ", "ES")
11
+ optional string product_name = 110103; // Product name
12
+ }
@@ -28,6 +28,19 @@ const PNL_FIELDS = {
28
28
  USECS: 150101,
29
29
  };
30
30
 
31
+ // Symbol/Contract field IDs (ResponseProductCodes, ResponseFrontMonthContract)
32
+ const SYMBOL_FIELDS = {
33
+ TEMPLATE_ID: 154467,
34
+ RP_CODE: 132766,
35
+ EXCHANGE: 110101,
36
+ PRODUCT_CODE: 110102, // Base symbol (ES, NQ, MNQ)
37
+ PRODUCT_NAME: 110103, // Product name
38
+ SYMBOL: 110100, // Full contract symbol (ESH26)
39
+ TRADING_SYMBOL: 157095, // Trading symbol
40
+ DESCRIPTION: 110114, // Contract description
41
+ USER_MSG: 132760,
42
+ };
43
+
31
44
  // Instrument PnL Position Update field IDs
32
45
  const INSTRUMENT_PNL_FIELDS = {
33
46
  TEMPLATE_ID: 154467,
@@ -377,6 +390,101 @@ class ProtobufHandler {
377
390
  }
378
391
  }
379
392
 
393
+ /**
394
+ * Decode ResponseProductCodes (template 112) - list of available symbols
395
+ */
396
+ function decodeProductCodes(buffer) {
397
+ const result = { rpCode: [] };
398
+ let offset = 0;
399
+
400
+ while (offset < buffer.length) {
401
+ try {
402
+ const [tag, tagOffset] = readVarint(buffer, offset);
403
+ const wireType = tag & 0x7;
404
+ const fieldNumber = tag >>> 3;
405
+ offset = tagOffset;
406
+
407
+ switch (fieldNumber) {
408
+ case SYMBOL_FIELDS.TEMPLATE_ID:
409
+ [result.templateId, offset] = readVarint(buffer, offset);
410
+ break;
411
+ case SYMBOL_FIELDS.RP_CODE:
412
+ let rpCode;
413
+ [rpCode, offset] = readLengthDelimited(buffer, offset);
414
+ result.rpCode.push(rpCode);
415
+ break;
416
+ case SYMBOL_FIELDS.EXCHANGE:
417
+ [result.exchange, offset] = readLengthDelimited(buffer, offset);
418
+ break;
419
+ case SYMBOL_FIELDS.PRODUCT_CODE:
420
+ [result.productCode, offset] = readLengthDelimited(buffer, offset);
421
+ break;
422
+ case SYMBOL_FIELDS.PRODUCT_NAME:
423
+ [result.productName, offset] = readLengthDelimited(buffer, offset);
424
+ break;
425
+ case SYMBOL_FIELDS.USER_MSG:
426
+ [result.userMsg, offset] = readLengthDelimited(buffer, offset);
427
+ break;
428
+ default:
429
+ offset = skipField(buffer, offset, wireType);
430
+ }
431
+ } catch (error) {
432
+ break;
433
+ }
434
+ }
435
+
436
+ return result;
437
+ }
438
+
439
+ /**
440
+ * Decode ResponseFrontMonthContract (template 114) - current tradeable contract
441
+ */
442
+ function decodeFrontMonthContract(buffer) {
443
+ const result = { rpCode: [] };
444
+ let offset = 0;
445
+
446
+ while (offset < buffer.length) {
447
+ try {
448
+ const [tag, tagOffset] = readVarint(buffer, offset);
449
+ const wireType = tag & 0x7;
450
+ const fieldNumber = tag >>> 3;
451
+ offset = tagOffset;
452
+
453
+ switch (fieldNumber) {
454
+ case SYMBOL_FIELDS.TEMPLATE_ID:
455
+ [result.templateId, offset] = readVarint(buffer, offset);
456
+ break;
457
+ case SYMBOL_FIELDS.RP_CODE:
458
+ let rpCode;
459
+ [rpCode, offset] = readLengthDelimited(buffer, offset);
460
+ result.rpCode.push(rpCode);
461
+ break;
462
+ case SYMBOL_FIELDS.SYMBOL:
463
+ [result.symbol, offset] = readLengthDelimited(buffer, offset);
464
+ break;
465
+ case SYMBOL_FIELDS.EXCHANGE:
466
+ [result.exchange, offset] = readLengthDelimited(buffer, offset);
467
+ break;
468
+ case SYMBOL_FIELDS.TRADING_SYMBOL:
469
+ [result.tradingSymbol, offset] = readLengthDelimited(buffer, offset);
470
+ break;
471
+ case SYMBOL_FIELDS.DESCRIPTION:
472
+ [result.description, offset] = readLengthDelimited(buffer, offset);
473
+ break;
474
+ case SYMBOL_FIELDS.USER_MSG:
475
+ [result.userMsg, offset] = readLengthDelimited(buffer, offset);
476
+ break;
477
+ default:
478
+ offset = skipField(buffer, offset, wireType);
479
+ }
480
+ } catch (error) {
481
+ break;
482
+ }
483
+ }
484
+
485
+ return result;
486
+ }
487
+
380
488
  // Singleton
381
489
  const proto = new ProtobufHandler();
382
490
 
@@ -384,6 +492,8 @@ module.exports = {
384
492
  proto,
385
493
  decodeAccountPnL,
386
494
  decodeInstrumentPnL,
495
+ decodeProductCodes,
496
+ decodeFrontMonthContract,
387
497
  readVarint,
388
498
  readLengthDelimited,
389
499
  skipField,