/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2006, Eli hayun"
#property link      "http://www.elihayun.com"

#property indicator_separate_window
#property indicator_minimum 1.0
#property indicator_maximum 4.0
#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

extern int BarWidth = 0;
extern color BarColorUp = Lime;
extern color BarColorDown = Red;
extern color TextColor = Orange;
extern int P1_position = 4;
extern int P2_position = 3;
extern int P3_position = 2;
extern int P4_position = 1;
extern bool showlabels = TRUE;
extern string note1 = "when true picks next 2 higher tfs";
extern string note2 = "when false select your own tfs";
extern string note3 = "Period types: 0 = slow, 1 = fast";
extern bool autotimeframe = FALSE;
extern string note4 = "when true picks 2 additional  higher tfs";
extern bool EnableAlerts = TRUE;
extern bool SoundAlertOnOptimum = FALSE;
extern string OptimumSound = "email.wav";
extern bool useSecondAutoTF = FALSE;
extern int ePeriod1 = 1;
extern int ePeriod1Type = 1;
extern int ePeriod2 = 1;
extern int ePeriod2Type = 0;
extern int ePeriod3 = 5;
extern int ePeriod3Type = 1;
extern int ePeriod4 = 5;
extern int ePeriod4Type = 0;
extern string note5 = "Gap between the lines of bars";
extern double Gap = 0.6;
extern string note6 = "Label verticle shift";
extern double v_shift = 0.5;
extern string note7 = "Label horizontal shift";
extern int h_shift = 15;
extern bool showcomment = FALSE;
double gda_248[];
double gda_252[];
double gda_256[];
double gda_260[];
double gda_264[];
double gda_268[];
double gda_272[];
double gda_276[];
string gs_280 = "";
double gd_288;
double gd_296;
int gi_308 = 110;
int gi_312 = 22834;
int gi_316;
int gi_320;
int gi_324;
int gi_328;
int gi_332;
string gs_336;
bool gi_344 = FALSE;
bool gi_348 = FALSE;
bool gi_352 = FALSE;
bool gi_356 = FALSE;
int gi_360 = 0;

int init() {
   gi_332 = TimeLocal();
   gs_336 = TimeToStr(gi_332, TIME_SECONDS);
   Comment(gi_332, " ", gs_336);
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexArrow(0, gi_308);
   SetIndexBuffer(0, gda_272);
   SetIndexEmptyValue(0, 0.0);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexArrow(1, gi_308);
   SetIndexBuffer(1, gda_276);
   SetIndexEmptyValue(1, 0.0);
   SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexArrow(2, gi_308);
   SetIndexBuffer(2, gda_264);
   SetIndexEmptyValue(2, 0.0);
   SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexArrow(3, gi_308);
   SetIndexBuffer(3, gda_268);
   SetIndexEmptyValue(3, 0.0);
   SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexArrow(4, gi_308);
   SetIndexBuffer(4, gda_256);
   SetIndexEmptyValue(4, 0.0);
   SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexArrow(5, gi_308);
   SetIndexBuffer(5, gda_260);
   SetIndexEmptyValue(5, 0.0);
   SetIndexStyle(6, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorUp);
   SetIndexArrow(6, gi_308);
   SetIndexBuffer(6, gda_248);
   SetIndexEmptyValue(6, 0.0);
   SetIndexStyle(7, DRAW_ARROW, STYLE_SOLID, BarWidth, BarColorDown);
   SetIndexArrow(7, gi_308);
   SetIndexBuffer(7, gda_252);
   SetIndexEmptyValue(7, 0.0);
   SetIndexLabel(0, NULL);
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL);
   SetIndexLabel(3, NULL);
   SetIndexLabel(4, NULL);
   SetIndexLabel(5, NULL);
   SetIndexLabel(6, NULL);
   SetIndexLabel(7, NULL);
   IndicatorDigits(0);
   getPeriod();
   gs_280 = gs_336 + "THV MTF Trix Histo V3.1.5.3 (" + gi_316 + "," + gi_320 + "," + gi_324 + "," + gi_328 + ")";
   IndicatorShortName(gs_280);
   return (0);
}

int deinit() {
   Comment("");
   return (0);
}

