/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "whitebloodcell"
#property link      "tmclayson@googlemail.com"

#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 9.0
#property indicator_buffers 7
#property indicator_color1 Aqua
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 LimeGreen
#property indicator_color5 Maroon
#property indicator_color6 Red
#property indicator_color7 DarkGreen

extern color FontColor = Yellow;
extern int TimeFrameMinutes = 0;
extern int HistoryBars = 200;
extern int RSIPeriod = 13;
extern double FiboValue = 0.618;
extern int CSIReadoutSize = 12;
extern int CSIReadoutX = 800;
extern int CSIReadoutY = 10;
string gsa_112[9] = {"USD", "EUR", "GBP", "CHF", "CAD", "AUD", "JPY", "NZD", "XAU"};
string gsa_116[];
string gs_csivalue_120 = "CSIValue";
int gi_unused_128 = 0;
int g_arr_size_132;
int g_str_len_136;
string gs_140;
string gs_148;
string gs_156;
string gs_dummy_164;
string gs_dummy_172;
double g_ibuf_180[];
double g_ibuf_184[];
double g_ibuf_188[];
double g_ibuf_192[];
double g_ibuf_196[];
double g_ibuf_200[];
double g_ibuf_204[];
int g_file_208 = -1;
bool gi_216 = TRUE;
string gs_220;
int g_period_228;
int g_period_232 = 2;
int g_slowing_236 = 2;

void log_open(string as_0 = "Expert") {
   int l_error_16;
   string l_name_8 = "logs\\" + as_0 + " (" + Symbol() + ", " + strPeriod(Period()) + ")\\" + TimeToStr(TimeLocal(), TIME_DATE) + ".txt";
   g_file_208 = FileOpen(l_name_8, FILE_WRITE|FILE_READ, " ");
   if (g_file_208 < 0) {
      l_error_16 = GetLastError();
      Print("FileOpen( ", l_name_8, ", FILE_READ | FILE_WRITE, \" \" ) - Error #", l_error_16);
   }
}

string strPeriod(int ai_0) {
   switch (ai_0) {
   case 43200:
      return ("Monthly");
   case 10080:
      return ("Weekly");
   case 1440:
      return ("Daily");
   case 240:
      return ("H4");
   case 60:
      return ("H1");
   case 30:
      return ("M30");
   case 15:
      return ("M15");
   case 5:
      return ("M5");
   case 1:
      return ("M1");
   }
   return ("UnknownPeriod");
}

void log_close() {
   if (g_file_208 > 0) FileClose(g_file_208);
}

void log(string a_str_concat_0) {
   int l_error_8 = 0;
   if (g_file_208 < 0) {
      Print("Log write error! Text: ", a_str_concat_0);
      return;
   }
   if (!FileSeek(g_file_208, 0, SEEK_END)) {
      l_error_8 = GetLastError();
      Print("FileSeek ( " + g_file_208 + ", 0, SEEK_END ) - Error #", l_error_8);
      return;
   }
   if (a_str_concat_0 != "\n" && a_str_concat_0 != "\r\n") a_str_concat_0 = StringConcatenate(TimeToStr(TimeLocal(), TIME_SECONDS), " - - - ", a_str_concat_0);
   if (FileWrite(g_file_208, a_str_concat_0) < 0) {
      l_error_8 = GetLastError();
      Print("FileWrite ( ", g_file_208, ", ", a_str_concat_0, " ) - Error #", l_error_8);
      return;
   }
   FileFlush(g_file_208);
}

void DeleteObjectsByString(string as_0) {
   for (int li_8 = ObjectsTotal() - 1; li_8 >= 0; li_8--)
      if (StringFind(ObjectName(li_8), as_0) != -1) ObjectDelete(ObjectName(li_8));
}

