hedgequantx 2.6.163 → 2.7.1

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.
Files changed (146) hide show
  1. package/README.md +15 -88
  2. package/bin/cli.js +0 -11
  3. package/dist/lib/api.jsc +0 -0
  4. package/dist/lib/api2.jsc +0 -0
  5. package/dist/lib/core.jsc +0 -0
  6. package/dist/lib/core2.jsc +0 -0
  7. package/dist/lib/data.js +1 -1
  8. package/dist/lib/data.jsc +0 -0
  9. package/dist/lib/data2.jsc +0 -0
  10. package/dist/lib/decoder.jsc +0 -0
  11. package/dist/lib/m/mod1.jsc +0 -0
  12. package/dist/lib/m/mod2.jsc +0 -0
  13. package/dist/lib/n/r1.jsc +0 -0
  14. package/dist/lib/n/r2.jsc +0 -0
  15. package/dist/lib/n/r3.jsc +0 -0
  16. package/dist/lib/n/r4.jsc +0 -0
  17. package/dist/lib/n/r5.jsc +0 -0
  18. package/dist/lib/n/r6.jsc +0 -0
  19. package/dist/lib/n/r7.jsc +0 -0
  20. package/dist/lib/o/util1.jsc +0 -0
  21. package/dist/lib/o/util2.jsc +0 -0
  22. package/package.json +8 -5
  23. package/src/app.js +40 -162
  24. package/src/config/constants.js +31 -33
  25. package/src/config/propfirms.js +13 -217
  26. package/src/config/settings.js +0 -43
  27. package/src/lib/api.js +198 -0
  28. package/src/lib/api2.js +353 -0
  29. package/src/lib/core.js +539 -0
  30. package/src/lib/core2.js +341 -0
  31. package/src/lib/data.js +555 -0
  32. package/src/lib/data2.js +492 -0
  33. package/src/lib/decoder.js +599 -0
  34. package/src/lib/m/s1.js +804 -0
  35. package/src/lib/m/s2.js +34 -0
  36. package/src/lib/n/r1.js +454 -0
  37. package/src/lib/n/r2.js +514 -0
  38. package/src/lib/n/r3.js +631 -0
  39. package/src/lib/n/r4.js +401 -0
  40. package/src/lib/n/r5.js +335 -0
  41. package/src/lib/n/r6.js +425 -0
  42. package/src/lib/n/r7.js +530 -0
  43. package/src/lib/o/l1.js +44 -0
  44. package/src/lib/o/l2.js +427 -0
  45. package/src/lib/python-bridge.js +206 -0
  46. package/src/menus/connect.js +14 -176
  47. package/src/menus/dashboard.js +65 -110
  48. package/src/pages/accounts.js +18 -18
  49. package/src/pages/algo/copy-trading.js +210 -240
  50. package/src/pages/algo/index.js +41 -104
  51. package/src/pages/algo/one-account.js +386 -33
  52. package/src/pages/algo/ui.js +312 -151
  53. package/src/pages/orders.js +3 -3
  54. package/src/pages/positions.js +3 -3
  55. package/src/pages/stats/chart.js +74 -0
  56. package/src/pages/stats/display.js +228 -0
  57. package/src/pages/stats/index.js +236 -0
  58. package/src/pages/stats/metrics.js +213 -0
  59. package/src/pages/user.js +6 -6
  60. package/src/services/hqx-server/constants.js +55 -0
  61. package/src/services/hqx-server/index.js +401 -0
  62. package/src/services/hqx-server/latency.js +81 -0
  63. package/src/services/index.js +12 -3
  64. package/src/services/rithmic/accounts.js +7 -32
  65. package/src/services/rithmic/connection.js +1 -204
  66. package/src/services/rithmic/contracts.js +116 -99
  67. package/src/services/rithmic/handlers.js +21 -196
  68. package/src/services/rithmic/index.js +63 -120
  69. package/src/services/rithmic/market.js +31 -0
  70. package/src/services/rithmic/orders.js +5 -111
  71. package/src/services/rithmic/protobuf.js +384 -138
  72. package/src/services/session.js +22 -173
  73. package/src/ui/box.js +10 -18
  74. package/src/ui/index.js +1 -3
  75. package/src/ui/menu.js +1 -1
  76. package/src/utils/prompts.js +2 -2
  77. package/dist/lib/m/s1.js +0 -1
  78. package/src/menus/ai-agent-connect.js +0 -181
  79. package/src/menus/ai-agent-models.js +0 -219
  80. package/src/menus/ai-agent-oauth.js +0 -292
  81. package/src/menus/ai-agent-ui.js +0 -141
  82. package/src/menus/ai-agent.js +0 -484
  83. package/src/pages/algo/algo-config.js +0 -195
  84. package/src/pages/algo/algo-multi.js +0 -801
  85. package/src/pages/algo/algo-utils.js +0 -58
  86. package/src/pages/algo/copy-engine.js +0 -449
  87. package/src/pages/algo/custom-strategy.js +0 -459
  88. package/src/pages/algo/logger.js +0 -245
  89. package/src/pages/algo/smart-logs-data.js +0 -218
  90. package/src/pages/algo/smart-logs.js +0 -387
  91. package/src/pages/algo/ui-constants.js +0 -144
  92. package/src/pages/algo/ui-summary.js +0 -184
  93. package/src/pages/stats-calculations.js +0 -191
  94. package/src/pages/stats-ui.js +0 -381
  95. package/src/pages/stats.js +0 -339
  96. package/src/services/ai/client-analysis.js +0 -194
  97. package/src/services/ai/client-models.js +0 -333
  98. package/src/services/ai/client.js +0 -343
  99. package/src/services/ai/index.js +0 -384
  100. package/src/services/ai/oauth-anthropic.js +0 -265
  101. package/src/services/ai/oauth-gemini.js +0 -223
  102. package/src/services/ai/oauth-iflow.js +0 -269
  103. package/src/services/ai/oauth-openai.js +0 -233
  104. package/src/services/ai/oauth-qwen.js +0 -279
  105. package/src/services/ai/providers/direct-providers.js +0 -323
  106. package/src/services/ai/providers/index.js +0 -62
  107. package/src/services/ai/providers/other-providers.js +0 -104
  108. package/src/services/ai/proxy-install.js +0 -249
  109. package/src/services/ai/proxy-manager.js +0 -494
  110. package/src/services/ai/proxy-remote.js +0 -161
  111. package/src/services/ai/strategy-supervisor.js +0 -1312
  112. package/src/services/ai/supervisor-data.js +0 -195
  113. package/src/services/ai/supervisor-optimize.js +0 -215
  114. package/src/services/ai/supervisor-sync.js +0 -178
  115. package/src/services/ai/supervisor-utils.js +0 -158
  116. package/src/services/ai/supervisor.js +0 -484
  117. package/src/services/ai/validation.js +0 -250
  118. package/src/services/hqx-server-events.js +0 -110
  119. package/src/services/hqx-server-handlers.js +0 -217
  120. package/src/services/hqx-server-latency.js +0 -136
  121. package/src/services/hqx-server.js +0 -403
  122. package/src/services/position-constants.js +0 -28
  123. package/src/services/position-exit-logic.js +0 -174
  124. package/src/services/position-manager.js +0 -438
  125. package/src/services/position-momentum.js +0 -206
  126. package/src/services/projectx/accounts.js +0 -142
  127. package/src/services/projectx/index.js +0 -443
  128. package/src/services/projectx/market.js +0 -172
  129. package/src/services/projectx/stats.js +0 -110
  130. package/src/services/projectx/trading.js +0 -180
  131. package/src/services/rithmic/latency-tracker.js +0 -182
  132. package/src/services/rithmic/market-data-decoders.js +0 -229
  133. package/src/services/rithmic/market-data.js +0 -272
  134. package/src/services/rithmic/orders-fast.js +0 -246
  135. package/src/services/rithmic/proto-decoders.js +0 -403
  136. package/src/services/rithmic/specs.js +0 -146
  137. package/src/services/rithmic/trade-history.js +0 -254
  138. package/src/services/session-history.js +0 -475
  139. package/src/services/strategy/hft-signal-calc.js +0 -147
  140. package/src/services/strategy/hft-tick.js +0 -407
  141. package/src/services/strategy/recovery-math.js +0 -402
  142. package/src/services/tradovate/constants.js +0 -109
  143. package/src/services/tradovate/index.js +0 -392
  144. package/src/services/tradovate/market.js +0 -47
  145. package/src/services/tradovate/orders.js +0 -145
  146. package/src/services/tradovate/websocket.js +0 -97
