Code Library / Risk Management

MQL4 Risk Percent Sizing

Position sizing based on fixed risk percentage.

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

Source code

double RiskLot(double riskPct,double slPips) {
   double risk=AccountBalance()*riskPct/100;
   double pv=MarketInfo(Symbol(),MODE_TICKVALUE);
   if(Digits==3||Digits==5) pv*=10;
   double lot=risk/(slPips*pv);
   double mn=MarketInfo(Symbol(),MODE_MINLOT);
   double st=MarketInfo(Symbol(),MODE_LOTSTEP);
   lot=MathFloor(lot/st)*st;
   return NormalizeDouble(MathMax(mn,lot),2);
}

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