hedgequantx 2.6.28 → 2.6.30

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.28",
3
+ "version": "2.6.30",
4
4
  "description": "HedgeQuantX - Prop Futures Trading CLI",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -16,12 +16,16 @@ const { checkMarketHours } = require('../../services/projectx/market');
16
16
  const { FAST_SCALPING } = require('../../config/settings');
17
17
  const { PositionManager } = require('../../services/position-manager');
18
18
 
19
- // Strategy & Market Data (obfuscated)
19
+ // Strategy & Market Data
20
20
  const { M1 } = require('../../../dist/lib/m/s1');
21
+ const { hftStrategy } = require('../../services/strategy/hft-tick');
21
22
  const { MarketDataFeed } = require('../../../dist/lib/data');
22
23
  const { RithmicMarketDataFeed } = require('../../services/rithmic/market-data');
23
24
  const { algoLogger } = require('./logger');
24
25
 
26
+ // Use HFT tick-based strategy for Rithmic (fast path), M1 for ProjectX
27
+ const USE_HFT_STRATEGY = true;
28
+
25
29
  // AI Strategy Supervisor - observes, learns, and optimizes the strategy
26
30
  const aiService = require('../../services/ai');
27
31
  const StrategySupervisor = require('../../services/ai/strategy-supervisor');
