I'm designing a grid system where the initial lot size is scaled such that, in the worst-case scenario (price never retraces and fully exits the grid), the drawdown remains controlled — say capped at 10%. At the same time, I ensure the required margin is always available. (my leverage is fixed at 30:1 in strategy tester)
Although I would urge you to reconsider the wisdom of Martingaling in the first place, I understand that you may need to code an EA in order to prove it to yourself by way of statistics.
I know ACCOUNT_MARGIN is not affected by the unrealized losses.
Although OrderCalcMargin() in your EA does not take floating P/L into account, the MT5 terminal and your broker-dealer certainly do.
"When you have open orders in your trading account, the Balance will not change and it will stay fixed but the Equity will be floated according to the profit/loss of your current orders" (Leverage, Margin, Balance, Equity, Free Margin, Margin Level, Margin Call Level and Stop Out Level - Trading Systems - 1 June 2018 - Traders' Blogs).
You might want to have a look at FreeMargin() and then code a minimum required for your purposes (FreeMargin - CAccountInfo - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5).
Most likely you faced the situation when trading symbol has a constantly increasing level of required margin by volume - this feature has been introduced in the build 4150.
There is still no access to this info from MQL5.
- 2024.01.18
- MetaQuotes
- www.metatrader5.com
Most likely you faced the situation when trading symbol has a constantly increasing level of required margin by volume - this feature has been introduced in the build 4150.
There is still no access to this info from MQL5.
Wow. I had no idea that this is legal outside of the U.S.
So whether the account is floating profits or losses in open positions, the trader is on the hook for an ever-increasing rate of margin required. Yikes!
In any event, here's some free margin code:
Forum on trading, automated trading systems and testing trading strategies
AccountFreeMarginCheck in MQL5 ?
Fyodor Korotkov, 2022.12.12 07:03
Thanks. Great approach.
Slightly adjusted it to new mql5 constant names:
double free_margin = AccountInfoDouble(ACCOUNT_MARGIN_FREE); double margin = 0.0; bool margin_check_result = OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, lots, ask, margin); if(free_margin - margin <= 0.0 || margin_check_result != true) { printf("Not enough money to send order with %f lot or Margin Calculation Error", lots); Sleep(1000); return (false); }
Most likely you faced the situation when trading symbol has a constantly increasing level of required margin by volume - this feature has been introduced in the build 4150.
There is still no access to this info from MQL5.
Thank you all for your initial replies — they've already helped clarify some key points.
I now understand that I should rely on Free Margin rather than just Margin, since Free Margin takes unrealized (floating) losses into account. The formula being:
Free Margin = Balance – Margin + P&L
And indeed, based on my new screenshots, the Free Margin calculations are correct:
-
Level 1
Free Margin = 100,000 – 2,966.67 – 14.65 = 97 018.68 -
Level 2
Free Margin = 100,000 – 8,900 – 262.13 = 90 837.87 -
Level 3
Free Margin = 100,000 – 29,061.77 – 862.32 = 70 075.91
So, the Free Margin is consistent and expected.
BUT my issue is not with Free Margin.
The problem remains: I still don't understand why the required margin to open the Level 3 orders is so high (29,061.77), even though the total lots are :
0.89 + 1.78 + 2.67 = 5.34 lots .
Indeed, regarding your link Stanislas, "the margin amount depends on the position volume, the corresponding levels will be shown in the dialog". (by the way are you sure that MetaTrader 5 build 4150 is deployed ?). But refer to the screen, I am still in the range 0-10 so no change in margin rate ...
For Level 1 and Level 2, the margin calculations make perfect sense:
-
Level 1: ~0.89 lots → 2,966.67 margin
-
Level 2: ~0.89 + 1.78 = 2.67 lots → 8,900 margin
But then suddenly at Level 3, we get ~5.34 lots → 29,061.77 margin, which doesn't scale proportionally and breaks the logic.
Any help understanding why the required margin jumps so drastically at Level 3 would be greatly appreciated!
PS : I am not connected to any brokers for the BT, only MetaQuotes Demo Account.
Thank you all for your initial replies — they've already helped clarify some key points.
I now understand that I should rely on Free Margin rather than just Margin, since Free Margin takes unrealized (floating) losses into account. The formula being:
Free Margin = Balance – Margin + P&L
And indeed, based on my new screenshots, the Free Margin calculations are correct:
-
Level 1
Free Margin = 100,000 – 2,966.67 – 14.65 = 97 018.68 -
Level 2
Free Margin = 100,000 – 8,900 – 262.13 = 90 837.87 -
Level 3
Free Margin = 100,000 – 29,061.77 – 862.32 = 70 075.91
So, the Free Margin is consistent and expected.
BUT my issue is not with Free Margin.
The problem remains: I still don't understand why the required margin to open the Level 3 orders is so high (29,061.77), even though the total lots are :
0.89 + 1.78 + 2.67 = 5.34 lots .
Indeed, regarding your link Stanislas, "the margin amount depends on the position volume, the corresponding levels will be shown in the dialog". (by the way are you sure that MetaTrader 5 build 4150 is deployed ?). But refer to the screen, I am still in the range 0-10 so no change in margin rate ...
For Level 1 and Level 2, the margin calculations make perfect sense:
-
Level 1: ~0.89 lots → 2,966.67 margin
-
Level 2: ~0.89 + 1.78 = 2.67 lots → 8,900 margin
But then suddenly at Level 3, we get ~5.34 lots → 29,061.77 margin, which doesn't scale proportionally and breaks the logic.
Any help understanding why the required margin jumps so drastically at Level 3 would be greatly appreciated!
PS : I am not connected to any brokers for the BT, only MetaQuotes Demo Account.
Stanislav gave you the answer.
Here are settings for EURUSD on MetaQuotes-Demo :

