Account
Members of MT5API. Read them after Connect.
Account data
AccountRec Account { get; }
The account record sent by the server. Public fields:
| Field | Type | Meaning |
|---|---|---|
Login | ulong | Account number |
Type | string | Account group or type text |
UserName | string | Account holder name |
Balance | double | Balance, in the account currency |
Credit | double | Credit |
Blocked | double | Blocked amount |
Leverage | int | Leverage |
TradeFlags | int | Raw permission flags. Prefer IsInvestor, IsTradeAllowed. |
Country, City, State, ZipCode, UserAddress, Phone, Email | string | Contact details held by the broker |
Null before Connect.
Console.WriteLine($"{api.Account.Login} balance {api.Account.Balance}, leverage 1:{api.Account.Leverage}");string AccountCurrency { get; }
Account currency, for example "USD".
string AccountCompanyName { get; }
The broker company name.
- Throws:
ConnectExceptionif not connected.
AccMethod AccountMethod { get; }
Netting or Hedging (Default also exists). Hedging accounts keep many positions per symbol. Netting accounts keep one.
- Throws:
ConnectExceptionif not connected.
Money figures
These update as quotes arrive. Call UpdateProfits to force a refresh.
double AccountProfit { get; }
Floating profit of all open positions.
double AccountMargin { get; }
Margin in use.
double AccountEquity { get; }
Balance + AccountProfit + Credit.
double AccountFreeMargin { get; }
AccountEquity - AccountMargin.
double MarginLevel { get; }
Equity / Margin * 100. Returns 0 when no margin is used.
void UpdateProfits(Quote quote = null)
Task UpdateProfitsAsync(Quote quote = null)
Recalculates the profit of every open position and the margin. Pass a quote to use it as the latest price.
void UpdateAccountProfit()
double UpdateAccountProfit(List<Order> orders)
Adds up the profit of the open positions (or of the list you pass) into AccountProfit. The second form also returns the sum.
Task<double> RequiredMargin(string symbol, double lots, DealType type = DealType.DealBuy, double price = 0.0)
Margin needed to open lots of symbol, in the account currency.
type:DealType.DealBuyorDealType.DealSell.price: use0to take the current ask (buy) or bid (sell).- Throws:
InvalidSymbolExceptionorTimeoutExceptionif there is no quote.
double margin = await api.RequiredMargin("EURUSD", 0.10);double CalculateOrderProfit(string symbol, double openPrice, double closePrice, double lots, bool buy)
Works out the profit of a trade from the two prices, in the account currency. Sends nothing to the server.
double GetContractSize(string symbol)
Contract size of the symbol.
Trading permissions
bool IsInvestor { get; }
True when you logged in with the investor (read-only) password. Orders then throw InvestorModeException.
bool IsTradeDisableOnServer { get; }
True when the broker has switched trading off for this account.
bool IsNotConfirmedAccount { get; }
True when the account is not yet confirmed by the broker.
bool IsTradeAllowed { get; }
True when none of the three above is set and the group allows trading.
if (!api.IsTradeAllowed) Console.WriteLine("Read-only or blocked account");Password
void ChangePassword(string password, bool isInvestor = false)
Changes the account password. The server uses at most 16 characters; longer text is cut. Pass isInvestor: true to change the investor password.
- Throws: errors from the connection if not connected.
List<MailMessage> Mails { get; }
Messages the broker has sent to the terminal. MailMessage has Id, Time, From, To, Subject, Body. A body may be empty until you ask for it.
void MailBodyRequest(long id)
Asks the server for the body of message id. The result arrives through OnMail.
Demo account
static AccountAnswer RequestDemoAccount(AccountRequest req, string host, int port)
Asks a broker access server to open a demo account. Fill UserName, AccType, Country, City, State, ZipCode, Address, Phone, Email, CompanyName, Deposit, Leverage, LanguageId, UtmCampaign, Flags, PushID, NetAddr, AgreeFlags on req first. Returns AccountAnswer with Login, Password and Investor (the investor password).
- Throws:
Exceptionwith the status text if the broker refuses, and connection errors.
var req = new AccountRequest {
UserName = "Test User", AccType = "demo", Email = "test@example.com",
Deposit = 10000, Leverage = 100, CompanyName = "", Country = "", City = "",
State = "", ZipCode = "", Address = "", Phone = "", UtmCampaign = "",
NetAddr = new byte[20]
};
AccountAnswer a = MT5API.RequestDemoAccount(req, "broker.example.com", 443);