
extern double  Lot=1;

extern int jarak=15,target=20,stop=70;
extern int JamMulaiTrading=17,JamSelesaiLimit=23;

int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
        EntryLimit(JamMulaiTrading,JamSelesaiLimit,jarak,target,stop);
//----
   return(0);
  }
//+------------------------------------------------------------------+


void EntryLimit(int jam,int selesai,int delta,int TP,int SL)
{   
   if (TimeHour(TimeCurrent())==selesai && ( ChkJmlTrd(2)>0 || ChkJmlTrd(3)>0)) DeleteSemuaLimitOrder();
   if (TimeHour(TimeCurrent())<selesai && TimeHour(TimeCurrent())>=jam && ChkJmlTrd(2)==0 && ChkJmlTrd(0)==0)
      OrderSend(Symbol(),OP_BUYLIMIT,Lot,Bid-delta*Point,0,Bid-(delta+SL)*Point,Bid+(TP-delta)*Point,"LB",0,0,Blue);
      
    if (TimeHour(TimeCurrent())<selesai && TimeHour(TimeCurrent())>=jam && ChkJmlTrd(3)==0 && ChkJmlTrd(1)==0) 
      OrderSend(Symbol(),OP_SELLLIMIT,Lot,Bid+delta*Point,0,Bid+(SL+delta)*Point,Bid+(delta-TP)*Point,"LS",0, 0,Red);
}

int ChkJmlTrd(int mode)//, int MagicEA)
{
   //Constant        Value Description 
   //--------        ----- ----------------------------
   //OP_BUY          0     Buying position. 
   //OP_SELL         1     Selling position. 
   //OP_BUYLIMIT     2     Buy limit pending position. 
   //OP_SELLLIMIT    3     Sell limit pending position. 
   //OP_BUYSTOP      4     Buy stop pending position. 
   //OP_SELLSTOP     5     Sell stop pending position. 

   //--------        ----- ----------------------------
   int i, JmlTrd;
   for (i=0; i<OrdersTotal(); i++)
   {
      OrderSelect(i,SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==mode && OrderSymbol()==Symbol())// && OrderMagicNumber()==MagicEA)
      {
         JmlTrd++;
      }
   }
   return(JmlTrd);
}

void DeleteSemuaLimitOrder()
{
   int i;
   for(i = OrdersTotal(); i >= 0; i--)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if((OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT) && OrderSymbol()==Symbol())  
      {
         OrderDelete(OrderTicket()); 
      }
   }
}



