/*
   Generated by EX4-TO-MQ4 decompiler V4.0.427.2 [-]
   Website: https://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2012, PimpMyEA.com"
#property link      "http://www.pimpmyea.com"

#property indicator_chart_window

extern string note_1 = "1-RSI; 2-WPR; 3-Stoch; 4-BB%B";
extern int algo = 2;
extern bool use_default_periods = TRUE;
extern int period = 20;
extern int correlation_level = 80;
extern int timeframe = 0;
extern bool real_time = FALSE;
extern int base_text_size = 7;
extern int startx = 0;
extern int starty = 0;
extern bool force_update = TRUE;
string gs_128;
string gsa_136[8];
string gsa_140[64];
double gda_unused_144[64];
int gi_148;
double gda_unused_152[];
double gda_unused_156[];
double gda_unused_160[];
string gs_164;
string gs_dummy_172;
bool gi_180 = TRUE;
datetime g_time_184;

int init() {
   string symbol_0;
   Comment("");
   IndicatorDigits(2);
   gs_128 = StringSubstr(Symbol(), 6, 0);
   gsa_136[0] = "AUD";
   gsa_136[1] = "CAD";
   gsa_136[2] = "CHF";
   gsa_136[3] = "EUR";
   gsa_136[4] = "GBP";
   gsa_136[5] = "JPY";
   gsa_136[6] = "NZD";
   gsa_136[7] = "USD";
   for (int index_8 = 0; index_8 < 8; index_8++) {
      for (int index_12 = 0; index_12 < 8; index_12++) {
         if (index_8 != index_12) {
            symbol_0 = gsa_136[index_8] + gsa_136[index_12] + gs_128;
            if (MarketInfo(symbol_0, MODE_TRADEALLOWED) == 1.0) {
               gsa_140[gi_148] = symbol_0;
               gi_148++;
            }
         }
      }
   }
   if (timeframe == 0) timeframe = Period();
   if (use_default_periods) {
      if (timeframe == 1) period = 30;
      if (timeframe == 5) period = 24;
      if (timeframe == 15) period = 24;
      if (timeframe == 30) period = 24;
      if (timeframe == 60) period = 24;
      if (timeframe == 240) period = 30;
      if (timeframe == 1440) period = 20;
      if (timeframe == 10080) period = 27;
      if (timeframe == 43200) period = 24;
   }
   if (timeframe == 1) gs_164 = "1M";
   if (timeframe == 5) gs_164 = "5M";
   if (timeframe == 15) gs_164 = "15M";
   if (timeframe == 30) gs_164 = "30M";
   if (timeframe == 60) gs_164 = "1H";
   if (timeframe == 240) gs_164 = "4H";
   if (timeframe == 1440) gs_164 = "1D";
   if (timeframe == 10080) gs_164 = "1W";
   if (timeframe == 43200) gs_164 = "1MN";
   return (0);
}

int deinit() {
   Comment("");
   ObjectsDeleteAll();
   return (0);
}

int start() {
   double ld_16;
   double ld_24;
   double ld_32;
   double ld_40;
   double ld_48;
   double ld_56;
   double ld_64;
   double ld_72;
   double ld_80;
   double ld_88;
   string name_96;
   double color_104;
   int fontsize_112;
   if (TimeCurrent() >= D'27.10.2012 07:00') {
      if (!(gi_180)) return (0);
      Alert("Version Expired! Please go to PimpMyEA.com to download an updated version. Thanks");
      gi_180 = FALSE;
      return (0);
   }
   int ind_counted_4 = IndicatorCounted();
   if (f0_2() || real_time == TRUE) {
      for (int index_8 = 0; index_8 < 8; index_8++) {
         ObjectDelete(gsa_136[index_8] + "x" + gs_164);
         ObjectCreate(gsa_136[index_8] + "x" + gs_164, OBJ_LABEL, 0, 0, 0);
         ObjectSetText(gsa_136[index_8] + "x" + gs_164, gsa_136[index_8], base_text_size, "SegoeUI", White);
         ObjectSet(gsa_136[index_8] + "x" + gs_164, OBJPROP_XDISTANCE, startx + 68 + 50 * index_8);
         ObjectSet(gsa_136[index_8] + "x" + gs_164, OBJPROP_YDISTANCE, starty + 35);
         for (int index_12 = 0; index_12 < index_8 + 1; index_12++) {
            ObjectDelete(gsa_136[index_12] + "y" + gs_164);
            ObjectCreate(gsa_136[index_12] + "y" + gs_164, OBJ_LABEL, 0, 0, 0);
            ObjectSetText(gsa_136[index_12] + "y" + gs_164, gsa_136[index_12], base_text_size, "SegoeUI", White);
            ObjectSet(gsa_136[index_12] + "y" + gs_164, OBJPROP_XDISTANCE, startx + 15);
            ObjectSet(gsa_136[index_12] + "y" + gs_164, OBJPROP_YDISTANCE, starty + 50 + 15 * index_12);
            name_96 = "PotenzaMatrix_" + gsa_136[index_8] + "-" + gsa_136[index_12] + gs_164;
            ObjectDelete(name_96 + "hline");
            ObjectCreate(name_96 + "hline", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_96 + "hline", "___________", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_96 + "hline", OBJPROP_XDISTANCE, startx + 50 + 50 * index_8);
            ObjectSet(name_96 + "hline", OBJPROP_YDISTANCE, starty + 54 + 15 * index_12);
            ObjectDelete(name_96 + "vline");
            ObjectCreate(name_96 + "vline", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_96 + "vline", "|", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_96 + "vline", OBJPROP_XDISTANCE, startx + 100 + 50 * index_8);
            ObjectSet(name_96 + "vline", OBJPROP_YDISTANCE, starty + 50 + 15 * index_12);
            ObjectDelete(name_96 + "hline_");
            ObjectCreate(name_96 + "hline_", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_96 + "hline_", "___________", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_96 + "hline_", OBJPROP_XDISTANCE, startx + 50 + 50 * index_12);
            ObjectSet(name_96 + "hline_", OBJPROP_YDISTANCE, starty + 54 + 15 * index_8);
            ObjectDelete(name_96 + "vline_");
            ObjectCreate(name_96 + "vline_", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_96 + "vline_", "|", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_96 + "vline_", OBJPROP_XDISTANCE, startx + 100 + 50 * index_12);
            ObjectSet(name_96 + "vline_", OBJPROP_YDISTANCE, starty + 50 + 15 * index_8);
            fontsize_112 = base_text_size;
            if (gsa_136[index_8] == gsa_136[index_12]) {
               ObjectDelete(name_96);
               ObjectCreate(name_96, OBJ_LABEL, 0, 0, 0);
               ObjectSetText(name_96, "----", fontsize_112 + 2, "SegoeUI", White);
               ObjectSet(name_96, OBJPROP_XDISTANCE, startx + 70 + 50 * index_8);
               ObjectSet(name_96, OBJPROP_YDISTANCE, starty + 48 + 15 * index_12);
            }
            if (gsa_136[index_8] != gsa_136[index_12]) {
               ld_24 = 0;
               ld_32 = 0;
               ld_40 = 0;
               ld_48 = 0;
               ld_56 = 0;
               for (int count_0 = 0; count_0 < period; count_0++) {
                  ld_64 = 2.0 * (f0_1(gsa_136[index_8], period, timeframe, count_0) - 50.0);
                  ld_72 = 2.0 * (f0_1(gsa_136[index_12], period, timeframe, count_0) - 50.0);
                  ld_24 += ld_64;
                  ld_32 += ld_72;
                  ld_40 += ld_64 * ld_72;
                  ld_48 += ld_64 * ld_64;
                  ld_56 += ld_72 * ld_72;
               }
               ld_80 = period * ld_40 - ld_24 * ld_32;
               ld_88 = MathSqrt(MathAbs((period * ld_48 - MathPow(ld_24, 2)) * (period * ld_56 - MathPow(ld_32, 2))));
               if (ld_88 != 0.0) ld_16 = ld_80 / ld_88;
               ld_16 = 100.0 * ld_16;
               if (ld_16 < 0.0) color_104 = f0_0(255, 0, 0, 55, 55, 55, 0, 100, MathAbs(ld_16));
               if (ld_16 > 0.0) color_104 = f0_0(0, 255, 0, 55, 55, 55, 0, 100, MathAbs(ld_16));
               if (MathAbs(ld_16) >= correlation_level) {
                  ObjectDelete(name_96);
                  ObjectCreate(name_96, OBJ_LABEL, 0, 0, 0);
                  ObjectSetText(name_96, "[ " + DoubleToStr(ld_16, 1) + " ]", fontsize_112 + 2, "SegoeUI", color_104);
                  ObjectSet(name_96, OBJPROP_XDISTANCE, startx + 59 + 50 * index_8);
                  ObjectSet(name_96, OBJPROP_YDISTANCE, starty + 49 + 15 * index_12);
                  ObjectDelete(name_96 + "_");
                  ObjectCreate(name_96 + "_", OBJ_LABEL, 0, 0, 0);
                  ObjectSetText(name_96 + "_", "[ " + DoubleToStr(ld_16, 1) + " ]", fontsize_112 + 2, "SegoeUI", color_104);
                  ObjectSet(name_96 + "_", OBJPROP_XDISTANCE, startx + 59 + 50 * index_12);
                  ObjectSet(name_96 + "_", OBJPROP_YDISTANCE, starty + 49 + 15 * index_8);
                  continue;
               }
               ObjectDelete(name_96);
               ObjectCreate(name_96, OBJ_LABEL, 0, 0, 0);
               ObjectSetText(name_96, DoubleToStr(ld_16, 1), fontsize_112, "SegoeUI", color_104);
               ObjectSet(name_96, OBJPROP_XDISTANCE, startx + 70 + 50 * index_8);
               ObjectSet(name_96, OBJPROP_YDISTANCE, starty + 50 + 15 * index_12);
               ObjectDelete(name_96 + "_");
               ObjectCreate(name_96 + "_", OBJ_LABEL, 0, 0, 0);
               ObjectSetText(name_96 + "_", DoubleToStr(ld_16, 1), fontsize_112, "SegoeUI", color_104);
               ObjectSet(name_96 + "_", OBJPROP_XDISTANCE, startx + 70 + 50 * index_12);
               ObjectSet(name_96 + "_", OBJPROP_YDISTANCE, starty + 50 + 15 * index_8);
            }
         }
      }
      ObjectDelete("Potenza_title" + gs_164);
      ObjectCreate("Potenza_title" + gs_164, OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Potenza_title" + gs_164, "POTENZA_FX CORRELATION MATRIX v1.0 - http://www.pimpmyea.com", fontsize_112 + 2, "SegoeUI", SkyBlue);
      ObjectSet("Potenza_title" + gs_164, OBJPROP_XDISTANCE, startx + 15);
      ObjectSet("Potenza_title" + gs_164, OBJPROP_YDISTANCE, starty);
      ObjectDelete("Potenza_title2" + gs_164);
      ObjectCreate("Potenza_title2" + gs_164, OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Potenza_title2" + gs_164, "TIMEFRAME:" + gs_164 + " - PERIOD: " + period, fontsize_112 + 2, "SegoeUI", Yellow);
      ObjectSet("Potenza_title2" + gs_164, OBJPROP_XDISTANCE, startx + 15);
      ObjectSet("Potenza_title2" + gs_164, OBJPROP_YDISTANCE, starty + 14);
   }
   return (0);
}

double f0_4(string a_symbol_0, int a_timeframe_8, int a_period_12, double ad_16, int ai_24) {
   double ld_28 = (iClose(a_symbol_0, a_timeframe_8, ai_24) - iBands(a_symbol_0, a_timeframe_8, a_period_12, ad_16, 0, PRICE_CLOSE, MODE_LOWER, ai_24)) / (iBands(a_symbol_0,
      a_timeframe_8, a_period_12, ad_16, 0, PRICE_CLOSE, MODE_UPPER, ai_24) - iBands(a_symbol_0, a_timeframe_8, a_period_12, ad_16, 0, PRICE_CLOSE, MODE_LOWER, ai_24));
   return (100.0 * ld_28);
}

double f0_1(string as_0, int a_period_8, int a_timeframe_12, int ai_16) {
   double ld_20;
   int li_28;
   for (int index_32 = 0; index_32 < gi_148; index_32++) {
      if (StringSubstr(gsa_140[index_32], 0, 3) == as_0) {
         if (algo == 1) ld_20 += iRSI(gsa_140[index_32], a_timeframe_12, a_period_8, PRICE_CLOSE, ai_16);
         if (algo == 2) ld_20 += iWPR(gsa_140[index_32], a_timeframe_12, a_period_8, ai_16) + 100.0;
         if (algo == 3) ld_20 += iStochastic(gsa_140[index_32], a_timeframe_12, a_period_8, 3, 3, MODE_SMA, 0, MODE_MAIN, ai_16);
         if (algo == 4) ld_20 += f0_4(gsa_140[index_32], a_timeframe_12, a_period_8, 2, ai_16);
         li_28++;
      }
      if (StringSubstr(gsa_140[index_32], 3, 3) == as_0) {
         if (algo == 1) ld_20 += 100 - iRSI(gsa_140[index_32], a_timeframe_12, a_period_8, PRICE_CLOSE, ai_16);
         if (algo == 2) ld_20 += 100 - (iWPR(gsa_140[index_32], a_timeframe_12, a_period_8, ai_16) + 100.0);
         if (algo == 3) ld_20 += 100 - iStochastic(gsa_140[index_32], a_timeframe_12, a_period_8, 3, 3, MODE_SMA, 0, MODE_MAIN, ai_16);
         if (algo == 4) ld_20 += 100 - f0_4(gsa_140[index_32], a_timeframe_12, a_period_8, 2, ai_16);
         li_28++;
      }
   }
   return (ld_20 / li_28);
}

int f0_2() {
   if (g_time_184 == Time[0]) return (0);
   g_time_184 = Time[0];
   return (1);
}

int f0_3(int ai_0, int ai_4, int ai_8) {
   int li_ret_12 = MathMin(255, MathAbs(ai_0)) + 256.0 * MathMin(255, MathAbs(ai_4)) + 65536.0 * MathMin(255, MathAbs(ai_8));
   return (li_ret_12);
}

int f0_0(int ai_0, int ai_4, int ai_8, int ai_12, int ai_16, int ai_20, double ad_24, double ad_32, double ad_40) {
   double ld_48 = ad_32 - ad_24;
   ad_40 = ad_32 - ad_40;
   double ld_56 = (ai_0 - ai_12) / (ld_48 - 1.0);
   double ld_64 = (ai_4 - ai_16) / (ld_48 - 1.0);
   double ld_72 = (ai_8 - ai_20) / (ld_48 - 1.0);
   return (f0_3(ai_0 - ld_56 * ad_40, ai_4 - ld_64 * ad_40, ai_8 - ld_72 * ad_40));
}