@@ -263,13 +267,15 @@ const launchAlgo = async (service, account, contract, config) => {
263
267
  let lastTradeCount = 0; // Track number of trades from API
264
268
  let lastPositionQty = 0; // Track position changes
265
269
 
266
- // Initialize Strategy FIRST (M1 is singleton instance)
267
- // Strategy needs to be initialized before PositionManager so it can access math models
268
- const strategy = M1;
270
+ // Initialize Strategy
271
+ // Use HFT tick-based strategy for Rithmic (fast path), M1 for ProjectX
272
+ const strategy = (USE_HFT_STRATEGY && useFastPath) ? hftStrategy : M1;
273
+ const strategyName = (USE_HFT_STRATEGY && useFastPath) ? 'HFT-TICK' : 'M1';
269
274
 
270
- // Only initialize strategy if we have tick data from API
275
+ // Initialize strategy with tick specs
271
276
  if (tickSize !== null && tickValue !== null) {
272
277
  strategy.initialize(contractId, tickSize, tickValue);
278
+ algoLogger.info(ui, 'STRATEGY', `${strategyName} initialized | tick=${tickSize} value=$${tickValue}`);
273
279
  } else {
274
280
  algoLogger.warning(ui, 'WARNING', 'Tick size/value not available from API');
275
281
  }
@@ -588,15 +594,19 @@ const launchAlgo = async (service, account, contract, config) => {
588
594
  // Heartbeat every 30 seconds - show strategy model values
589
595
  if (Date.now() - lastHeartbeat > 30000) {
590
596
  // Try to get model values from strategy
591
- const modelValues = strategy.getModelValues?.(contractId) || strategy.getState?.() || null;
592
- if (modelValues && (modelValues.ofi !== undefined || modelValues.delta !== undefined)) {
593
- const ofi = (modelValues.ofi || 0).toFixed(1);
594
- const delta = (modelValues.delta || 0).toFixed(1);
595
- const zscore = (modelValues.zscore || modelValues.zScore || 0).toFixed(2);
596
- const momentum = modelValues.momentum ? (modelValues.momentum > 0 ? 'BULL' : 'BEAR') : 'FLAT';
597
- algoLogger.info(ui, 'M1 STATUS', `OFI=${ofi} Delta=${delta} Z=${zscore} ${momentum} | ${tps} ticks/30s`);
597
+ const modelValues = strategy.getModelValues?.() || strategy.getModelValues?.(contractId) || null;
598
+
599
+ if (modelValues && modelValues.ofi !== undefined) {
600
+ // HFT Strategy - pure tick values
601
+ const ofi = (modelValues.ofi || 0).toFixed(2);
602
+ const delta = modelValues.delta || 0;
603
+ const zscore = (modelValues.zscore || 0).toFixed(2);
604
+ const mom = (modelValues.momentum || 0).toFixed(1);
605
+ const signals = modelValues.signalCount || 0;
606
+ const deltaStr = delta >= 0 ? `+${delta}` : `${delta}`;
607
+ algoLogger.info(ui, strategyName, `OFI=${ofi} Z=${zscore} Mom=${mom} Δ=${deltaStr} | ${signals} signals | ${tps} t/30s`);
598
608
  } else {
599
- // Show tick count and price to confirm data is flowing
609
+ // Fallback
600
610
  algoLogger.info(ui, 'SCANNING', `${tps} ticks/30s | ${tickCount} total | price=${tickData.price}`);
601
611
  }
602
612
  lastHeartbeat = Date.now();
@@ -0,0 +1,507 @@
1
+ /**
2
+ * =============================================================================
3
+ * HFT TICK-BY-TICK STRATEGY
4
+ * =============================================================================
5
+ * Pure tick-based strategy for high-frequency trading
6
+ * No bars, no candles - every tick counts
7
+ *
8
+ * MODELS:
9
+ * 1. Order Flow Imbalance (OFI) - Real-time buy/sell pressure
10
+ * 2. Cumulative Delta - Net volume direction
11
+ * 3. Bid/Ask Pressure - Spread and size analysis
12
+ * 4. Momentum - Price velocity and acceleration
13
+ * 5. Mean Reversion - Z-score on tick prices
14
+ *
15
+ * SIGNALS:
16
+ * - Generated on each tick when conditions align
17
+ * - Sub-second decision making
18
+ * - Designed for <50ms execution
19
+ */
20
+
21
+ const EventEmitter = require('events');
22
+ const { logger } = require('../../utils/logger');
23
+
24
+ const log = logger.scope('HFT');
25
+
26
+ /**
27
+ * Circular buffer for efficient tick storage
28
+ */
29
+ class CircularBuffer {
30
+ constructor(size) {
31
+ this.size = size;
32
+ this.buffer = new Array(size);
33
+ this.head = 0;
34
+ this.count = 0;
35
+ }
36
+
37
+ push(item) {
38
+ this.buffer[this.head] = item;
39
+ this.head = (this.head + 1) % this.size;
40
+ if (this.count < this.size) this.count++;
41
+ }
42
+
43
+ get(index) {
44
+ if (index >= this.count) return null;
45
+ const actualIndex = (this.head - this.count + index + this.size) % this.size;
46
+ return this.buffer[actualIndex];
47
+ }
48
+
49
+ getLast(n = 1) {
50
+ const result = [];
51
+ const count = Math.min(n, this.count);
52
+ for (let i = this.count - count; i < this.count; i++) {
53
+ result.push(this.get(i));
54
+ }
55
+ return result;
56
+ }
57
+
58
+ getAll() {
59
+ return this.getLast(this.count);
60
+ }
61
+
62
+ get length() {
63
+ return this.count;
64
+ }
65
+
66
+ clear() {
67
+ this.head = 0;
68
+ this.count = 0;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * HFT Tick Strategy
74
+ */
75
+ class HFTTickStrategy extends EventEmitter {
76
+ constructor() {
77
+ super();
78
+
79
+ // Configuration - MUST be set via initialize() with API data
80
+ this.tickSize = null;
81
+ this.tickValue = null;
82
+ this.contractId = null;
83
+ this.initialized = false;
84
+
85
+ // Tick buffers (circular for memory efficiency)
86
+ this.tickBuffer = new CircularBuffer(1000); // Last 1000 ticks
87
+ this.priceBuffer = new CircularBuffer(500); // Last 500 prices
88
+
89
+ // Real-time metrics
90
+ this.cumulativeDelta = 0;
91
+ this.buyVolume = 0;
92
+ this.sellVolume = 0;
93
+ this.lastPrice = 0;
94
+ this.lastBid = 0;
95
+ this.lastAsk = 0;
96
+
97
+ // ═══════════════════════════════════════════════════════════════════════
98
+ // STRATEGY CONFIGURATION PARAMETERS
99
+ // These are algorithm tuning parameters, NOT trading data
100
+ // They define HOW the strategy calculates signals from real tick data
101
+ // ═══════════════════════════════════════════════════════════════════════
102
+
103
+ // OFI calculation windows (algorithm config)
104
+ this.ofiWindow = 50; // Number of ticks for OFI calculation
105
+ this.ofiValue = 0; // Calculated from real ticks
106
+
107
+ // Momentum windows (algorithm config)
108
+ this.momentumWindow = 20; // Number of ticks for momentum
109
+ this.momentum = 0; // Calculated from real tick prices
110
+ this.acceleration = 0; // Calculated from real tick prices
111
+
112
+ // Mean reversion windows (algorithm config)
113
+ this.zscoreWindow = 100; // Number of ticks for z-score
114
+ this.zscore = 0; // Calculated from real tick prices
115
+ this.mean = 0; // Calculated from real tick prices
116
+ this.std = 0; // Calculated from real tick prices
117
+
118
+ // Signal thresholds (algorithm tuning - from backtests)
119
+ this.ofiThreshold = 0.3; // |OFI| threshold for signal generation
120
+ this.zscoreThreshold = 1.5; // |Z| threshold for mean reversion
121
+ this.momentumThreshold = 0.5; // Momentum threshold for confirmation
122
+ this.minConfidence = 0.6; // Minimum composite score for signal
123
+
124
+ // Risk parameters in ticks (algorithm config)
125
+ this.baseStopTicks = 8; // Base stop distance in ticks
126
+ this.baseTargetTicks = 12; // Base target distance in ticks
127
+
128
+ // State tracking
129
+ this.tickCount = 0; // Counter from real ticks received
130
+ this.signalCount = 0; // Counter of signals generated
131
+ this.lastSignalTime = 0; // Timestamp of last signal
132
+ this.cooldownMs = 5000; // Cooldown between signals (ms)
133
+ }
134
+
135
+ /**
136
+ * Initialize strategy for a contract
137
+ * @param {string} contractId - Contract identifier from API
138
+ * @param {number} tickSize - Tick size from API (REQUIRED - no default)
139
+ * @param {number} tickValue - Tick value from API (REQUIRED - no default)
140
+ */
141
+ initialize(contractId, tickSize, tickValue) {
142
+ if (!contractId || tickSize === undefined || tickValue === undefined) {
143
+ throw new Error('HFT Strategy requires contractId, tickSize, and tickValue from API');
144
+ }
145
+
146
+ this.contractId = contractId;
147
+ this.tickSize = tickSize;
148
+ this.tickValue = tickValue;
149
+ this.initialized = true;
150
+
151
+ // Reset state
152
+ this.tickBuffer.clear();
153
+ this.priceBuffer.clear();
154
+ this.cumulativeDelta = 0;
155
+ this.buyVolume = 0;
156
+ this.sellVolume = 0;
157
+ this.tickCount = 0;
158
+
159
+ log.info(`Initialized for ${contractId}: tick=${tickSize}, value=${tickValue}`);
160
+ }
161
+
162
+ /**
163
+ * Process a single tick - CORE HFT FUNCTION
164
+ * Called on every market tick
165
+ */
166
+ processTick(tick) {
167
+ if (!this.initialized) return;
168
+
169
+ const { price, bid, ask, volume, side, timestamp } = tick;
170
+
171
+ // Store tick
172
+ this.tickBuffer.push({
173
+ price,
174
+ bid: bid || this.lastBid,
175
+ ask: ask || this.lastAsk,
176
+ volume: volume || 1,
177
+ side: side || this.inferSide(price, bid, ask),
178
+ timestamp: timestamp || Date.now(),
179
+ });
180
+
181
+ this.priceBuffer.push(price);
182
+ this.tickCount++;
183
+
184
+ // Update last values
185
+ this.lastPrice = price;
186
+ if (bid) this.lastBid = bid;
187
+ if (ask) this.lastAsk = ask;
188
+
189
+ // Update real-time metrics
190
+ this._updateDelta(tick);
191
+ this._updateOFI();
192
+ this._updateMomentum();
193
+ this._updateZScore();
194
+
195
+ // Check for signal (every tick!)
196
+ this._checkSignal(tick);
197
+ }
198
+
199
+ /**
200
+ * Infer trade side from price position
201
+ */
202
+ inferSide(price, bid, ask) {
203
+ if (!bid || !ask) return 'unknown';
204
+ const mid = (bid + ask) / 2;
205
+ return price >= mid ? 'BUY' : 'SELL';
206
+ }
207
+
208
+ /**
209
+ * Update cumulative delta
210
+ */
211
+ _updateDelta(tick) {
212
+ const vol = tick.volume || 1;
213
+ const side = tick.side?.toUpperCase() || this.inferSide(tick.price, tick.bid, tick.ask);
214
+
215
+ if (side === 'BUY') {
216
+ this.buyVolume += vol;
217
+ this.cumulativeDelta += vol;
218
+ } else if (side === 'SELL') {
219
+ this.sellVolume += vol;
220
+ this.cumulativeDelta -= vol;
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Calculate Order Flow Imbalance on recent ticks
226
+ */
227
+ _updateOFI() {
228
+ const ticks = this.tickBuffer.getLast(this.ofiWindow);
229
+ if (ticks.length < 10) {
230
+ this.ofiValue = 0;
231
+ return;
232
+ }
233
+
234
+ let buyPressure = 0;
235
+ let sellPressure = 0;
236
+
237
+ for (const tick of ticks) {
238
+ const vol = tick.volume || 1;
239
+ const side = (tick.side || '').toUpperCase();
240
+
241
+ if (side === 'BUY') {
242
+ buyPressure += vol;
243
+ } else if (side === 'SELL') {
244
+ sellPressure += vol;
245
+ }
246
+ }
247
+
248
+ const total = buyPressure + sellPressure;
249
+ if (total > 0) {
250
+ // OFI range: -1 (all sells) to +1 (all buys)
251
+ this.ofiValue = (buyPressure - sellPressure) / total;
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Calculate price momentum
257
+ */
258
+ _updateMomentum() {
259
+ const prices = this.priceBuffer.getLast(this.momentumWindow);
260
+ if (prices.length < 5) {
261
+ this.momentum = 0;
262
+ this.acceleration = 0;
263
+ return;
264
+ }
265
+
266
+ // Momentum = price change over window
267
+ const first = prices[0];
268
+ const last = prices[prices.length - 1];
269
+ this.momentum = (last - first) / this.tickSize;
270
+
271
+ // Acceleration = change in momentum
272
+ if (prices.length >= 10) {
273
+ const mid = prices[Math.floor(prices.length / 2)];
274
+ const firstHalf = (mid - first) / this.tickSize;
275
+ const secondHalf = (last - mid) / this.tickSize;
276
+ this.acceleration = secondHalf - firstHalf;
277
+ }
278
+ }
279
+
280
+ /**
281
+ * Calculate Z-Score for mean reversion
282
+ */
283
+ _updateZScore() {
284
+ const prices = this.priceBuffer.getLast(this.zscoreWindow);
285
+ if (prices.length < 20) {
286
+ this.zscore = 0;
287
+ return;
288
+ }
289
+
290
+ // Calculate mean
291
+ this.mean = prices.reduce((a, b) => a + b, 0) / prices.length;
292
+
293
+ // Calculate standard deviation
294
+ const variance = prices.reduce((sum, p) => sum + Math.pow(p - this.mean, 2), 0) / prices.length;
295
+ this.std = Math.sqrt(variance);
296
+
297
+ // Z-Score
298
+ if (this.std > 0.0001) {
299
+ this.zscore = (this.lastPrice - this.mean) / this.std;
300
+ } else {
301
+ this.zscore = 0;
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Check for trading signal
307
+ */
308
+ _checkSignal(tick) {
309
+ // Need minimum data
310
+ if (this.tickCount < 50) return;
311
+
312
+ // Cooldown check
313
+ const now = Date.now();
314
+ if (now - this.lastSignalTime < this.cooldownMs) return;
315
+
316
+ // Calculate composite score
317
+ const { direction, confidence, scores } = this._calculateSignal();
318
+
319
+ if (direction === 'none' || confidence < this.minConfidence) return;
320
+
321
+ // Generate signal
322
+ this.signalCount++;
323
+ this.lastSignalTime = now;
324
+
325
+ const signal = this._buildSignal(direction, confidence, scores, tick);
326
+
327
+ log.info(`SIGNAL #${this.signalCount}: ${direction.toUpperCase()} @ ${tick.price} | ` +
328
+ `OFI=${this.ofiValue.toFixed(2)} Z=${this.zscore.toFixed(2)} Mom=${this.momentum.toFixed(1)} | ` +
329
+ `Conf=${(confidence * 100).toFixed(0)}%`);
330
+
331
+ this.emit('signal', signal);
332
+ }
333
+
334
+ /**
335
+ * Calculate trading signal from all models
336
+ */
337
+ _calculateSignal() {
338
+ let direction = 'none';
339
+ let confidence = 0;
340
+
341
+ const scores = {
342
+ ofi: 0,
343
+ zscore: 0,
344
+ momentum: 0,
345
+ delta: 0,
346
+ composite: 0,
347
+ };
348
+
349
+ // === MODEL 1: OFI ===
350
+ const absOfi = Math.abs(this.ofiValue);
351
+ if (absOfi > this.ofiThreshold) {
352
+ scores.ofi = Math.min(1.0, absOfi / 0.6);
353
+ }
354
+
355
+ // === MODEL 2: Z-Score Mean Reversion ===
356
+ const absZ = Math.abs(this.zscore);
357
+ if (absZ > this.zscoreThreshold) {
358
+ scores.zscore = Math.min(1.0, absZ / 3.0);
359
+ }
360
+
361
+ // === MODEL 3: Momentum ===
362
+ const absMom = Math.abs(this.momentum);
363
+ if (absMom > this.momentumThreshold) {
364
+ scores.momentum = Math.min(1.0, absMom / 3.0);
365
+ }
366
+
367
+ // === MODEL 4: Delta ===
368
+ const totalVol = this.buyVolume + this.sellVolume;
369
+ if (totalVol > 0) {
370
+ const deltaRatio = this.cumulativeDelta / totalVol;
371
+ scores.delta = Math.min(1.0, Math.abs(deltaRatio) * 2);
372
+ }
373
+
374
+ // === COMPOSITE SCORE ===
375
+ scores.composite =
376
+ scores.ofi * 0.35 + // OFI: 35%
377
+ scores.zscore * 0.25 + // Z-Score: 25%
378
+ scores.momentum * 0.20 + // Momentum: 20%
379
+ scores.delta * 0.20; // Delta: 20%
380
+
381
+ confidence = scores.composite;
382
+
383
+ // === DETERMINE DIRECTION ===
384
+ // Mean reversion: go opposite to z-score
385
+ // Momentum: confirm with OFI and delta
386
+
387
+ if (scores.composite >= this.minConfidence) {
388
+ // Primary: Mean reversion
389
+ if (absZ > this.zscoreThreshold) {
390
+ direction = this.zscore > 0 ? 'short' : 'long';
391
+
392
+ // Confirm with OFI
393
+ const ofiConfirms =
394
+ (direction === 'long' && this.ofiValue > 0) ||
395
+ (direction === 'short' && this.ofiValue < 0);
396
+
397
+ if (ofiConfirms) {
398
+ confidence += 0.1;
399
+ } else if (Math.abs(this.ofiValue) > 0.2) {
400
+ // OFI contradicts - reduce confidence
401
+ confidence -= 0.15;
402
+ }
403
+ }
404
+ // Fallback: Momentum breakout
405
+ else if (absMom > this.momentumThreshold * 2 && absOfi > this.ofiThreshold) {
406
+ direction = this.momentum > 0 ? 'long' : 'short';
407
+ // Must be confirmed by OFI
408
+ if ((direction === 'long' && this.ofiValue < 0) ||
409
+ (direction === 'short' && this.ofiValue > 0)) {
410
+ direction = 'none';
411
+ }
412
+ }
413
+ }
414
+
415
+ confidence = Math.min(1.0, Math.max(0, confidence));
416
+
417
+ return { direction, confidence, scores };
418
+ }
419
+
420
+ /**
421
+ * Build signal object
422
+ */
423
+ _buildSignal(direction, confidence, scores, tick) {
424
+ const entry = tick.price;
425
+ const isLong = direction === 'long';
426
+
427
+ // Adaptive stops based on volatility (std dev)
428
+ const volMult = Math.max(0.5, Math.min(2.0, this.std / this.tickSize / 4));
429
+ const stopTicks = Math.round(this.baseStopTicks * volMult);
430
+ const targetTicks = Math.round(this.baseTargetTicks * volMult);
431
+
432
+ const stopLoss = isLong
433
+ ? entry - stopTicks * this.tickSize
434
+ : entry + stopTicks * this.tickSize;
435
+
436
+ const takeProfit = isLong
437
+ ? entry + targetTicks * this.tickSize
438
+ : entry - targetTicks * this.tickSize;
439
+
440
+ return {
441
+ id: `hft-${Date.now()}-${this.signalCount}`,
442
+ timestamp: Date.now(),
443
+ contractId: this.contractId,
444
+ direction,
445
+ side: isLong ? 0 : 1,
446
+ entry,
447
+ stopLoss,
448
+ takeProfit,
449
+ confidence,
450
+ scores,
451
+ // Model values for logging
452
+ ofi: this.ofiValue,
453
+ zscore: this.zscore,
454
+ momentum: this.momentum,
455
+ delta: this.cumulativeDelta,
456
+ // Tick count
457
+ tickCount: this.tickCount,
458
+ };
459
+ }
460
+
461
+ /**
462
+ * Get current model values for monitoring
463
+ */
464
+ getModelValues() {
465
+ return {
466
+ ofi: this.ofiValue,
467
+ delta: this.cumulativeDelta,
468
+ zscore: this.zscore,
469
+ momentum: this.momentum,
470
+ mean: this.mean,
471
+ std: this.std,
472
+ buyVolume: this.buyVolume,
473
+ sellVolume: this.sellVolume,
474
+ tickCount: this.tickCount,
475
+ signalCount: this.signalCount,
476
+ };
477
+ }
478
+
479
+ /**
480
+ * Get state for display
481
+ */
482
+ getState() {
483
+ return this.getModelValues();
484
+ }
485
+
486
+ /**
487
+ * Reset strategy state
488
+ */
489
+ reset() {
490
+ this.tickBuffer.clear();
491
+ this.priceBuffer.clear();
492
+ this.cumulativeDelta = 0;
493
+ this.buyVolume = 0;
494
+ this.sellVolume = 0;
495
+ this.tickCount = 0;
496
+ this.signalCount = 0;
497
+ this.ofiValue = 0;
498
+ this.zscore = 0;
499
+ this.momentum = 0;
500
+ log.info('Strategy reset');
501
+ }
502
+ }
503
+
504
+ // Singleton instance
505
+ const hftStrategy = new HFTTickStrategy();
506
+
507
+ module.exports = { HFTTickStrategy, hftStrategy };