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.
Files changed (138) 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 +6 -3
  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 +235 -0
  67. package/src/services/rithmic/handlers.js +21 -196
  68. package/src/services/rithmic/index.js +60 -291
  69. package/src/services/rithmic/market.js +31 -0
  70. package/src/services/rithmic/orders.js +5 -361
  71. package/src/services/rithmic/protobuf.js +5 -195
  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/index.js +0 -526
  106. package/src/services/ai/proxy-install.js +0 -249
  107. package/src/services/ai/proxy-manager.js +0 -494
  108. package/src/services/ai/proxy-remote.js +0 -161
  109. package/src/services/ai/strategy-supervisor.js +0 -1312
  110. package/src/services/ai/supervisor-data.js +0 -195
  111. package/src/services/ai/supervisor-optimize.js +0 -215
  112. package/src/services/ai/supervisor-sync.js +0 -178
  113. package/src/services/ai/supervisor-utils.js +0 -158
  114. package/src/services/ai/supervisor.js +0 -484
  115. package/src/services/ai/validation.js +0 -250
  116. package/src/services/hqx-server-events.js +0 -110
  117. package/src/services/hqx-server-handlers.js +0 -217
  118. package/src/services/hqx-server-latency.js +0 -136
  119. package/src/services/hqx-server.js +0 -403
  120. package/src/services/position-constants.js +0 -28
  121. package/src/services/position-manager.js +0 -528
  122. package/src/services/position-momentum.js +0 -206
  123. package/src/services/projectx/accounts.js +0 -142
  124. package/src/services/projectx/index.js +0 -443
  125. package/src/services/projectx/market.js +0 -172
  126. package/src/services/projectx/stats.js +0 -110
  127. package/src/services/projectx/trading.js +0 -180
  128. package/src/services/rithmic/latency-tracker.js +0 -182
  129. package/src/services/rithmic/market-data.js +0 -549
  130. package/src/services/rithmic/specs.js +0 -146
  131. package/src/services/rithmic/trade-history.js +0 -254
  132. package/src/services/session-history.js +0 -475
  133. package/src/services/strategy/hft-tick.js +0 -507
  134. package/src/services/strategy/recovery-math.js +0 -402
  135. package/src/services/tradovate/constants.js +0 -109
  136. package/src/services/tradovate/index.js +0 -505
  137. package/src/services/tradovate/market.js +0 -47
  138. package/src/services/tradovate/websocket.js +0 -97
