Code Library / Risk Management
ATR Stop Loss
Dynamic stop loss using ATR in MQL5.
Language: MQL5 · Category: Risk Management · Tags: ATR, stop loss, MQL5
Source code
double ATR_SL(ENUM_ORDER_TYPE t, double m=1.5){
int h=iATR(_Symbol,0,14);double b[];ArraySetAsSeries(b,true);
CopyBuffer(h,0,0,1,b);
return t==ORDER_TYPE_BUY?SymbolInfoDouble(_Symbol,SYMBOL_BID)-b[0]*m:SymbolInfoDouble(_Symbol,SYMBOL_ASK)+b[0]*m;
}How to use this snippet
This is a building block, not a finished trading system. Paste it into your own MQL5 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.