Get Position Stats By Symbol (GetPositionStatsBySymbolAsync)ΒΆ
Portfolio method: Aggregate position statistics per symbol - count, volume, and P&L.
API: MT5Service.GetPositionStatsBySymbolAsync(...)
Region: [15] POSITION MONITORING
SignatureΒΆ
public static async Task<Dictionary<string, (int count, double totalVolume, double totalPnL)>>
GetPositionStatsBySymbolAsync(
this MT5Service svc,
int timeoutSec = 20,
CancellationToken ct = default)
ReturnsΒΆ
Dictionary
- Key: Symbol name (e.g., "EURUSD", "XAUUSD")
- Value Tuple:
count- Number of open positionstotalVolume- Sum of all position volumes (lots)totalPnL- Total profit/loss for this symbol
ExamplesΒΆ
// Get portfolio breakdown
var stats = await svc.GetPositionStatsBySymbolAsync();
Console.WriteLine("π Portfolio Statistics:");
foreach (var (symbol, (count, volume, pnl)) in stats)
{
string plEmoji = pnl >= 0 ? "π’" : "π΄";
Console.WriteLine($"{plEmoji} {symbol}: {count} positions, {volume:F2} lots, ${pnl:F2} P&L");
}
// Example output:
// π Portfolio Statistics:
// π’ EURUSD: 3 positions, 0.30 lots, $45.50 P&L
// π΄ GBPUSD: 2 positions, 0.20 lots, -$12.30 P&L
// π’ XAUUSD: 1 positions, 0.05 lots, $89.00 P&L
// Find worst-performing symbol
var stats = await svc.GetPositionStatsBySymbolAsync();
var worstSymbol = stats
.OrderBy(kvp => kvp.Value.totalPnL)
.FirstOrDefault();
if (worstSymbol.Key != null)
{
Console.WriteLine($"β οΈ Worst performer: {worstSymbol.Key} with ${worstSymbol.Value.totalPnL:F2}");
if (worstSymbol.Value.totalPnL < -100)
{
Console.WriteLine($"π¨ Closing all {worstSymbol.Key} positions!");
// Close all positions for this symbol
var positions = await svc.GetLosingPositionsAsync(worstSymbol.Key);
// ... close logic
}
}
// Portfolio diversification check
var stats = await svc.GetPositionStatsBySymbolAsync();
int symbolCount = stats.Count;
double totalVolume = stats.Sum(kvp => kvp.Value.totalVolume);
Console.WriteLine($"π Trading {symbolCount} symbols with total {totalVolume:F2} lots");
foreach (var (symbol, (count, volume, pnl)) in stats)
{
double volumePercent = (volume / totalVolume) * 100;
Console.WriteLine($" {symbol}: {volumePercent:F1}% of portfolio");
if (volumePercent > 40)
{
Console.WriteLine($" β οΈ Over-concentrated in {symbol}!");
}
}
// Symbol exposure limits
var maxPositionsPerSymbol = 5;
var stats = await svc.GetPositionStatsBySymbolAsync();
bool canTrade = true;
if (stats.TryGetValue("EURUSD", out var eurusdStats))
{
if (eurusdStats.count >= maxPositionsPerSymbol)
{
Console.WriteLine($"β οΈ EURUSD limit reached: {eurusdStats.count}/{maxPositionsPerSymbol}");
canTrade = false;
}
}
if (canTrade)
{
await svc.BuyMarketByRisk("EURUSD", 50, 100);
}
SummaryΒΆ
- β Returns dictionary grouped by symbol
- β Includes count, total volume, and total P&L
- β Perfect for portfolio analytics and risk management