#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Orange
#property indicator_color3 Yellow
#property indicator_color4 White
#property indicator_color5 White
#property indicator_color6 Yellow
#property indicator_color7 Orange
#property indicator_color8 Red

extern int BarsCount  = 500;
extern int MAPeriod   = 100;
extern int MAMethod   = 0;
extern int MAPrice    = 0;
extern int fontsize   = 10;
extern int font_shift = 40;
double max, min, gd_112, gd_120, gd_128, delta_max, gd_130, MA_100;
double fu_63[], fu_50[], fu_38[], fu_23[], fd_23[], fd_38[], fd_50[], fd_63[];

int init() {
   SetIndexStyle(0, DRAW_LINE);   SetIndexBuffer(0, fu_63);   SetIndexLabel(0, "61.8%");
   SetIndexStyle(1, DRAW_LINE);   SetIndexBuffer(1, fu_50);   SetIndexLabel(1, "50%");
   SetIndexStyle(2, DRAW_LINE);   SetIndexBuffer(2, fu_38);   SetIndexLabel(2, "38.2%");
   SetIndexStyle(3, DRAW_LINE);   SetIndexBuffer(3, fu_23);   SetIndexLabel(3, "23.5%");
   SetIndexStyle(4, DRAW_LINE);   SetIndexBuffer(4, fd_23);   SetIndexLabel(4, "23.5%");
   SetIndexStyle(5, DRAW_LINE);   SetIndexBuffer(5, fd_38);   SetIndexLabel(5, "38.2%");
   SetIndexStyle(6, DRAW_LINE);   SetIndexBuffer(6, fd_50);   SetIndexLabel(6, "50%");
   SetIndexStyle(7, DRAW_LINE);   SetIndexBuffer(7, fd_63);   SetIndexLabel(7, "61.8%");
   ObjectCreate("l1", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l1", "61.8%", fontsize, "Arial", Red);
   ObjectCreate("l2", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l2", "50%",   fontsize, "Arial", Red);
   ObjectCreate("l3", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l3", "38.2%", fontsize, "Arial", Red);
   ObjectCreate("l4", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l4", "23.5%", fontsize, "Arial", Red);
   ObjectCreate("l5", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l5", "23.5%", fontsize, "Arial", Red);
   ObjectCreate("l6", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l6", "38.2%", fontsize, "Arial", Red);
   ObjectCreate("l7", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l7", "50%",   fontsize, "Arial", Red);
   ObjectCreate("l8", OBJ_TEXT, 0, 0, 0);   ObjectSetText("l8", "61.8%", fontsize, "Arial", Red);
   return (0);
}

int deinit() {
   ObjectDelete("l1");   ObjectDelete("l2");   ObjectDelete("l3");   ObjectDelete("l4");
   ObjectDelete("l5");   ObjectDelete("l6");   ObjectDelete("l7");   ObjectDelete("l8");
   return (0);
}

int start() {
   double MA, delta_up, delta_dn;
   max = 0;
   min = 0;
   if (iBars(NULL, 0) < BarsCount) BarsCount = iBars(NULL, 0) - MAPeriod - 1;
   for (int i = BarsCount; i >= 0; i--) {
      MA = iMA(NULL, 0, MAPeriod, 0, MAMethod, MAPrice, i);
      delta_up = High[i] - MA;
      if (delta_up > max) max = delta_up;
      delta_dn = Low[i] - MA;
      if (delta_dn < min) min = delta_dn;
   }
   if (MathAbs(max) > MathAbs(min)) delta_max = max;   else delta_max = min;
   gd_128 = 0.618 * delta_max;
   gd_120 = 0.500 * delta_max;
   gd_112 = 0.236 * delta_max;
   gd_130 = 0.382 * delta_max;
   for (i = BarsCount; i >= 0; i--) {
      MA_100 = iMA(NULL, 0, MAPeriod, 0, MAMethod, MAPrice, i);
      fu_63[i] = MA_100 + gd_128;
      fu_50[i] = MA_100 + gd_120;
      fu_38[i] = MA_100 + gd_130;
      fu_23[i] = MA_100 + gd_112;
      fd_23[i] = MA_100 - gd_112;
      fd_38[i] = MA_100 - gd_130;
      fd_50[i] = MA_100 - gd_120;
      fd_63[i] = MA_100 - gd_128;
   }
   ObjectMove("l1", 0, Time[font_shift], fu_63[font_shift]);
   ObjectMove("l2", 0, Time[font_shift], fu_50[font_shift]);
   ObjectMove("l3", 0, Time[font_shift], fu_38[font_shift]);
   ObjectMove("l4", 0, Time[font_shift], fu_23[font_shift]);
   ObjectMove("l5", 0, Time[font_shift], fd_23[font_shift]);
   ObjectMove("l6", 0, Time[font_shift], fd_38[font_shift]);
   ObjectMove("l7", 0, Time[font_shift], fd_50[font_shift]);
   ObjectMove("l8", 0, Time[font_shift], fd_63[font_shift]);
   return (0);
}