/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2008,FXDB, LLC"
#property link      "http://www.ForexDealButler.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Black
#property indicator_color2 Black

extern int Lines_reversal = 3;
extern bool ignore_inside_bars = TRUE;
double gd_84;
double gd_92;
bool gi_100 = TRUE;
int gi_104;
datetime g_time_108;
int gi_112;
double g_pips_116;
int gi_124;
double g_ibuf_128[];
double g_ibuf_132[];

int init() {
   SetIndexBuffer(0, g_ibuf_128);
   SetIndexBuffer(1, g_ibuf_132);
   SetIndexStyle(0, DRAW_NONE);
   SetIndexStyle(1, DRAW_NONE);
   SetIndexEmptyValue(0, 0);
   SetIndexEmptyValue(1, 0);
   IndicatorShortName("3BarReversal_v2");
   SetIndexLabel(0, "3BarReversal_v2");
   SetIndexDrawBegin(0, Lines_reversal);
   SetIndexDrawBegin(1, Lines_reversal);
   return (0);
}

int start() {
   int l_count_0;
   int l_count_4;
   double ld_8;
   int li_16 = IndicatorCounted();
   if (li_16 < 0) return (-1);
   if (li_16 > 0) li_16--;
   int li_20 = Bars - 1 - Lines_reversal;
   int li_24 = Bars - 1 - li_16;
   if (li_24 > li_20) {
      for (int li_28 = li_24; li_28 > li_20; li_28--) {
         g_ibuf_128[li_28] = 0.0;
         g_ibuf_132[li_28] = 0.0;
      }
      li_24 = li_20;
   }
   datetime l_time_32 = Time[li_24 + 1];
   if (li_24 < li_20) {
      if (l_time_32 == g_time_108) gi_100 = gi_112;
      else
         if (l_time_32 > g_time_108) return (-1);
   }
   if (newbar(Time[0])) g_pips_116 = NormalizeDouble(MarketInfo(Symbol(), MODE_SPREAD), Digits);
   for (li_28 = li_24; li_28 >= 0; li_28--) {
      if (li_28 == 1) {
         if ((li_24 == 1 && g_time_108 != Time[2]) || li_24 > 1) {
            g_time_108 = Time[2];
            gi_112 = gi_100;
         }
      }
      l_count_0 = 0;
      l_count_4 = 0;
      if (ignore_inside_bars == TRUE) {
         for (int li_36 = 1; li_36 < Lines_reversal * 2; li_36++) {
            if (High[li_28 + li_36] > High[li_28 + li_36 + 1] || Low[li_28 + li_36] < Low[li_28 + li_36 + 1]) l_count_4++;
            else
               if (High[li_28 + li_36] < High[li_28 + li_36 + 1] || Low[li_28 + li_36] > Low[li_28 + li_36 + 1]) l_count_0++;
            if (l_count_4 == Lines_reversal) break;
         }
      }
      gi_104 = gi_100;
      gd_84 = High[iHighest(NULL, 0, MODE_HIGH, Lines_reversal + l_count_0, li_28 + 1)];
      gd_92 = Low[iLowest(NULL, 0, MODE_LOW, Lines_reversal + l_count_0, li_28 + 1)];
      if (gi_104 && Low[li_28] <= gd_92) gi_100 = FALSE;
      if (!gi_104 && High[li_28] >= gd_84) gi_100 = TRUE;
      ld_8 = NormalizeDouble(MarketInfo(Symbol(), MODE_SPREAD), Digits);
      if (ld_8 > g_pips_116) {
         g_pips_116 = ld_8;
         newbar(Time[0]);
      }
      if (!gi_100) {
         g_ibuf_128[li_28] = gd_84 + g_pips_116 * Point;
         g_ibuf_132[li_28] = 0;
      }
      if (gi_100) {
         g_ibuf_132[li_28] = gd_92;
         g_ibuf_128[li_28] = 0;
      }
   }
   return (0);
}

bool newbar(int ai_0) {
   if (ai_0 != gi_124) {
      gi_124 = ai_0;
      return (TRUE);
   }
   return (FALSE);
}
