hedgequantx 2.6.162 → 2.7.0
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/README.md +15 -88
- package/bin/cli.js +0 -11
- package/dist/lib/api.jsc +0 -0
- package/dist/lib/api2.jsc +0 -0
- package/dist/lib/core.jsc +0 -0
- package/dist/lib/core2.jsc +0 -0
- package/dist/lib/data.js +1 -1
- package/dist/lib/data.jsc +0 -0
- package/dist/lib/data2.jsc +0 -0
- package/dist/lib/decoder.jsc +0 -0
- package/dist/lib/m/mod1.jsc +0 -0
- package/dist/lib/m/mod2.jsc +0 -0
- package/dist/lib/n/r1.jsc +0 -0
- package/dist/lib/n/r2.jsc +0 -0
- package/dist/lib/n/r3.jsc +0 -0
- package/dist/lib/n/r4.jsc +0 -0
- package/dist/lib/n/r5.jsc +0 -0
- package/dist/lib/n/r6.jsc +0 -0
- package/dist/lib/n/r7.jsc +0 -0
- package/dist/lib/o/util1.jsc +0 -0
- package/dist/lib/o/util2.jsc +0 -0
- package/package.json +6 -3
- package/src/app.js +40 -162
- package/src/config/constants.js +31 -33
- package/src/config/propfirms.js +13 -217
- package/src/config/settings.js +0 -43
- package/src/lib/api.js +198 -0
- package/src/lib/api2.js +353 -0
- package/src/lib/core.js +539 -0
- package/src/lib/core2.js +341 -0
- package/src/lib/data.js +555 -0
- package/src/lib/data2.js +492 -0
- package/src/lib/decoder.js +599 -0
- package/src/lib/m/s1.js +804 -0
- package/src/lib/m/s2.js +34 -0
- package/src/lib/n/r1.js +454 -0
- package/src/lib/n/r2.js +514 -0
- package/src/lib/n/r3.js +631 -0
- package/src/lib/n/r4.js +401 -0
- package/src/lib/n/r5.js +335 -0
- package/src/lib/n/r6.js +425 -0
- package/src/lib/n/r7.js +530 -0
- package/src/lib/o/l1.js +44 -0
- package/src/lib/o/l2.js +427 -0
- package/src/lib/python-bridge.js +206 -0
- package/src/menus/connect.js +14 -176
- package/src/menus/dashboard.js +65 -110
- package/src/pages/accounts.js +18 -18
- package/src/pages/algo/copy-trading.js +210 -240
- package/src/pages/algo/index.js +41 -104
- package/src/pages/algo/one-account.js +386 -33
- package/src/pages/algo/ui.js +312 -151
- package/src/pages/orders.js +3 -3
- package/src/pages/positions.js +3 -3
- package/src/pages/stats/chart.js +74 -0
- package/src/pages/stats/display.js +228 -0
- package/src/pages/stats/index.js +236 -0
- package/src/pages/stats/metrics.js +213 -0
- package/src/pages/user.js +6 -6
- package/src/services/hqx-server/constants.js +55 -0
- package/src/services/hqx-server/index.js +401 -0
- package/src/services/hqx-server/latency.js +81 -0
- package/src/services/index.js +12 -3
- package/src/services/rithmic/accounts.js +7 -32
- package/src/services/rithmic/connection.js +1 -204
- package/src/services/rithmic/contracts.js +235 -0
- package/src/services/rithmic/handlers.js +21 -196
- package/src/services/rithmic/index.js +60 -291
- package/src/services/rithmic/market.js +31 -0
- package/src/services/rithmic/orders.js +5 -361
- package/src/services/rithmic/protobuf.js +5 -195
- package/src/services/session.js +22 -173
- package/src/ui/box.js +10 -18
- package/src/ui/index.js +1 -3
- package/src/ui/menu.js +1 -1
- package/src/utils/prompts.js +2 -2
- package/dist/lib/m/s1.js +0 -1
- package/src/menus/ai-agent-connect.js +0 -181
- package/src/menus/ai-agent-models.js +0 -219
- package/src/menus/ai-agent-oauth.js +0 -292
- package/src/menus/ai-agent-ui.js +0 -141
- package/src/menus/ai-agent.js +0 -484
- package/src/pages/algo/algo-config.js +0 -195
- package/src/pages/algo/algo-multi.js +0 -801
- package/src/pages/algo/algo-utils.js +0 -58
- package/src/pages/algo/copy-engine.js +0 -449
- package/src/pages/algo/custom-strategy.js +0 -459
- package/src/pages/algo/logger.js +0 -245
- package/src/pages/algo/smart-logs-data.js +0 -218
- package/src/pages/algo/smart-logs.js +0 -387
- package/src/pages/algo/ui-constants.js +0 -144
- package/src/pages/algo/ui-summary.js +0 -184
- package/src/pages/stats-calculations.js +0 -191
- package/src/pages/stats-ui.js +0 -381
- package/src/pages/stats.js +0 -339
- package/src/services/ai/client-analysis.js +0 -194
- package/src/services/ai/client-models.js +0 -333
- package/src/services/ai/client.js +0 -343
- package/src/services/ai/index.js +0 -384
- package/src/services/ai/oauth-anthropic.js +0 -265
- package/src/services/ai/oauth-gemini.js +0 -223
- package/src/services/ai/oauth-iflow.js +0 -269
- package/src/services/ai/oauth-openai.js +0 -233
- package/src/services/ai/oauth-qwen.js +0 -279
- package/src/services/ai/providers/index.js +0 -526
- package/src/services/ai/proxy-install.js +0 -249
- package/src/services/ai/proxy-manager.js +0 -494
- package/src/services/ai/proxy-remote.js +0 -161
- package/src/services/ai/strategy-supervisor.js +0 -1312
- package/src/services/ai/supervisor-data.js +0 -195
- package/src/services/ai/supervisor-optimize.js +0 -215
- package/src/services/ai/supervisor-sync.js +0 -178
- package/src/services/ai/supervisor-utils.js +0 -158
- package/src/services/ai/supervisor.js +0 -484
- package/src/services/ai/validation.js +0 -250
- package/src/services/hqx-server-events.js +0 -110
- package/src/services/hqx-server-handlers.js +0 -217
- package/src/services/hqx-server-latency.js +0 -136
- package/src/services/hqx-server.js +0 -403
- package/src/services/position-constants.js +0 -28
- package/src/services/position-manager.js +0 -528
- package/src/services/position-momentum.js +0 -206
- package/src/services/projectx/accounts.js +0 -142
- package/src/services/projectx/index.js +0 -443
- package/src/services/projectx/market.js +0 -172
- package/src/services/projectx/stats.js +0 -110
- package/src/services/projectx/trading.js +0 -180
- package/src/services/rithmic/latency-tracker.js +0 -182
- package/src/services/rithmic/market-data.js +0 -549
- package/src/services/rithmic/specs.js +0 -146
- package/src/services/rithmic/trade-history.js +0 -254
- package/src/services/session-history.js +0 -475
- package/src/services/strategy/hft-tick.js +0 -507
- package/src/services/strategy/recovery-math.js +0 -402
- package/src/services/tradovate/constants.js +0 -109
- package/src/services/tradovate/index.js +0 -505
- package/src/services/tradovate/market.js +0 -47
- package/src/services/tradovate/websocket.js +0 -97
package/src/lib/n/r5.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rithmic P&L and Positions
|
|
3
|
+
* Handles real-time P&L updates from PNL_PLANT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const EventEmitter = require('events');
|
|
7
|
+
const { RithmicConnection } = require('./r2');
|
|
8
|
+
const { TEMPLATE_IDS, INFRA_TYPES } = require('./r7');
|
|
9
|
+
|
|
10
|
+
class RithmicPnL extends EventEmitter {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
super();
|
|
13
|
+
|
|
14
|
+
this.connection = null;
|
|
15
|
+
this.config = null;
|
|
16
|
+
|
|
17
|
+
// Login info
|
|
18
|
+
this.fcmId = null;
|
|
19
|
+
this.ibId = null;
|
|
20
|
+
|
|
21
|
+
// Account P&L cache
|
|
22
|
+
this.accountPnL = new Map(); // accountId -> P&L data
|
|
23
|
+
|
|
24
|
+
// Instrument P&L cache
|
|
25
|
+
this.instrumentPnL = new Map(); // `${accountId}:${symbol}` -> P&L data
|
|
26
|
+
|
|
27
|
+
// Subscriptions
|
|
28
|
+
this.subscriptions = new Set(); // accountIds subscribed
|
|
29
|
+
|
|
30
|
+
// Options
|
|
31
|
+
this.debug = options.debug || false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Connect to Rithmic PNL_PLANT
|
|
36
|
+
* @param {Object} credentials - { userId, password, systemName, gateway }
|
|
37
|
+
*/
|
|
38
|
+
async connect(credentials) {
|
|
39
|
+
this.config = credentials;
|
|
40
|
+
|
|
41
|
+
this.connection = new RithmicConnection({
|
|
42
|
+
debug: this.debug,
|
|
43
|
+
maxReconnectAttempts: 5
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Forward connection events
|
|
47
|
+
this.connection.on('loggedIn', (data) => {
|
|
48
|
+
this.fcmId = data.fcmId;
|
|
49
|
+
this.ibId = data.ibId;
|
|
50
|
+
this.emit('connected', data);
|
|
51
|
+
|
|
52
|
+
// Resubscribe after reconnect
|
|
53
|
+
for (const accountId of this.subscriptions) {
|
|
54
|
+
this._sendSubscribe(accountId);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
this.connection.on('error', (error) => {
|
|
59
|
+
this.emit('error', error);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
this.connection.on('disconnected', (data) => {
|
|
63
|
+
this.emit('disconnected', data);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Handle P&L updates
|
|
67
|
+
this.connection.on('AccountPnLPositionUpdate', (update) => this._handleAccountPnL(update));
|
|
68
|
+
this.connection.on('InstrumentPnLPositionUpdate', (update) => this._handleInstrumentPnL(update));
|
|
69
|
+
|
|
70
|
+
// Connect
|
|
71
|
+
await this.connection.connect({
|
|
72
|
+
userId: credentials.userId,
|
|
73
|
+
password: credentials.password,
|
|
74
|
+
systemName: credentials.systemName,
|
|
75
|
+
gateway: credentials.gateway,
|
|
76
|
+
infraType: INFRA_TYPES.PNL_PLANT
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Subscribe to P&L updates for an account
|
|
84
|
+
*/
|
|
85
|
+
async subscribe(accountId) {
|
|
86
|
+
if (!this.connection || !this.connection.isReady) {
|
|
87
|
+
throw new Error('Not connected');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
this.subscriptions.add(accountId);
|
|
91
|
+
return this._sendSubscribe(accountId);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Internal subscribe
|
|
96
|
+
*/
|
|
97
|
+
_sendSubscribe(accountId) {
|
|
98
|
+
try {
|
|
99
|
+
this.connection.send('RequestPnLPositionUpdates', {
|
|
100
|
+
templateId: TEMPLATE_IDS.REQUEST_PNL_POSITION_UPDATES,
|
|
101
|
+
request: 1, // Subscribe
|
|
102
|
+
fcmId: this.fcmId,
|
|
103
|
+
ibId: this.ibId,
|
|
104
|
+
accountId: accountId.toString()
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return true;
|
|
108
|
+
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error('[RITHMIC:PnL] Subscribe error:', error.message);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Unsubscribe from P&L updates
|
|
117
|
+
*/
|
|
118
|
+
async unsubscribe(accountId) {
|
|
119
|
+
if (!this.connection || !this.connection.isReady) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.subscriptions.delete(accountId);
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
this.connection.send('RequestPnLPositionUpdates', {
|
|
127
|
+
templateId: TEMPLATE_IDS.REQUEST_PNL_POSITION_UPDATES,
|
|
128
|
+
request: 2, // Unsubscribe
|
|
129
|
+
fcmId: this.fcmId,
|
|
130
|
+
ibId: this.ibId,
|
|
131
|
+
accountId: accountId.toString()
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return true;
|
|
135
|
+
|
|
136
|
+
} catch (error) {
|
|
137
|
+
console.error('[RITHMIC:PnL] Unsubscribe error:', error.message);
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Handle account-level P&L update
|
|
144
|
+
*/
|
|
145
|
+
_handleAccountPnL(update) {
|
|
146
|
+
const accountId = update.accountId;
|
|
147
|
+
|
|
148
|
+
const pnlData = {
|
|
149
|
+
accountId: accountId,
|
|
150
|
+
|
|
151
|
+
// P&L
|
|
152
|
+
openPositionPnL: this._parseNumber(update.openPositionPnl),
|
|
153
|
+
closedPositionPnL: this._parseNumber(update.closedPositionPnl),
|
|
154
|
+
dayOpenPnL: this._parseNumber(update.dayOpenPnl),
|
|
155
|
+
dayClosedPnL: this._parseNumber(update.dayClosedPnl),
|
|
156
|
+
dayPnL: this._parseNumber(update.dayPnl),
|
|
157
|
+
|
|
158
|
+
// Positions
|
|
159
|
+
openPositionQuantity: update.openPositionQuantity || 0,
|
|
160
|
+
closedPositionQuantity: update.closedPositionQuantity || 0,
|
|
161
|
+
netQuantity: update.netQuantity || 0,
|
|
162
|
+
|
|
163
|
+
// Volume
|
|
164
|
+
fillBuyQty: update.fillBuyQty || 0,
|
|
165
|
+
fillSellQty: update.fillSellQty || 0,
|
|
166
|
+
buyQty: update.buyQty || 0,
|
|
167
|
+
sellQty: update.sellQty || 0,
|
|
168
|
+
|
|
169
|
+
// Balances
|
|
170
|
+
accountBalance: this._parseNumber(update.accountBalance),
|
|
171
|
+
cashOnHand: this._parseNumber(update.cashOnHand),
|
|
172
|
+
marginBalance: this._parseNumber(update.marginBalance),
|
|
173
|
+
minMarginBalance: this._parseNumber(update.minMarginBalance),
|
|
174
|
+
availableBuyingPower: this._parseNumber(update.availableBuyingPower),
|
|
175
|
+
usedBuyingPower: this._parseNumber(update.usedBuyingPower),
|
|
176
|
+
|
|
177
|
+
// Risk
|
|
178
|
+
percentMaximumAllowableLoss: this._parseNumber(update.percentMaximumAllowableLoss),
|
|
179
|
+
|
|
180
|
+
// Timestamps
|
|
181
|
+
isSnapshot: update.isSnapshot || false,
|
|
182
|
+
timestamp: Date.now()
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// Calculate total P&L
|
|
186
|
+
pnlData.totalPnL = pnlData.openPositionPnL + pnlData.closedPositionPnL;
|
|
187
|
+
|
|
188
|
+
// Store
|
|
189
|
+
this.accountPnL.set(accountId, pnlData);
|
|
190
|
+
|
|
191
|
+
// Debug
|
|
192
|
+
if (this.debug) {
|
|
193
|
+
console.log(`[RITHMIC:PnL] Account ${accountId}: Open=$${pnlData.openPositionPnL.toFixed(2)}, Closed=$${pnlData.closedPositionPnL.toFixed(2)}, Net=${pnlData.netQuantity}`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Emit
|
|
197
|
+
this.emit('accountPnL', pnlData);
|
|
198
|
+
this.emit('pnl', pnlData);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Handle instrument-level P&L update
|
|
203
|
+
*/
|
|
204
|
+
_handleInstrumentPnL(update) {
|
|
205
|
+
const accountId = update.accountId;
|
|
206
|
+
const symbol = update.symbol;
|
|
207
|
+
const key = `${accountId}:${symbol}`;
|
|
208
|
+
|
|
209
|
+
const pnlData = {
|
|
210
|
+
accountId: accountId,
|
|
211
|
+
symbol: symbol,
|
|
212
|
+
exchange: update.exchange,
|
|
213
|
+
productCode: update.productCode,
|
|
214
|
+
instrumentType: update.instrumentType,
|
|
215
|
+
|
|
216
|
+
// P&L
|
|
217
|
+
openPositionPnL: this._parseNumber(update.openPositionPnl),
|
|
218
|
+
closedPositionPnL: this._parseNumber(update.closedPositionPnl),
|
|
219
|
+
dayOpenPnL: update.dayOpenPnl || 0,
|
|
220
|
+
dayClosedPnL: update.dayClosedPnl || 0,
|
|
221
|
+
dayPnL: update.dayPnl || 0,
|
|
222
|
+
|
|
223
|
+
// Position
|
|
224
|
+
openPositionQuantity: update.openPositionQuantity || 0,
|
|
225
|
+
closedPositionQuantity: update.closedPositionQuantity || 0,
|
|
226
|
+
netQuantity: update.netQuantity || 0,
|
|
227
|
+
avgOpenFillPrice: update.avgOpenFillPrice || 0,
|
|
228
|
+
|
|
229
|
+
// Volume
|
|
230
|
+
fillBuyQty: update.fillBuyQty || 0,
|
|
231
|
+
fillSellQty: update.fillSellQty || 0,
|
|
232
|
+
buyQty: update.buyQty || 0,
|
|
233
|
+
sellQty: update.sellQty || 0,
|
|
234
|
+
|
|
235
|
+
// Timestamps
|
|
236
|
+
isSnapshot: update.isSnapshot || false,
|
|
237
|
+
timestamp: Date.now()
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// Calculate total P&L
|
|
241
|
+
pnlData.totalPnL = pnlData.openPositionPnL + pnlData.closedPositionPnL;
|
|
242
|
+
|
|
243
|
+
// Store
|
|
244
|
+
this.instrumentPnL.set(key, pnlData);
|
|
245
|
+
|
|
246
|
+
// Emit
|
|
247
|
+
this.emit('instrumentPnL', pnlData);
|
|
248
|
+
this.emit('position', pnlData);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Parse number from string or return 0
|
|
253
|
+
*/
|
|
254
|
+
_parseNumber(value) {
|
|
255
|
+
if (typeof value === 'number') return value;
|
|
256
|
+
if (typeof value === 'string') {
|
|
257
|
+
const parsed = parseFloat(value);
|
|
258
|
+
return isNaN(parsed) ? 0 : parsed;
|
|
259
|
+
}
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Get account P&L
|
|
265
|
+
*/
|
|
266
|
+
getAccountPnL(accountId) {
|
|
267
|
+
return this.accountPnL.get(accountId) || null;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get instrument P&L
|
|
272
|
+
*/
|
|
273
|
+
getInstrumentPnL(accountId, symbol) {
|
|
274
|
+
return this.instrumentPnL.get(`${accountId}:${symbol}`) || null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Get all positions for an account
|
|
279
|
+
*/
|
|
280
|
+
getPositions(accountId) {
|
|
281
|
+
const positions = [];
|
|
282
|
+
for (const [key, pnl] of this.instrumentPnL) {
|
|
283
|
+
if (key.startsWith(`${accountId}:`) && pnl.netQuantity !== 0) {
|
|
284
|
+
positions.push(pnl);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return positions;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Get all account P&L data
|
|
292
|
+
*/
|
|
293
|
+
getAllAccountPnL() {
|
|
294
|
+
return Array.from(this.accountPnL.values());
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Get summary for an account
|
|
299
|
+
*/
|
|
300
|
+
getSummary(accountId) {
|
|
301
|
+
const account = this.accountPnL.get(accountId);
|
|
302
|
+
const positions = this.getPositions(accountId);
|
|
303
|
+
|
|
304
|
+
return {
|
|
305
|
+
accountId,
|
|
306
|
+
pnl: account,
|
|
307
|
+
positions,
|
|
308
|
+
openPositionCount: positions.length,
|
|
309
|
+
totalNetQuantity: positions.reduce((sum, p) => sum + p.netQuantity, 0)
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Check if connected
|
|
315
|
+
*/
|
|
316
|
+
get isConnected() {
|
|
317
|
+
return this.connection && this.connection.isReady;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Disconnect
|
|
322
|
+
*/
|
|
323
|
+
async disconnect() {
|
|
324
|
+
if (this.connection) {
|
|
325
|
+
await this.connection.disconnect();
|
|
326
|
+
this.connection = null;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
this.subscriptions.clear();
|
|
330
|
+
this.accountPnL.clear();
|
|
331
|
+
this.instrumentPnL.clear();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
module.exports = { RithmicPnL };
|