Formula to calculate Maximum Lot size based on Account Balance, Leverage and Currency Pair

Enivid

Administrator
Staff member
Nov 30, 2008
19,158
1,493
144
Odesa
www.earnforex.com

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
39
You can also use our calculator:
https://www.earnforex.com/max-position-size-margin-stop-out-calculator/

I don't think there is a point in such a calculation without knowing your broker's stop-out level and your expected stop-loss distance. Or do you want a position that would get opened only to close immediately due to the spread?

I am not worried about Stop Out Level and i am not keeping any Stop Loss. I want to calculate the Maximum Lot i can trade with 100% of FreeMargin and Leverage.

I am trying something like this

MQL4:
double MaxLot_UsingTickSize = NormalizeDouble(((AccountFreeMargin() * AccountLeverage()) / Bid) / (1 / MarketInfo(Symbol(), MODE_TICKSIZE)), 2);
double MaxLot_UsingLotSize = NormalizeDouble(((AccountFreeMargin() * AccountLeverage()) / Bid) / (MarketInfo(Symbol(), MODE_LOTSIZE)), 2);

Is this correct?
 
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
19,158
1,493
144
Odesa
www.earnforex.com
I am not worried about Stop Out Level and i am not keeping any Stop Loss. I want to calculate the Maximum Lot i can trade with 100% of FreeMargin and Leverage.

I am trying something like this

MQL4:
double MaxLot_UsingTickSize = NormalizeDouble(((AccountFreeMargin() * AccountLeverage()) / Bid) / (1 / MarketInfo(Symbol(), MODE_TICKSIZE)), 2);
double MaxLot_UsingLotSize = NormalizeDouble(((AccountFreeMargin() * AccountLeverage()) / Bid) / (MarketInfo(Symbol(), MODE_LOTSIZE)), 2);

Is this correct?
In the most basic case, it's much simpler than that. It's just:

MQL4:
MaxLotSize = AccountFreeMargin() / MarketInfo(Symbol(), MODE_MARGINREQUIRED);

However, there are two issues to be aware about here:
1. If you open a hedging position (e.g., you have a Buy 1 lot position and you open a Sell 1 lot position), for most brokers that will eliminate the margin requirement, so the max size should be calculated taking that into account.
2. Opening a trade using that formula will likely lead to an immediate stop-out event and closing of the said trade.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,158
1,493
144
Odesa
www.earnforex.com
EURUSD, GBPUSD, USDJPY, etc Contract Size is : 100000
XAUUSD Contract size is : 100
XAGUSD Contract Size is : maybe 1000 or 5000

FOR USDJPY, All 3 formula (Including Yours, MarginRequired) provides different values

View attachment 28413
MODE_MARGINREQUIRED returns the amount of margin required to trade 1 lot in Buy direction. There are only two cases when this will be inaccurate:
1. You are selling, so the margin required is slightly different, but this is really only a minor difference.
2. Your broker's server is misconfigured and it returns one value to MetaTrader, but uses a different value internally. This is very very rare.
 

shanmugapradeep

Active Trader
Dec 18, 2020
119
5
34
39
1. You are selling, so the margin required is slightly different, but this is really only a minor difference.
Selling requires more Margin than Buying or Less Margin than buying?

Additionally, i am getting error with both Buy and Sell from the calculated Lot size from Margin Required code which you provided.

1718192795467.png
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,158
1,493
144
Odesa
www.earnforex.com
Selling requires more Margin than Buying or Less Margin than buying?

Additionally, i am getting error with both Buy and Sell from the calculated Lot size from Margin Required code which you provided.

View attachment 28414
Selling is done at a Bid price (rather than Ask), so should require a little bit less margin.

It could be rounded up. Did you try 104.52?
 

fargana

Active Trader
Nov 14, 2022
200
27
39
34
Hello,

Let say,

Account Balance / Free Margin : $200
Leverage : 1000
Currency Pair : GBPUSD
Account Currency : USD

Currently i am using : https://www.hfm.com/int/en/calculators/position-size-calculator and this show accurate Lot size but i am looking for formula so i can code into my EA.

How can i calculate the Maximum Lot size it can take. What are the formula or code to calculate?
You can't calculate max lot size it's a kind of restricted parameter provided by the broker. I'd never recommend to go max lot size, i.e. utilize all leverage because even tiny price swings may lead to account wipe out. Surely it's not what you'd expect.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,158
1,493
144
Odesa
www.earnforex.com
You can't calculate max lot size it's a kind of restricted parameter provided by the broker. I'd never recommend to go max lot size, i.e. utilize all leverage because even tiny price swings may lead to account wipe out. Surely it's not what you'd expect.
That's not what @shanmugapradeep was asking about. What he asked about can be calculated and he even provided the code to do that. Whether it's recommended or not to trade like that is a completely different question.
 

Chadpowell

Trader
Apr 8, 2023
89
21
24
New York
Hello,

Let say,

Account Balance / Free Margin : $200
Leverage : 1000
Currency Pair : GBPUSD
Account Currency : USD

Currently i am using : https://www.hfm.com/int/en/calculators/position-size-calculator and this show accurate Lot size but i am looking for formula so i can code into my EA.

How can i calculate the Maximum Lot size it can take. What are the formula or code to calculate?
lot size multiply by contract size divided by leverage will be your margin requirement, calculated in the first currency (for example in AUDNZD, that amount will be in AUD) then multiply that to its price to USD and you will get the final answer in USD.
 

Zerologic

Trader
Jul 17, 2024
120
2
19
I mostly start trading with the smallest position size of 0.01 lots, sometimes splitting orders with the same stop loss with different profit targets.