extern int per    = 14;  // Period for signal
extern int update = 1;   // 1 = continuously update, 0 = freeze

extern int dDay = 0;
extern bool pCloseBool;
extern double pClose;

extern int Par1 = 4;

extern int Hr = 10;
 
int init() {
   return(0);
}

int start() {
   int    counted_bars = IndicatorCounted();
   int    shift, i, limit, lim;
   double prevSignal = -1, barClose = -1, imp;
   
   double dat[15];
   int cnt;

   imp = iCustom(NULL, 0, "DayImpulse2DDMA", per, 0, 0, 0, 0, 0, per) * Point;

   barClose = Close[0] - iCustom(NULL, 0, "DayImpulse2DDMA", per, 0, per) * Point;

   //Print(TimeToStr(Time[0]));
   cnt = -1;
   for(shift = per - 1; shift >= 0; shift--) {
      imp = iCustom(NULL, 0, "DayImpulse2DDMA", per, 0, shift) * Point;
      
      cnt++;
      if(cnt <= Par1) dat[cnt] = barClose + imp;
      
      //Print(barClose + imp);
      //Print(dat[cnt]);
      
      //drawBuffer[shift] = barClose + imp;
   }

   if((Hour() == Hr) && (dDay != Day()))
   {
      dDay = Day();
   
      double min = 999.0;
      for(i=0;i<=Par1;i++) 
      {   
         if(dat[i] != 0)
         {
            if(min > dat[i]) min = dat[i];
         }
      }
      //Print("min: ", min);
   
      double max = 0;
      for(i=0;i<=Par1;i++) 
      {   
         if(max < dat[i]) max = dat[i];
         //Print(dat[i]);
      }
   
      //Print("-------");
      //Print("max: ", max);
      
      //if(MathAbs(min - max) > 0.0020)
      //{
         if(OrdersTotal() == 0)
         {
            if(MathAbs(max - Close[0]) > MathAbs(min - Close[0]))
            {
               OrderSend(Symbol(),OP_BUY,1.5,Ask,3,0,Ask+0.0025,"",2,0,Blue);
               pClose = 25;
               pCloseBool = false;
               //OrderSend(Symbol(),OP_BUY,1,Ask,3,0,Ask+MathAbs(min - max),"",2,0,Blue);
            
               //OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-MathAbs(min - max)-0.0010,Ask+MathAbs(min - max),"",2,0,Blue);
               //OrderSend(Symbol(),OP_SELLSTOP,1,Ask-MathAbs(min - max)-0.0010,3,0,Ask-2*MathAbs(min - max)-0.0010,"",2,0,Blue);
            }

            if(MathAbs(max - Close[0]) < MathAbs(min - Close[0]))
            {
               OrderSend(Symbol(),OP_SELL,1.5,Bid,3,0,Bid-0.0025,"",1,0,Red);
               pClose = 25;
               pCloseBool = false;
               //OrderSend(Symbol(),OP_SELL,1,Bid,3,0,Bid-MathAbs(min - max),"",1,0,Red);
            
               //OrderSend(Symbol(),OP_SELL,1,Bid,3,Bid+MathAbs(min - max)+0.0010,Bid-MathAbs(min - max),"",1,0,Red);
               //OrderSend(Symbol(),OP_BUYSTOP,1,Bid+MathAbs(min - max)+0.0010,3,0,Bid+2*MathAbs(min - max)+0.0010,"",1,0,Red);
            }
         }
      //}
   }
  
   /*int total;   
   double prof;
   if(OrdersTotal() > 0)
   {
      total = OrdersTotal();
      for(cnt=0;cnt<total;cnt++)
      {             
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);         
         
         prof = OrderProfit() / 10;
         
         if(prof > 15) {pCloseBool = true; Print("prof > 15");}
         if(pCloseBool && (prof < 10)) {pClose = 4; Print(pClose);}
         
         Print("prof: ", prof);
         if(prof > pClose)
         {
            if(OrderType()==OP_BUY)
            {
               OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);   
            }
            if(OrderType()==OP_SELL)
            {
               OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
            }
         }
      }
   }*/


   return(0);
}

