#property copyright "Tudor Ceban"
#property link      "www.scientificforex.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

double g_ibuf_76[];
double g_ibuf_80[];
extern int FractalsCount = 9;
extern int ots = 5;
int gi_88;
double g_point_92;


int init() {
   if (Point == 0.00001) g_point_92 = 0.0001;
   else {
      if (Point == 0.001) g_point_92 = 0.01;
      else g_point_92 = Point;
   }
   SetIndexBuffer(0, g_ibuf_76);
   SetIndexArrow(0, SYMBOL_ARROWDOWN);
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, 1);
   SetIndexBuffer(1, g_ibuf_80);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, 1);
   SetIndexArrow(1, SYMBOL_ARROWUP);
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
   gi_88 = MathRound(FractalsCount + 1) / 2.0;
   return (0);
}

int start() {
   int l_ind_counted_0 = IndicatorCounted();
   int li_4 = Bars - l_ind_counted_0 - 1;
   if (li_4 < FractalsCount) li_4 = FractalsCount;
   while (li_4 > 0) {
      if (is_fractal(li_4, gi_88) == 1) g_ibuf_76[li_4] = High[li_4] + ots * g_point_92;
      else g_ibuf_76[li_4] = 0;
      if (is_fractal(li_4, gi_88) == 2) g_ibuf_80[li_4] = Low[li_4] - ots * g_point_92;
      else g_ibuf_80[li_4] = 0;
      li_4--;
   }
   return (0);
}

int deinit() {
   return (0);
}

int is_fractal(int ai_0, int ai_4) {
   int li_8 = 1;
   int l_count_12 = 0;
   int l_count_16 = 0;
   while (li_8 < ai_4) {
      if (High[ai_0] > High[ai_0 + li_8]) {
         if (ai_0 - li_8 >= 0) {
            if (High[ai_0] > High[ai_0 - li_8]) l_count_12++;
         } else l_count_12++;
      }
      if (Low[ai_0] < Low[ai_0 + li_8]) {
         if (ai_0 - li_8 >= 0) {
            if (Low[ai_0] < Low[ai_0 - li_8]) l_count_16++;
         } else l_count_16++;
      }
      li_8++;
   }
   if (l_count_12 == ai_4 - 1) return (1);
   if (l_count_16 == ai_4 - 1) return (2);
   return (-1);
}