hedgequantx 2.9.235 → 2.9.236

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.
@@ -13,18 +13,44 @@
13
13
 
14
14
  /**
15
15
  * MODEL 1: Z-SCORE MEAN REVERSION
16
+ * Matches Python backtest: calculates from first tick using cumulative method
16
17
  * @param {number[]} prices - Price array
17
18
  * @param {number} window - Lookback window
18
19
  * @returns {number} Z-Score value
19
20
  */
20
21
  function computeZScore(prices, window = 50) {
21
- if (prices.length < window) return 0;
22
- const recentPrices = prices.slice(-window);
23
- const mean = recentPrices.reduce((a, b) => a + b, 0) / window;
24
- const variance = recentPrices.reduce((sum, p) => sum + Math.pow(p - mean, 2), 0) / window;
25
- const std = Math.sqrt(variance);
22
+ if (prices.length === 0) return 0;
23
+
24
+ const currentPrice = prices[prices.length - 1];
25
+
26
+ // Use all available prices (like Python backtest)
27
+ const availablePrices = prices.length < window ? prices : prices.slice(-window);
28
+ const n = availablePrices.length;
29
+
30
+ // Calculate mean
31
+ const mean = availablePrices.reduce((a, b) => a + b, 0) / n;
32
+
33
+ // Calculate variance (cumulative method like Python)
34
+ const sumSquares = availablePrices.reduce((sum, p) => sum + p * p, 0);
35
+ const variance = (sumSquares / n) - (mean * mean);
36
+
37
+ // If we have enough data (100+), blend cumulative and rolling std (like Python)
38
+ let std;
39
+ if (prices.length >= 100) {
40
+ const cumulativeStd = Math.sqrt(Math.max(0, variance));
41
+ const recentPrices = prices.slice(-100);
42
+ const recentMean = recentPrices.reduce((a, b) => a + b, 0) / 100;
43
+ const recentVariance = recentPrices.reduce((sum, p) => sum + Math.pow(p - recentMean, 2), 0) / 100;
44
+ const rollingStd = Math.sqrt(recentVariance);
45
+ // Blend: 30% cumulative, 70% rolling (like Python)
46
+ std = cumulativeStd * 0.3 + rollingStd * 0.7;
47
+ } else {
48
+ std = Math.sqrt(Math.max(0, variance));
49
+ }
50
+
26
51
  if (std < 0.0001) return 0;
27
- return (prices[prices.length - 1] - mean) / std;
52
+
53
+ return (currentPrice - mean) / std;
28
54
  }
29
55
 
30
56
  /**
@@ -4,28 +4,27 @@
4
4
  * =============================================================================
5
5
  * 6 Mathematical Models with 4-Layer Trailing Stop System
6
6
  *
7
- * BACKTEST RESULTS (162 tests, V4):
8
- * - Net P&L: $195,272.52
9
- * - Win Rate: 86.3%
10
- * - Profit Factor: 34.44
11
- * - Sharpe: 1.29
12
- * - Tests Passed: 150/162 (92.6%)
7
+ * BACKTEST RESULTS (Jan 2020 - Nov 2025, 1667 files):
8
+ * - Net P&L: $2,012,373.75
9
+ * - Trades: 146,685
10
+ * - Win Rate: 71.1%
11
+ * - Avg P&L/Trade: $13.72
12
+ * - Exit Types: Z-Score 79.3%, Stops 11.5%, Trails 5.8%, Targets 3.4%
13
13
  *
14
- * MATHEMATICAL MODELS:
15
- * 1. Z-Score Mean Reversion (Entry: |Z| > threshold, Exit: |Z| < 0.5)
16
- * 2. VPIN (Volume-Synchronized Probability of Informed Trading)
17
- * 3. Kyle's Lambda (Price Impact / Liquidity Measurement)
18
- * 4. Kalman Filter (Signal Extraction from Noise)
19
- * 5. Volatility Regime Detection (Low/Normal/High adaptive)
20
- * 6. Order Flow Imbalance (OFI) - Directional Bias Confirmation
14
+ * MATHEMATICAL MODELS (Weighted Composite):
15
+ * 1. Z-Score Mean Reversion (30%) - Entry: |Z| > 2.5, Exit: |Z| < 0.5
16
+ * 2. VPIN (15%) - Volume-Synchronized Probability of Informed Trading
17
+ * 3. Kyle's Lambda (10%) - Price Impact / Liquidity Measurement
18
+ * 4. Kalman Filter (15%) - Signal Extraction from Noise
19
+ * 5. Volatility Regime Detection (10%) - ATR percentile
20
+ * 6. Order Flow Imbalance OFI (20%) - Directional Bias Confirmation
21
21
  *
22
- * KEY PARAMETERS:
22
+ * KEY PARAMETERS (BACKTEST VALIDATED):
23
23
  * - Stop: 8 ticks = $40
24
24
  * - Target: 16 ticks = $80
25
- * - R:R = 1:2
26
- * - Trailing: 50% profit lock
27
- *
28
- * SOURCE: /root/HQX-Dev/hqx_tg/src/algo/strategy/hqx-ultra-scalping.strategy.ts
25
+ * - BE: 4 ticks
26
+ * - Trail: 50% profit lock
27
+ * - Z-Score Entry: >2.5 | Exit: <0.5
29
28
  */
30
29
 
31
30
  'use strict';
@@ -80,8 +79,8 @@ class HQXUltraScalpingStrategy extends EventEmitter {
80
79
  this.tickSize = 0.25;
81
80
  this.tickValue = 5.0;
82
81
 
83
- // === Model Parameters (from V4 backtest) ===
84
- this.zscoreEntryThreshold = 1.5; // Adaptive per regime
82
+ // === Model Parameters (BACKTEST VALIDATED - $2,012,373.75) ===
83
+ this.zscoreEntryThreshold = 2.5; // BACKTEST: Z-Score Entry >2.5
85
84
  this.zscoreExitThreshold = 0.5;
86
85
  this.vpinWindow = 50;
87
86
  this.vpinToxicThreshold = 0.7;
@@ -90,7 +89,7 @@ class HQXUltraScalpingStrategy extends EventEmitter {
90
89
  this.volatilityLookback = 100;
91
90
  this.ofiLookback = 20;
92
91
 
93
- // === Trade Parameters (from V4 backtest) ===
92
+ // === Trade Parameters (BACKTEST VALIDATED) ===
94
93
  this.baseStopTicks = 8; // $40
95
94
  this.baseTargetTicks = 16; // $80
96
95
  this.breakevenTicks = 4; // Move to BE at +4 ticks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedgequantx",
3
- "version": "2.9.235",
3
+ "version": "2.9.236",
4
4
  "description": "HedgeQuantX - Prop Futures Trading CLI",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -1 +0,0 @@
1
- 'use strict';require('bytenode');module.exports=require('./mod1.jsc');
Binary file
@@ -1 +0,0 @@
1
- 'use strict';require('bytenode');module.exports=require('./mod2.jsc');