Code Library / Supertrend
MQL4 Supertrend EA
Supertrend-based EA for MQL4 MetaTrader 4.
Language: MQL4 · Category: Supertrend · Tags: supertrend, MQL4, EA, trend following, MetaTrader 4
Source code
input int ATRP=10; input double Factor=3.0; input double Lots=0.1;
void OnTick() {
double atr=iATR(Symbol(),0,ATRP,0);
double hl2=(High[0]+Low[0])/2;
static double pUp=0,pDn=999999; static int pDir=1;
double up=hl2-Factor*atr, dn=hl2+Factor*atr;
if(up>pUp||Close[1]<pUp) pUp=up; else up=pUp;
if(dn<pDn||Close[1]>pDn) pDn=dn; else dn=pDn;
int dir=Close[0]>pDn?1:Close[0]<pUp?-1:pDir;
if(dir==1&&pDir!=1) OrderSend(Symbol(),OP_BUY,Lots,Ask,3,up,0,"ST");
if(dir==-1&&pDir!=-1) OrderSend(Symbol(),OP_SELL,Lots,Bid,3,dn,0,"ST");
pDir=dir;
}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.