#property indicator_chart_window
#property indicator_color1 CLR_NONE
#property indicator_buffers 3
#property indicator_color2  CLR_NONE
 
extern int             ma_per = 10;
extern int             ma_met = 3;
extern color          col_ang = clrBlue;
extern int           ExtDepth = 21;
extern int                ext = 0;
extern int           Complect = 0;
double varMA[], value[];

//-------------------------------------------------------------------------
int init()
  {
   IndicatorBuffers(3);   
   IndicatorDigits(Digits+1);   
   SetIndexBuffer(0,varMA);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1, value);

   return(0);
  }

//-------------------------------------------------------------------------
int deinit()
  {
   ObjectDelete("AngleA "+Complect);
   ObjectDelete("AngleText "+Complect);
   Comment("");
   return(0);
  }
  
//-------------------------------------------------------------------------
int start()
  {
   int i, limit, counted_bars=IndicatorCounted();
   limit = Bars-counted_bars-1; 
   double MALast, MACurr;

   for(i=limit; i>=0; i--)
   {
      varMA[i] = iMA(NULL,0,ma_per,0,ma_met,PRICE_CLOSE,i);
      int nb_0 = GetZZbar(ext);
      
      MALast= varMA[i+nb_0];
      MACurr= varMA[i+1];
 
      if (ObjectFind("AngleA "+Complect)<0)
      {
        ObjectCreate("AngleA "+Complect,OBJ_TRENDBYANGLE,0,Time[i+nb_0],MALast,Time[i+1],MACurr);
        ObjectSet("AngleA "+Complect,OBJPROP_STYLE,2);
      }
      
      if (ObjectFind("AngleText "+Complect)==-1)
          ObjectCreate("AngleText "+Complect,OBJ_TEXT,0,0,0);
      ObjectSet("AngleA "+Complect,OBJPROP_TIME2,Time[i+1]);
      ObjectSet("AngleA "+Complect,OBJPROP_PRICE2,MACurr);
      ObjectSet("AngleA "+Complect,OBJPROP_TIME1,Time[i+nb_0]);
      ObjectSet("AngleA "+Complect,OBJPROP_PRICE1,MALast);
      ObjectSet("AngleA "+Complect,OBJPROP_COLOR,col_ang);
      value[i] = (double)ObjectGet("AngleA "+Complect,OBJPROP_ANGLE);
      if(value[i]>90)
        value[i] = value[i]-360;
      value[i+1] = value[i];
      ObjectSetText("AngleText "+Complect,DoubleToStr(value[i],2),8,"Verdana",col_ang);
      ObjectMove("AngleText "+Complect,0,Time[0]+5*Period()*60,ObjectGetValueByShift("AngleA "+Complect,i)); 
   }
   Comment(value[0]);
   
   return(0);
  }

//-------------------------------------------------------------------------
int GetZZbar(int ne=0)
 {
  double zz;
  int    j, k=iBars(NULL, 0), ke=0;
  
  for (j =1; j <k; j++)
   {
    zz=iCustom(NULL, 0, "ZigZag", ExtDepth,5,3, 0,j);
    if (zz !=0)
     {
      ke++;
      if (ke > ne) 
        return(j);
     }
   }
  
  
  return(-1);
 }
 
//-------------------------------------------------------------------------
