//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern int     MagicNumber = 16384;
extern double  TakeProfit = 50;
extern double  StopLoss = 50;
extern double  Lots = 0.1;
extern int     OpenTradeTime = 1100;      // Open Trade time
extern int     MinutesToWaitForTick = 2;
extern bool    OpenBuy = true;
extern bool    OpenSell = false;
extern int     NumBuys = 1;
extern int     NumSells = 1;
extern int     Slippage = 3;



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int cnt, ticket, total;
   int ct, EndTradeTime;
   
   
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
//   if(TakeProfit<10)
//     {
//      Print("TakeProfit less than 10");
//      return(0);  // check TakeProfit
//     }

   ct = Hour() * 100 + Minute();
   EndTradeTime = OpenTradeTime + MinutesToWaitForTick;

   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if(ct >= OpenTradeTime && ct < EndTradeTime)
      {
         if (OpenBuy)
         {
           for ( cnt = 0; cnt < NumBuys; cnt++)
           {
             ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Bid - StopLoss * Point,Ask+TakeProfit*Point,"",MagicNumber,0,Green);
             if(ticket>0)
             {
              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
             }
             else Print("Error opening BUY order : ",GetLastError()); 
           }
          }
      // check for short position (SELL) possibility
          if(OpenSell)
          {
           for ( cnt = 0; cnt < NumSells; cnt++)
           {
             ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Ask + StopLoss * Point,Bid-TakeProfit*Point,"",MagicNumber,0,Red);
             if(ticket>0)
             {
              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
             }
             else Print("Error opening SELL order : ",GetLastError());
           } 
          }
       }
     }
   return(0);
  }
// the end.