/*
   Generated by ex4-to-mq4 decompiler FREEWARE 4.0.451.7
   Website: hTt p :/ / ww W.ME TA quot E s.NeT
   E-mail :  s u P P oR T @me T A q uO t E S .n E t
*/
#property copyright "Attila"
#property link      "pricebartrader@yahoo.com"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Black
#property indicator_color2 Black
#property indicator_color3 Black
#property indicator_color4 Black
#property indicator_color5 Black
#property indicator_color6 Black

extern int barsback = 50;
extern double C_range = 0.3;
extern bool arrows = TRUE;
extern int arrowsize = 1;
extern color bullarrow = Blue;
extern color beararrow = Red;
extern bool paintbar = false;
extern int barthickness = 2;
extern color bullishcol = Blue;
extern color bearishcol = Red;
extern bool alarm = FALSE;
extern string sound = ".wave file here";
extern bool alert = FALSE;
extern bool M5 = False;
extern bool M15 = False;
extern bool M30 = False;
extern bool H1 = False;
extern bool H4 = True;
extern bool D1 = True;
extern bool W1 = False;
//extern int dist = 30;
int dist = 30;
extern int OffsetX = 20;
extern int OffsetY = 110;
extern bool  DrawBackground = True;
extern color BackgroundColor = DarkGray;
extern color LabelsColor = RoyalBlue;
extern bool  LeftCaption = true;

double G_ibuf_164[];
double G_ibuf_168[];
double G_ibuf_172[];
double G_ibuf_176[];
double G_ibuf_180[];
double G_ibuf_184[];
string Gsa_188[7] = {"W1", "D1", "4H", "60M", "30M", "15M", "5M"};
int G_datetime_192 = 0;
int G_datetime_196 = 0;
int G_datetime_200 = 0;
int G_datetime_204 = 0;
int G_datetime_208 = 0;
int G_datetime_212 = 0;
int G_datetime_216 = 0;

// 30B3B5D50BCEFE00DB45435011417B86
int f0_0(int A_timeframe_0, double Ad_unused_4) {
   int Li_ret_100;
   double ihigh_12 = iHigh(NULL, A_timeframe_0, 1);
   double ilow_20 = iLow(NULL, A_timeframe_0, 1);
   double iclose_28 = iClose(NULL, A_timeframe_0, 1);
   double iopen_36 = iOpen(NULL, A_timeframe_0, 1);
   double ihigh_44 = iHigh(NULL, A_timeframe_0, 2);
   double ilow_52 = iLow(NULL, A_timeframe_0, 2);
   double iclose_60 = iClose(NULL, A_timeframe_0, 2);
   double iopen_68 = iOpen(NULL, A_timeframe_0, 2);
   double Ld_76 = ihigh_12 - ilow_20;
   double Ld_84 = ilow_20 + Ld_76 * C_range;
   double Ld_92 = ihigh_12 - Ld_76 * C_range;
   if (ihigh_12 > ihigh_44 && ilow_20 < ilow_52 && iclose_28 > ihigh_44) Li_ret_100 = 65280;
   if (ihigh_12 > ihigh_44 && ilow_20 < ilow_52 && iclose_28 < ilow_52) Li_ret_100 = 255;
   if (ihigh_12 > ihigh_44 && ilow_20 < ilow_52 && iclose_28 > Ld_92 && iopen_36 > Ld_92) Li_ret_100 = 65280;
   if (ihigh_12 > ihigh_44 && ilow_20 < ilow_52 && iclose_28 < Ld_84 && iopen_36 < Ld_84) Li_ret_100 = 255;
   else Li_ret_100 = 5197615;
   return (Li_ret_100);
}

// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
   if (paintbar == TRUE) {
      SetIndexBuffer(0, G_ibuf_164);
      SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, barthickness, bullishcol);
      SetIndexEmptyValue(0, 0.0);
      SetIndexBuffer(1, G_ibuf_168);
      SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, barthickness, bullishcol);
      SetIndexEmptyValue(1, 0.0);
      SetIndexBuffer(2, G_ibuf_172);
      SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, barthickness, bearishcol);
      SetIndexEmptyValue(2, 0.0);
      SetIndexBuffer(3, G_ibuf_176);
      SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_SOLID, barthickness, bearishcol);
      SetIndexEmptyValue(3, 0.0);
   }
   if (arrows == TRUE) {
      SetIndexBuffer(4, G_ibuf_180);
      SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, arrowsize, bullarrow);
      SetIndexArrow(4, SYMBOL_ARROWUP);
      SetIndexEmptyValue(4, 0.0);
      SetIndexBuffer(5, G_ibuf_184);
      SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, arrowsize, beararrow);
      SetIndexArrow(5, SYMBOL_ARROWDOWN);
      SetIndexEmptyValue(5, 0.0);
   }
   ObjectCreate("oub_caption", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("oub_caption", OBJPROP_CORNER, 0);
   ObjectSet("oub_caption", OBJPROP_XDISTANCE, OffsetX);
   ObjectSet("oub_caption", OBJPROP_YDISTANCE, OffsetY);
   ObjectSetText("oub_caption", "Outside Bar", 10, "Arial", LabelsColor);
//   for (int Li_0 = 6; Li_0 >= 0; Li_0--) {
//      ObjectCreate("otf1" + Li_0, OBJ_LABEL, 0, 0, 0, 0, 0);
//      ObjectSet("otf1" + Li_0, OBJPROP_CORNER, 0);
//      ObjectSet("otf1" + Li_0, OBJPROP_XDISTANCE, oX);
//      ObjectSet("otf1" + Li_0, OBJPROP_YDISTANCE, oY + 20);
//      ObjectSetText("otf1" + Li_0, Gsa_188[Li_0], 10, "Arial", LabelsColor);
//      ObjectCreate("osig" + Li_0, OBJ_LABEL, 0, 0, 0, 0, 0);
//      ObjectSet("osig" + Li_0, OBJPROP_CORNER, 0);
//      ObjectSet("osig" + Li_0, OBJPROP_XDISTANCE, oX);
//      ObjectSet("osig" + Li_0, OBJPROP_YDISTANCE, oY + 30);
//      oX += dist;
//   }
   DrawCaptionBG();
   CreateSignalLabels();
   return (0);
}

void CreateSignalLabels() {
   if (LeftCaption) OffsetX = OffsetX+96;
      else OffsetY = OffsetY + 18;
   if (M5) CreateSignalLabels_One("M5");
   if (M15) CreateSignalLabels_One("M15");
   if (M30) CreateSignalLabels_One("M30");
   if (H1) CreateSignalLabels_One("H1");
   if (H4) CreateSignalLabels_One("H4");
   if (D1) CreateSignalLabels_One("D1");
   if (W1) CreateSignalLabels_One("W1");
}

