Page 4 of 5

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:56 am
by PTScalper
2. The Portfolio Risk Cap

Even with uncorrelated assets, multiple simultaneous trades increase the probability of a combined drawdown. Professional traders establish an absolute ceiling on total open risk, usually set at a conservative fraction of the Kelly maximum.

If your standard single-trade size is a "Quarter-Kelly" of 2.5%, you might set a hard Portfolio Cap of 10%. This means you can have a maximum of four simultaneous full-sized trades open at any one time. If a fifth pristine setup triggers on the M15 chart, you are mathematically forbidden from taking it unless you manually scale down the sizes of all open positions to fit within the 10% cap.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:56 am
by PTScalper
3. The "Risk-Free" Sequencing Rule

Since you prefer to scale from profit using anti-martingale structures, the safest way to bypass the Portfolio Cap is to use market money.

You only open a new simultaneous position if an existing position has its stop-loss moved to break-even or better. If Trade A is secured and physically cannot result in a loss of initial capital, it is removed from the aggregate risk calculation. You are then free to deploy your Kelly allocation into Trade B. This allows you to build a massive, highly profitable concurrent portfolio without ever exposing the account to a simultaneous multi-stop wipeout.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:57 am
by PTScalper
A covariance matrix is a mathematical grid that quantifies exactly how multiple assets move in relation to one another, serving as the core engine algorithms use to prevent systemic risk across simultaneous trades. Instead of viewing trades in isolation, an algorithm uses this matrix to measure the overlapping risk of an entire portfolio.

The Structure of the MatrixIf an algorithm is evaluating three currency pairs (A, B, and C), the covariance matrix ($\Sigma$) is a $3 \times 3$ grid:

The Diagonal (Variance): The top-left to bottom-right diagonal represents the variance (historical volatility) of each individual asset.

The Off-Diagonals (Covariance): Every other cell measures the directional relationship and magnitude of price movements between two different assets.

Positive Number: The assets move together (e.g., EURUSD and GBPUSD).

Negative Number: The assets move inversely (e.g., often USDCHF and EURUSD).

Near Zero: The assets move independently.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:58 am
by PTScalper
How Algorithms Use It for Position Sizing

When an algorithm determines position sizes, it does not just sum up the risk of Trade A + Trade B. It uses matrix multiplication to calculate the Total Portfolio Variance ($\sigma_p^2$).If the algorithm wants to allocate a specific weight (position size) to each asset, represented by a vector array of weights ($w$), the total risk is calculated as:

$$\sigma_p^2 = w^T \Sigma w$$

(Where $w^T$ is the transposed weights vector, and $\Sigma$ is the covariance matrix).By running this calculation in real-time, the algorithm dynamically adjusts the sizes of individual trades ($w$) to stay under a hard portfolio risk cap. It does this through two primary mechanisms:

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:58 am
by PTScalper
1. Penalizing Correlation (Shrinking Sizes)

If a trend-following algorithm identifies long setups on AUDUSD, NZDUSD, and Gold simultaneously, the covariance matrix will show high positive values between all three. When the algorithm runs the formula, the high covariance spikes the projected portfolio variance. To prevent a single US Dollar rally from triggering three full-sized stop-losses, the algorithm is mathematically forced to reduce the position sizes ($w$) across all three trades to keep the total portfolio variance below the programmed threshold.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:58 am
by PTScalper
2. Rewarding Diversification (Expanding Sizes)

If the algorithm finds a long setup on XAUUSD and a completely uncorrelated setup on a European equity index, the off-diagonal covariance is near zero. Because the assets act independently, the matrix math returns a much lower aggregate portfolio variance. The algorithm is then cleared to allocate much closer to the maximum Kelly fraction for both trades, knowing that a loss in one is statistically unlikely to drag down the other.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 10:59 am
by PTScalper
The Algorithmic Loop