@@ -0,0 +1,341 @@
1
+ /**
2
+ * Copy Trading Engine - Lightweight
3
+ * Trades on Lead account, mirrors to Follower
4
+ */
5
+
6
+ const EventEmitter = require('events');
7
+ const { A1 } = require('./api');
8
+ const { D1 } = require('./data');
9
+ const { M2 } = require('./m/s2');
10
+
11
+ // Core module
12
+ const {
13
+ RithmicAdapter: N1,
14
+ isRithmicPropFirm: isN,
15
+ getPropFirmConfig: getPC,
16
+ buildRithmicSymbol: buildS,
17
+ getCurrentFrontMonth: getFM
18
+ } = require('./n/r1');
19
+
20
+ class C2 extends EventEmitter {
21
+ constructor(sessionId, config, callbacks) {
22
+ super();
23
+ this.sessionId = sessionId;
24
+ this.config = config;
25
+ this.callbacks = callbacks;
26
+ this.running = false;
27
+
28
+ // APIs
29
+ this.leadApi = null;
30
+ this.followerApi = null;
31
+ this.marketData = null;
32
+ this.strategy = null;
33
+
34
+ // Rithmic adapters (new unified module)
35
+ this.leadN1 = null;
36
+ this.followerN1 = null;
37
+
38
+ // State
39
+ this.position = null;
40
+ this.lastPrice = 0;
41
+ this.stats = { trades: 0, wins: 0, losses: 0, pnl: 0 };
42
+
43
+ // P&L from API - NO local calculations
44
+ this.apiPnL = { lead: 0, follower: 0, lastClosedPnL: 0 };
45
+ }
46
+
47
+ async start() {
48
+ this.running = true;
49
+ this._log('info', '=== COPY TRADING ENGINE ===');
50
+
51
+ // Init Lead API
52
+ if (isN(this.config.leadPropfirm)) {
53
+ const creds = this.config.leadRithmicCredentials;
54
+ const propfirmConfig = getPC(this.config.leadPropfirm);
55
+
56
+ this.leadN1 = new N1({ debug: false, usePool: true });
57
+ await this.leadN1.connect({
58
+ userId: creds.userId,
59
+ password: creds.password,
60
+ propfirm: this.config.leadPropfirm
61
+ }, { marketData: true, trading: true, pnl: true });
62
+
63
+ // Listen for P&L updates from API
64
+ this.leadN1.on('accountPnL', (p) => {
65
+ this.apiPnL.lead = p.closedPnL || p.totalPnL || 0;
66
+ });
67
+
68
+ this.leadApi = this._createRithmicTradingWrapper(this.leadN1, this.config.leadSymbol);
69
+ this._log('info', `Lead: Rithmic connected (${propfirmConfig?.displayName || this.config.leadPropfirm})`);
70
+ } else {
71
+ this.leadApi = new A1(this.config.leadToken, this.config.leadPropfirm);
72
+ this._log('info', 'Lead: ProjectX ready');
73
+ }
74
+
75
+ // Init Follower API
76
+ if (isN(this.config.followerPropfirm)) {
77
+ const creds = this.config.followerRithmicCredentials;
78
+ const propfirmConfig = getPC(this.config.followerPropfirm);
79
+
80
+ this.followerN1 = new N1({ debug: false, usePool: true });
81
+ await this.followerN1.connect({
82
+ userId: creds.userId,
83
+ password: creds.password,
84
+ propfirm: this.config.followerPropfirm
85
+ }, { marketData: false, trading: true, pnl: false });
86
+
87
+ this.followerApi = this._createRithmicTradingWrapper(this.followerN1, this.config.followerSymbol || this.config.leadSymbol);
88
+ this._log('info', `Follower: Rithmic connected (${propfirmConfig?.displayName || this.config.followerPropfirm})`);
89
+ } else {
90
+ this.followerApi = new A1(this.config.followerToken, this.config.followerPropfirm);
91
+ this._log('info', 'Follower: ProjectX ready');
92
+ }
93
+
94
+ // Init market data from Lead
95
+ await this._initMarketData();
96
+
97
+ // Init strategy
98
+ this.strategy = new M2({
99
+ dailyTarget: this.config.dailyTarget,
100
+ maxRisk: this.config.maxRisk,
101
+ contracts: this.config.leadContracts || 1
102
+ });
103
+
104
+ this.strategy.on('signal', (s) => this._onSignal(s));
105
+ this.strategy.on('log', (l) => this._log(l.type, l.message));
106
+
107
+ this._log('info', 'Monitoring market...');
108
+ }
109
+
110
+ async _initMarketData() {
111
+ if (isN(this.config.leadPropfirm)) {
112
+ // Use the lead Rithmic adapter for market data (already connected with marketData: true)
113
+ const sym = this._rithmicSymbol(this.config.leadSymbol);
114
+ await this.leadN1.subscribe(sym, 'CME');
115
+ this._log('info', `Subscribed: ${sym}`);
116
+
117
+ // Create a market data wrapper for event forwarding
118
+ const EventEmitter = require('events');
119
+ this.marketData = new EventEmitter();
120
+
121
+ this.leadN1.on('tick', (d) => {
122
+ this.marketData.emit('tick', d);
123
+ });
124
+ this.leadN1.on('trade', (d) => {
125
+ this.marketData.emit('trade', d);
126
+ });
127
+ this.leadN1.on('quote', (d) => {
128
+ this.marketData.emit('tick', {
129
+ price: d.price || (d.bid + d.ask) / 2,
130
+ bid: d.bid,
131
+ ask: d.ask,
132
+ spread: d.spread,
133
+ timestamp: d.timestamp
134
+ });
135
+ });
136
+
137
+ this.marketData.disconnect = async () => {
138
+ // Disconnection handled by adapter cleanup
139
+ };
140
+ } else {
141
+ this.marketData = new D1({
142
+ symbol: this.config.leadSymbol,
143
+ contractId: this.config.leadContractId
144
+ });
145
+ await this.marketData.connect(
146
+ this.config.leadToken,
147
+ this.config.leadPropfirm,
148
+ this.config.leadContractId
149
+ );
150
+ this._log('info', `Subscribed: ${this.config.leadSymbol}`);
151
+ }
152
+
153
+ this.marketData.on('tick', (d) => {
154
+ this.lastPrice = d.price;
155
+ if (this.strategy) this.strategy.onTick(d);
156
+ });
157
+
158
+ this.marketData.on('trade', (d) => {
159
+ if (this.strategy) this.strategy.onTrade(d);
160
+ });
161
+ }
162
+
163
+ async _onSignal(signal) {
164
+ if (!this.running) return;
165
+
166
+ const { side, action, reason } = signal;
167
+
168
+ if (action === 'open') {
169
+ this._log('signal', `${side.toUpperCase()} @ ${this.lastPrice.toFixed(2)}`);
170
+ await this._openBoth(side);
171
+ } else if (action === 'close') {
172
+ this._log('signal', `CLOSE - ${reason}`);
173
+ await this._closeBoth(reason);
174
+ }
175
+ }
176
+
177
+ async _openBoth(side) {
178
+ const lQty = this.config.leadContracts || 1;
179
+ const fQty = this.config.followerContracts || lQty;
180
+
181
+ // Open Lead
182
+ const leadRes = await this._order(this.leadApi, this.config.leadAccountId, this.config.leadContractId, side, lQty);
183
+ if (!leadRes.success) {
184
+ this._log('error', `Lead order failed: ${leadRes.error}`);
185
+ return;
186
+ }
187
+ this._log('trade', `Lead: ${side.toUpperCase()} ${lQty}`);
188
+
189
+ // Copy to Follower
190
+ const followerRes = await this._order(this.followerApi, this.config.followerAccountId, this.config.followerContractId, side, fQty);
191
+ if (followerRes.success) {
192
+ this._log('trade', `Follower: ${side.toUpperCase()} ${fQty}`);
193
+ this.callbacks.onCopy?.({ side, quantity: fQty });
194
+ } else {
195
+ this._log('error', `Follower failed - closing Lead`);
196
+ await this._close(this.leadApi, this.config.leadAccountId, this.config.leadContractId);
197
+ return;
198
+ }
199
+
200
+ this.position = { side, leadQty: lQty, followerQty: fQty, entry: this.lastPrice };
201
+ this.stats.trades++;
202
+ }
203
+
204
+ async _closeBoth(reason) {
205
+ if (!this.position) return;
206
+
207
+ // Close both in parallel
208
+ await Promise.all([
209
+ this._close(this.leadApi, this.config.leadAccountId, this.config.leadContractId),
210
+ this._close(this.followerApi, this.config.followerAccountId, this.config.followerContractId)
211
+ ]);
212
+
213
+ // P&L comes from API - NO local calculation
214
+ // Use delta from last closed P&L
215
+ const currentClosedPnL = this.apiPnL.lead;
216
+ const pnl = currentClosedPnL - this.apiPnL.lastClosedPnL;
217
+ this.apiPnL.lastClosedPnL = currentClosedPnL;
218
+ this.stats.pnl = currentClosedPnL; // Total P&L from API
219
+ pnl >= 0 ? this.stats.wins++ : this.stats.losses++;
220
+
221
+ this._log(pnl >= 0 ? 'trade' : 'loss', `Closed: ${pnl >= 0 ? '+' : ''}$${pnl.toFixed(2)}`);
222
+
223
+ this.callbacks.onTrade?.({ pnl, reason });
224
+ this.position = null;
225
+
226
+ // Check targets
227
+ if (this.stats.pnl >= this.config.dailyTarget) {
228
+ this._log('success', `TARGET! +$${this.stats.pnl.toFixed(2)}`);
229
+ await this.stop('target');
230
+ } else if (this.stats.pnl <= -this.config.maxRisk) {
231
+ this._log('error', `MAX RISK! -$${Math.abs(this.stats.pnl).toFixed(2)}`);
232
+ await this.stop('risk');
233
+ }
234
+ }
235
+
236
+ async _order(api, accountId, contractId, side, qty) {
237
+ try {
238
+ return await api.placeMarketOrder(accountId, contractId, side, qty, this.lastPrice);
239
+ } catch (e) {
240
+ return { success: false, error: e.message };
241
+ }
242
+ }
243
+
244
+ async _close(api, accountId, contractId) {
245
+ try {
246
+ if (api.closePosition) {
247
+ return await api.closePosition(accountId, contractId);
248
+ }
249
+ // Rithmic: place opposite order
250
+ const closeSide = this.position.side === 'long' ? 'sell' : 'buy';
251
+ const qty = api === this.leadApi ? this.position.leadQty : this.position.followerQty;
252
+ return await api.placeMarketOrder(accountId, contractId, closeSide, qty);
253
+ } catch (e) {
254
+ return { success: false, error: e.message };
255
+ }
256
+ }
257
+
258
+ // NO LOCAL P&L CALCULATION
259
+ // P&L comes from API: PNL_PLANT (Rithmic) or /api/Position (ProjectX)
260
+
261
+ _rithmicSymbol(sym) {
262
+ if (/^[A-Z]{2,4}[FGHJKMNQUVXZ]\d{1,2}$/.test(sym)) return sym;
263
+ const bases = ['MNQ', 'NQ', 'MES', 'ES', 'MYM', 'YM'];
264
+ let base = 'MNQ';
265
+ for (const b of bases) {
266
+ if (sym.toUpperCase().includes(b)) { base = b; break; }
267
+ }
268
+ // Use new module to get front month - returns full symbol directly
269
+ return getFM(base);
270
+ }
271
+
272
+ /**
273
+ * Create a trading API wrapper for Rithmic adapter
274
+ */
275
+ _createRithmicTradingWrapper(adapter, symbolInput) {
276
+ const symbol = this._rithmicSymbol(symbolInput);
277
+
278
+ return {
279
+ async placeMarketOrder(accountId, contractId, side, quantity) {
280
+ try {
281
+ const result = await adapter.placeMarketOrder(accountId, symbol, side, quantity, 'CME');
282
+ return { success: true, orderId: result.orderId };
283
+ } catch (error) {
284
+ return { success: false, error: error.message };
285
+ }
286
+ },
287
+
288
+ async closePosition(accountId, contractId) {
289
+ try {
290
+ await adapter.closePosition(accountId, symbol, 'CME');
291
+ return { success: true };
292
+ } catch (error) {
293
+ return { success: false, error: error.message };
294
+ }
295
+ },
296
+
297
+ disconnect() {
298
+ // Handled by adapter cleanup in stop()
299
+ }
300
+ };
301
+ }
302
+
303
+ async stop(reason = 'manual') {
304
+ if (!this.running) return;
305
+ this.running = false;
306
+
307
+ this._log('info', `Stopping: ${reason}`);
308
+
309
+ // Close positions
310
+ if (this.position) {
311
+ await this._closeBoth(reason);
312
+ }
313
+
314
+ // Cleanup market data
315
+ if (this.marketData?.disconnect) await this.marketData.disconnect();
316
+
317
+ // Cleanup Rithmic adapters
318
+ if (this.leadN1) {
319
+ await this.leadN1.disconnect();
320
+ this.leadN1 = null;
321
+ }
322
+ if (this.followerN1) {
323
+ await this.followerN1.disconnect();
324
+ this.followerN1 = null;
325
+ }
326
+
327
+ // Cleanup ProjectX APIs
328
+ if (this.leadApi?.disconnect && !this.leadN1) this.leadApi.disconnect();
329
+ if (this.followerApi?.disconnect && !this.followerN1) this.followerApi.disconnect();
330
+
331
+ this._log('info', `Final P&L: $${this.stats.pnl.toFixed(2)}`);
332
+ this.emit('stopped', { reason, stats: this.stats });
333
+ }
334
+
335
+ _log(type, message) {
336
+ console.log(`[COPY] [${type.toUpperCase()}] ${message}`);
337
+ this.callbacks.onLog?.({ type, message });
338
+ }
339
+ }
340
+
341
+ module.exports = { C2 };