int init() {
   double ld_0;
   double ld_8;
   int li_16 = 35;
   int li_20 = 10;
   int li_24 = 12;
   CreatePairArray(gsa_112, gsa_116);
   g_arr_size_132 = ArraySize(gsa_116);
   PairLabel();
   gs_220 = "CSI_v2";
   IndicatorShortName(gs_220);
   DeleteObjectsByString(gs_220);
   if (gi_216) log_open(gs_220);
   if (gi_216) for (int l_index_28 = 0; l_index_28 < g_arr_size_132; l_index_28++) log(StringConcatenate(gsa_116[l_index_28], ""));
   string ls_32 = "******";
   if (IsDllsAllowed() == FALSE) {
      Alert("Allow for DLL calls in the \'properties-->common\' popup window");
      ld_0 = 0;
      ld_8 = 1 / ld_0;
      return (0);
   }
   if (TimeFrameMinutes == 0) TimeFrameMinutes = Period();
   g_period_228 = 1440 / TimeFrameMinutes;
   if (TimeFrameMinutes >= 1440) g_period_228 = 6;
   if (TimeFrameMinutes < 60 && TimeFrameMinutes >= 15) g_period_228 = 12;
   if (TimeFrameMinutes < 15 && TimeFrameMinutes >= 5) g_period_228 = 36;
   if (TimeFrameMinutes <= 1) g_period_228 = 60;
   int li_40 = g_period_228;
   SetIndexBuffer(0, g_ibuf_180);
   SetIndexBuffer(1, g_ibuf_184);
   SetIndexBuffer(2, g_ibuf_188);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(3, g_ibuf_192);
   SetIndexStyle(3, DRAW_HISTOGRAM);
   SetIndexLabel(3, "");
   SetIndexBuffer(4, g_ibuf_196);
   SetIndexStyle(4, DRAW_HISTOGRAM);
   SetIndexLabel(4, "");
   SetIndexBuffer(5, g_ibuf_200);
   SetIndexStyle(5, DRAW_HISTOGRAM);
   SetIndexLabel(5, "");
   SetIndexBuffer(6, g_ibuf_204);
   SetIndexStyle(6, DRAW_HISTOGRAM);
   SetIndexLabel(6, "");
   IndicatorDigits(2);
   SetIndexDrawBegin(0, li_40);
   SetIndexDrawBegin(1, li_40);
   SetIndexDrawBegin(2, li_40);
   SetIndexDrawBegin(3, li_40);
   SetIndexDrawBegin(4, li_40);
   SetIndexDrawBegin(5, li_40);
   SetIndexDrawBegin(6, li_40);
   SetIndexLabel(0, gs_148);
   SetIndexLabel(1, gs_156);
   SetLevelValue(0, 4.5);
   SetLevelValue(1, 2);
   SetLevelValue(2, 7);
   CreateLabelObject2("zera", li_16, li_20 + li_24, gsa_112[0], 9, "Arial", FontColor);
   CreateLabelObject2("zerb", li_16, li_20 + li_24 * 2, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("one", li_16, li_20 + 3 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("onea", li_16, li_20 + li_24 << 2, gsa_112[1], 9, "Arial", FontColor);
   CreateLabelObject2("oneb", li_16, li_20 + 5 * li_24, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("two", li_16, li_20 + 6 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("twoa", li_16, li_20 + 7 * li_24, gsa_112[2], 9, "Arial", FontColor);
   CreateLabelObject2("twob", li_16, li_20 + li_24 << 3, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("thr", li_16, li_20 + 9 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("thra", li_16, li_20 + 10 * li_24, gsa_112[3], 9, "Arial", FontColor);
   CreateLabelObject2("thrb", li_16, li_20 + 11 * li_24, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("fou", li_16, li_20 + 12 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("foua", li_16, li_20 + 13 * li_24, gsa_112[4], 9, "Arial", FontColor);
   CreateLabelObject2("foub", li_16, li_20 + 14 * li_24, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("fiv", li_16, li_20 + 15 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("fiva", li_16, li_20 + li_24 << 4, gsa_112[5], 9, "Arial", FontColor);
   CreateLabelObject2("fivb", li_16, li_20 + 17 * li_24, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("six", li_16, li_20 + 18 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("sixa", li_16, li_20 + 19 * li_24, gsa_112[6], 9, "Arial", FontColor);
   CreateLabelObject2("sixb", li_16, li_20 + 20 * li_24, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   CreateLabelObject2("sev", li_16, li_20 + 21 * li_24, ls_32, 9, "Arial", FontColor);
   CreateLabelObject2("seva", li_16, li_20 + 22 * li_24, gsa_112[7], 9, "Arial", FontColor);
   CreateLabelObject2("sevb", li_16, li_20 + 23 * li_24, DoubleToStr(0.0, 2), 9, "Arial", FontColor);
   return (0);
}

int deinit() {
   if (gi_216) log_close();
   DeleteObjectsByString(gs_220);
   return (0);
}

int start() {
   double ld_12;
   double ld_20;
   double ld_28;
   double ld_36;
   int li_0 = IndicatorCounted();
   if (li_0 > 0) li_0--;
   int li_8 = Bars - 1;
   int li_4 = MathMin(li_8 - li_0, HistoryBars);
   for (int li_44 = li_4; li_44 >= 0; li_44--) FXCXCSI(li_44);
   for (int li_48 = li_4; li_48 >= 0; li_48--) {
      ld_28 = MathAbs(g_ibuf_180[li_48] - g_ibuf_184[li_48]);
      ld_36 = MathAbs(g_ibuf_180[li_48 + 1] - (g_ibuf_184[li_48 + 1]));
      ld_12 = g_ibuf_180[li_48] - g_ibuf_184[li_48];
      ld_20 = g_ibuf_180[li_48 + 1] - (g_ibuf_184[li_48 + 1]);
      if (ld_28 > ld_36) {
         if (ld_12 > 0.0 && ld_20 > 0.0) {
            g_ibuf_192[li_48] = ld_28;
            g_ibuf_196[li_48] = 0.0;
            g_ibuf_200[li_48] = 0.0;
            g_ibuf_204[li_48] = 0.0;
            ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, LimeGreen);
         } else {
            if (ld_12 < 0.0 && ld_20 > 0.0) {
               g_ibuf_192[li_48] = 0.0;
               g_ibuf_196[li_48] = 0.0;
               g_ibuf_200[li_48] = ld_28;
               g_ibuf_204[li_48] = 0.0;
               if (gi_216) Print(StringConcatenate(gs_220, "_", gs_csivalue_120));
               ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, Red);
            } else {
               if (ld_12 > 0.0 && ld_20 < 0.0) {
                  g_ibuf_192[li_48] = ld_28;
                  g_ibuf_196[li_48] = 0.0;
                  g_ibuf_200[li_48] = 0.0;
                  g_ibuf_204[li_48] = 0.0;
                  ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, LimeGreen);
               } else {
                  if (ld_12 < 0.0 && ld_20 < 0.0) {
                     g_ibuf_192[li_48] = 0.0;
                     g_ibuf_196[li_48] = 0.0;
                     g_ibuf_200[li_48] = ld_28;
                     g_ibuf_204[li_48] = 0.0;
                     ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, Red);
                  }
               }
            }
         }
      } else {
         if (ld_12 < 0.0 && ld_20 < 0.0) {
            g_ibuf_192[li_48] = 0.0;
            g_ibuf_196[li_48] = ld_28;
            g_ibuf_200[li_48] = 0.0;
            g_ibuf_204[li_48] = 0.0;
            ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, Red);
         } else {
            if (ld_12 < 0.0 && ld_20 > 0.0) {
               g_ibuf_192[li_48] = 0.0;
               g_ibuf_196[li_48] = 0.0;
               g_ibuf_200[li_48] = ld_28;
               g_ibuf_204[li_48] = 0.0;
               ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, Red);
            } else {
               if (ld_12 > 0.0 && ld_20 < 0.0) {
                  g_ibuf_192[li_48] = ld_28;
                  g_ibuf_196[li_48] = 0.0;
                  g_ibuf_200[li_48] = 0.0;
                  g_ibuf_204[li_48] = 0.0;
                  ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, LimeGreen);
               } else {
                  if (ld_12 > 0.0 && ld_20 > 0.0) {
                     g_ibuf_192[li_48] = 0.0;
                     g_ibuf_196[li_48] = 0.0;
                     g_ibuf_200[li_48] = 0.0;
                     g_ibuf_204[li_48] = ld_28;
                     ObjectSet(StringConcatenate(gs_220, "_", gs_csivalue_120), OBJPROP_COLOR, LimeGreen);
                  }
               }
            }
         }
      }
   }
   return (0);
}

void FXCXCSI(int a_shift_0) {
   int l_count_4;
   string l_symbol_8;
   string ls_16;
   string ls_24;
   string ls_32;
   string ls_40;
   double l_istochastic_48;
   double l_irsi_56;
   double ld_64;
   double ld_72;
   double lda_80[8];
   double lda_84[];
   double lda_88[];
   int lia_96[8];
   ArrayResize(lda_84, g_arr_size_132);
   ArrayResize(lda_88, g_arr_size_132);
   int li_100 = FindIndex(gs_148);
   int li_104 = FindIndex(gs_156);
   for (int l_index_92 = 0; l_index_92 < g_arr_size_132; l_index_92++) {
      l_symbol_8 = gsa_116[l_index_92];
      l_istochastic_48 = 0.0;
      l_irsi_56 = 0.0;
      l_istochastic_48 = iStochastic(l_symbol_8, 0, g_period_228, g_period_232, g_slowing_236, MODE_SMA, 1, MODE_MAIN, a_shift_0);
      l_irsi_56 = iRSI(l_symbol_8, 0, RSIPeriod, PRICE_CLOSE, a_shift_0);
      ld_64 = (FiboValue * l_istochastic_48 + (1 - FiboValue) * l_irsi_56) / 100.0;
      lda_84[l_index_92] = 9.0 * ld_64;
      lda_88[l_index_92] = 9 - lda_84[l_index_92];
   }
   for (int l_index_108 = 0; l_index_108 < 8; l_index_108++) {
      l_count_4 = 0;
      ld_72 = 0;
      ls_16 = gsa_112[l_index_108];
      for (int l_index_112 = 0; l_index_112 < g_arr_size_132; l_index_112++) {
         ls_24 = gsa_116[l_index_112];
         ls_32 = StringSubstr(ls_24, 0, 3);
         ls_40 = StringSubstr(ls_24, 3, 3);
         if (ls_32 == ls_16) {
            ld_72 += lda_84[l_index_112];
            l_count_4++;
         }
         if (ls_40 == ls_16) {
            ld_72 += lda_88[l_index_112];
            l_count_4++;
         }
      }
      lda_80[l_index_108] = NormalizeDouble(ld_72 / l_count_4, 2);
   }
   g_ibuf_180[a_shift_0] = lda_80[li_100];
   g_ibuf_184[a_shift_0] = lda_80[li_104];
   g_ibuf_188[a_shift_0] = MathAbs(lda_80[li_100] - lda_80[li_104]);
   string l_dbl2str_116 = DoubleToStr(g_ibuf_188[a_shift_0], 2);
   CreateLabelObject2(gs_csivalue_120, CSIReadoutX, CSIReadoutY, "CSI = " + l_dbl2str_116, CSIReadoutSize, "Arial", Gray);
   for (int l_index_124 = 0; l_index_124 < 8; l_index_124++) {
      lia_96[l_index_124] = FontColor;
      if (l_index_124 == li_100) lia_96[l_index_124] = 16776960;
      if (l_index_124 == li_104) lia_96[l_index_124] = 255;
   }
   string l_dbl2str_128 = DoubleToStr(lda_80[0], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "zerb"), l_dbl2str_128, 9, "Arial", lia_96[0]);
   l_dbl2str_128 = DoubleToStr(lda_80[1], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "oneb"), l_dbl2str_128, 9, "Arial", lia_96[1]);
   l_dbl2str_128 = DoubleToStr(lda_80[2], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "twob"), l_dbl2str_128, 9, "Arial", lia_96[2]);
   l_dbl2str_128 = DoubleToStr(lda_80[3], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "thrb"), l_dbl2str_128, 9, "Arial", lia_96[3]);
   l_dbl2str_128 = DoubleToStr(lda_80[4], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "foub"), l_dbl2str_128, 9, "Arial", lia_96[4]);
   l_dbl2str_128 = DoubleToStr(lda_80[5], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "fivb"), l_dbl2str_128, 9, "Arial", lia_96[5]);
   l_dbl2str_128 = DoubleToStr(lda_80[6], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "sixb"), l_dbl2str_128, 9, "Arial", lia_96[6]);
   l_dbl2str_128 = DoubleToStr(lda_80[7], 2);
   ObjectSetText(StringConcatenate(gs_220, "_", "sevb"), l_dbl2str_128, 9, "Arial", lia_96[7]);
}