@@ -1,528 +0,0 @@
1
- /**
2
- * @fileoverview Position Manager for Fast Scalping
3
- * @module services/position-manager
4
- *
5
- * Manages position lifecycle with:
6
- * - 10 second minimum hold (prop firm rule - NON-NEGOTIABLE)
7
- * - Intelligent exit based on momentum (OFI 50%, Kalman 25%, Z-Score 25%)
8
- * - Trailing stop after profit threshold
9
- * - 60 second failsafe exit (NON-NEGOTIABLE)
10
- * - VPIN protection filter
11
- *
12
- * Data source: Rithmic ORDER_PLANT (fills), PNL_PLANT (positions), TICKER_PLANT (prices)
13
- */
14
-
15
- const EventEmitter = require('events');
16
- const { FAST_SCALPING } = require('../config/settings');
17
- const { logger } = require('../utils/logger');
18
- const { MOMENTUM, WEIGHTS } = require('./position-constants');
19
- const { calculateMomentum, getVPIN } = require('./position-momentum');
20
-
21
- const log = logger.scope('PositionMgr');
22
-
23
- /**
24
- * Position Manager Service
25
- * Handles position lifecycle for fast scalping strategy
26
- */
27
- class PositionManager extends EventEmitter {
28
- constructor(rithmicService, strategy = null) {
29
- super();
30
- this.rithmic = rithmicService;
31
- this.strategy = strategy;
32
-
33
- this.positions = new Map();
34
- this.latestPrices = new Map();
35
- this.contractInfo = new Map();
36
-
37
- this._monitorInterval = null;
38
- this._isRunning = false;
39
-
40
- this._onOrderFilled = this._onOrderFilled.bind(this);
41
- this._onPriceUpdate = this._onPriceUpdate.bind(this);
42
- this._onPositionUpdate = this._onPositionUpdate.bind(this);
43
- }
44
-
45
- setStrategy(strategy) {
46
- this.strategy = strategy;
47
- log.debug('Strategy reference set');
48
- }
49
-
50
- setContractInfo(symbol, info) {
51
- this.contractInfo.set(symbol, {
52
- tickSize: info.tickSize,
53
- tickValue: info.tickValue,
54
- contractId: info.contractId,
55
- });
56
- log.debug('Contract info set', { symbol, tickSize: info.tickSize, tickValue: info.tickValue });
57
- }
58
-
59
- start() {
60
- if (this._isRunning) return;
61
-
62
- log.info('Starting position manager', {
63
- minHoldMs: FAST_SCALPING.MIN_HOLD_MS,
64
- maxHoldMs: FAST_SCALPING.MAX_HOLD_MS,
65
- targetTicks: FAST_SCALPING.TARGET_TICKS,
66
- stopTicks: FAST_SCALPING.STOP_TICKS,
67
- momentumWeights: WEIGHTS,
68
- });
69
-
70
- this.rithmic.on('orderFilled', this._onOrderFilled);
71
- this.rithmic.on('priceUpdate', this._onPriceUpdate);
72
- this.rithmic.on('positionUpdate', this._onPositionUpdate);
73
-
74
- this._monitorInterval = setInterval(() => {
75
- this._monitorPositions();
76
- }, FAST_SCALPING.MONITOR_INTERVAL_MS);
77
-
78
- this._isRunning = true;
79
- log.debug('Position manager started');
80
- }
81
-
82
- stop() {
83
- if (!this._isRunning) return;
84
-
85
- log.info('Stopping position manager');
86
-
87
- this.rithmic.off('orderFilled', this._onOrderFilled);
88
- this.rithmic.off('priceUpdate', this._onPriceUpdate);
89
- this.rithmic.off('positionUpdate', this._onPositionUpdate);
90
-
91
- if (this._monitorInterval) {
92
- clearInterval(this._monitorInterval);
93
- this._monitorInterval = null;
94
- }
95
-
96
- this._isRunning = false;
97
- log.debug('Position manager stopped');
98
- }
99
-
100
- registerEntry(entryResult, orderData, contractInfo = null) {
101
- if (!entryResult.success) {
102
- log.warn('Cannot register failed entry', { error: entryResult.error });
103
- return null;
104
- }
105
-
106
- const { orderTag, entryTime, latencyMs } = entryResult;
107
-
108
- const info = contractInfo || this.contractInfo.get(orderData.symbol) || {
109
- tickSize: null,
110
- tickValue: null,
111
- contractId: orderData.contractId || orderData.symbol,
112
- };
113
-
114
- const position = {
115
- orderTag,
116
- accountId: orderData.accountId,
117
- symbol: orderData.symbol,
118
- exchange: orderData.exchange || 'CME',
119
- side: orderData.side,
120
- size: orderData.size,
121
- entryPrice: null,
122
- entryTime,
123
- fillTime: null,
124
- highWaterMark: null,
125
- lowWaterMark: null,
126
- status: 'pending',
127
- holdComplete: false,
128
- breakevenActive: false,
129
- breakevenPrice: null,
130
- exitReason: null,
131
- latencyMs,
132
- tickSize: info.tickSize,
133
- tickValue: info.tickValue,
134
- contractId: info.contractId,
135
- };
136
-
137
- this.positions.set(orderTag, position);
138
-
139
- log.debug('Registered entry', {
140
- orderTag,
141
- symbol: orderData.symbol,
142
- side: orderData.side === 0 ? 'LONG' : 'SHORT',
143
- size: orderData.size,
144
- latencyMs,
145
- tickSize: info.tickSize,
146
- });
147
-
148
- return orderTag;
149
- }
150
-
151
- _onOrderFilled(fillInfo) {
152
- const { orderTag, avgFillPrice, symbol } = fillInfo;
153
-
154
- if (!orderTag) return;
155
-
156
- const position = this.positions.get(orderTag);
157
- if (!position) {
158
- // Check if this is an exit fill
159
- for (const [tag, pos] of this.positions) {
160
- if (pos.status === 'exiting' && pos.symbol === symbol) {
161
- this._handleExitFill(tag, pos, avgFillPrice);
162
- return;
163
- }
164
- }
165
- log.debug('Fill for untracked order', { orderTag });
166
- return;
167
- }
168
-
169
- if (position.status === 'pending') {
170
- this._handleEntryFill(orderTag, position, avgFillPrice);
171
- } else if (position.status === 'exiting') {
172
- this._handleExitFill(orderTag, position, avgFillPrice);
173
- }
174
- }
175
-
176
- _handleEntryFill(orderTag, position, avgFillPrice) {
177
- position.entryPrice = avgFillPrice;
178
- position.fillTime = Date.now();
179
- position.highWaterMark = avgFillPrice;
180
- position.lowWaterMark = avgFillPrice;
181
- position.status = 'holding';
182
-
183
- const fillLatency = position.fillTime - position.entryTime;
184
-
185
- log.info('ENTRY FILLED', {
186
- orderTag,
187
- symbol: position.symbol,
188
- side: position.side === 0 ? 'LONG' : 'SHORT',
189
- size: position.size,
190
- price: avgFillPrice,
191
- entryLatencyMs: position.latencyMs,
192
- fillLatencyMs: fillLatency,
193
- });
194
-
195
- this.emit('entryFilled', { orderTag, position, fillLatencyMs: fillLatency });
196
-
197
- setTimeout(() => this._onHoldComplete(orderTag), FAST_SCALPING.MIN_HOLD_MS);
198
- setTimeout(() => this._failsafeExit(orderTag), FAST_SCALPING.MAX_HOLD_MS);
199
- }
200
-
201
- _handleExitFill(orderTag, position, avgFillPrice) {
202
- position.status = 'closed';
203
- const holdDuration = Date.now() - position.fillTime;
204
- const pnlTicks = this._calculatePnlTicks(position, avgFillPrice);
205
-
206
- log.info('EXIT FILLED', {
207
- orderTag,
208
- symbol: position.symbol,
209
- exitPrice: avgFillPrice,
210
- entryPrice: position.entryPrice,
211
- pnlTicks,
212
- holdDurationMs: holdDuration,
213
- reason: position.exitReason,
214
- });
215
-
216
- this.emit('exitFilled', {
217
- orderTag,
218
- position,
219
- exitPrice: avgFillPrice,
220
- pnlTicks,
221
- holdDurationMs: holdDuration,
222
- });
223
-
224
- this.positions.delete(orderTag);
225
- }
226
-
227
- _failsafeExit(orderTag) {
228
- const position = this.positions.get(orderTag);
229
- if (!position) return;
230
-
231
- if (position.status === 'holding' || position.status === 'active') {
232
- const currentPrice = this.latestPrices.get(position.symbol);
233
- const pnlTicks = currentPrice ? this._calculatePnlTicks(position, currentPrice) : 0;
234
-
235
- log.warn('FAILSAFE EXIT - 60s max hold exceeded', { orderTag, symbol: position.symbol, pnlTicks });
236
-
237
- this._executeExit(orderTag, {
238
- type: 'failsafe',
239
- reason: '60s max hold exceeded (NON-NEGOTIABLE)',
240
- pnlTicks,
241
- });
242
- }
243
- }
244
-
245
- _onPriceUpdate(priceData) {
246
- const { symbol, price } = priceData;
247
-
248
- this.latestPrices.set(symbol, price);
249
-
250
- for (const [, position] of this.positions) {
251
- if (position.symbol === symbol && (position.status === 'holding' || position.status === 'active') && position.entryPrice) {
252
- if (position.side === 0) {
253
- position.highWaterMark = Math.max(position.highWaterMark, price);
254
- } else {
255
- position.lowWaterMark = Math.min(position.lowWaterMark, price);
256
- }
257
- }
258
- }
259
- }
260
-
261
- _onPositionUpdate(posData) {
262
- log.debug('Position update from PNL_PLANT', { symbol: posData?.symbol, qty: posData?.quantity });
263
- }
264
-
265
- _onHoldComplete(orderTag) {
266
- const position = this.positions.get(orderTag);
267
- if (!position) return;
268
-
269
- if (position.status === 'holding') {
270
- position.holdComplete = true;
271
- position.status = 'active';
272
-
273
- log.info('Hold complete - now monitoring for exit', {
274
- orderTag,
275
- symbol: position.symbol,
276
- entryPrice: position.entryPrice,
277
- });
278
-
279
- this.emit('holdComplete', { orderTag, position });
280
- }
281
- }
282
-
283
- _monitorPositions() {
284
- const now = Date.now();
285
-
286
- for (const [orderTag, position] of this.positions) {
287
- if (position.status !== 'active') continue;
288
-
289
- const currentPrice = this.latestPrices.get(position.symbol);
290
- if (!currentPrice) continue;
291
-
292
- const holdDuration = now - position.fillTime;
293
- const pnlTicks = this._calculatePnlTicks(position, currentPrice);
294
-
295
- const exitReason = this._checkExitConditions(position, currentPrice, pnlTicks, holdDuration);
296
-
297
- if (exitReason) {
298
- this._executeExit(orderTag, exitReason);
299
- }
300
- }
301
- }
302
-
303
- _checkExitConditions(position, currentPrice, pnlTicks) {
304
- if (pnlTicks === null) return null;
305
-
306
- const targetTicks = FAST_SCALPING.TARGET_TICKS;
307
- const stopTicks = FAST_SCALPING.STOP_TICKS;
308
-
309
- // 1. TARGET HIT
310
- if (pnlTicks >= targetTicks) {
311
- return { type: 'target', reason: 'Target reached', pnlTicks };
312
- }
313
-
314
- // 2. BREAKEVEN CHECK
315
- if (position.breakevenActive && position.breakevenPrice !== null) {
316
- const tickSize = this._getTickSize(position);
317
- if (tickSize) {
318
- if (position.side === 0 && currentPrice <= position.breakevenPrice) {
319
- return { type: 'breakeven', reason: 'Breakeven stop hit', pnlTicks };
320
- } else if (position.side === 1 && currentPrice >= position.breakevenPrice) {
321
- return { type: 'breakeven', reason: 'Breakeven stop hit', pnlTicks };
322
- }
323
- }
324
- }
325
-
326
- // 3. STOP HIT (only if BE not active)
327
- if (!position.breakevenActive && pnlTicks <= -stopTicks) {
328
- return { type: 'stop', reason: 'Stop loss hit', pnlTicks };
329
- }
330
-
331
- // 4. ACTIVATE BREAKEVEN
332
- if (!position.breakevenActive && pnlTicks >= FAST_SCALPING.BREAKEVEN_ACTIVATION_TICKS) {
333
- const tickSize = this._getTickSize(position);
334
- if (tickSize && position.entryPrice) {
335
- const offset = FAST_SCALPING.BREAKEVEN_OFFSET_TICKS * tickSize;
336
- position.breakevenPrice = position.side === 0
337
- ? position.entryPrice + offset
338
- : position.entryPrice - offset;
339
- position.breakevenActive = true;
340
-
341
- log.info('BREAKEVEN ACTIVATED', {
342
- symbol: position.symbol,
343
- entryPrice: position.entryPrice,
344
- bePrice: position.breakevenPrice,
345
- pnlTicks,
346
- });
347
-
348
- this.emit('breakevenActivated', {
349
- orderTag: position.orderTag,
350
- position,
351
- breakevenPrice: position.breakevenPrice,
352
- pnlTicks,
353
- });
354
- }
355
- }
356
-
357
- // 5. VPIN DANGER
358
- const vpin = getVPIN(this.strategy, position.contractId || position.symbol);
359
- if (vpin !== null && vpin > MOMENTUM.VPIN_DANGER) {
360
- return { type: 'vpin', reason: `VPIN spike ${(vpin * 100).toFixed(0)}% - informed traders`, pnlTicks, vpin };
361
- }
362
-
363
- // 6. TRAILING STOP
364
- if (pnlTicks >= FAST_SCALPING.TRAILING_ACTIVATION_TICKS) {
365
- const trailingPnl = this._calculateTrailingPnl(position, currentPrice);
366
- if (trailingPnl !== null && trailingPnl <= -FAST_SCALPING.TRAILING_DISTANCE_TICKS) {
367
- return { type: 'trailing', reason: 'Trailing stop triggered', pnlTicks, trailingPnl };
368
- }
369
- }
370
-
371
- // 7. MOMENTUM-BASED EXIT
372
- const momentum = calculateMomentum({
373
- strategy: this.strategy,
374
- contractId: position.contractId || position.symbol,
375
- side: position.side,
376
- currentPrice: this.latestPrices.get(position.symbol),
377
- tickSize: this._getTickSize(position),
378
- });
379
-
380
- if (momentum !== null) {
381
- if (momentum > MOMENTUM.STRONG_FAVORABLE && pnlTicks > 4) {
382
- return null; // Don't exit - momentum is strong
383
- }
384
-
385
- if (momentum < MOMENTUM.WEAK_THRESHOLD && pnlTicks > 0) {
386
- return { type: 'momentum_weak', reason: 'Weak momentum - securing profit', pnlTicks, momentum };
387
- }
388
-
389
- if (momentum < MOMENTUM.ADVERSE_THRESHOLD) {
390
- return { type: 'momentum_adverse', reason: 'Adverse momentum detected', pnlTicks, momentum };
391
- }
392
- }
393
-
394
- return null;
395
- }
396
-
397
- _executeExit(orderTag, exitReason) {
398
- const position = this.positions.get(orderTag);
399
- if (!position || position.status === 'exiting' || position.status === 'closed') return;
400
-
401
- position.status = 'exiting';
402
- position.exitReason = exitReason;
403
-
404
- log.info('Executing EXIT', {
405
- orderTag,
406
- symbol: position.symbol,
407
- reason: exitReason.reason,
408
- pnlTicks: exitReason.pnlTicks,
409
- momentum: exitReason.momentum,
410
- });
411
-
412
- const exitSide = position.side === 0 ? 1 : 0;
413
-
414
- const exitResult = this.rithmic.fastExit({
415
- accountId: position.accountId,
416
- symbol: position.symbol,
417
- exchange: position.exchange,
418
- size: position.size,
419
- side: exitSide,
420
- });
421
-
422
- if (exitResult.success) {
423
- log.debug('Exit order fired', {
424
- orderTag: position.orderTag,
425
- exitOrderTag: exitResult.orderTag,
426
- latencyMs: exitResult.latencyMs,
427
- });
428
-
429
- this.emit('exitOrderFired', {
430
- orderTag,
431
- exitOrderTag: exitResult.orderTag,
432
- exitReason,
433
- latencyMs: exitResult.latencyMs,
434
- });
435
- } else {
436
- log.error('Exit order FAILED', { orderTag, error: exitResult.error });
437
- position.status = 'active';
438
- position.exitReason = null;
439
- }
440
- }
441
-
442
- _getTickSize(position) {
443
- if (position.tickSize !== null && position.tickSize !== undefined) {
444
- return position.tickSize;
445
- }
446
-
447
- const info = this.contractInfo.get(position.symbol);
448
- if (info && info.tickSize) {
449
- return info.tickSize;
450
- }
451
-
452
- log.warn('No tick size available for symbol', { symbol: position.symbol });
453
- return null;
454
- }
455
-
456
- _getTickValue(position) {
457
- if (position.tickValue !== null && position.tickValue !== undefined) {
458
- return position.tickValue;
459
- }
460
-
461
- const info = this.contractInfo.get(position.symbol);
462
- if (info && info.tickValue) {
463
- return info.tickValue;
464
- }
465
-
466
- log.warn('No tick value available for symbol', { symbol: position.symbol });
467
- return null;
468
- }
469
-
470
- _calculatePnlTicks(position, currentPrice) {
471
- if (position.entryPrice === null || currentPrice === null) return null;
472
-
473
- const tickSize = this._getTickSize(position);
474
- if (tickSize === null) {
475
- log.error('Cannot calculate PnL - no tick size from API', { symbol: position.symbol });
476
- return null;
477
- }
478
-
479
- const priceDiff = currentPrice - position.entryPrice;
480
- const signedDiff = position.side === 0 ? priceDiff : -priceDiff;
481
-
482
- return Math.round(signedDiff / tickSize);
483
- }
484
-
485
- _calculateTrailingPnl(position, currentPrice) {
486
- const tickSize = this._getTickSize(position);
487
- if (tickSize === null) return null;
488
-
489
- if (position.side === 0) {
490
- if (position.highWaterMark === null) return null;
491
- const dropFromHigh = position.highWaterMark - currentPrice;
492
- return -Math.round(dropFromHigh / tickSize);
493
- } else {
494
- if (position.lowWaterMark === null) return null;
495
- const riseFromLow = currentPrice - position.lowWaterMark;
496
- return -Math.round(riseFromLow / tickSize);
497
- }
498
- }
499
-
500
- getActivePositions() {
501
- return Array.from(this.positions.values()).filter(
502
- p => p.status === 'holding' || p.status === 'active'
503
- );
504
- }
505
-
506
- getPosition(orderTag) {
507
- return this.positions.get(orderTag) || null;
508
- }
509
-
510
- canEnter(symbol) {
511
- for (const position of this.positions.values()) {
512
- if (position.symbol === symbol && position.status !== 'closed') {
513
- return false;
514
- }
515
- }
516
- return true;
517
- }
518
-
519
- getMomentumThresholds() {
520
- return { ...MOMENTUM };
521
- }
522
-
523
- getMomentumWeights() {
524
- return { ...WEIGHTS };
525
- }
526
- }
527
-
528
- module.exports = { PositionManager };