#property copyright "Aiman."
#property show_inputs
#include <stdlib.mqh>

extern bool    use_timer   = false;
extern string  timer       = "08:30";
extern double  Lots        = 0.01;
extern int     Pips        = 10;
extern int     StopLoss    = 0;      
extern int     TakeProfit  = 13;
extern bool    Buy_Stop    = true;
extern bool    Sell_Stop   = true;
extern bool    Buy_Limit   = false;
extern bool    Sell_Limit  = false;

       int     Slippage    = NULL;
       
double bid,ask;

int deinit ()
{
   Comment ("");
   return (0);
}
   
int start()
{
   if (use_timer) countdown ();
   else
   {
      bid=Bid;
      ask=Ask;
      if (Buy_Stop)   PO_order(OP_BUYSTOP);
      if (Sell_Stop)  PO_order(OP_SELLSTOP);
      if (Buy_Limit)  PO_order(OP_BUYLIMIT);
      if (Sell_Limit) PO_order(OP_SELLLIMIT);
   }
   return(0);
}

void PO_order (int PO_type)
{
   int err;
   double PO,SL,TP;
   string comment;
   
   if (PO_type==OP_BUYSTOP)
   {
      comment  ="PO BUY";
      PO       =ask+Pips*Point;
      if (StopLoss>0)   SL=ask-(StopLoss*Point)+Pips*Point; else SL=0;
      if (TakeProfit>0) TP=bid+(TakeProfit*Point)+Pips*Point; else TP=0;     
   }

   if (PO_type==OP_SELLSTOP)
   {
      comment  ="PO SELL";
      PO       =bid-Pips*Point;
      if (StopLoss>0)   SL=bid+(StopLoss*Point)-Pips*Point; else SL=0;
      if (TakeProfit>0) TP=ask-(TakeProfit*Point)-Pips*Point; else TP=0;      
   }
   
   if (PO_type==OP_BUYLIMIT)
   {
      comment  ="Placing BuyLimit = ";
      PO       =ask-Pips*Point;
      if (StopLoss>0)   SL=ask-(StopLoss*Point)-Pips*Point; else SL=0;
      if (TakeProfit>0) TP=bid+(TakeProfit*Point)-Pips*Point; else TP=0;
   }

   if (PO_type==OP_SELLLIMIT)
   {
      comment  ="Placing SellLimit = ";
      PO       =bid+Pips*Point;
      if (StopLoss>0)   SL=bid+(StopLoss*Point)+Pips*Point; else SL=0;
      if (TakeProfit>0) TP=ask-(TakeProfit*Point)+Pips*Point; else TP=0;
   }

   bool loop=true;
   int cnt=0;
   while(loop)
   {
      OrderSend(Symbol(),PO_type,Lots,PO,Slippage,SL,TP,comment,0,0,CLR_NONE);
      err=GetLastError();
      Print (comment, ErrorDescription(err));
      cnt++;
      if (cnt>20 || err==0) loop = false;
      Sleep(50);
   }
   if (err>0) Print (comment+ErrorDescription(err));
}

void countdown ()
{
   string rotate []={" /"," -"," \\"," |"},textComment;
   int i;
   bool loop=true;
  
   while (loop)
   {
      i++; if (i>=3) i=0;
      
      textComment = StringConcatenate(rotate[i]," SCRIPT IS ACTIVE ",rotate[i],"\n",
                    "Current Time = ",TimeToStr(TimeLocal(), TIME_SECONDS),"\n",
                    "Execute Time = ",timer,
                    "\n\nPending Order to be executed: ");
                    
      if (Buy_Stop) textComment=textComment+"\nBuy Stop";
      if (Sell_Stop) textComment=textComment+"\nSell Stop";
      if (Buy_Limit) textComment=textComment+"\nBuy Limit";
      if (Sell_Limit) textComment=textComment+"\nSell Limit";
      
      textComment=textComment+"\n\nSettings:\nLots : "+DoubleToStr(Lots,2);  
      textComment=textComment+"\nPips : "+DoubleToStr(Pips,0); 
      textComment=textComment+"\nStopLoss : "+DoubleToStr(StopLoss,0); 
      textComment=textComment+"\nTakeProfit : "+DoubleToStr(TakeProfit,0);  
   
      if (TimeLocal()==StrToTime(timer)) loop=false;
      Sleep (1000);
      Comment (textComment);
   }
   
   use_timer=false;   
   Comment ("SENDING PENDING ORDER...");
   start();
}
