Open-source MQL5 library for calculating position size using account risk and stop loss.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-07-28 12:12:23 +00:00
Docs Modificar Docs/Usage.md 2026-07-28 12:11:37 +00:00
Include Adicionar Include/SmartLotCalculator.mqh 2026-07-28 12:08:11 +00:00
Scripts Modificar Scripts/SmartLotCalculatorExample.mq5 2026-07-28 12:10:52 +00:00
LICENSE Modificar LICENSE 2026-07-28 12:12:23 +00:00
README.md Modificar README.md 2026-07-28 12:08:40 +00:00

Smart Lot Calculator for MQL5

Smart Lot Calculator is an open-source MQL5 library for calculating position size according to the amount of money a trader is willing to risk.

The library uses MetaTrader 5's native OrderCalcProfit() function to estimate the potential loss between the entry price and the Stop Loss price in the account currency.

Features

  • Calculates volume by risk percentage
  • Calculates volume by a fixed monetary risk
  • Supports balance, equity and free-margin risk bases
  • Uses the broker's own symbol calculation settings
  • Supports buy and sell calculations
  • Respects minimum and maximum volume
  • Respects the symbol volume step
  • Rounds volume down to avoid exceeding the selected risk
  • Provides clear error messages
  • Does not open or manage trades

Repository structure

Include/SmartLotCalculator.mqh
Scripts/SmartLotCalculatorExample.mq5
Docs/Usage.md

Basic example

#include "../Include/SmartLotCalculator.mqh"

void OnStart()
{
   MqlTick tick;

   if(!SymbolInfoTick(_Symbol, tick))
      return;

   double entry_price = tick.ask;
   double stop_price  = entry_price - (500 * _Point);
   string error       = "";

   double volume = CSmartLotCalculator::CalculateByRiskPercent(
      _Symbol,
      ORDER_TYPE_BUY,
      entry_price,
      stop_price,
      1.0,
      RISK_BASE_EQUITY,
      error
   );

   if(volume <= 0.0)
   {
      Print("Calculation error: ", error);
      return;
   }

   Print("Recommended volume: ", DoubleToString(volume, 2));
}

Important

This project is a position-sizing reference tool. It does not guarantee profits, prevent losses or provide trading signals.

Always verify the calculated volume before trading. Symbol specifications and trading conditions can differ between brokers.

License

MIT License.