Page 5 of 5
Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging
Posted: Sun Oct 18, 2026 11:01 am
by PTScalper
3. Implementing the "Risk-Free" Bypass
In MQL5, you can easily bypass these correlation caps if an open trade is already secured. Before running either correlation check, inspect the open position's Stop Loss. If the Stop Loss is locked beyond the open price (a risk-free trade), ignore it in your correlation loop.
Code: Select all
bool IsPositionRiskFree(ulong ticket) {
if(PositionSelectByTicket(ticket)) {
double open_price = PositionGetDouble(POSITION_PRICE_OPEN);
double sl = PositionGetDouble(POSITION_SL);
long type = PositionGetInteger(POSITION_TYPE);
if(type == POSITION_TYPE_BUY && sl > open_price) return true;
if(type == POSITION_TYPE_SELL && sl > 0 && sl < open_price) return true;
}
return false;
}
Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging
Posted: Sun Oct 18, 2026 11:01 am
by PTScalper
By adding if(IsPositionRiskFree(PositionGetTicket(i))) continue; to your loops, the EA will automatically clear the runway for new, correlated pyramiding positions using entirely unrealized market profit.
Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging
Posted: Sun Oct 18, 2026 11:02 am
by PTScalper
Implementing this in cTrader is highly efficient using the cAlgo.API. Because cTrader natively supports C#, you can bypass the clunky MQL array copying and directly query the MarketData.GetBars() collection.
Here is the lightweight C# implementation for a cBot.
Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging
Posted: Sun Oct 18, 2026 11:03 am
by PTScalper
1. The Pearson Correlation Method
In cTrader, the ClosePrices.Last(i) index accesses data from right to left, where Last(0) is the currently fluctuating unclosed bar, and Last(1) is the most recently closed bar. This loop locks in the closed bars to ensure stability.
Code: Select all
using cAlgo.API;
using System;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class ScalpingRiskManager : Robot
{
private double GetPearsonCorrelation(string sym1, string sym2, int periods, TimeFrame tf)
{
// Retrieve historical bar data for the specified symbols
var bars1 = MarketData.GetBars(tf, sym1);
var bars2 = MarketData.GetBars(tf, sym2);
// Ensure sufficient historical data is available
if (bars1.Count < periods || bars2.Count < periods)
return 0.0;
double sum1 = 0, sum2 = 0, sum1Sq = 0, sum2Sq = 0, pSum = 0;
// Iterate through the most recently CLOSED bars (skipping index 0)
for (int i = 1; i <= periods; i++)
{
double price1 = bars1.ClosePrices.Last(i);
double price2 = bars2.ClosePrices.Last(i);
sum1 += price1;
sum2 += price2;
sum1Sq += Math.Pow(price1, 2);
sum2Sq += Math.Pow(price2, 2);
pSum += (price1 * price2);
}
double num = pSum - ((sum1 * sum2) / periods);
double den = Math.Sqrt((sum1Sq - Math.Pow(sum1, 2) / periods) * (sum2Sq - Math.Pow(sum2, 2) / periods));
return den == 0 ? 0.0 : num / den;
}
}
}
Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging
Posted: Sun Oct 18, 2026 11:03 am
by PTScalper
2. The Risk-Free Position Check
cTrader’s Position object exposes everything directly as properties. This evaluates if the stop-loss is securely past the entry price.
Code: Select all
private bool IsPositionRiskFree(Position position)
{
if (!position.HasStopLoss) return false;
if (position.TradeType == TradeType.Buy && position.StopLoss > position.EntryPrice)
return true;
if (position.TradeType == TradeType.Sell && position.StopLoss < position.EntryPrice)
return true;
return false;
}
Re: Aggregating Liquidity: How to Scale Into Sweeps Using Cost Averaging
Posted: Sun Oct 18, 2026 11:03 am
by PTScalper
3. Execution Logic in OnBar or OnTick
When your core entry logic detects a setup (e.g., on XAGUSD), you iterate through Positions to check for active correlation roadblocks. This loop automatically skips risk-free trades (which are financed by unrealized profit) and scales down the lot size if a highly correlated trade is actively tying up initial risk.
Code: Select all
protected override void OnBar()
{
string candidateSymbol = "XAGUSD";
double kellyLotSize = 2.0; // Assume standard calculated lots
double correlationThreshold = 0.75;
// Setup detected...
bool setupTriggered = true;
if (setupTriggered)
{
foreach (var position in Positions)
{
// Ignore trades that can no longer cause an account loss
if (IsPositionRiskFree(position))
continue;
// Ignore the candidate symbol itself (let your pyramiding logic handle this)
if (position.SymbolName == candidateSymbol)
continue;
// Calculate correlation against the open position's symbol
double r = GetPearsonCorrelation(candidateSymbol, position.SymbolName, 30, TimeFrame.Hour4);
// If strongly correlated (positive or negative), apply a 50% haircut
if (Math.Abs(r) >= correlationThreshold)
{
kellyLotSize = Math.Round(kellyLotSize * 0.5, 2);
Print($"Correlated risk detected with {position.SymbolName} (r={r:F2}). Scaled lots to: {kellyLotSize}");
}
}
// Execute trade with final lot size
// ExecuteMarketOrder(TradeType.Buy, candidateSymbol, kellyLotSize, "PyramidEntry", sl, tp);
}
}