int start() {
   int lia_28[];
   int lia_32[];
   int lia_36[];
   int lia_40[];
   int li_60;
   int li_64;
   int li_0 = IndicatorCounted();
   if (li_0 < 0) return (-1);
   int li_4 = Bars - li_0;
   int li_16 = 0;
   double ld_20 = Time[0] - Time[1];
   cleanLabel();
   placeLabels();
   li_16 = checkAlarm(gda_272[1], gda_276[1], gda_264[1], gda_268[1], gda_256[1], gda_260[1], gda_248[1], gda_252[1]);
   if (li_16 == 1) {
      if (EnableAlerts == TRUE) {
         if (SoundAlertOnOptimum == TRUE) PlaySound(OptimumSound);
         else DisplayAlert("Optimum trading conditions on: ");
      }
   }
   ArrayCopySeries(lia_40, 5, Symbol(), gi_316);
   ArrayCopySeries(lia_36, 5, Symbol(), gi_320);
   ArrayCopySeries(lia_32, 5, Symbol(), gi_324);
   ArrayCopySeries(lia_28, 5, Symbol(), gi_328);
   int li_44 = 0;
   int li_48 = 0;
   int li_52 = 0;
   int li_56 = 0;
   int li_8 = 0;
   li_44 = 0;
   li_48 = 0;
   li_52 = 0;
   li_56 = 0;
   while (li_8 < li_4) {
      if (Time[li_8] < lia_40[li_44]) li_44++;
      if (Time[li_8] < lia_36[li_48]) li_48++;
      if (Time[li_8] < lia_32[li_52]) li_52++;
      if (Time[li_8] < lia_28[li_56]) li_56++;
      for (int li_12 = 1; li_12 <= 4; li_12++) {
         switch (li_12) {
         case 1:
            li_64 = gi_316;
            li_60 = li_44;
            break;
         case 2:
            li_64 = gi_320;
            li_60 = li_48;
            break;
         case 3:
            li_64 = gi_324;
            li_60 = li_52;
            break;
         case 4:
            li_64 = gi_328;
            li_60 = li_56;
         }
         gd_288 = iCustom(NULL, li_64, "THV3 Trix for MTFHisto", 2, li_60);
         gd_296 = iCustom(NULL, li_64, "THV3 Trix for MTFHisto", 0, li_60);
         if (autotimeframe == TRUE) {
            switch (li_12) {
            case 1:
               gda_276[li_8] = EMPTY_VALUE;
               gda_272[li_8] = EMPTY_VALUE;
               if (gd_288 == EMPTY_VALUE) gda_276[li_8] = Gap * P1_position + 1.0;
               else gda_272[li_8] = Gap * P1_position + 1.0;
               break;
            case 2:
               gda_268[li_8] = EMPTY_VALUE;
               gda_264[li_8] = EMPTY_VALUE;
               if (gd_296 == EMPTY_VALUE) gda_268[li_8] = Gap * P2_position + 1.0;
               else gda_264[li_8] = Gap * P2_position + 1.0;
               break;
            case 3:
               gda_260[li_8] = EMPTY_VALUE;
               gda_256[li_8] = EMPTY_VALUE;
               if (gd_288 == EMPTY_VALUE) gda_260[li_8] = Gap * P3_position + 1.0;
               else gda_256[li_8] = Gap * P3_position + 1.0;
               break;
            case 4:
               gda_252[li_8] = EMPTY_VALUE;
               gda_248[li_8] = EMPTY_VALUE;
               if (gd_296 == EMPTY_VALUE) gda_252[li_8] = Gap * P4_position + 1.0;
               else gda_248[li_8] = Gap * P4_position + 1.0;
            }
         } else {
            if (autotimeframe == FALSE) {
               switch (li_12) {
               case 1:
                  gda_276[li_8] = EMPTY_VALUE;
                  gda_272[li_8] = EMPTY_VALUE;
                  if (ePeriod1Type == 0) {
                     if (gd_296 == EMPTY_VALUE) gda_276[li_8] = Gap * P1_position + 1.0;
                     else gda_272[li_8] = Gap * P1_position + 1.0;
                  }
                  if (ePeriod1Type == 1) {
                     if (gd_288 == EMPTY_VALUE) gda_276[li_8] = Gap * P1_position + 1.0;
                     else gda_272[li_8] = Gap * P1_position + 1.0;
                  }
                  break;
               case 2:
                  gda_268[li_8] = EMPTY_VALUE;
                  gda_264[li_8] = EMPTY_VALUE;
                  if (ePeriod2Type == 0) {
                     if (gd_296 == EMPTY_VALUE) gda_268[li_8] = Gap * P2_position + 1.0;
                     else gda_264[li_8] = Gap * P2_position + 1.0;
                  }
                  if (ePeriod2Type == 1) {
                     if (gd_288 == EMPTY_VALUE) gda_268[li_8] = Gap * P2_position + 1.0;
                     else gda_264[li_8] = Gap * P2_position + 1.0;
                  }
                  break;
               case 3:
                  gda_260[li_8] = EMPTY_VALUE;
                  gda_256[li_8] = EMPTY_VALUE;
                  if (ePeriod3Type == 0) {
                     if (gd_296 == EMPTY_VALUE) gda_260[li_8] = Gap * P3_position + 1.0;
                     else gda_256[li_8] = Gap * P3_position + 1.0;
                  }
                  if (ePeriod3Type == 1) {
                     if (gd_288 == EMPTY_VALUE) gda_260[li_8] = Gap * P3_position + 1.0;
                     else gda_256[li_8] = Gap * P3_position + 1.0;
                  }
                  break;
               case 4:
                  gda_252[li_8] = EMPTY_VALUE;
                  gda_248[li_8] = EMPTY_VALUE;
                  if (ePeriod4Type == 0) {
                     if (gd_296 == EMPTY_VALUE) gda_252[li_8] = Gap * P4_position + 1.0;
                     else gda_248[li_8] = Gap * P4_position + 1.0;
                  }
                  if (ePeriod4Type == 1) {
                     if (gd_288 == EMPTY_VALUE) gda_252[li_8] = Gap * P4_position + 1.0;
                     else gda_248[li_8] = Gap * P4_position + 1.0;
                  }
               }
            }
         }
      }
      li_8++;
   }
   return (0);
}