You have 5.34 lots, leverage 1:30, so :
Margin = 300 000/30 + 200 000/30 * 2 + 34 000 / 30 *3 = 10 000 + 13 333.333 + 3 400 = 26 733.33 EUR
Well that doesn't match so probably the real calculation is done a bit differently or I missed a detail. But it should give you the idea.
Please see also https://www.mql5.com/en/forum/486895
May be helpful.
- 2025.05.19
- Dr Matthias Hammelsbeck
- www.mql5.com
The problem remains: I still don't understand why the required margin to open the Level 3 orders is so high (29,061.77), even though the total lots are :
0.89 + 1.78 + 2.67 = 5.34 lots .
Indeed, regarding your link Stanislas, "the margin amount depends on the position volume, the corresponding levels will be shown in the dialog". (by the way are you sure that MetaTrader 5 build 4150 is deployed ?). But refer to the screen, I am still in the range 0-10 so no change in margin rate ...
For Level 1 and Level 2, the margin calculations make perfect sense:
-
Level 1: ~0.89 lots → 2,966.67 margin
-
Level 2: ~0.89 + 1.78 = 2.67 lots → 8,900 margin
But then suddenly at Level 3, we get ~5.34 lots → 29,061.77 margin, which doesn't scale proportionally and breaks the logic.
Any help understanding why the required margin jumps so drastically at Level 3 would be greatly appreciated!
Of course, all features introduced in any build is now working in all successive builds, so despite the fact that the build 4150 is already substituted many times with later releases, the function keeps working as described in the release notes (until possible annoncement of elimination, if it happens someday - sometime old features are extracted indeed, for example support of 32-bit OSes).
As for the calculation, I made a small rounding for simplification, and got this:
- 1 lot -> 3000 of margin
- +2 lots -> 6000 of margin (+3000 from 1 lot gives cumulative sum 9000)
- +3 lots -> (maps into the 2-nd row with multiplication factor 2.0) = 9000 * 2 of margin (+9000 from previous 3 lots) -> 27000
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey everyone,
Has anyone here struggled with margin calculation issues in MT5? I’ve been facing a weird one for a few weeks now, and it’s driving me a bit crazy.
What I'm trying to do:
I'm designing a grid system where the initial lot size is scaled such that, in the worst-case scenario (price never retraces and fully exits the grid), the drawdown remains controlled — say capped at 10%. At the same time, I ensure the required margin is always available. (my leverage is fixed at 30:1 in strategy tester)
To double-check the margin values and leverage, I print this:
And I get:
This makes sense. With 1:30 leverage, margin required = 1/30 = 3.33% of the position size.
So for 1 lot of EURUSD (~100,000 EUR), I expect ~3,333 EUR margin.
Now here’s where it gets weird:
In the backtest, as my grid progresses and I add positions, the margin used jumps massively between levels.
For example:
Level 1 (0.89 lots): (screen 1)
Level 2 (1.78 lots): (screen 2)
Level 3 (2.67 lots): margin spikes from ~8,900 to 29,000+ EUR (screen 3)
But if I calculate it manually, I should have:
Position 1 (0.89 lots): ~2,966.67 EUR (It's what I have OK)
Position 2 (1.78 lots): ~5,933.33 EUR (8900 cumulative OK)
Position 3 (2.67 lots): ~8,900.00 EUR (Not OK)
Total margin for 5.34 lots: ~17,800 EUR (Not OK)
Yet the backtest shows ~29,000 EUR margin usage at that point, which doesn’t add up.
Has anyone faced this kind of margin inconsistency in MT5 during a grid strategy? Except error, I know ACCOUNT_MARGIN is not affected by the unrealized losses.
Any ideas or things I should double-check?
Thanks in advance for any help 🙏
PS : You will find screen fils in attachment and my needed codes. In the main I called function to place orders. In the .mqh there are functions that contains orders placing logic and the logic for lot size computation (respecting max chosen drawdown and available margin in case of failure)