Code Library / Supertrend
MQL5 Supertrend
Supertrend indicator for MQL5 MetaTrader 5.
Language: MQL5 · Category: Supertrend · Tags: supertrend, MQL5, indicator, ATR, MetaTrader 5
Source code
double STUp[],STDn[]; int STDir[];
void CalcSupertrend(int atrP,double mult,int bars) {
double atr[],h[],l[],c[];
int ah=iATR(_Symbol,PERIOD_CURRENT,atrP);
CopyBuffer(ah,0,0,bars,atr); CopyHigh(_Symbol,PERIOD_CURRENT,0,bars,h);
CopyLow(_Symbol,PERIOD_CURRENT,0,bars,l); CopyClose(_Symbol,PERIOD_CURRENT,0,bars,c);
ArrayResize(STUp,bars); ArrayResize(STDn,bars); ArrayResize(STDir,bars);
for(int i=1;i<bars;i++) {
double hl2=(h[i]+l[i])/2;
STUp[i]=hl2-mult*atr[i]; STDn[i]=hl2+mult*atr[i];
STDir[i]=c[i]>STDn[i-1]?1:-1;
}
IndicatorRelease(ah);
}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.