//+------------------------------------------------------------------+
//|                                                     EmaCross.mq4 |
//|                                         Copyright © 2007, ledzep |
//|                                          http://www.x-trader.net |
//+------------------------------------------------------------------+

/********************************************************************/

//
/********************************************************************/

#define  MAGICMA        20050610
#define  UPPERBAND      0
#define  MIDDLEBAND     1
#define  LOWERBAND      2
#define  BULL           1
#define  BEAR           0
#define  BUY            1
#define  SELL           0



extern double  Lots               = 0.1;
extern int     SlowEmaPeriod      = 20;
extern int     FastEmaPeriod      = 5;
extern double  TakeProfit         = 50;
extern double  StopLoss           = 10;
extern int     Slippage           = 1;



double         FastEma;
double         SlowEma;
int            ticket;
int            lstOrder;
bool           Cross=false;
bool           NewBar;

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   
   
   //TimeFiter
   /*
   if( TimeFilter )
      {
      if((( FromHourTrade <= ToHourTrade ) && ( Hour() < FromHourTrade || Hour() > ToHourTrade ))
         ||
         (( FromHourTrade >  ToHourTrade ) && ( Hour() < FromHourTrade && Hour() > ToHourTrade )))
         {return(0);}
       }
 
   */
   if(FastEma<SlowEma && OrdersTotal()==0 && NewBar && lstOrder==BUY)
      {
      if(StopLoss!=0 && TakeProfit!=0) ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, Bid + StopLoss*Point, Bid-TakeProfit*Point, "SELL", MAGICMA, 0, Red);    
      if(StopLoss==0 && TakeProfit!=0) ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, Bid-TakeProfit*Point, "SELL", MAGICMA, 0, Red);
      if(StopLoss!=0 && TakeProfit==0) ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, Bid + StopLoss*Point, 0, "SELL", MAGICMA, 0, Red);        
      if(StopLoss==0 && TakeProfit==0) ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "SELL", MAGICMA, 0, Red);          
      NewBar=false;
      lstOrder=SELL;
      }
      
    if(FastEma>SlowEma && OrdersTotal()==0 && NewBar && lstOrder==SELL)
      {
      if(StopLoss!=0 && TakeProfit!=0) ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, Ask - StopLoss*Point, Ask + TakeProfit*Point,"BUY",MAGICMA,0,Blue);  
      if(StopLoss==0 && TakeProfit!=0) ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, Ask + TakeProfit*Point, "BUY", MAGICMA, 0, Blue);  
      if(StopLoss!=0 && TakeProfit==0) ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, Ask - StopLoss*Point, 0, "BUY", MAGICMA, 0, Blue);       
      if(StopLoss==0 && TakeProfit==0) ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "BUY", MAGICMA, 0, Blue);     
      NewBar=false;
      lstOrder=BUY;
      }   
      
   
   return;
  }

//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
   {
 
   
  
      
   
   }

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
   {
 
   //check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
 
   
   FastEma=iMA(NULL,0,FastEmaPeriod,1,MODE_EMA,PRICE_CLOSE,0);
   SlowEma=iMA(NULL,0,SlowEmaPeriod,1,MODE_EMA,PRICE_CLOSE,0);
  
   
   if(Volume[0]==1)NewBar=true;
         
   if(OrdersTotal()==0)
      {
      CheckForOpen();      
      }
   else 
      {
      CheckForClose();
      
      }


   }

//+------------------------------------------------------------------+
//| General functions                                                |
//+------------------------------------------------------------------+

void CloseAllOrders()  
   {
   while (OrdersTotal()>0)
      {
      OrderSelect(OrdersTotal()-1,SELECT_BY_POS);
      if (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3,Red);
      if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
      }
   }
   
//Regresa el tipo de la ultima orden abierta
int iOrderType()
   {   
   for(int order=OrdersTotal();order>=0;order--)
      {
      OrderSelect(order,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()==OP_BUY)return(OP_BUY);
      if(OrderType()==OP_SELL)return(OP_SELL);
      }
   return(0);
   }
   
   