//+------------------------------------------------------------------+
//|                                          6pending daily v1-0.mq4 |
//|                                                       Eben Brand |
//|                                         http://www.ebenbrand.com |
//+------------------------------------------------------------------+
#property copyright "Eben Brand"
#property link      "http://www.ebenbrand.com"


extern int       TriggerTime=17;
extern int    ExpirationTime=23;
extern int       magicno=888;
static int       LASTRUN;
extern double    Lots=0.01;
extern double    MaxLots=10;
extern double    Risk=0.075;
extern double    SL=30;
extern double    TP1=15;
extern double    TP2=30;
extern double    TP3=50;
extern bool UseMoneyManagement = true;
extern bool AccountIsMicro = false;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

   double yesterday_high=0;
   double yesterday_low=0;
   double yesterday_close=0;
   double maxdiff=0;
   double highdiff=0;
   double lowdiff=0;
   double stop=0;
   double TP=0;
   double lots=1;
   double price=0;
   int seed=0;
   int slip=5;
   int ticket1=0;
   int ticket2=0;
   int ticket3=0;
   int ticket4=0;
   int ticket5=0;
   int ticket6=0;
   int i=0; // Counter


// Number of bars in previous day

   double prev_day_highs[24];
   double prev_day_lows[24];




   if(UseMoneyManagement==true) 
      {
      Lots = DefineLotSize(); //Adjust the lot size     total
      }





   while((TimeDayOfWeek(Time[0])  > 0) && (TimeDayOfWeek(Time[0]) <6))
      {
      if ((LASTRUN == Time[0]))
         {
         return(0);
         }
      else
         {
         LASTRUN = Time[0];
         }



//Now we check whether the trigger time has come up
         if (TimeHour(Time[0]) != TriggerTime)
            return(0);




//---- Get new daily prices

      ArrayCopySeries(prev_day_highs, MODE_HIGH, Symbol(), PERIOD_H1);
      ArrayCopySeries(prev_day_lows, MODE_LOW, Symbol(), PERIOD_H1);
      yesterday_close = iClose(Symbol(), PERIOD_H1, 1);
      yesterday_high = prev_day_highs[ArrayMaximum(prev_day_highs, 24, 0)];
      yesterday_low = prev_day_lows[ArrayMinimum(prev_day_lows, 24, 0)];
//Print("New daily stats : ", "Date : ", Month( ), "/", Day(), "Close ", yesterday_close, "  ", "High ", yesterday_high, "  ", "Low ", yesterday_low);



//Buystop
   if (CountLongs(magicno) == 0)
      {
      price = yesterday_high + 5*Point;
      stop = yesterday_high - SL*Point;

            ticket1 = OrderSend(Symbol(), OP_BUYSTOP, Lots, price, slip, stop, price+TP1*Point, "6pending Buy Order", magicno, StrToTime(ExpirationTime+":00"), Blue);
//            Print("Buy =  ", price, "    lots = ", lots, "    Stop = ", stop, "    TP1 = ", TP1, "    Order no = ", ticket1);
            if(ticket1<0)
               {
               Print("Buy Order failed with error #",GetLastError());
               ticket1 = 0;
               return(0);
               }

            ticket2 = OrderSend(Symbol(), OP_BUYSTOP, Lots, price, slip, stop, price+TP2*Point, "6pending Buy Order", magicno, StrToTime(ExpirationTime+":00"), Blue);
//            Print("Buy =  ", price, "    lots = ", lots, "    Stop = ", stop, "    TP2 = ", TP2, "    Order no = ", ticket2);
            if(ticket2<0)
               {
               Print("Buy Order failed with error #",GetLastError());
               ticket2 = 0;
               return(0);
               }

            ticket3 = OrderSend(Symbol(), OP_BUYSTOP, Lots, price, slip, stop, price+TP3*Point, "6pending Buy Order", magicno, StrToTime(ExpirationTime+":00"), Blue);
//            Print("Buy =  ", price, "    lots = ", lots, "    Stop = ", stop, "    TP3 = ", TP3, "    Order no = ", ticket3);
            if(ticket3<0)
               {
               Print("Buy Order failed with error #",GetLastError());
               ticket3 = 0;
               return(0);
               }
       }

//Sellstop
   if (CountShorts(magicno) == 0)
      {
      price = yesterday_low - 5*Point;
      stop = yesterday_low + SL*Point;
         
            ticket4 = OrderSend(Symbol(), OP_SELLSTOP, Lots, price, slip, stop, price-TP1*Point, "Sell Order", magicno, StrToTime(ExpirationTime+":00"), Blue);
//            Print("Sell =  ", price, "lots = ", lots, "    Stop = ", stop, "    TP1 = ", TP1, "     Order no = ", ticket4);
            if(ticket4<0)
               {
               Print("Buy Order failed with error #",GetLastError());
               ticket4 = 0;
               return(0);
               }
         
            ticket5 = OrderSend(Symbol(), OP_SELLSTOP, Lots, price, slip, stop, price-TP2*Point, "Sell Order", magicno, StrToTime(ExpirationTime+":00"), Blue);
//            Print("Sell =  ", price, "lots = ", lots, "    Stop = ", stop, "    TP2 = ", TP2, "     Order no = ", ticket5);
            if(ticket5<0)
               {
               Print("Buy Order failed with error #",GetLastError());
               ticket5 = 0;
               return(0);
               }
         
            ticket6 = OrderSend(Symbol(), OP_SELLSTOP, Lots, price, slip, stop, price-TP3*Point, "Sell Order", magicno, StrToTime(ExpirationTime+":00"), Blue);
//            Print("Sell =  ", price, "lots = ", lots, "    Stop = ", stop, "    TP3 = ", TP3, "     Order no = ", ticket6);
            if(ticket6<0)
               {
               Print("Buy Order failed with error #",GetLastError());
               ticket6 = 0;
               return(0);
               }
       }






   
//----
   return(0);
  }
  }
//+------------------------------------------------------------------+


double DefineLotSize() {
   double lotMM = 0;
   if(AccountIsMicro==false) 
      { 
      lotMM = NormalizeDouble((Risk * AccountEquity() / ((Ask+Bid)/(Point*20))), 1);
      } 
   else 
      { //micro account
      lotMM = NormalizeDouble((Risk * AccountEquity() / ((Ask+Bid)/(Point*20))), 2);
      }
   return (lotMM);
}



//+------------------------------------------------------------------+
//| Calculate concurrent Long position                               |
//+------------------------------------------------------------------+
int CountLongs(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if( OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_BUY || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)
   count++;
 }//for
 return(count);
}




//+------------------------------------------------------------------+
//| Calculate concurrent short position                              |
//+------------------------------------------------------------------+
int CountShorts(int MagicNumber)
{
 int count=0;
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber )
   continue;
   
  if(OrderType()==OP_SELL || OrderType()==OP_SELLLIMIT || OrderType()==OP_SELLSTOP)
  count++;
 }//for
 return(count);
}





