/*
   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_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 5.5
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Red
#property indicator_color6 Blue
#property indicator_color7 Red
#property indicator_color8 Blue

//#include <FDB_File.mqh>

extern double TF2_factor = 1.2;
extern double TF3_factor = 1.4;
extern double TF4_factor = 1.6;
extern int BarWidth = 2;
extern color BarColorUp = Blue;
extern color BarColorDown = Red;
extern color TextColor = SkyBlue;
extern double Gap = 1.0;
int gi_124 = 14;
int gi_128 = 3;
int gi_132 = 5;
int gi_136 = 0;
int gi_140 = 0;
int gi_144 = 110;
int gi_148 = 4242;
int g_str2dbl_152 = EMPTY_VALUE;
int g_str2dbl_156 = EMPTY_VALUE;
int gi_160;
int gi_164;
int gi_168;
int gi_172;
int gi_176 = 0;
bool gi_180;
string gs_184 = "";
double g_icustom_192;
double g_icustom_200;
double g_ibuf_208[];
double g_ibuf_212[];
double g_ibuf_216[];
double g_ibuf_220[];
double g_ibuf_224[];
double g_ibuf_228[];
double g_ibuf_232[];
double g_ibuf_236[];

int init() {
   SetIndexBuffer(0, g_ibuf_208);
   SetIndexBuffer(1, g_ibuf_212);
   SetIndexBuffer(2, g_ibuf_216);
   SetIndexBuffer(3, g_ibuf_220);
   SetIndexBuffer(4, g_ibuf_224);
   SetIndexBuffer(5, g_ibuf_228);
   SetIndexBuffer(6, g_ibuf_232);
   SetIndexBuffer(7, g_ibuf_236);
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexStyle(6, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexStyle(7, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexArrow(0, gi_144);
   SetIndexArrow(1, gi_144);
   SetIndexArrow(2, gi_144);
   SetIndexArrow(3, gi_144);
   SetIndexArrow(4, gi_144);
   SetIndexArrow(5, gi_144);
   SetIndexArrow(6, gi_144);
   SetIndexArrow(7, gi_144);
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
   SetIndexEmptyValue(2, 0.0);
   SetIndexEmptyValue(3, 0.0);
   SetIndexEmptyValue(4, 0.0);
   SetIndexEmptyValue(5, 0.0);
   SetIndexEmptyValue(6, 0.0);
   SetIndexEmptyValue(7, 0.0);
   getPeriod();
   SetIndexLabel(0, "FDB_FXT_Bars(" + gi_172 + ")");
   SetIndexLabel(1, "FDB_FXT_Bars(" + gi_172 + ")");
   SetIndexLabel(2, "FDB_FXT_Bars(" + gi_168 + ")");
   SetIndexLabel(3, "FDB_FXT_Bars(" + gi_168 + ")");
   SetIndexLabel(4, "FDB_FXT_Bars(" + gi_164 + ")");
   SetIndexLabel(5, "FDB_FXT_Bars(" + gi_164 + ")");
   SetIndexLabel(6, "FDB_FXT_Bars(" + gi_160 + ")");
   SetIndexLabel(7, "FDB_FXT_Bars(" + gi_160 + ")");
   IndicatorDigits(0);
   gs_184 = "FDB_FXT_Bars";
   IndicatorShortName(gs_184);
//   if (FDB_Open("FDB.rkey")) {
//      if (FDB_GetStatus(1) == 1) g_str2dbl_156 = StrToDouble(FDB_GetValue(1));
//      if (FDB_GetStatus(2) == 1) g_str2dbl_152 = StrToDouble(FDB_GetValue(2));
//      Print("Run_Key loaded from file.");
//   } else Print("Run_Key file not found.");
   gi_180 = TRUE;
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   string l_text_0;
   double l_price_8;
   string l_name_16;
   int li_24;
   int li_36;
   if (!IsConnected()) {
      if (gi_176 > 1) Print("No login Connection.");
      gi_176++;
   }
//   if (CheckKey() == 0 && AccountNumber() != 0) {
      for (int li_28 = 0; li_28 < 6; li_28++) {
//         if (CheckKey() == 1) break;
//         if (li_28 == 5 && CheckKey() == 0) {
//            Print("Registration Key Failed for Account#: " + AccountNumber());
//            deinit();
//            return (0);
//         }
//      }
   }
   int l_ind_counted_40 = IndicatorCounted();
   if (l_ind_counted_40 < 0) return (-1);
   int li_44 = Bars - l_ind_counted_40 - 1;
   double ld_48 = Time[0] - Time[1];
   for (li_28 = ObjectsTotal() - 1; li_28 > -1; li_28--)
      if (StringFind(ObjectName(li_28), "FDB_" + gi_148 + "_") >= 0) ObjectDelete(ObjectName(li_28));
   double ld_56 = 0.2;
   for (li_28 = 1; li_28 <= 4; li_28++) {
      l_text_0 = "??";
      switch (li_28) {
      case 1:
         l_text_0 = tf2txt(gi_172);
         l_price_8 = 3.0 * Gap + 1.0 + ld_56;
         break;
      case 2:
         l_text_0 = tf2txt(gi_168);
         l_price_8 = 2.0 * Gap + 1.0 + ld_56;
         break;
      case 3:
         l_text_0 = tf2txt(gi_164);
         l_price_8 = Gap + 1.0 + ld_56;
         break;
      case 4:
         l_text_0 = tf2txt(gi_160);
         l_price_8 = ld_56 + 1.5;
      }
      l_name_16 = "FDB_" + gi_148 + "_" + li_28 + "_" + l_text_0;
      ObjectCreate(l_name_16, OBJ_TEXT, WindowFind(gs_184), iTime(NULL, 0, 0) + 3.0 * ld_48, l_price_8);
      ObjectSetText(l_name_16, l_text_0, 8, "Arial", TextColor);
   }
   if (TF4_factor < 1.0) TF4_factor = 1;
   if (TF3_factor < 1.0) TF3_factor = 1;
   if (TF2_factor < 1.0) TF2_factor = 1;
   if (WindowFirstVisibleBar() + gi_124 > li_44) li_44 = WindowFirstVisibleBar() + gi_124;
   for (li_28 = li_44; li_28 >= 0; li_28--) {
      for (int li_32 = 1; li_32 <= 4; li_32++) {
         switch (li_32) {
         case 1:
            li_24 = gi_172;
            li_36 = MathRound(gi_124 / TF4_factor);
            break;
         case 2:
            li_24 = gi_168;
            li_36 = MathRound(gi_124 / TF3_factor);
            break;
         case 3:
            li_24 = gi_164;
            li_36 = MathRound(gi_124 / TF2_factor);
            break;
         case 4:
            li_24 = gi_160;
            li_36 = gi_124;
         }
         g_icustom_200 = iCustom(NULL, 0, "FDB_FXT", li_24, li_36, gi_128, gi_132, gi_136, gi_140, 0, li_28 + 1);
         g_icustom_192 = iCustom(NULL, 0, "FDB_FXT", li_24, li_36, gi_128, gi_132, gi_136, gi_140, 1, li_28 + 1);
         switch (li_32) {
         case 4:
            g_ibuf_236[li_28] = EMPTY_VALUE;
            g_ibuf_232[li_28] = EMPTY_VALUE;
            if ((g_icustom_192 >= g_icustom_200 && g_icustom_200 < 80.0) || g_icustom_200 <= 20.0) g_ibuf_236[li_28] = 1;
            else
               if ((g_icustom_192 < g_icustom_200 && g_icustom_200 > 20.0) || g_icustom_200 >= 80.0) g_ibuf_232[li_28] = 1;
            break;
         case 3:
            g_ibuf_228[li_28] = EMPTY_VALUE;
            g_ibuf_224[li_28] = EMPTY_VALUE;
            if ((g_icustom_192 >= g_icustom_200 && g_icustom_200 < 80.0) || g_icustom_200 <= 20.0) g_ibuf_228[li_28] = 1.0 * Gap + 1.0;
            else
               if ((g_icustom_192 < g_icustom_200 && g_icustom_200 > 20.0) || g_icustom_200 >= 80.0) g_ibuf_224[li_28] = 1.0 * Gap + 1.0;
            break;
         case 2:
            g_ibuf_220[li_28] = EMPTY_VALUE;
            g_ibuf_216[li_28] = EMPTY_VALUE;
            if ((g_icustom_192 >= g_icustom_200 && g_icustom_200 < 80.0) || g_icustom_200 <= 20.0) g_ibuf_220[li_28] = 2.0 * Gap + 1.0;
            else
               if ((g_icustom_192 < g_icustom_200 && g_icustom_200 > 20.0) || g_icustom_200 >= 80.0) g_ibuf_216[li_28] = 2.0 * Gap + 1.0;
            break;
         case 1:
            g_ibuf_212[li_28] = EMPTY_VALUE;
            g_ibuf_208[li_28] = EMPTY_VALUE;
            if ((g_icustom_192 >= g_icustom_200 && g_icustom_200 < 80.0) || g_icustom_200 <= 20.0) g_ibuf_212[li_28] = 3.0 * Gap + 1.0;
            else
               if ((g_icustom_192 < g_icustom_200 && g_icustom_200 > 20.0) || g_icustom_200 >= 80.0) g_ibuf_208[li_28] = 3.0 * Gap + 1.0;
         }
      }
   }
   watermark();
   return (0);
}

void getPeriod() {
   switch (Period()) {
   case PERIOD_M1:
      gi_160 = 1;
      gi_164 = 5;
      gi_168 = 15;
      gi_172 = 30;
      return;
   case PERIOD_M5:
      gi_160 = 5;
      gi_164 = 15;
      gi_168 = 30;
      gi_172 = 60;
      return;
   case PERIOD_M15:
      gi_160 = 15;
      gi_164 = 30;
      gi_168 = 60;
      gi_172 = 240;
      return;
   case PERIOD_M30:
      gi_160 = 30;
      gi_164 = 60;
      gi_168 = 240;
      gi_172 = 1440;
      return;
   case PERIOD_H1:
      gi_160 = 60;
      gi_164 = 240;
      gi_168 = 1440;
      gi_172 = 10080;
      return;
   case PERIOD_H4:
      gi_160 = 240;
      gi_164 = 1440;
      gi_168 = 10080;
      gi_172 = 43200;
      return;
   case PERIOD_D1:
      gi_160 = 1440;
      gi_164 = 10080;
      gi_168 = 43200;
      gi_172 = 43200;
      return;
   case PERIOD_W1:
      gi_160 = 10080;
      gi_164 = 43200;
      gi_168 = 43200;
      gi_172 = 43200;
      return;
   case PERIOD_MN1:
      gi_160 = 43200;
      gi_164 = 43200;
      gi_168 = 43200;
      gi_172 = 43200;
      return;
      return;
   }
}

string tf2txt(int ai_0) {
   if (ai_0 == 1) return ("M1");
   if (ai_0 == 5) return ("M5");
   if (ai_0 == 15) return ("M15");
   if (ai_0 == 30) return ("M30");
   if (ai_0 == 60) return ("H1");
   if (ai_0 == 240) return ("H4");
   if (ai_0 == 1440) return ("D1");
   if (ai_0 == 10080) return ("W1");
   if (ai_0 == 43200) return ("MN");
   return ("??");
}

/*int CheckKey() {
   double ld_0;
   double ld_8;
   double ld_16;
   int l_month_24 = TimeMonth(TimeCurrent());
   if (IsDemo() == TRUE) {
      ld_0 = NormalizeDouble(MathAbs(AccountNumber() << 2 - 3912671 * l_month_24), 0);
      ld_16 = NormalizeDouble(MathAbs(g_str2dbl_152), 0);
   } else {
      ld_0 = NormalizeDouble(MathAbs(AccountNumber() << 2 + 10110110 - 5382974 * l_month_24), 0);
      ld_16 = NormalizeDouble(MathAbs(g_str2dbl_156), 0);
   }
   if (TimeMonth(TimeCurrent() - 259200) == l_month_24 - 1 && ld_16 > 0.0) {
      if (IsDemo() == TRUE) ld_8 = NormalizeDouble(MathAbs(AccountNumber() << 2 - 3912671 * (l_month_24 - 1)), 0);
      else ld_8 = NormalizeDouble(MathAbs(AccountNumber() << 2 + 10110110 - 5382974 * (l_month_24 - 1)), 0);
   }
   if (gi_180 == TRUE && ld_16 == ld_8) {
      Print("Registration Key expires on the 3rd at Midnite.  Login and generate a new key.");
      gi_180 = FALSE;
   }
   if (ld_16 == ld_0 || ld_16 == ld_8 && ld_16 > 0.0) return (1);
   return (0);
} */

void watermark() {
   ObjectCreate(gs_184, OBJ_LABEL, WindowFind(gs_184), 0, 0);
   ObjectSetText(gs_184, "Forex Deal Butler", 9, "Lucida Handwriting", RoyalBlue);
   ObjectSet(gs_184, OBJPROP_CORNER, 3);
   ObjectSet(gs_184, OBJPROP_XDISTANCE, 5);
   ObjectSet(gs_184, OBJPROP_YDISTANCE, 10);
}