Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1601

 

Another AI disagreed with the conclusions of the first one:

The maths given by the other tool contains a logical error in the interpretation of the term "free margin" and the principles of the trading terminal.

The error lies in the concept of "safety buffer". In trading terminals (such as MetaTrader), the indicator "Margin" is a fixed amount that the broker blocks against the security buffer. "Free Margin" is the balance of funds in your account that can be used to open new trades. The broker does not add a "buffer" to the collateral; the collateral (margin) is calculated strictly according to a formula.

Let's see why my calculation and your terminal data (4986.5) differ from standard currency pairs:

  1. Instrument: If at leverage 1 : 1000 1:1000 1:1000 1 : 1000 the margin for 1 lot is 4986.5, it means that you are trading not a standard currency pair (where the margin would be about $100), but a very expensive asset. It could be shares of large companies, an expensive crypto asset or a specific index where the value of one lot is almost $5 million.
  2. Direct correlation: Collateral (Margin) always changes indirect proportion to leverage. If you reduce the leverage by 33.3 times (from 1000 to 30), then the amount of collateral increases exactly the same amount.

The correct calculation looks like this:

Let's calculate the coefficient of leverage change:

1000 / 30 = 33.333... 1000 / 30 = 33.333...1000/30 = 33.333....

Now multiply the current required amount by this factor:

4986.5 × 33.333 = 166216.66... 4986.5 \times 33.333 = 166216.66... 4986.5× 33.333 = 166216.66...

The other AI's answer (8478.5) is wrong because it is based on the assumption that the "safety buffer" is a constant. In practice, if you change leverage to 1 : 30 1:30 1:30 1 : 30, your broker will simply block 33 times as much money in the margin column. And if you have only 8478.5 in your account, you won't be able to open a trade, as the system will require more than 166 thousand to secure the position.

Important nuance: if the figure 4986.5 is not the system's requirement for one lot, but your current balance ("Free Margin" in the terminal window), then the calculations are meaningless without reference to a specific instrument. But if it is exactly the parameter "Required Margin" for 1 lot, then my calculation is mathematically correct for any market.

MathML Namespace
  • www.w3.org
MathML Namespace
 

Pitted these two AIs against each other. Gave the price of gold right now, right from the terminal. Here's the final verdict:

Perfect! Everything fits perfectly now.Gold price Bid = $4986.00 per ounce is the key information.

Let's do a final accurate calculation:

Actual data:

  • Instrument: XAUUSD (Gold)

  • Current Price: $4,986.00 per 1 troy ounce

  • Lot Size: 1 standard lot = 100 oz.

  • Your current leverage: 1:1000

  • Current margin for 1 lot at 1:1000: $4986.5 (as you indicated earlier)

Calculating the cost of 1 lot:

1 lot XAUUSD = 100 ounces × $4986.00 =$498,600.00

Checking your current condition (1:1000):

Theoretical margin using the cost/shoulder formula:

  • 498,600$ ÷ 1000 =498.6$

But your broker is demanding$4986.5 - that ' s10 times more!

Broker's claim amplification factor:

  • $4986.5$ ÷ $498.6$ =10.0 ( exactly 10 times)

This is a standard practice for gold - brokers sethigher margin requirements ( usually 0.5-2% instead of 0.1%).

Calculation for leverage 1:30:

1. Theoretical base margin at 1:30:

  • 498,600$ ÷ 30 =16,620.00$

2. Apply the broker's gain factor (×10):

  • 16,620.00$ × 10 =166,200.00$

3- Alternative calculation via leverage change:

  • Change Factor: 1000 ÷ 30 = 33.333....

  • 4986.5$ × 33.333... =166,216.67$

The small difference of $16.67 is rounding or the accuracy of the calculation.

Final answer:

To open 1 lot of XAUUSD at:

  • Current price: $4,986.00 per ounce

  • Leverage: 1:30

  • Your broker's terms and conditions ( 10 times the standard requirements)

You will need in your account:
$166,200.00 ( approximately$166.2 thousand).

 
Vitaly Murlenko #:
Open a trade with the minimum lot and see how much money you have blocked. This will allow you to understand how much money (minimum) you need to have in your account to open a lot = 1.

I understand that, but I want to have a script, and I don't have money on my account yet.

I have a script for mt4 and it works fine, but here it is not clear what to do with the obtained coefficient.

if you have a script for mt5, please share it.

 
Max N #:
and there's no money in the account yet.
Well, use a demo account.
In general, it's elementary, it's open information from the broker, everything has already been stolen (calculated) before us. For example, in the mobile application Metatrader you go to Quotes, long tap on the desired one (for example, XAUUSD) and there in the menu Properties. A lot of things open up, and there is a whole section - Margin. Now for gold on the Russian broker's ruble account it is 1,700,000 and something per lot. For a 0.01 position, for example, it is 100 times less, i.e. 17 thousand roubles. Surely it is somewhere in the desktop version, I just have never looked for it, as I look in mobile or in my mind I calculate.
And finally, it is elementary to calculate it on a calculator, in the mind or, if necessary, programmatically.
Here is the price of gold now, roughly, 5000 quid per ounce. The contract size is 100 ounces, that is 100*5000. Your leverage on gold, for example, is 20. So 5000*100/20 is the margin, plus or minus. That's per lot, in quid. And for 0.01 lot - 100 times less.
In currencies, the contract size is, for example, 100000 USD, 100000 Euro, etc..
 
