#property copyright "Copyright � 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Gray
#property indicator_color2 Gray
#property indicator_color3 Gray
#property indicator_color4 Gray

extern int pivotStrength = 5;
extern int maxLookBackBars = 200;
extern int pivotPipDiff = 10;
extern int zonePipSize = 1;
extern int maxLevels = 3;
extern color Color_1 = LightGray;
extern color Color_2 = DimGray;

bool GLi_96 = TRUE;
double GL_ibuf_100[];
double GL_ibuf_104[];
double GL_ibuf_108[];
double GL_ibuf_112[];
string GL_symbol_116 = "";
double GL_point_124 = 0.0;
double GL_digits_132 = 0.0;
int GLi_140 = 1;

// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
   IndicatorBuffers(4);
   SetIndexBuffer(0, GL_ibuf_100);
   SetIndexBuffer(1, GL_ibuf_104);
   SetIndexBuffer(2, GL_ibuf_108);
   SetIndexBuffer(3, GL_ibuf_112);
   GL_symbol_116 = Symbol();
   GL_point_124 = MarketInfo(GL_symbol_116, MODE_POINT);
   GL_digits_132 = MarketInfo(GL_symbol_116, MODE_DIGITS);
   if (GL_point_124 == 0.001) {
      GL_point_124 = 0.01;
      GL_digits_132 = 3;
   } else {
      if (GL_point_124 == 0.00001) {
         GL_point_124 = 0.0001;
         GL_digits_132 = 5;
      }
   }
   if (GL_digits_132 == 3.0 || GL_digits_132 == 5.0) GLi_140 = 10;
   return (0);
}

// 52D46093050F38C27267BCE42543EF60
int deinit() {
   return (0);
}

// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   int LCi_16;
   double point_20;
   double LCd_28;
   double LCd_36;
   string LCs_44;
   bool LCi_52;
   int ind_counted_0 = IndicatorCounted();
   int LCi_4 = Bars - ind_counted_0;
   int LCi_8 = 0;
   if (GLi_96) LCi_8 = 1;
   for (int LCi_12 = LCi_4; LCi_12 >= LCi_8; LCi_12--) {
      LCi_16 = iHighest(Symbol(), 0, MODE_HIGH, pivotStrength * 2 + 1, LCi_12);
      GL_ibuf_100[LCi_12] = 0;
      GL_ibuf_104[LCi_12] = 0;
      GL_ibuf_108[LCi_12] = 0;
      GL_ibuf_112[LCi_12] = 0;
      if (LCi_16 == LCi_12 + pivotStrength) {
         GL_ibuf_100[LCi_12] = High[LCi_16];
         GL_ibuf_104[LCi_12] = Time[LCi_16];
      }
      LCi_16 = iLowest(Symbol(), 0, MODE_LOW, pivotStrength * 2 + 1, LCi_12);
      if (LCi_16 == LCi_12 + pivotStrength) {
         GL_ibuf_108[LCi_12] = Low[LCi_16];
         GL_ibuf_112[LCi_12] = Time[LCi_16];
      }
      if (LCi_12 == LCi_8) {
         point_20 = MarketInfo(Symbol(), MODE_POINT);
         LCd_28 = Close[LCi_8];
         LCd_36 = 0;
         LCs_44 = "";
         LCi_52 = FALSE;
         for (int LCi_56 = 1; LCi_56 <= maxLevels; LCi_56++) {
            LCd_28 = f0_0(LCd_28, 1, LCi_8);
            if (LCd_28 == LCd_36) LCs_44 = LCs_44 + " L" + LCi_56;
            else {
               LCs_44 = "L" + LCi_56;
               LCi_52 = LCi_56;
            }
            f0_1("resZone" + LCi_52, LCd_28 - zonePipSize * point_20 * GLi_140 / 2.0, LCd_28 + zonePipSize * point_20 * GLi_140 / 2.0, Time[LCi_8], Time[LCi_8 + maxLookBackBars - 1],
               Color_1, 3);
            f0_2("resLine" + LCi_52, LCd_28 - zonePipSize * point_20 * GLi_140 / 2.0, Time[LCi_8], Time[LCi_8 + maxLookBackBars - 1], STYLE_SOLID, Color_1, 1);
            f0_3("resTag" + LCi_52, LCs_44, Time[3], LCd_28, Color_1);
            LCd_36 = LCd_28;
            LCd_28 += pivotPipDiff * point_20 * GLi_140;
         }
         LCd_28 = Close[LCi_8];
         LCd_36 = 0;
         LCs_44 = "";
         LCi_52 = FALSE;
         for (LCi_56 = 1; LCi_56 <= maxLevels; LCi_56++) {
            LCd_28 = f0_0(LCd_28, -1, LCi_8);
            if (LCd_28 == LCd_36) LCs_44 = LCs_44 + " L" + LCi_56;
            else {
               LCs_44 = "L" + LCi_56;
               LCi_52 = LCi_56;
            }
            f0_1("supZone" + LCi_52, LCd_28 - zonePipSize * point_20 * GLi_140 / 2.0, LCd_28 + zonePipSize * point_20 * GLi_140 / 2.0, Time[LCi_8], Time[LCi_8 + maxLookBackBars - 1],
               Color_2, 3);
            f0_2("supLine" + LCi_52, LCd_28 - zonePipSize * point_20 * GLi_140 / 2.0, Time[LCi_8], Time[LCi_8 + maxLookBackBars - 1], STYLE_SOLID, Color_2, 1);
            f0_3("supTag" + LCi_52, LCs_44, Time[3], LCd_28, Color_2);
            LCd_36 = LCd_28;
            LCd_28 -= pivotPipDiff * point_20 * GLi_140;
         }
      }
   }
   return (0);
}