void getPeriod() {
   if (autotimeframe) {
      if (useSecondAutoTF == TRUE) {
         switch (Period()) {
         case PERIOD_M1:
            gi_316 = 30;
            gi_320 = 30;
            gi_324 = 60;
            gi_328 = 60;
            return;
         case PERIOD_M5:
            gi_316 = 60;
            gi_320 = 60;
            gi_324 = 240;
            gi_328 = 240;
            return;
         case PERIOD_M15:
            gi_316 = 240;
            gi_320 = 240;
            gi_324 = 1440;
            gi_328 = 1440;
            return;
         case PERIOD_M30:
            gi_316 = 1440;
            gi_320 = 1440;
            gi_324 = 10080;
            gi_328 = 10080;
            return;
         case PERIOD_H1:
            gi_316 = 10080;
            gi_320 = 10080;
            gi_324 = 43200;
            gi_328 = 43200;
            return;
         case PERIOD_H4:
            gi_316 = 1440;
            gi_320 = 1440;
            gi_324 = 10080;
            gi_328 = 10080;
            return;
         case PERIOD_D1:
            gi_316 = 10080;
            gi_320 = 10080;
            gi_324 = 43200;
            gi_328 = 43200;
            return;
         case PERIOD_W1:
            gi_316 = 43200;
            gi_320 = 43200;
            gi_324 = 43200;
            gi_328 = 43200;
            return;
         case PERIOD_MN1:
            gi_316 = 43200;
            gi_320 = 43200;
            gi_324 = 43200;
            gi_328 = 43200;
            return;
         }
      }
      switch (Period()) {
      case PERIOD_M1:
         gi_316 = 5;
         gi_320 = 5;
         gi_324 = 15;
         gi_328 = 15;
         return;
      case PERIOD_M5:
         gi_316 = 15;
         gi_320 = 15;
         gi_324 = 30;
         gi_328 = 30;
         return;
      case PERIOD_M15:
         gi_316 = 30;
         gi_320 = 30;
         gi_324 = 60;
         gi_328 = 60;
         return;
      case PERIOD_M30:
         gi_316 = 60;
         gi_320 = 60;
         gi_324 = 240;
         gi_328 = 240;
         return;
      case PERIOD_H1:
         gi_316 = 240;
         gi_320 = 240;
         gi_324 = 1440;
         gi_328 = 1440;
         return;
      case PERIOD_H4:
         gi_316 = 1440;
         gi_320 = 1440;
         gi_324 = 10080;
         gi_328 = 10080;
         return;
      case PERIOD_D1:
         gi_316 = 10080;
         gi_320 = 10080;
         gi_324 = 43200;
         gi_328 = 43200;
         return;
      case PERIOD_W1:
         gi_316 = 43200;
         gi_320 = 43200;
         gi_324 = 43200;
         gi_328 = 43200;
         return;
      case PERIOD_MN1:
         gi_316 = 43200;
         gi_320 = 43200;
         gi_324 = 43200;
         gi_328 = 43200;
         return;
      }
   }
   string ls_8 = "Invalid timeframe. Please enter a valid timeframe";
   if (validateInput(ePeriod1) == 0) Alert(ls_8);
   else gi_316 = ePeriod1;
   if (validateInput(ePeriod2) == 0) Alert(ls_8);
   else gi_320 = ePeriod2;
   if (validateInput(ePeriod3) == 0) Alert(ls_8);
   else gi_324 = ePeriod3;
   if (validateInput(ePeriod4) == 0) {
      Alert(ls_8);
      return;
   }
   gi_328 = ePeriod4;
}

