Skip to main content

Overview

The AccountApi provides methods to retrieve detailed information about trading accounts in MetaTrader 5. This guide demonstrates how to fetch account data, including balance, equity, margin, and trading permissions.

Getting Account Information

Use the accountLoginGet() method to retrieve comprehensive account details for a specific user.
1

Initialize the API Client

Set up the AccountApi with your configuration and authentication token.
use D4T\MT5Sdk\MT5Manager\AccountApi;
use D4T\MT5Sdk\Configuration;
use GuzzleHttp\Client;

// Configure API client
$config = Configuration::getDefaultConfiguration()
    ->setHost('https://your-mt5-server.com/api')
    ->setAccessToken('your_bearer_token');

// Initialize AccountApi
$accountApi = new AccountApi(
    new Client(),
    $config
);
2

Retrieve Account Data

Call the accountLoginGet() method with the user’s login ID.
try {
    // Get account information for login 12345
    $account = $accountApi->accountLoginGet('12345');
    
    // Access account properties
    echo "Login: " . $account->getLogin() . "\n";
    echo "Balance: $" . $account->getBalance() . "\n";
    echo "Equity: $" . $account->getEquity() . "\n";
    echo "Margin: $" . $account->getMargin() . "\n";
    echo "Free Margin: $" . $account->getMarginFree() . "\n";
    echo "Margin Level: " . $account->getMarginLevel() . "%\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Response Body: " . $e->getResponseBody() . "\n";
}
3

Handle the Response

The method returns an Account model object with all account properties.
// Check account status
if ($account->getMarginLevel() < 100) {
    echo "Warning: Low margin level!\n";
}

// Display account metrics
$profit = $account->getEquity() - $account->getBalance();
echo "Current Profit/Loss: $" . number_format($profit, 2) . "\n";

// Check trading permissions
if ($account->getTradeDisabled()) {
    echo "Trading is currently disabled for this account.\n";
}

Complete Working Example

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use D4T\MT5Sdk\MT5Manager\AccountApi;
use D4T\MT5Sdk\Configuration;
use GuzzleHttp\Client;

// Configure the API
$config = Configuration::getDefaultConfiguration()
    ->setHost('https://your-mt5-server.com/api')
    ->setAccessToken('your_bearer_token');

$accountApi = new AccountApi(new Client(), $config);

try {
    $userLogin = '12345';
    $account = $accountApi->accountLoginGet($userLogin);
    
    echo "=== Account Information ===". "\n";
    echo "Login: " . $account->getLogin() . "\n";
    echo "Name: " . $account->getName() . "\n";
    echo "Group: " . $account->getGroup() . "\n";
    echo "Balance: $" . number_format($account->getBalance(), 2) . "\n";
    echo "Equity: $" . number_format($account->getEquity(), 2) . "\n";
    echo "Credit: $" . number_format($account->getCredit(), 2) . "\n";
    
} catch (\D4T\MT5Sdk\ApiException $e) {
    echo "API Error: " . $e->getMessage() . "\n";
    if ($e->getCode() === 400) {
        echo "Invalid request. Check the login ID.\n";
    }
}

Account Properties

The Account model returned by accountLoginGet() includes:
PropertyTypeDescription
loginintegerAccount login ID
namestringAccount holder name
groupstringTrading group name
balancefloatAccount balance
equityfloatCurrent equity
creditfloatCredit amount
marginfloatUsed margin
margin_freefloatFree margin available
margin_levelfloatMargin level percentage
profitfloatCurrent profit/loss
trade_disabledbooleanTrading permissions status
The accountLoginGet() method requires a valid Bearer token obtained from the /init/ endpoint. See the Authentication guide for details.

Error Handling

The API throws ApiException when errors occur. Common error codes:
// Bad Request - Invalid login ID or parameters
try {
    $account = $accountApi->accountLoginGet('invalid');
} catch (\D4T\MT5Sdk\ApiException $e) {
    if ($e->getCode() === 400) {
        echo "Invalid request: " . $e->getMessage();
        // Check response body for details
        $errorDetails = json_decode($e->getResponseBody(), true);
        print_r($errorDetails);
    }
}
Always validate the login ID before making API calls to avoid unnecessary errors. Account data is sensitive - ensure proper access control in your application.

Best Practices

  1. Cache Account Data: Avoid excessive API calls by caching account information when appropriate
  2. Error Handling: Always wrap API calls in try-catch blocks
  3. Token Management: Implement token refresh logic for long-running applications
  4. Validation: Validate login IDs before making requests
  5. Async Operations: Use async methods for better performance when fetching multiple accounts

Next Steps