Code Library / Martingale
MQL4 Safe Martingale
Martingale with max drawdown limit and level cap for safety.
Language: MQL4 · Category: Martingale · Tags: safe martingale, MQL4, drawdown, risk limit, EA
Source code
input double BaseLot=0.01; input double MaxDD=20; input int MaxLvl=5;
double initBal;
bool SafeToIncrease() {
return (initBal-AccountEquity())/initBal*100<MaxDD;
}
double SafeLot(int lvl) {
if(!SafeToIncrease()) return BaseLot;
double lot=BaseLot*MathPow(2,MathMin(lvl,MaxLvl));
return NormalizeDouble(MathMin(lot,AccountFreeMargin()/(MarketInfo(Symbol(),MODE_MARGINREQUIRED)*2)),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.