In modern quantitative systems, this is solved as a continuous optimization problem. The algorithm feeds current market volatility and correlations into the matrix, sets the target risk limit (e.g., a 5% maximum portfolio drawdown), and solves for the vector $w$. The output instantly dictates the precise lot sizes required for every open position, continuously rebalancing as market correlations shift.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 11:00 am
by PTScalper
Matrix decomposition is unnecessarily heavy for an MQL5 Expert Advisor running on every tick. For real-time execution in MetaTrader, you can bypass the covariance matrix entirely by implementing $O(n)$ logic that checks correlation only at the exact moment a new signal fires.

When trading structural overlapping assets like XAUUSD, XAGUSD, and major currency pairs, you can implement one of two lightweight gating mechanisms before sending the OrderSend() request.

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 11:00 am
by PTScalper
Method 1: The Currency String Exposure Check (Zero Math)

The fastest execution method uses zero historical price data. It simply parses the symbol strings of your currently open positions to ensure you are not doubling down on a specific base or quote currency. If your EA finds a long setup on EURUSD, going long on GBPUSD functionally doubles your short exposure to the US Dollar.

This snippet extracts the 3-character currency codes and blocks the new trade if an open trade already holds risk in either currency.

Code: Select all

bool HasOverlappingExposure(string candidate_symbol) {
   // Extract Base and Quote (e.g., "EUR" and "USD" from "EURUSD")
   string cand_base = StringSubstr(candidate_symbol, 0, 3);
   string cand_quote = StringSubstr(candidate_symbol, 3, 3);
   
   for(int i = PositionsTotal() - 1; i >= 0; i--) {
      string open_symbol = PositionGetSymbol(i);
      
      // Ignore the exact same symbol (let your max-tiers logic handle pyramiding)
      if(open_symbol == candidate_symbol) continue;
      
      string open_base = StringSubstr(open_symbol, 0, 3);
      string open_quote = StringSubstr(open_symbol, 3, 3);
      
      // If the new trade shares any currency with an open trade, flag as correlated
      if(cand_base == open_base || cand_quote == open_quote ||
         cand_base == open_quote || cand_quote == open_base) {
         return true; // Reject or halve the position size
      }
   }
   return false;
}

Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging

Posted: Sun Oct 18, 2026 11:00 am
by PTScalper
Method 2: On-Demand Pairwise Pearson Correlation

If you want mathematical correlation without the background matrix, you calculate the Pearson correlation coefficient ($r$) on demand. When a signal fires on the M15 or H4 timeframe, the EA loops through the currently open positions and calculates the correlation of the candidate symbol against the open symbols over the last 30 bars.If the absolute correlation $\vert{}r\vert{}$ exceeds your threshold (e.g., 0.75), the EA scales down the Kelly allocation for the new trade.

Code: Select all

double GetPearsonCorrelation(string sym1, string sym2, int periods, ENUM_TIMEFRAMES tf) {
   double prices1[], prices2[];
   
   // Fetch close prices; ensure arrays are synchronized
   if(CopyClose(sym1, tf, 0, periods, prices1) < periods || 
      CopyClose(sym2, tf, 0, periods, prices2) < periods) {
      return 0.0; 
   }
   
   double sum1 = 0, sum2 = 0, sum1Sq = 0, sum2Sq = 0, pSum = 0;
   
   for(int i = 0; i < periods; i++) {
      sum1 += prices1[i];
      sum2 += prices2[i];
      sum1Sq += MathPow(prices1[i], 2);
      sum2Sq += MathPow(prices2[i], 2);
      pSum += (prices1[i] * prices2[i]);
   }
   
   double num = pSum - ((sum1 * sum2) / periods);
   double den = MathSqrt((sum1Sq - MathPow(sum1, 2) / periods) * (sum2Sq - MathPow(sum2, 2) / periods));
   
   if(den == 0) return 0.0;
   return num / den;
}

// Execution Logic Example:
// double r = GetPearsonCorrelation("XAUUSD", "XAGUSD", 30, PERIOD_H4);
// if (r > 0.80) { lot_size = lot_size * 0.5; } // Apply Half-Kelly haircut