// A35D1FA551F99704156133930369CEBE
double f0_0(double ARd_0, int ARi_8, int ARi_12) {
   double LCda_48[];
   double LCda_52[];
   int index_16 = 0;
   int index_20 = 0;
   double LCd_ret_24 = 0;
   int LCi_32 = 0;
   int LCi_36 = 0;
   int LCi_40 = -1;
   int LCi_44 = -1;
   ArrayResize(LCda_48, maxLookBackBars);
   ArrayInitialize(LCda_48, 0);
   ArrayResize(LCda_52, maxLookBackBars);
   ArrayInitialize(LCda_52, 0);
   index_16 = ARi_12;
   for (index_20 = 0; index_20 < MathMin(maxLookBackBars, Bars) && index_16 < Bars; index_16++) {
      if (GL_ibuf_100[index_16] != 0.0) {
         LCda_48[index_20] = GL_ibuf_100[index_16];
         LCda_52[index_20] = GL_ibuf_104[index_16];
         index_20++;
      }
      if (GL_ibuf_108[index_16] != 0.0) {
         LCda_48[index_20] = GL_ibuf_108[index_16];
         LCda_52[index_20] = GL_ibuf_112[index_16];
         index_20++;
      }
   }
   double point_56 = MarketInfo(Symbol(), MODE_POINT);
   int count_64 = 0;
   int count_68 = 0;
   LCi_32 = 0;
   LCi_36 = 0;
   LCi_40 = -1;
   LCi_44 = -1;
   for (index_16 = 0; index_16 <= maxLookBackBars - 1; index_16++) {
      if (LCda_52[index_16] < Time[maxLookBackBars]) break;
      count_64 = 0;
      count_68 = 0;
      for (index_20 = maxLookBackBars - 2; index_20 >= 0; index_20--) {
         if (LCda_52[index_20] > Time[maxLookBackBars] && LCda_48[index_20] > 0.0) {
            if (ARi_8 > 0 && LCda_48[index_16] > 0.0 && LCda_48[index_16] >= ARd_0) {
               if (MathAbs(LCda_48[index_16] - LCda_48[index_20]) / (point_56 * GLi_140) <= pivotPipDiff) count_64++;
               if (count_64 > LCi_32) {
                  LCi_32 = count_64;
                  LCd_ret_24 = LCda_48[index_16];
                  LCi_40 = index_16;
               }
            }
            if (ARi_8 < 0 && LCda_48[index_16] > 0.0 && LCda_48[index_16] <= ARd_0) {
               if (MathAbs(LCda_48[index_16] - LCda_48[index_20]) / (point_56 * GLi_140) <= pivotPipDiff) count_68++;
               if (count_68 > LCi_36) {
                  LCi_36 = count_68;
                  LCd_ret_24 = LCda_48[index_16];
                  LCi_44 = index_16;
               }
            }
         }
      }
   }
   if (ARi_8 > 0) {
      if (LCi_40 >= 0) LCd_ret_24 = LCda_48[LCi_40];
      else LCd_ret_24 = High[iHighest(Symbol(), 0, MODE_HIGH, maxLookBackBars, ARi_12)];
   }
   if (ARi_8 < 0) {
      if (LCi_44 >= 0) LCd_ret_24 = LCda_48[LCi_44];
      else LCd_ret_24 = Low[iLowest(Symbol(), 0, MODE_LOW, maxLookBackBars, ARi_12)];
   }
   return (LCd_ret_24);
}