int FindIndex(string as_0) {
   if (as_0 == "USD") return (0);
   if (as_0 == "EUR") return (1);
   if (as_0 == "GBP") return (2);
   if (as_0 == "CHF") return (3);
   if (as_0 == "CAD") return (4);
   if (as_0 == "AUD") return (5);
   if (as_0 == "JPY") return (6);
   if (as_0 == "NZD") return (7);
   return (0);
}

void CreateLabelObject2(string a_str_concat_0, int a_x_8, int a_y_12, string a_text_16, int a_fontsize_24, string a_fontname_28, color a_color_36 = -1) {
   a_str_concat_0 = StringConcatenate(gs_220, "_", a_str_concat_0);
   if (ObjectFind(a_str_concat_0) == -1) {
      ObjectCreate(a_str_concat_0, OBJ_LABEL, 0, 0, 0);
      ObjectSet(a_str_concat_0, OBJPROP_CORNER, 1);
      ObjectSet(a_str_concat_0, OBJPROP_XDISTANCE, a_x_8);
      ObjectSet(a_str_concat_0, OBJPROP_YDISTANCE, a_y_12);
      ObjectSetText(a_str_concat_0, a_text_16, a_fontsize_24, a_fontname_28, a_color_36);
      return;
   }
   ObjectSetText(a_str_concat_0, a_text_16, a_fontsize_24, a_fontname_28, a_color_36);
   ObjectSet(a_str_concat_0, OBJPROP_CORNER, 1);
   ObjectSet(a_str_concat_0, OBJPROP_XDISTANCE, a_x_8);
   ObjectSet(a_str_concat_0, OBJPROP_YDISTANCE, a_y_12);
}