Max N #:

It's understandable, but I want to have a script, and there's no money in the account yet.

There is a script for mt4 and it works perfectly, but here it is not clear what to do with the obtained coefficient.

if you have a script for mt5, please share it.

Here is a script that has not been finalised.

#define    DEVIATION     5              // permissible deviation from the price 
#define    VOLUME        1.0            // order volume 
#define    EXPERT_MAGIC  123            // MagicNumber 
#define    DIRECTION     ORDER_TYPE_BUY // direction of the opened position (ORDER_TYPE_BUY or ORDER_TYPE_SELL) 
//---
MqlTradeRequest     request= {};
MqlTradeCheckResult check  = {};
MqlTradeResult      result = {};
/*******************Script programme start function********************/
void OnStart()
 {
  MqlTick tick; 
  SymbolInfoTick(_Symbol, tick);
  request.action    = TRADE_ACTION_DEAL; // type of trade operation
  request.symbol    = _Symbol;           // symbol
  request.volume    = VOLUME;            // volume
  request.type      = DIRECTION;         // order type
  request.price     = tick.ask;          // price to open
  request.deviation = DEVIATION;         // permissible deviation from the price
  request.magic     = EXPERT_MAGIC;      // MagicNumber of the order
  OrderCheck(request, check);
  DebugBreak();
 }/******************************************************************/
/****************************End_program*****************************/

Almost everything is taken from the documentation.

And here is the result:

From the specification

Margin obtained by the script.

From my point of view this is the best variant of margin definition.

 
Max N #:

hint, there is a script in the Help for margin calculation for mt5 https://www.mql5.com/en/docs/marketinformation/symbolinfomarginrate

but i don't understand how it works, i run it on XAUUSD chart i get a value of 1.81

I have a leverage of 1/30, I have a ruble account, why I get 1.81 and what to do with this figure, how to understand how much margin is needed to open 1 lot?

What is wrong with OrderCalcMargin() ?
 
Aleksandr Slavskii #:
OrderCalcMargin() , what's wrong with it?
thanks, it's strange that search didn't give me information on this function at once, the example from the help seems to work.
 
Jack_the_singer #:
Here is the price of gold now, roughly, 5000 quid per ounce. The contract size is 100 ounces, that is 100*5000. Your leverage on gold, for example, is 20. So 5000*100/20 is the margin, plus or minus. That's per lot, in quid. And for 0.01 of a lot, it's 100 times less.

I think my leverage is 40, so another script shows. I tried to calculate it manually using this formula and the answer does not match the result of the OrderCalcMargin( ) script from the Help.

script gives margin for 0.01 lot = 17555 rubles, and manual calculation 5090*100/40=12725$, divided by 100 we get 127.25$, in rubles multiplied by 75.8 we get 9645 :))))

If we multiply by that mythical coefficient 1.81, we get approximately the same number. it remains only to understand what this coefficient is.

Документация по MQL5: OrderCalcMargin / Торговые функции
Документация по MQL5: OrderCalcMargin / Торговые функции
  • www.mql5.com
Вычисляет размер маржи, необходимой для указанного типа ордера на текущем счете и при текущем рыночном окружении без учета текущих отложенных...
 
Jack_the_singer #:
I'm sure it's somewhere in the desktop version too, I just never looked for it, as I look in mobile or calculate in my mind.
There is, I just didn't scroll down the list, the margin is at the bottom. but the script is more convenient anyway :) I immediately corrected it to 0.01 lot.
 
Max N #:
There is, I just didn't scroll down the list, the margin is at the bottom. but the script is more convenient anyway :) I immediately corrected it to 0.01 lot.

Take this script

Forum on trading, automated trading systems and testing trading strategies.

Questions from beginners MQL5 MT5 MetaTrader 5

Alexey Viktorov, 2026.01.24 08:52 AM

Here is a script that has not been finalised.

#define    DEVIATION     5              // permissible deviation from the price 
#define    VOLUME        1.0            // order volume 
#define    EXPERT_MAGIC  123            // MagicNumber 
#define    DIRECTION     ORDER_TYPE_BUY // direction of the opened position (ORDER_TYPE_BUY or ORDER_TYPE_SELL) 
//---
MqlTradeRequest     request= {};
MqlTradeCheckResult check  = {};
MqlTradeResult      result = {};
/*******************Script programme start function********************/
void OnStart()
 {
  MqlTick tick; 
  SymbolInfoTick(_Symbol, tick);
  request.action    = TRADE_ACTION_DEAL; // type of trade operation
  request.symbol    = _Symbol;           // symbol
  request.volume    = VOLUME;            // volume
  request.type      = DIRECTION;         // order type
  request.price     = tick.ask;          // price to open
  request.deviation = DEVIATION;         // permissible deviation from the price
  request.magic     = EXPERT_MAGIC;      // MagicNumber of the order
  OrderCheck(request, check);
  DebugBreak();
 }/******************************************************************/
/****************************End_program*****************************/

Almost everything is taken from the documentation.

And here is the result:

From the specification

Margin obtained by the script.

From my point of view this is the best way to determine the margin.

Check the readings with the specification. Show me what you got.