string getType(int ai_0) {
   string ls_4;
   if (ai_0 == 0) ls_4 = " slow";
   else
      if (ai_0 == 1) ls_4 = " fast";
   return (ls_4);
}

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 validateInput(int ai_0) {
   bool li_4 = FALSE;
   switch (ai_0) {
   case 1:
      li_4 = TRUE;
      break;
   case 5:
      li_4 = TRUE;
      break;
   case 15:
      li_4 = TRUE;
      break;
   case 30:
      li_4 = TRUE;
      break;
   case 1:
      li_4 = TRUE;
      break;
   case 60:
      li_4 = TRUE;
      break;
   case 240:
      li_4 = TRUE;
      break;
   case 1440:
      li_4 = TRUE;
      break;
   case 10080:
      li_4 = TRUE;
      break;
   case 43200:
      li_4 = TRUE;
   }
   return (li_4);
}

int placeLabels() {
   string ls_12;
   double ld_20;
   string ls_36;
   double ld_0 = Time[0] - Time[1];
   if (showlabels == TRUE) {
      for (int li_8 = 1; li_8 <= 4; li_8++) {
         ls_12 = "??";
         if (autotimeframe == TRUE) {
            switch (li_8) {
            case 1:
               ls_12 = tf2txt(gi_316) + " f";
               ld_20 = Gap * P1_position + 1.0 + v_shift;
               break;
            case 2:
               ls_12 = tf2txt(gi_320);
               ld_20 = Gap * P2_position + 1.0 + v_shift;
               break;
            case 3:
               ls_12 = tf2txt(gi_324) + " f";
               ld_20 = Gap * P3_position + 1.0 + v_shift;
               break;
            case 4:
               ls_12 = tf2txt(gi_328);
               ld_20 = Gap * P4_position + 1.0 + v_shift;
            }
         } else {
            if (autotimeframe == FALSE) {
               switch (li_8) {
               case 1:
                  ls_12 = tf2txt(gi_316) + getType(ePeriod1Type);
                  ld_20 = Gap * P1_position + 1.0 + v_shift;
                  break;
               case 2:
                  ls_12 = tf2txt(gi_320) + getType(ePeriod2Type);
                  ld_20 = Gap * P2_position + 1.0 + v_shift;
                  break;
               case 3:
                  ls_12 = tf2txt(gi_324) + getType(ePeriod3Type);
                  ld_20 = Gap * P3_position + 1.0 + v_shift;
                  break;
               case 4:
                  ls_12 = tf2txt(gi_328) + getType(ePeriod4Type);
                  ld_20 = Gap * P4_position + 1.0 + v_shift;
               }
            }
         }
         ls_36 = WindowFind(gs_280) + "FF_" + gi_312 + "_" + li_8 + "_" + ls_12;
         ObjectCreate(ls_36, OBJ_TEXT, WindowFind(gs_280), iTime(NULL, 0, 0) + ld_0 * h_shift, ld_20);
         ObjectSetText(ls_36, ls_12, 8, "Arial", TextColor);
      }
   }
   return (0);
}

