/*
   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 int number_of_pairs = 30;
extern string pair_01 = "AUDCAD";
extern string pair_02 = "AUDCHF";
extern string pair_03 = "AUDJPY";
extern string pair_04 = "AUDNZD";
extern string pair_05 = "AUDUSD";
extern string pair_06 = "CADCHF";
extern string pair_07 = "CADJPY";
extern string pair_08 = "CHFJPY";
extern string pair_09 = "EURAUD";
extern string pair_10 = "EURCAD";
extern string pair_11 = "EURCHF";
extern string pair_12 = "EURGBP";
extern string pair_13 = "EURJPY";
extern string pair_14 = "EURNZD";
extern string pair_15 = "EURUSD";
extern string pair_16 = "GBPAUD";
extern string pair_17 = "GBPCAD";
extern string pair_18 = "GBPCHF";
extern string pair_19 = "GBPJPY";
extern string pair_20 = "GBPNZD";
extern string pair_21 = "GBPUSD";
extern string pair_22 = "NZDCAD";
extern string pair_23 = "NZDCHF";
extern string pair_24 = "NZDJPY";
extern string pair_25 = "NZDUSD";
extern string pair_26 = "USDCAD";
extern string pair_27 = "USDCHF";
extern string pair_28 = "USDJPY";
extern string pair_29 = "XAUUSD";
extern string pair_30 = "XAGUSD";
extern bool real_time = FALSE;
extern int period = 120;
extern int correlation_level = 80;
extern int base_text_size = 7;
extern int startx = 0;
extern int starty = 0;
string gsa_344[30];
string gsa_348[30];
int gia_352[25] = {0, 0, 0, 0, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, 36, 37, 39, 40};
int gi_356;
int gi_360;
string gs_364;

datetime g_time_376;

int init() {
   gsa_344[0] = pair_01;
   gsa_344[1] = pair_02;
   gsa_344[2] = pair_03;
   gsa_344[3] = pair_04;
   gsa_344[4] = pair_05;
   gsa_344[5] = pair_06;
   gsa_344[6] = pair_07;
   gsa_344[7] = pair_08;
   gsa_344[8] = pair_09;
   gsa_344[9] = pair_10;
   gsa_344[10] = pair_11;
   gsa_344[11] = pair_12;
   gsa_344[12] = pair_13;
   gsa_344[13] = pair_14;
   gsa_344[14] = pair_15;
   gsa_344[15] = pair_16;
   gsa_344[16] = pair_17;
   gsa_344[17] = pair_18;
   gsa_344[18] = pair_19;
   gsa_344[19] = pair_20;
   gsa_344[20] = pair_21;
   gsa_344[21] = pair_22;
   gsa_344[22] = pair_23;
   gsa_344[23] = pair_24;
   gsa_344[24] = pair_25;
   gsa_344[25] = pair_26;
   gsa_344[26] = pair_27;
   gsa_344[27] = pair_28;
   gsa_344[28] = pair_29;
   gsa_344[29] = pair_30;
   for (int index_0 = 0; index_0 < 30; index_0++) gsa_348[index_0] = StringSubstr(gsa_344[index_0], 0, 6);
   if (Period() == PERIOD_M1) gs_364 = "1M";
   if (Period() == PERIOD_M5) gs_364 = "5M";
   if (Period() == PERIOD_M15) gs_364 = "15M";
   if (Period() == PERIOD_M30) gs_364 = "30M";
   if (Period() == PERIOD_H1) gs_364 = "1H";
   if (Period() == PERIOD_H4) gs_364 = "4H";
   if (Period() == PERIOD_D1) gs_364 = "1D";
   if (Period() == PERIOD_W1) gs_364 = "1W";
   if (Period() == PERIOD_MN1) gs_364 = "1MN";
   if (base_text_size < 4) base_text_size = 4;
   if (base_text_size > 24) base_text_size = 24;
   gi_356 = 5 * base_text_size + 15;
   gi_360 = gia_352[base_text_size];
   return (0);
}

int deinit() {
   ObjectsDeleteAll();
   return (0);
}

int start() {
   string name_20;
   double color_28;
   int fontsize_36;
   double ld_40;
   
   int ind_counted_0 = IndicatorCounted();
   if (f0_1() || real_time == TRUE) {
      for (int index_12 = 0; index_12 < number_of_pairs; index_12++) {
         ObjectDelete(gsa_344[index_12] + "x");
         ObjectCreate(gsa_344[index_12] + "x", OBJ_LABEL, 0, 0, 0);
         ObjectSetText(gsa_344[index_12] + "x", gsa_348[index_12], base_text_size, "SegoeUI", White);
         ObjectSet(gsa_344[index_12] + "x", OBJPROP_XDISTANCE, startx + gi_356 + 15 + index_12 * gi_356);
         ObjectSet(gsa_344[index_12] + "x", OBJPROP_YDISTANCE, starty + gi_360 * 2 + 5);
         for (int index_16 = 0; index_16 < index_12 + 1; index_16++) {
            ObjectDelete(gsa_344[index_16] + "y");
            ObjectCreate(gsa_344[index_16] + "y", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(gsa_344[index_16] + "y", gsa_348[index_16], base_text_size, "SegoeUI", White);
            ObjectSet(gsa_344[index_16] + "y", OBJPROP_XDISTANCE, startx + gi_360);
            ObjectSet(gsa_344[index_16] + "y", OBJPROP_YDISTANCE, starty + gi_356 + index_16 * gi_360);
            name_20 = "Duetto_" + gsa_344[index_12] + "-" + gsa_344[index_16];
            ObjectDelete(name_20 + "hline");
            ObjectCreate(name_20 + "hline", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_20 + "hline", "___________", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_20 + "hline", OBJPROP_XDISTANCE, startx + gi_356 + 10 + index_12 * gi_356);
            ObjectSet(name_20 + "hline", OBJPROP_YDISTANCE, starty + 3 * gi_360 + 9 + index_16 * gi_360);
            ObjectDelete(name_20 + "vline");
            ObjectCreate(name_20 + "vline", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_20 + "vline", "|", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_20 + "vline", OBJPROP_XDISTANCE, startx + gi_356 * 2 + 10 + index_12 * gi_356);
            ObjectSet(name_20 + "vline", OBJPROP_YDISTANCE, starty + 3 * gi_360 + 5 + index_16 * gi_360);
            ObjectDelete(name_20 + "hline_");
            ObjectCreate(name_20 + "hline_", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_20 + "hline_", "___________", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_20 + "hline_", OBJPROP_XDISTANCE, startx + gi_356 + 10 + index_16 * gi_356);
            ObjectSet(name_20 + "hline_", OBJPROP_YDISTANCE, starty + 3 * gi_360 + 9 + index_12 * gi_360);
            ObjectDelete(name_20 + "vline_");
            ObjectCreate(name_20 + "vline_", OBJ_LABEL, 0, 0, 0);
            ObjectSetText(name_20 + "vline_", "|", base_text_size, "SegoeUI", C'0x20,0x20,0x20');
            ObjectSet(name_20 + "vline_", OBJPROP_XDISTANCE, startx + gi_356 * 2 + 10 + index_16 * gi_356);
            ObjectSet(name_20 + "vline_", OBJPROP_YDISTANCE, starty + 3 * gi_360 + 5 + index_12 * gi_360);
            fontsize_36 = base_text_size;
            if (gsa_344[index_12] == gsa_344[index_16]) {
               ObjectDelete(name_20);
               ObjectCreate(name_20, OBJ_LABEL, 0, 0, 0);
               ObjectSetText(name_20, "----", fontsize_36 + 2, "SegoeUI", White);
               ObjectSet(name_20, OBJPROP_XDISTANCE, startx + gi_356 + 30 + index_12 * gi_356);
               ObjectSet(name_20, OBJPROP_YDISTANCE, starty + 3 * gi_360 + 3 + index_16 * gi_360);
            }
            if (gsa_344[index_12] != gsa_344[index_16]) {
               ld_40 = 100.0 * f0_3(gsa_344[index_12], gsa_344[index_16], period, 0);
               if (ld_40 < 0.0) color_28 = f0_0(255, 0, 0, 55, 55, 55, 0, 100, MathAbs(ld_40));
               if (ld_40 > 0.0) color_28 = f0_0(0, 255, 0, 55, 55, 55, 0, 100, MathAbs(ld_40));
               if (MathAbs(ld_40) >= correlation_level) {
                  ObjectDelete(name_20);
                  ObjectCreate(name_20, OBJ_LABEL, 0, 0, 0);
                  ObjectSetText(name_20, "[ " + DoubleToStr(ld_40, 1) + " ]", fontsize_36 + 2, "SegoeUI", color_28);
                  ObjectSet(name_20, OBJPROP_XDISTANCE, startx + gi_356 + 19 + index_12 * gi_356);
                  ObjectSet(name_20, OBJPROP_YDISTANCE, starty + 3 * gi_360 + 4 + index_16 * gi_360);
                  ObjectDelete(name_20 + "_");
                  ObjectCreate(name_20 + "_", OBJ_LABEL, 0, 0, 0);
                  ObjectSetText(name_20 + "_", "[ " + DoubleToStr(ld_40, 1) + " ]", fontsize_36 + 2, "SegoeUI", color_28);
                  ObjectSet(name_20 + "_", OBJPROP_XDISTANCE, startx + gi_356 + 19 + index_16 * gi_356);
                  ObjectSet(name_20 + "_", OBJPROP_YDISTANCE, starty + 3 * gi_360 + 4 + index_12 * gi_360);
                  continue;
               }
               ObjectDelete(name_20);
               ObjectCreate(name_20, OBJ_LABEL, 0, 0, 0);
               ObjectSetText(name_20, DoubleToStr(ld_40, 1), fontsize_36, "SegoeUI", color_28);
               ObjectSet(name_20, OBJPROP_XDISTANCE, startx + gi_356 + 30 + index_12 * gi_356);
               ObjectSet(name_20, OBJPROP_YDISTANCE, starty + 3 * gi_360 + 5 + index_16 * gi_360);
               ObjectDelete(name_20 + "_");
               ObjectCreate(name_20 + "_", OBJ_LABEL, 0, 0, 0);
               ObjectSetText(name_20 + "_", DoubleToStr(ld_40, 1), fontsize_36, "SegoeUI", color_28);
               ObjectSet(name_20 + "_", OBJPROP_XDISTANCE, startx + gi_356 + 30 + index_16 * gi_356);
               ObjectSet(name_20 + "_", OBJPROP_YDISTANCE, starty + 3 * gi_360 + 5 + index_12 * gi_360);
            }
         }
      }
      ObjectDelete("Duetto_title");
      ObjectCreate("Duetto_title", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Duetto_title", "DUETTO_FX CORRELATION MATRIX v1.1 - http://www.pimpmyea.com", fontsize_36 + 2, "SegoeUI", SkyBlue);
      ObjectSet("Duetto_title", OBJPROP_XDISTANCE, startx + 15);
      ObjectSet("Duetto_title", OBJPROP_YDISTANCE, starty);
      ObjectDelete("Duetto_title2");
      ObjectCreate("Duetto_title2", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Duetto_title2", "TIMEFRAME:" + gs_364 + " - PERIOD: " + period, fontsize_36 + 2, "SegoeUI", Yellow);
      ObjectSet("Duetto_title2", OBJPROP_XDISTANCE, startx + 15);
      ObjectSet("Duetto_title2", OBJPROP_YDISTANCE, starty + gi_360 - 1);
   }
   return (0);
}

double f0_3(string a_symbol_0, string a_symbol_8, int a_period_16, int a_timeframe_20) {
   double ima_24 = iMA(a_symbol_8, a_timeframe_20, a_period_16, 0, MODE_SMA, PRICE_CLOSE, 0);
   double ima_32 = iMA(a_symbol_0, a_timeframe_20, a_period_16, 0, MODE_SMA, PRICE_CLOSE, 0);
   double ld_40 = 0;
   double ld_48 = 0;
   double ld_56 = 0;
   for (int li_64 = 0; li_64 <= a_period_16; li_64++) {
      ld_40 += (iClose(a_symbol_8, a_timeframe_20, li_64) - ima_24) * (iClose(a_symbol_0, a_timeframe_20, li_64) - ima_32);
      ld_48 += MathPow(iClose(a_symbol_8, a_timeframe_20, li_64) - ima_24, 2);
      ld_56 += MathPow(iClose(a_symbol_0, a_timeframe_20, li_64) - ima_32, 2);
   }
   ld_48 = MathSqrt(ld_48);
   ld_56 = MathSqrt(ld_56);
   double ld_68 = ld_48 * ld_56;
   double ld_ret_76 = ld_40 / ld_68;
   return (ld_ret_76);
}

int f0_1() {
   if (g_time_376 == Time[0]) return (0);
   g_time_376 = Time[0];
   return (1);
}

int f0_2(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_2(ai_0 - ld_56 * ad_40, ai_4 - ld_64 * ad_40, ai_8 - ld_72 * ad_40));
}