MetaTrader 5 features hedging position accounting system

MetaQuotes | 21 March, 2016

The MetaTrader 5 platform was originally designed for trading within the netting position accounting system. The netting system allows having only one position per financial instrument meaning that all further operations at that instrument lead only to closing, reversal or changing the volume of the already existing position. In order to expand possibilities of retail Forex traders, we have added the second accounting system — hedging. Now, it is possible to have multiple positions per symbol, including oppositely directed ones. This paves the way to implementing trading strategies based on the so-called "locking" — if the price moves against a trader, they can open a position in the opposite direction.

Since the new system is similar to the one used in MetaTrader 4, it will be familiar to traders. At the same time, traders will be able to enjoy all the advantages of the fifth platform version — filling orders using multiple deals (including partial fills), multicurrency and multithreaded tester with support for MQL5 Cloud Network, and much more.

Now, you can use one account to trade the markets that adhere to the netting system and allow having only one position per instrument, and use another account in the same platform to trade Forex and apply hedging.

This article describes the netting and hedging systems in details, as well as sheds light on the changes related to the implementation of the second accounting system.


Position accounting depends on a trading account

A position accounting system is set at an account level and displayed in the terminal window header and the Journal:

Position accounting system on the current account

To open a demo account with hedging, enable the appropriate option:

Opening the demo account with hedging

To open a real account with hedging, contact your broker.


Netting system

With this system, you can have only one common position for a symbol at the same time:

It does not matter, what has caused the opposite deal — an executed market order or a triggered pending order.

The below example shows execution of two EURUSD Buy deals 0.5 lots each:

Netting

Execution of both deals resulted in one common position of 1 lot.


Hedging system

With this system, you can have multiple open positions of one and the same symbol, including opposite positions.

If you have an open position for a symbol, and execute a new deal (or a pending order triggers), a new position is additionally opened. Your current position does not change.

The below example shows execution of two EURUSD Buy deals 0.5 lots each:

Hedging

Execution of these deals resulted in opening two separate positions.


Impact of the system selected

Depending on the position accounting system, some of the platform functions may have different behavior:


New trade operation type - Close By

The new trade operation type has been added for hedging accounts — closing a position by an opposite one. This operation allows closing two oppositely directed positions at a single symbol. If the opposite positions have different numbers of lots, only one order of the two remains open. Its volume will be equal to the difference of lots of the closed positions, while the position direction and open price will match (by volume) the greater of the closed positions.

Compared with a single closure of the two positions, the closing by an opposite position allows traders to save one spread:

Closing the position by the opposite one

In the latter case, a "close by" order is placed. Tickets of closed positions are specified in its comment. A pair of opposite positions is closed by two "out by" deals. Total profit/loss resulting from closing the both positions is specified only in one deal.

"Close by" operation in History


Margin calculation in the hedging system of position accounting

If the hedging position accounting system is used, the margin is calculated using the same formulas and principles as described above. However, there are some additional features for multiple positions of the same symbol.

Positions/orders open in the same direction

Their volumes are summed up and the weighted average open price is calculated for them. The resulting values are used for calculating margin by the formula corresponding to the symbol type.

For pending orders (if the margin ratio is non-zero), margin is calculated separately.

Opposite positions/orders

Oppositely directed open positions of the same symbol are considered hedged or covered. Two margin calculation methods are possible for such positions. The calculation method is determined by the broker.

Hedged margin calculation settings in the contract specification


Basic calculation
Using the larger leg
Used if "calculate using larger leg" is not specified in the "Hedged margin" field of contract specification.

The calculation consists of several steps:
  • For uncovered volume
  • For covered volume (if hedged margin size is specified)
  • For pending orders
The resulting margin value is calculated as the sum of margins calculated at each step.

Calculation for uncovered volume
  • Calculation of the total volume of all positions and market orders for each of the legs — buy and sell.
  • Calculation of the weighted average position and market order open price for each leg: (open price of position or order 1 * volume of position or order 1 + ... + open price of position or order N * volume of position or order N) / (volume of position or order 1 + ... + volume of position or order N).
  • Calculation of uncovered volume (smaller leg volume is subtracted from the larger one).
  • The calculated volume and weighted average price are used then to calculate margin by the appropriate formula corresponding to the symbol type.
  • The weighted average value of the ratio and rate is used when taking into account the margin ratio and converting margin currency to deposit currency.

Calculation for covered volume
Used if the "Hedged margin" value is specified in a contract specification. In this case margin is charged for hedged, as well as uncovered volume.

If the initial margin is specified for a symbol, the hedged margin is specified as an absolute value (in monetary terms).

If the initial margin is not specified (equal to 0), the contract size is specified in the "Hedged" field. The margin is calculated by the appropriate formula in accordance with the type of the financial instrument, using the specified contract size. For example, we have two positions Buy EURUSD 1 lot and Sell EURUSD 1 lot, the contract size is 100,000. If the value of 100,000 is specified in the "Hedged field", the margin for the two positions will be calculated as per 1 lot. If you specify 0, no margin is charged for the hedged (covered) volume.

Per each hedged lot of a position, the margin is charged in accordance with the value specified in the "Hedged Margin" field in the contract specification:
  • Calculation of hedged volume for all open positions and market orders (uncovered volume is subtracted from the larger leg).
  • Calculation of the weighted average position and market order open price: (open price of position or order 1 * volume of position or order 1 + ... + open price of position or order N * volume of position or order N) / (volume of position or order 1 + ... + volume of position or order N).
  • The calculated volume, weighted average price and the hedged margin value are used then to calculate margin by the appropriate formula corresponding to the symbol type.
  • The weighted average value of the ratio and rate is used when taking into account the margin ratio and converting margin currency to deposit currency.

