Maximum lot allowed by margin (OrderCalcMargin)
Computes the maximum volume affordable within a given margin budget. OrderCalcMargin accounts for the actual leverage, the symbol's calculation type (Forex, CFD, futures) and the margin currency.
Prerequisites
MetaTrader 5, MQL5
MQL5
double MaxLotByMargin(string symbol, ENUM_ORDER_TYPE type, double marginBudget)
{
double price = (type == ORDER_TYPE_BUY)
? SymbolInfoDouble(symbol, SYMBOL_ASK)
: SymbolInfoDouble(symbol, SYMBOL_BID);
double marginOneLot = 0.0;
if(!OrderCalcMargin(type, symbol, 1.0, price, marginOneLot) || marginOneLot <= 0.0)
return 0.0; // symbole non synchronisé ou calcul impossible
return NormalizeLot(symbol, marginBudget / marginOneLot);
}
// Exemple : ne jamais engager plus de 25% de la marge libre
double budget = AccountInfoDouble(ACCOUNT_MARGIN_FREE) * 0.25;
double maxLot = MaxLotByMargin(_Symbol, ORDER_TYPE_BUY, budget);Result
2026.06.10 11:12:40.207 MarginCap (US30,M5) Marge libre 9850.00 USD -> budget 25% = 2462.50 USD 2026.06.10 11:12:40.208 MarginCap (US30,M5) OrderCalcMargin(BUY, 1.00 lot @ 42985.0) = 2149.25 USD 2026.06.10 11:12:40.208 MarginCap (US30,M5) Lot max finançable = 2462.50 / 2149.25 = 1.1457 -> normalisé 1.14 2026.06.10 11:12:40.209 MarginCap (US30,M5) Levier réel et devise de marge intégrés par le serveur
OrderCalcMarginMargeRisk ManagementIndices