//+------------------------------------------------------------------+
//|                                                     SimpleEA_MA.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                            https://triantoadi.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://triantoadi.com"
#property version   "1.00"
#property strict

extern int StartHour = 9;
extern double Lots = 0.2;
extern int SL = 40;
extern int TP = 40;
extern int MA_Period = 14;

void OnTick()
{
static bool FirstTick = true;
static int ticket = 0;
double ma = iMA(Symbol(), Period(), MA_Period, 0, 0, 0, 1);

if(Hour() == StartHour)
{
if(FirstTick == true)
{
FirstTick = false;

   bool res;
   res = OrderSelect(ticket, SELECT_BY_TICKET);
   
   if(res == true)
   {
      if(OrderCloseTime() == 0)
      {
         bool res2;
         res2 = OrderClose(ticket, Lots, OrderClosePrice(), 10);
         
         if(res2 == false)
         {
            Alert("Error Closing Order #", ticket);
         }
      }
   }

   if(Open[0] < Open[StartHour])
   {
   
      //ma checking
      
      if(Close[1] < ma)
      {
      ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 10,Bid - SL*Point*10, Bid + TP*Point*10, "Simple EA" );
      
      if(ticket < 0)
      {
         Alert("Sending Order Error !");
      }
      }
   }
   else
   {
   
      //ma checking
      if(Close[1] > ma)
      {
      ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 10,Ask + SL*Point*10, Ask - TP*Point*10, "Simple EA" );
      
      if(ticket < 0)
      {
         Alert("Sending Order Error !");
      }
      }
   }
}

}
else
{
FirstTick = true;
}

}