Calculation for pending orders
  • Calculation of margin for each pending order type separately (Buy Limit, Sell Limit, etc.).
  • The weighted average value of the ratio and rate for each pending order type is used when taking into account the margin ratio and converting margin currency to deposit currency.
Used if "calculate using larger leg" is specified in the "Hedged margin" field of contract specification.
  • Calculation of margin for shorter and longer legs for all open positions and market orders.
  • Calculation of margin for each pending order type separately (Buy Limit, Sell Limit, etc.).
  • Summing up a longer leg margin: long positions and market orders + long pending orders.
  • Summing up a shorter leg margin: short positions and market orders + short pending orders.
  • The largest one of all calculated values is used as the final margin value.


Changes in MQL5

Now, each position has its unique ticket. It usually corresponds to the ticket of an order used to open the position. A ticket is assigned automatically to all available positions after the terminal update.

When modifying or closing a position in the hedging system, make sure to specify its ticket (MqlTradeRequest::ticket). You can specify a ticket in the netting system as well, however positions are identified by a symbol name.


MqlTradeRequest

MqlTradeRequest features two new fields:

struct MqlTradeRequest
  {
   ENUM_TRADE_REQUEST_ACTIONS    action;           // Performed action type
   ulong                         magic;            // Expert Advisor magic number
   ulong                         order;            // Order ticket
   string                        symbol;           // Symbol name
   double                        volume;           // Requested deal volume in lots
   double                        price;            // Price 
   double                        stoplimit;        // Stop Limit order level
   double                        sl;               // Stop Loss order level
   double                        tp;               // Take Profit order level
   ulong                         deviation;        // Maximum allowable deviation from the requested price
   ENUM_ORDER_TYPE               type;             // Order type
   ENUM_ORDER_TYPE_FILLING       type_filling;     // Order filling type
   ENUM_ORDER_TYPE_TIME          type_time;        // Order time type
   datetime                      expiration;       // Order expiration date (for ORDER_TIME_SPECIFIED type orders)
   string                        comment;          // Order comment
   ulong                         position;         // Position ticket
   ulong                         position_by;      // Opposite position ticket
  };

MqlTradeTransaction

MqlTradeTransaction also features the two similar fields:

struct MqlTradeTransaction
  {
   ulong                         deal;             // Deal ticket
   ulong                         order;            // Order ticket
   string                        symbol;           // Symbol name
   ENUM_TRADE_TRANSACTION_TYPE   type;             // Transaction type
   ENUM_ORDER_TYPE               order_type;       // Order type
   ENUM_ORDER_STATE              order_state;      // Order state
   ENUM_DEAL_TYPE                deal_type;        // Deal type
   ENUM_ORDER_TYPE_TIME          time_type;        // Order time type
   datetime                      time_expiration;  // Order expiration date
   double                        price;            // Price 
   double                        price_trigger;    // Stop limit order trigger price
   double                        price_sl;         // Stop Loss level
   double                        price_tp;         // Take Profit level
   double                        volume;           // Volume in lots
   ulong                         position;         // Position tickets
   ulong                         position_by;      // Opposite position tickets
  };


PositionGetTicket

The new PositionGetTicket function returns a position ticket by an index in the list of open positions and automatically selects that position for further work using the PositionGetDouble, PositionGetInteger, and PositionGetString functions.

ulong  PositionGetTicket(
   int  index      // index in the list of positions
   );


PositionSelectByTicket

The new PositionSelectByTicket function selects an open position for further work by a specified ticket.

bool  PositionSelectByTicket(
   ulong   ticket     // position ticket
   );


PositionSelect

PositionSelect selects a position by a symbol name for further work using the PositionGetDouble, PositionGetInteger, and PositionGetString functions. In the hedging system (where there can be multiple positions at a single symbol), the function selects a position with the lowest ticket.


ACCOUNT_MARGIN_MODE

The new property ACCOUNT_MARGIN_MODE allows receiving the mode of margin calculation and position accounting on a trading account:

Identifier
Description
ACCOUNT_MARGIN_MODE_RETAIL_NETTING
Used for the over-the-counter market when accounting positions in the netting mode (one position per symbol). Margin calculation is based on a symbol type (SYMBOL_TRADE_CALC_MODE).
ACCOUNT_MARGIN_MODE_EXCHANGE
Used on the exchange markets. Margin calculation is based on the discounts specified in symbol settings. Discounts are set by the broker, however they cannot be lower than the exchange set values.
ACCOUNT_MARGIN_MODE_RETAIL_HEDGING
Used for the over-the-counter market with independent position accounting (hedging, there can be multiple positions at a single symbol). Margin calculation is based on a symbol type (SYMBOL_TRADE_CALC_MODE). The presence of multiple positions at a single symbol is considered.


SYMBOL_MARGIN_HEDGED

The new property SYMBOL_MARGIN_HEDGED allows receiving the value of a hedged margin by a trading symbol. Margin calculation in the hedging system of position accounting has been described above.


New trading constants

Due to the addition of the new Close By operation type, the new trading properties have appeared as well:


Extra bonus — hedging and MQL5 Cloud Network

Now, you can use MetaTrader 5 to trade both stock markets and the popular retail Forex with hedging. Developers of the automated systems applying hedging have received another important advantage. Apart from the multithreaded tester, the entire computing capacity of the MQL5 Cloud Network is at their disposal now.

Update your platform and try the new features!