MT5Service - Account Convenience MethodsΒΆ
10 convenient methods for quick access to account properties without
.Valueunwrapping
π― Why These Methods ExistΒΆ
Problem: In MT5Account, every call returns a Data object that needs unwrapping:
var data = await account.AccountInfoDoubleAsync(AccountInfoDoublePropertyType.AccountBalance);
double balance = data.Value; // β unwrapping required!
Solution: MT5Service provides direct methods that return values immediately:
Benefits:
- β Less code (1 line instead of 2)
- β
No
.Valueceremony - β
Readable method names (
GetBalancevsAccountInfoDouble(BALANCE)) - β
Fewer errors (can't forget
.Value)
π All 10 MethodsΒΆ
| Method | Returns | Low-Level Equivalent |
|---|---|---|
GetBalanceAsync() |
Account balance | AccountInfoDoubleAsync(AccountBalance) |
GetEquityAsync() |
Equity (balance + floating P/L) | AccountInfoDoubleAsync(AccountEquity) |
GetMarginAsync() |
Used margin | AccountInfoDoubleAsync(AccountMargin) |
GetFreeMarginAsync() |
Free margin | AccountInfoDoubleAsync(AccountMarginFree) |
GetProfitAsync() |
Current floating profit/loss | AccountInfoDoubleAsync(AccountProfit) |
GetLoginAsync() |
Account number (login) | AccountInfoIntegerAsync(AccountLogin) |
GetLeverageAsync() |
Leverage ratio | AccountInfoIntegerAsync(AccountLeverage) |
GetAccountNameAsync() |
Account owner name | AccountInfoStringAsync(AccountName) |
GetServerNameAsync() |
MT5 server name | AccountInfoStringAsync(AccountServer) |
GetCurrencyAsync() |
Account currency | AccountInfoStringAsync(AccountCurrency) |
π‘ Usage ExamplesΒΆ
Example 1: Checking Balance and EquityΒΆ
// β BEFORE (MT5Account) - 4 lines:
var balanceData = await account.AccountInfoDoubleAsync(AccountInfoDoublePropertyType.AccountBalance);
double balance = balanceData.Value;
var equityData = await account.AccountInfoDoubleAsync(AccountInfoDoublePropertyType.AccountEquity);
double equity = equityData.Value;
// β
AFTER (MT5Service) - 2 lines:
double balance = await service.GetBalanceAsync();
double equity = await service.GetEquityAsync();
Console.WriteLine($"Balance: ${balance:F2}, Equity: ${equity:F2}");
Example 2: Calculating Margin LevelΒΆ
// β BEFORE (MT5Account) - 6 lines with unwrapping:
var equityData = await account.AccountInfoDoubleAsync(AccountInfoDoublePropertyType.AccountEquity);
double equity = equityData.Value;
var marginData = await account.AccountInfoDoubleAsync(AccountInfoDoublePropertyType.AccountMargin);
double margin = marginData.Value;
double marginLevel = (margin > 0) ? (equity / margin * 100) : 0;
Console.WriteLine($"Margin Level: {marginLevel:F2}%");
// β
AFTER (MT5Service) - 3 lines, no ceremony:
double equity = await service.GetEquityAsync();
double margin = await service.GetMarginAsync();
double marginLevel = (margin > 0) ? (equity / margin * 100) : 0;
Console.WriteLine($"Margin Level: {marginLevel:F2}%");
Code reduction: 50% (6 lines β 3 lines)
Example 3: Checking Free Margin Before Opening PositionΒΆ
// β BEFORE (MT5Account):
var freeMarginData = await account.AccountInfoDoubleAsync(
AccountInfoDoublePropertyType.AccountMarginFree);
double freeMargin = freeMarginData.Value;
if (freeMargin >= 100.0)
{
// Can open position
}
// β
AFTER (MT5Service):
double freeMargin = await service.GetFreeMarginAsync();
if (freeMargin >= 100.0)
{
// Can open position
}
Readability: Intent is clear from the first line!
Example 4: Displaying Account InformationΒΆ
// β BEFORE (MT5Account) - 12 lines:
var loginData = await account.AccountInfoIntegerAsync(AccountInfoIntegerPropertyType.AccountLogin);
long login = loginData.Value;
var nameData = await account.AccountInfoStringAsync(AccountInfoStringPropertyType.AccountName);
string name = nameData.Value;
var serverData = await account.AccountInfoStringAsync(AccountInfoStringPropertyType.AccountServer);
string server = serverData.Value;
var currencyData = await account.AccountInfoStringAsync(AccountInfoStringPropertyType.AccountCurrency);
string currency = currencyData.Value;
var leverageData = await account.AccountInfoIntegerAsync(AccountInfoIntegerPropertyType.AccountLeverage);
long leverage = leverageData.Value;
Console.WriteLine($"Account: {login} ({name})");
Console.WriteLine($"Server: {server}, Currency: {currency}, Leverage: 1:{leverage}");
// β
AFTER (MT5Service) - 6 lines:
long login = await service.GetLoginAsync();
string name = await service.GetAccountNameAsync();
string server = await service.GetServerNameAsync();
string currency = await service.GetCurrencyAsync();
long leverage = await service.GetLeverageAsync();
Console.WriteLine($"Account: {login} ({name})");
Console.WriteLine($"Server: {server}, Currency: {currency}, Leverage: 1:{leverage}");
Code reduction: 50% (12 lines β 6 lines)
Example 5: Monitoring Current ProfitΒΆ
// β BEFORE (MT5Account):
var profitData = await account.AccountInfoDoubleAsync(AccountInfoDoublePropertyType.AccountProfit);
double profit = profitData.Value;
if (profit > 0)
Console.WriteLine($"β
Profit: +${profit:F2}");
else
Console.WriteLine($"β Loss: ${profit:F2}");
// β
AFTER (MT5Service):
double profit = await service.GetProfitAsync();
if (profit > 0)
Console.WriteLine($"β
Profit: +${profit:F2}");
else
Console.WriteLine($"β Loss: ${profit:F2}");
Clean: One line for data retrieval, rest is business logic.
Example 6: Parallel Retrieval of All Account MetricsΒΆ
// β
All methods are async - can run in parallel!
var balanceTask = service.GetBalanceAsync();
var equityTask = service.GetEquityAsync();
var marginTask = service.GetMarginAsync();
var freeMarginTask = service.GetFreeMarginAsync();
var profitTask = service.GetProfitAsync();
await Task.WhenAll(balanceTask, equityTask, marginTask, freeMarginTask, profitTask);
Console.WriteLine($"Balance: ${balanceTask.Result:F2}");
Console.WriteLine($"Equity: ${equityTask.Result:F2}");
Console.WriteLine($"Margin: ${marginTask.Result:F2}");
Console.WriteLine($"Free Margin: ${freeMarginTask.Result:F2}");
Console.WriteLine($"Profit: ${profitTask.Result:F2}");
Performance: All requests execute concurrently!
Example 7: Validating Account Before TradingΒΆ
// Check essential account parameters
var leverage = await service.GetLeverageAsync();
var currency = await service.GetCurrencyAsync();
var freeMargin = await service.GetFreeMarginAsync();
if (leverage < 100)
{
Console.WriteLine("β οΈ Warning: Low leverage, large margin required!");
}
if (currency != "USD")
{
Console.WriteLine($"βΉοΈ Account currency: {currency} (not USD)");
}
if (freeMargin < 500.0)
{
Console.WriteLine("β Not enough free margin to trade!");
return;
}
Console.WriteLine("β
Account ready for trading!");
π Key BenefitsΒΆ
| Aspect | MT5Account (Low-Level) | MT5Service (Convenience) |
|---|---|---|
| Code | 2 lines per value | 1 line per value |
| Readability | AccountInfoDoubleAsync(AccountBalance) |
GetBalanceAsync() |
| Unwrapping | Always need .Value |
β Not needed |
| Errors | Can forget .Value |
β Impossible |
| Write Speed | Slower | β Faster |
| Maintenance | Harder | β Easier |
π Code Reduction StatisticsΒΆ
| Task | Lines (MT5Account) | Lines (MT5Service) | Reduction |
|---|---|---|---|
| Get 1 value | 2 | 1 | 50% |
| Get 5 values | 10 | 5 | 50% |
| Display account info | 12 | 6 | 50% |
| Calculate margin level | 6 | 3 | 50% |
Average reduction: 50% for account operations!
π When to UseΒΆ
β Use MT5Service when:ΒΆ
- Need to quickly get account property value
- Writing trading bot or strategy
- Development speed matters
- Want clean, readable code
β οΈ Use MT5Account when:ΒΆ
- Need exotic properties (no convenience method available)
- Require full control over low-level API
- Building custom wrapper on top of MT5Account
π See AlsoΒΆ
- MT5Service Overview - Complete description of MT5Service improvements
- Symbol Convenience Methods - Convenient methods for symbols
- Trading Convenience Methods - Simplified trading methods
- MT5Account - Low-level API reference
π‘ SummaryΒΆ
10 methods that make account operations 2x faster and 2x cleaner. No ceremony, no .Value - just what you need!
// It's simple:
var balance = await service.GetBalanceAsync();
var equity = await service.GetEquityAsync();
var profit = await service.GetProfitAsync();
Console.WriteLine($"Balance: ${balance:F2}, Equity: ${equity:F2}, P/L: ${profit:F2}");
Write less, do more! π