//+------------------------------------------------------------------+
//|                                          rsaravananrcrossing.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
/*
Hi.,
Would you write me an EA for the following conditions,
BUY when 15 EMA crosses 20 SMA upward
SELL when 15 EMA crosses 20 SMA downward
I can develop the EA from there.
I need EA to work with my Oanda MT4 account.
Thanks in advance.
*/

input int Magic_Number = 103;
input double LotSize = 0.1;   //Lot Size
input double TP = 30;        //Take Profit in pips
input double SL = 30;        //Stop Loss in pips

double ema15[5];
double ema20[5];
datetime LastTrade = 0;

int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      double xTP=0;
      double xSL=0;
      int xticket=0;
      for(int i=1; i<5; i++){
         ema15[i] = iMA(NULL,0,15,0,MODE_EMA,PRICE_CLOSE,i);
         ema20[i] = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,i);
      }
      if(ema15[1]>ema20[1] && ema15[4]<ema20[4]){  //Buy
         if(xTradeAllowed()){
            xTP = ND(Ask + nPips(TP));
            xSL = ND(Bid - nPips(SL));
            xticket=OrderSend(Symbol(),OP_BUY,LotSize,ND(Bid),3,xSL,xTP,NULL,Magic_Number);
            if(xticket>0){
               LastTrade = TimeCurrent() + PeriodSeconds();
            }
         }
      }
      if(ema15[1]<ema20[1] && ema15[4]>ema20[4]){  //Sell
         if(xTradeAllowed()){
            xTP = ND(Bid - nPips(TP));
            xSL = ND(Ask + nPips(SL));
            xticket=OrderSend(Symbol(),OP_SELL,LotSize,ND(Bid),3,xSL,xTP,NULL,Magic_Number);
            if(xticket>0){
               LastTrade = TimeCurrent() + PeriodSeconds();
            }
         }
      }
      
  }
//+------------------------------------------------------------------+
double ND(double val)
{
   return(NormalizeDouble(val,Digits));
}
//+------------------------------------------------------------------+
double nPips(double pip)
  {
   if(Digits<=3)
      return(0.01*pip);
   if(Digits>=4)
      return(0.0001*pip);
   return(0);
   }
   
   
bool xTradeAllowed()
  {
   bool retVal=False;
   datetime xt= TimeCurrent();
   if(xt>LastTrade)retVal=True;
   return(retVal);
  }