void PairLabel() {
   string ls_unused_0;
   string ls_unused_8;
   gs_140 = Symbol();
   g_str_len_136 = StringLen(gs_140);
   gs_148 = StringSubstr(gs_140, 0, 3);
   gs_156 = StringSubstr(gs_140, 3, 3);
}

void CreatePairArray(string asa_0[8], string &asa_4[]) {
   string ls_8;
   string l_symbol_32;
   string ls_16 = Symbol();
   int l_arr_size_24 = ArraySize(asa_0);
   int l_str_len_28 = StringLen(ls_16);
   if (l_str_len_28 > 6) ls_8 = StringSubstr(ls_16, 6, l_str_len_28 - 6);
   int l_index_40 = 0;
   for (int l_index_44 = 0; l_index_44 < l_arr_size_24 - 1; l_index_44++) {
      for (int li_48 = l_index_44 + 1; li_48 < l_arr_size_24; li_48++) {
         l_symbol_32 = asa_0[l_index_44] + asa_0[li_48] + ls_8;
         if (MarketInfo(l_symbol_32, MODE_BID) > 0.0) {
            ArrayResize(asa_4, l_index_40 + 1);
            asa_4[l_index_40] = l_symbol_32;
            l_index_40++;
         }
         l_symbol_32 = asa_0[li_48] + asa_0[l_index_44] + ls_8;
         if (MarketInfo(l_symbol_32, MODE_BID) > 0.0) {
            ArrayResize(asa_4, l_index_40 + 1);
            asa_4[l_index_40] = l_symbol_32;
            l_index_40++;
         }
      }
   }
   g_arr_size_132 = ArraySize(asa_4);
}