MQL5

Lot máximo permitido por el margen (OrderCalcMargin)

Calcula el volumen máximo financiable con un presupuesto de margen dado. OrderCalcMargin integra el apalancamiento real, el tipo de cálculo del símbolo (Forex, CFD, futuros) y la divisa de margen.

Requisitos

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);

Resultado

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

Snippets relacionados

Volver al Data Lab