Account Mathematics

Brief

The article contains description of Gross P/L and Margin calculations.

Details

Gross Profit/Loss

The gross profit loss of the position is the profit and loss expressed in the account currency.

You can get the gross profit loss as for account as well as for each opened positions in Accounts.GrossPL and Trades.GrossPL function.

Also, there is useful column PipCost of the Offers table. This column contains the profit/loss of the 1-lot position for the specified instrument per 1 pip change of the price. The value is expressed in the account currency.

So, for example if you need how your long position Gross P/L will be changed if the price will be increased by 10 pips, you can use the following formula:

Projected Gross P/L = Trade.Lot / BaseUnitSize * PipCost * (Project Close Price - Open Price) / Offer.PointSize

1-level Margin

In case the account is 1-level margin account, the Maintenance Margin Requirement (MMR) is only the margin requirement specified per account and instrument. You can get the margin requirement for the chosen account and instrument using host:execute("getTradingProperty", ...) method. The value is specified as the amount in the account currency per 1 lot of the instrument.

In order to be able to open the position, you must have at least MMR * Size Of Position / BaseUnitSize amount as usable margin on your account. So, the maximum size of the position you are able to created at particular moment is
math.floor(usable margin / MMR) * BaseUnitSize.

However, in the situation when the usable margin is close to the margin required to open the position, the formula above may fail, because the actual validation is made by the following formula:
Size of Position / BaseUnitSize * MMR < (Usable Margin - (Size of Position / BaseUnitSize * PipCost * Spread))
, where the last part of the formula is the Gross P/L of the position immediately after opening. Unfortunately, the equation about can be solved only in iterative manner.

When position is opened, MMR * Size of Position / BaseUnitSize is locked as used margin.

When the equity of the account falls below the used margin, all the positions are closed by Margin Call.

3-levels Margin

In case of 3-level margin is applied on the account, there is Entry Margin Requirement, Maintenance Margin Requirement and Liquidation Margin Requirements. As for 1-level margin, all these requirements are expressed as the amount in the account currency per 1 lot of the specified instrument.

Liquidation Margin Requirements is the amount to be locked as the usable margin when 1 lot position is created. As for 1-level margin, when when the equity of the account falls below the used margin, all the positions are closed by Margin Call.

Maintenance Margin Requirement is the amount to be calculated the maintenance margin. When the equity of the account falls below the maintenance margin, the account is switched into Margin Call Aware status and no new position may be created.

The Entry Margin Requirement is used to check the usable margin before opening the position. The following formula is used:

(Equity - Maintenance Margin for All Positions) > (Size of Position / Base Unit Size * EMR).

back