int cleanLabel() {
   for (int li_0 = ObjectsTotal() - 1; li_0 > -1; li_0--)
      if (StringFind(ObjectName(li_0), WindowFind(gs_280) + "FF_" + gi_312 + "_") >= 0) ObjectDelete(ObjectName(li_0));
   return (0);
}

int checkAlarm(double ad_0, double ad_8, double ad_16, double ad_24, double ad_32, double ad_40, double ad_48, double ad_56) {
   double ld_72 = ad_0;
   double ld_80 = ad_16;
   double ld_88 = ad_32;
   double ld_96 = ad_48;
   double ld_104 = ad_8;
   double ld_112 = ad_24;
   double ld_120 = ad_40;
   double ld_128 = ad_56;
   string ls_64 = "before if soundUpAlarmOld= " + gi_344 + " soundUpAlarmNew= " + gi_352 + " soundDownAlarmOld= " + gi_348 + " soundDownAlarmNew= " + gi_356 
      + "\n" 
      + "B1UpIsEmpty= " + ld_72 + " B2UpIsEmpty= " + ld_80 + " B3UpIsEmpty= " + ld_88 + " B4UpIsEmpty= " + ld_96 
      + "\n" 
   + " B1DownIsEmpty= " + ld_104 + " B2DownIsEmpty= " + ld_112 + " B3DownIsEmpty= " + ld_120 + " B4DownIsEmpty= " + ld_128;
   if (showcomment == TRUE) Comment(ls_64);
   if (ld_72 != EMPTY_VALUE && ld_80 != EMPTY_VALUE && ld_88 != EMPTY_VALUE && ld_96 != EMPTY_VALUE) {
      gi_352 = TRUE;
      if (gi_344 != gi_352) {
         gi_344 = gi_352;
         gi_348 = FALSE;
         gi_356 = FALSE;
         ls_64 = "In first if soundUpAlarmOld= " + gi_344 + " soundUpAlarmNew= " + gi_352 + " soundDownAlarmOld= " + gi_348 + " soundDownAlarmNew= " + gi_356 
            + "\n" 
            + "B1UpIsEmpty= " + ld_72 + " B2UpIsEmpty= " + ld_80 + " B3UpIsEmpty= " + ld_88 + " B4UpIsEmpty= " + ld_96 
            + "\n" 
         + " B1DownIsEmpty= " + ld_104 + " B2DownIsEmpty= " + ld_112 + " B3DownIsEmpty= " + ld_120 + " B4DownIsEmpty= " + ld_128;
         if (showcomment == TRUE) Comment(ls_64);
         return (gi_352);
      }
      return (0);
   }
   if (ld_104 != EMPTY_VALUE && ld_112 != EMPTY_VALUE && ld_120 != EMPTY_VALUE && ld_128 != EMPTY_VALUE) {
      gi_356 = TRUE;
      if (gi_348 != gi_356) {
         gi_348 = gi_356;
         gi_344 = FALSE;
         gi_352 = FALSE;
         ls_64 = "In second if soundUpAlarmOld= " + gi_344 + " soundUpAlarmNew= " + gi_352 + " soundDownAlarmOld= " + gi_348 + " soundDownAlarmNew= " + gi_356 
            + "\n" 
            + "B1UpIsEmpty= " + ld_72 + " B2UpIsEmpty= " + ld_80 + " B3UpIsEmpty= " + ld_88 + " B4UpIsEmpty= " + ld_96 
            + "\n" 
         + " B1DownIsEmpty= " + ld_104 + " B2DownIsEmpty= " + ld_112 + " B3DownIsEmpty= " + ld_120 + " B4DownIsEmpty= " + ld_128;
         if (showcomment == TRUE) Comment(ls_64);
         return (gi_356);
      }
      return (0);
   }
   gi_344 = FALSE;
   gi_348 = FALSE;
   gi_352 = FALSE;
   gi_356 = FALSE;
   return (0);
}

void DisplayAlert(string as_0) {
   string ls_8;
   Alert(as_0, Symbol(), " , ", tf2txt(Period()), " minutes chart");
}
