Code Library / Risk Management

Lot Size Calculator

Calculate lot size based on risk percentage and stop loss.

Language: MQL4 · Category: Risk Management · Tags: lot size, risk, money management, MQL4

Source code

double CalcLots(double riskPct, double slPips) {
   double risk = AccountBalance() * riskPct / 100;
   double pipVal = MarketInfo(Symbol(), MODE_TICKVALUE);
   return MathMax(0.01, risk / (slPips * pipVal));
}

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own MQL4 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.

Related snippets