void CreateSignalLabels_One(string LabelName) {
   string _LabelName = "oub_tf_" + LabelName;
   ObjectCreate(_LabelName, OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet(_LabelName, OBJPROP_CORNER, 0);
   ObjectSet(_LabelName, OBJPROP_XDISTANCE, OffsetX);
   ObjectSet(_LabelName, OBJPROP_YDISTANCE, OffsetY);
   ObjectSetText(_LabelName, LabelName, 10, "Arial", LabelsColor);
   _LabelName = "oub_sig_" + LabelName;
   ObjectCreate(_LabelName, OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet(_LabelName, OBJPROP_CORNER, 0);
   ObjectSet(_LabelName, OBJPROP_XDISTANCE, OffsetX);
   ObjectSet(_LabelName, OBJPROP_YDISTANCE, OffsetY + 14);
   OffsetX += dist;
}

void DrawCaptionBG()
{
   if (!DrawBackground) return(0);
   
   int CountX = 0;
   int _YDISTANCE = 0;
   if (LeftCaption) {CountX = 3;}
   if (M5)  CountX=CountX +1;
   if (M15) CountX=CountX +1;
   if (M30) CountX=CountX +1;
   if (H1)  CountX=CountX +1;
   if (H4)  CountX=CountX +1;
   if (D1)  CountX=CountX +1;
   if (W1)  CountX=CountX +1;
   int CountY;
   if (DrawBackground) CountY = 2; else CountY = 3;
   if ((!LeftCaption) && (CountX<3)) CountX=3;
   
   int ix, iy;
   string _LabelName;
   for(ix=1; ix<=CountX; ix++)
   {
      for(iy=1; iy<=CountY; iy++)
      {
         _LabelName = "oub_bg_" + DoubleToStr(ix,0) + DoubleToStr(iy,0);
         ObjectCreate(_LabelName, OBJ_LABEL, 0, 0, 0, 0, 0);
         ObjectSet(_LabelName, OBJPROP_CORNER, 0);
         ObjectSet(_LabelName, OBJPROP_XDISTANCE, OffsetX-7+(ix-1)*32);
         _YDISTANCE=OffsetY-7+(iy-1)*33;
         if ((LeftCaption) && (iy>1)) _YDISTANCE=_YDISTANCE-15;
         ObjectSet(_LabelName, OBJPROP_YDISTANCE, _YDISTANCE);
         ObjectSetText(_LabelName, "g", 25, "Webdings", BackgroundColor);
      }
   }
}

// 52D46093050F38C27267BCE42543EF60
int deinit() {
//   for (int count_0 = 0; count_0 <= 7; count_0++) {
//      ObjectDelete("otf1" + count_0);
//      ObjectDelete("osig" + count_0);
//   }
   ObjectDelete("oub_caption");
   int i;
   int ot=ObjectsTotal();
   string _n;
   for(i=0; i<ot; i++)
   {
      _n=ObjectName(i);
      if ((StringFind(_n,"oub_tf")>=0) || (StringFind(_n,"oub_sig")>=0) || (StringFind(_n,"oub_bg")>=0)) 
         {
            ObjectDelete(_n);
            i--;
         }
   }
//   Comment("");
   return (0);
}

// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   double pips_12;
   double Ld_24;
   double Ld_32;
   double Ld_40;
   color color_48;
   color color_52;
   color color_56;
   color color_60;
   int Li_0 = IndicatorCounted();
   if (Li_0 < 0) return (0);
   if (Li_0 > 1) Li_0--;
   int Li_4 = Bars - Li_0 - 2;
   switch (Period()) {
   case PERIOD_M5:
      pips_12 = 50;
      break;
   case PERIOD_M15:
      pips_12 = 70;
      break;
   case PERIOD_M30:
      pips_12 = 80;
      break;
   case PERIOD_H1:
      pips_12 = 100;
      break;
   case PERIOD_H4:
      pips_12 = 150;
      break;
   case PERIOD_D1:
      pips_12 = 500;
      break;
   case PERIOD_W1:
      pips_12 = 1500;
   }
   for (int Li_20 = barsback; Li_20 >= 1; Li_20--) {
      Ld_24 = High[Li_20] - Low[Li_20];
      Ld_32 = Low[Li_20] + Ld_24 * C_range;
      Ld_40 = High[Li_20] - Ld_24 * C_range;
      if (High[Li_20] > High[Li_20 + 1] && Low[Li_20] < Low[Li_20 + 1] && Close[Li_20] > High[Li_20 + 1]) {
         G_ibuf_164[Li_20] = Low[Li_20];
         G_ibuf_168[Li_20] = High[Li_20];
         G_ibuf_180[Li_20] = Low[Li_20] - pips_12 * Point;
      }
      if (High[Li_20] > High[Li_20 + 1] && Low[Li_20] < Low[Li_20 + 1] && Close[Li_20] < Low[Li_20 + 1]) {
         G_ibuf_172[Li_20] = Low[Li_20];
         G_ibuf_176[Li_20] = High[Li_20];
         G_ibuf_184[Li_20] = High[Li_20] + pips_12 * Point;
      }
      if (High[Li_20] > High[Li_20 + 1] && Low[Li_20] < Low[Li_20 + 1] && Close[Li_20] > Ld_40 && Open[Li_20] > Ld_40) {
         G_ibuf_164[Li_20] = Low[Li_20];
         G_ibuf_168[Li_20] = High[Li_20];
         G_ibuf_180[Li_20] = Low[Li_20] - pips_12 * Point;
      }
      if (High[Li_20] > High[Li_20 + 1] && Low[Li_20] < Low[Li_20 + 1] && Close[Li_20] < Ld_32 && Open[Li_20] < Ld_32) {
         G_ibuf_172[Li_20] = Low[Li_20];
         G_ibuf_176[Li_20] = High[Li_20];
         G_ibuf_184[Li_20] = High[Li_20] + pips_12 * Point;
      }
   }
   if (M5 == TRUE) {
      color_48 = f0_0(PERIOD_M5, C_range);
      if (iTime(NULL, PERIOD_M5, 0) != G_datetime_192 && color_48 != DarkSlateGray) {
         G_datetime_192 = iTime(NULL, PERIOD_M5, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_M5", CharToStr(110), 18, "Wingdings", color_48);
   }
   if (M15 == TRUE) {
      color_52 = f0_0(PERIOD_M15, C_range);
      if (iTime(NULL, PERIOD_M15, 0) != G_datetime_196 && color_52 != DarkSlateGray) {
         G_datetime_196 = iTime(NULL, PERIOD_M15, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_M15", CharToStr(110), 18, "Wingdings", color_52);
   }
   if (M30 == TRUE) {
      color_56 = f0_0(PERIOD_M30, C_range);
      if (iTime(NULL, PERIOD_M30, 0) != G_datetime_200 && color_56 != DarkSlateGray) {
         G_datetime_200 = iTime(NULL, PERIOD_M30, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_M30", CharToStr(110), 18, "Wingdings", color_56);
   }
   if (H1 == TRUE) {
      color_60 = f0_0(PERIOD_H1, C_range);
      if (iTime(NULL, PERIOD_H1, 0) != G_datetime_204 && color_60 != DarkSlateGray) {
         G_datetime_204 = iTime(NULL, PERIOD_H1, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_H1", CharToStr(110), 18, "Wingdings", color_60);
   }
   if (H4 == TRUE) {
      color color_64 = f0_0(PERIOD_H4, C_range);
      if (iTime(NULL, PERIOD_H4, 0) != G_datetime_208 && color_64 != DarkSlateGray) {
         G_datetime_208 = iTime(NULL, PERIOD_H4, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_H4", CharToStr(110), 18, "Wingdings", color_64);
   }
   if (D1 == TRUE) {
      color color_68 = f0_0(PERIOD_D1, C_range);
      if (iTime(NULL, PERIOD_D1, 0) != G_datetime_212 && color_68 != DarkSlateGray) {
         G_datetime_212 = iTime(NULL, PERIOD_D1, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_D1", CharToStr(110), 18, "Wingdings", color_68);
   }
   if (W1 == TRUE) {
      color color_72 = f0_0(PERIOD_W1, C_range);
      if (iTime(NULL, PERIOD_W1, 0) != G_datetime_216 && color_72 != DarkSlateGray) {
         G_datetime_216 = iTime(NULL, PERIOD_W1, 0);
         DoAlert();
      }
      ObjectSetText("oub_sig_W1", CharToStr(110), 18, "Wingdings", color_72);
   }
   //WindowRedraw();
   return (0);
}

void DoAlert() {
   if (alert == TRUE) Alert("Outside bar on ", Symbol(), GetTFname(Period()));
   if (alarm == TRUE) PlaySound(sound);
}

string GetTFname (int aPeriod)
{
  string _sResult;
  switch(aPeriod)
     {
      case 1  : _sResult="Ì1";   break;
      case 5  : _sResult="Ì5";   break;
      case 15 : _sResult="Ì15";  break;
      case 30 : _sResult="Ì30";  break;
      case 60 : _sResult="H1";   break;
      case 240 : _sResult="H4";  break;
      case 1440 : _sResult="D1"; break;
     }
  return (_sResult);
}