//+------------------------------------------------------------------+
//|                                              Simple MA cross.mq4 |
//|                                                       Ravi Desai |
//|                      Licensed under GNU LGPL version 3 and later.|
//|                             License found at www.gnu.org/licenses|
//+------------------------------------------------------------------+
#property copyright "Ravi Desai"
#property link      ""

/*
Things to do:

*/

//---- input parameters
extern int       MA_period=12;
extern double    TP = 10;
extern double    SL = 20;
extern double    lot_size = 0.2;
extern datetime new_time;
int ticket;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   //Set new_time
   new_time = Time[0];
   //Set up ticket.
   ticket = 0;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  //Check if you are between 1200 and 1800, and trade only between those time
//  if(TimeHour(Time[0]) >= 1200 && TimeHour(Time[0]) <= 1800)
      if(Hour() >= 12 && Hour() <= 18)
   {
      //Check if there is a ticket open.
      if(ticket == 0 || OrderSelect(0, SELECT_BY_POS, MODE_TRADES) == false) //To make sure we do one trade at one time.
      {
         Enter_trade(); //If no ticket open, enter trade.
         return(0);
      }
   }
  return(0);
  }
  
void Enter_trade()
{
  //Check if new bar has opened.
  if(new_time < Time[1])
  {
   double sl, tp;
//Check if previous bar close is greater than MA_period.
   if(Close[1] > iMA(Symbol(), 0, MA_period, 0, MODE_SMA, PRICE_CLOSE, 1)
      && Close[2] <= iMA(Symbol(), 0, MA_period, 0, MODE_SMA, PRICE_CLOSE, 2))
   {
      ticket = OrderSend(Symbol(), OP_BUY, lot_size, Ask, 3, Ask - (SL * Point), Ask + (TP * Point), "Buy at market - SMA cross", 0, 0, Blue);
   }
   if(Close[1] < iMA(Symbol(), 0, MA_period, 0, MODE_SMA, PRICE_CLOSE, 1)
      && Close[2] >= iMA(Symbol(), 0, MA_period, 0, MODE_SMA, PRICE_CLOSE, 2))
  {
      ticket = OrderSend(Symbol(), OP_SELL, lot_size, Bid, 3, Bid + (SL * Point), Bid - (TP * Point), "Sell at market - SMA cross", 0, 0, Red);
   }
   new_time = Time[1];
  }
   return(0);
}

