Code Library / Moving Average

MQL4 EMA Crossover EA

Classic EMA crossover EA trading golden cross and death cross.

Language: MQL4 · Category: Moving Average · Tags: EMA, crossover, MQL4, EA, golden cross

Source code

input int Fast=10; input int Slow=50; input double Lots=0.1;
void OnTick() {
   double f0=iMA(Symbol(),0,Fast,0,MODE_EMA,PRICE_CLOSE,0);
   double f1=iMA(Symbol(),0,Fast,0,MODE_EMA,PRICE_CLOSE,1);
   double s0=iMA(Symbol(),0,Slow,0,MODE_EMA,PRICE_CLOSE,0);
   double s1=iMA(Symbol(),0,Slow,0,MODE_EMA,PRICE_CLOSE,1);
   if(f1<=s1&&f0>s0) OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"Cross");
   if(f1>=s1&&f0<s0) OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"Cross");
}

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