// EED6EE06967100CA6A4AC543C5CD83C5
void f0_2(string AR_name_0, double ARd_8, int AR_datetime_16, int AR_datetime_20, int AR_style_24, double AR_color_28, int AR_width_36) {
   if (ObjectFind(AR_name_0) == -1) ObjectCreate(AR_name_0, OBJ_TREND, 0, 0, 0);
   ObjectSet(AR_name_0, OBJPROP_BACK, FALSE);
   ObjectSet(AR_name_0, OBJPROP_PRICE1, ARd_8);
   ObjectSet(AR_name_0, OBJPROP_PRICE2, ARd_8);
   ObjectSet(AR_name_0, OBJPROP_COLOR, AR_color_28);
   ObjectSet(AR_name_0, OBJPROP_STYLE, AR_style_24);
   ObjectSet(AR_name_0, OBJPROP_WIDTH, AR_width_36);
   ObjectSet(AR_name_0, OBJPROP_TIME1, AR_datetime_16);
   ObjectSet(AR_name_0, OBJPROP_TIME2, AR_datetime_20);
   ObjectSet(AR_name_0, OBJPROP_RAY, FALSE);
}

// F4D97453AAC62369841FFF4CBBADE270
void f0_3(string AR_name_0, string AR_text_8, int AR_datetime_16, double AR_price_20, color AR_color_28) {
   if (ObjectFind(AR_name_0) != 0) ObjectCreate(AR_name_0, OBJ_TEXT, 0, AR_datetime_16, AR_price_20);
   else ObjectMove(AR_name_0, 0, AR_datetime_16, AR_price_20);
   ObjectSetText(AR_name_0, AR_text_8, 8, "Arial", AR_color_28);
}

// D128187CF2E53899B703BEB28921A08E
void f0_1(string AR_name_0, double ARd_8, double ARd_16, int AR_datetime_24, int AR_datetime_28, double AR_color_32, int AR_width_40) {
   if (ObjectFind(AR_name_0) == -1) ObjectCreate(AR_name_0, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
   ObjectSet(AR_name_0, OBJPROP_TIME1, AR_datetime_24);
   ObjectSet(AR_name_0, OBJPROP_TIME2, AR_datetime_28);
   ObjectSet(AR_name_0, OBJPROP_COLOR, AR_color_32);
   ObjectSet(AR_name_0, OBJPROP_PRICE1, ARd_8);
   ObjectSet(AR_name_0, OBJPROP_PRICE2, ARd_16);
   ObjectSet(AR_name_0, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(AR_name_0, OBJPROP_WIDTH, AR_width_40);
   ObjectSet(AR_name_0, OBJPROP_BACK, TRUE);
}