/*
   Generated by EX4-TO-MQ4 decompiler LITE V4.0.427.3 [-]
   Website: https://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2012, Andrea Salvatore"
#property link      "http://www.pimpmyea.com"

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 C'0x40,0x00,0x00'
#property indicator_color2 Black
#property indicator_color3 C'0x00,0x40,0x00'
#property indicator_color4 Black
#property indicator_color5 White
#property indicator_color6 White
#property indicator_color7 Yellow
#property indicator_color8 Yellow
#property indicator_width1 17
#property indicator_level1 50.0
#property indicator_width2 17
#property indicator_level2 60.0
#property indicator_width3 17
#property indicator_level3 40.0
#property indicator_width4 17
#property indicator_level4 70.0
#property indicator_width5 2
#property indicator_level5 30.0
#property indicator_width6 1
#property indicator_level6 55.0
#property indicator_width7 2
#property indicator_level7 45.0

extern int period = 120;
extern string instrument_1 = "EURUSD";
extern string instrument_2 = "USDCHF";
extern int smoothing = 2;
extern double first_pair_lot = 1.0;
extern bool show_spread = TRUE;
extern bool show_spread_ma = FALSE;
extern int MA_period = 24;
extern int MA_mode = 2;
extern bool show_text_interface = TRUE;
extern bool show_crosses = TRUE;
extern int bar = 1;
extern bool show_labels = TRUE;
extern int text_size = 10;
extern string text_font = "Segoe UI";
double gda_152[];
double gda_156[];
double gda_160[];
double gda_164[];
double gda_168[];
double gda_172[];
double gda_176[];
double gda_180[];
int gi_184;
double gd_188;
int gi_196;
string gs_200;
int gi_208;
int gi_212;
double gd_216;
double gd_224;
double gd_232;
double gd_240;
double gd_248;
double gd_256;
double gd_264;
double gd_272;
double gd_280;
int gi_288 = Blue;
string gs_duettofx_292 = "DuettoFX";
bool gi_300 = FALSE;

int init() {
   IndicatorBuffers(8);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(0, gda_152);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(1, gda_160);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   SetIndexBuffer(2, gda_156);
   SetIndexStyle(3, DRAW_HISTOGRAM);
   SetIndexBuffer(3, gda_164);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexBuffer(4, gda_168);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexBuffer(5, gda_172);
   if (show_spread) {
      SetIndexStyle(6, DRAW_LINE);
      SetIndexBuffer(6, gda_176);
   }
   if (show_spread_ma) {
      SetIndexStyle(7, DRAW_LINE);
      SetIndexBuffer(7, gda_180);
   }
   SetIndexDrawBegin(0, period);
   IndicatorShortName(gs_duettofx_292);
   gi_184 = Digits;
   if (gi_184 == 3 || gi_184 == 5) gd_188 = 10.0 * Point;
   else gd_188 = Point;
   Comment("");
   return (0);
}

int deinit() {
   ObjectsDeleteAll();
   Comment("");
   return (0);
}

int start() {
   double ld_12;
   string ls_20;
   string ls_28;
   int li_36;
   int li_40;
   gi_196 = WindowFind(gs_duettofx_292);
   int li_4 = IndicatorCounted();
   if (Bars <= period) return (0);
   if (li_4 < 1) {
      for (int li_0 = 1; li_0 <= period; li_0++) {
         gda_152[Bars - li_0] = 0.0;
         gda_156[Bars - li_0] = 0.0;
         gda_160[Bars - li_0] = 0.0;
         gda_164[Bars - li_0] = 0.0;
         gda_168[Bars - li_0] = 0.0;
         gda_172[Bars - li_0] = 0.0;
         gda_176[Bars - li_0] = 0.0;
         gda_180[Bars - li_0] = 0.0;
      }
   }
   li_0 = Bars - period - 1;
   if (li_4 >= period) li_0 = Bars - li_4 - 1;
   while (li_0 >= 0) {
      gda_168[li_0] = 0;
      gda_172[li_0] = 0;
      for (int li_8 = 0; li_8 < smoothing; li_8++) {
         gda_168[li_0] += iRSI(instrument_1, 0, period, PRICE_CLOSE, li_0 + li_8);
         gda_172[li_0] += iRSI(instrument_2, 0, period, PRICE_CLOSE, li_0 + li_8);
      }
      gda_168[li_0] = gda_168[li_0] / smoothing;
      gda_172[li_0] = gda_172[li_0] / smoothing;
      gda_176[li_0] = gda_168[li_0] - gda_172[li_0] + 50.0;
      ld_12 = gda_168[li_0] - gda_172[li_0];
      if (ld_12 > 0.0) {
         gda_156[li_0] = gda_168[li_0];
         gda_164[li_0] = gda_172[li_0];
         gda_152[li_0] = EMPTY_VALUE;
         gda_160[li_0] = EMPTY_VALUE;
      } else {
         gda_156[li_0] = EMPTY_VALUE;
         gda_164[li_0] = EMPTY_VALUE;
         gda_152[li_0] = gda_172[li_0];
         gda_160[li_0] = gda_168[li_0];
      }
      if (gda_168[li_0 + 1 + bar] < gda_172[li_0 + 1 + bar] && gda_168[li_0 + bar] > gda_172[li_0 + bar]) {
         ls_20 = gs_duettofx_292 + instrument_1 + instrument_2 + "BUY" + ((Time[li_0 + bar]));
         if (show_crosses) {
            ObjectCreate(ls_20, OBJ_VLINE, 0, Time[li_0 + bar], 0);
            ObjectSet(ls_20, OBJPROP_BACK, TRUE);
            ObjectSet(ls_20, OBJPROP_TIME1, Time[li_0 + bar]);
            ObjectSet(ls_20, OBJPROP_COLOR, Green);
            ObjectSet(ls_20, OBJPROP_STYLE, STYLE_DOT);
         }
         gi_208 = iTime(Symbol(), 0, li_0 + bar);
      }
      if (gda_168[li_0 + 1 + bar] > gda_172[li_0 + 1 + bar] && gda_168[li_0 + bar] < gda_172[li_0 + bar]) {
         ls_28 = gs_duettofx_292 + instrument_1 + instrument_2 + "SELL" + ((Time[li_0 + bar]));
         if (show_crosses) {
            ObjectCreate(ls_28, OBJ_VLINE, 0, Time[li_0 + bar], 0);
            ObjectSet(ls_28, OBJPROP_BACK, TRUE);
            ObjectSet(ls_28, OBJPROP_TIME1, Time[li_0 + bar]);
            ObjectSet(ls_28, OBJPROP_COLOR, Maroon);
            ObjectSet(ls_28, OBJPROP_STYLE, STYLE_DOT);
         }
         gi_212 = iTime(Symbol(), 0, li_0 + bar);
      }
      li_0--;
   }
   li_0 = Bars - period - 1;
   if (li_4 >= period) li_0 = Bars - li_4 - 1;
   while (li_0 >= 0) {
      gda_180[li_0] = iMAOnArray(gda_176, 0, MA_period, 0, MA_mode, li_0);
      li_0--;
   }
   if (show_text_interface) {
      if (gi_208 > gi_212) {
         gd_224 = iClose(instrument_1, 0, iBarShift(instrument_1, 0, gi_208, FALSE));
         gd_232 = iClose(instrument_2, 0, iBarShift(instrument_2, 0, gi_208, FALSE));
         gd_240 = iClose(instrument_1, 0, 0) - gd_224;
         gd_248 = gd_232 - iClose(instrument_2, 0, 0);
      } else {
         gd_224 = iClose(instrument_1, 0, iBarShift(instrument_1, 0, gi_212, FALSE));
         gd_232 = iClose(instrument_2, 0, iBarShift(instrument_2, 0, gi_212, FALSE));
         gd_240 = gd_224 - iClose(instrument_1, 0, 0);
         gd_248 = iClose(instrument_2, 0, 0) - gd_232;
      }
      gd_240 /= gd_188;
      gd_248 /= gd_188;
      li_36 = period * Period() / 60;
      li_40 = li_36 / 24;
      li_36 %= 24;
      gd_256 = f0_2(instrument_1, PERIOD_D1, li_40, 0);
      gd_264 = f0_2(instrument_2, PERIOD_D1, li_40, 0);
      gd_272 = 100.0 * f0_3(instrument_1, instrument_2, period, 0);
      gd_280 = first_pair_lot * (gd_256 / gd_264);
      gd_216 = gd_240 * first_pair_lot + gd_248 * gd_280;
      gs_200 = "\n              DUETTO FX v1.0" 
         + "\n         by www.pimpmyea.com\n" + "   ------------------------------------------------\n" + "   Spread on: " + instrument_1 + " - " + instrument_2 
         + "\n" 
         + "   Period: " + period + " bars (" + li_40 + " days " + li_36 + " hours)\n" + "   Period Correlation: " + DoubleToStr(gd_272, 0) + "% \n" + "   ------------------------------------------------\n" + "   " + instrument_1 + " ADR: " + DoubleToStr(gd_256, 2) + "   pips\n   " + instrument_1 + " Lot Size: " + DoubleToStr(first_pair_lot, 2) + " lots \n" + "   " + instrument_1 + " Entry Level @ " + DoubleToStr(gd_224, Digits) 
         + "\n" 
         + "   " + instrument_1 + " Gain: " + DoubleToStr(gd_240, 2) + " pips \n" + "   ------------------------------------------------\n" + "   " + instrument_2 + " ADR: " + DoubleToStr(gd_264, 2) + "   pips\n   " + instrument_2 + " Lot Size: " + DoubleToStr(gd_280, 2) + " lots \n" + "   " + instrument_2 + " Entry Level @ " + DoubleToStr(gd_232, Digits) 
         + "\n" 
      + "   " + instrument_2 + " Gain: " + DoubleToStr(gd_248, 2) + " pips\n" + "   -------------------------------------------------\n" + "   Total Normalized Pips: " + DoubleToStr(gd_216,
         2) + " pips";
      Comment(gs_200);
      if (gda_176[0] < 50.0) gi_288 = f0_0(255, 0, 0, 0, 0, 0, 0, 4.61512, MathLog(MathAbs(gda_176[0] - 50.0)));
      if (gda_176[0] > 50.0) gi_288 = f0_0(0, 255, 0, 0, 0, 0, 0, 4.61512, MathLog(MathAbs(gda_176[0] - 50.0)));
      if (Seconds() % 5 == 0) {
         for (int li_44 = 0; li_44 < 8; li_44++) {
            for (int li_48 = 0; li_48 < 10; li_48++) {
               ObjectDelete(gs_duettofx_292 + "background" + li_44 + li_48);
               ObjectDelete(gs_duettofx_292 + "background" + li_44 + ((li_48 + 1)));
               ObjectDelete(gs_duettofx_292 + "background" + li_44 + ((li_48 + 2)));
               ObjectCreate(gs_duettofx_292 + "background" + li_44 + li_48, OBJ_LABEL, 0, 0, 0);
               ObjectSetText(gs_duettofx_292 + "background" + li_44 + li_48, "n", 30, "Wingdings", gi_288);
               ObjectSet(gs_duettofx_292 + "background" + li_44 + li_48, OBJPROP_XDISTANCE, 20 * li_44);
               ObjectSet(gs_duettofx_292 + "background" + li_44 + li_48, OBJPROP_YDISTANCE, 23 * li_48 + 7);
            }
         }
      }
   }
   if (show_labels) {
      ObjectDelete(gs_duettofx_292 + "Pair1");
      ObjectCreate(gs_duettofx_292 + "Pair1", OBJ_TEXT, gi_196, TimeCurrent(), gda_168[0]);
      ObjectSetText(gs_duettofx_292 + "Pair1", "                           " + instrument_1 + ": " + DoubleToStr(gda_168[0], 2), text_size, text_font, Lime);
      ObjectDelete(gs_duettofx_292 + "Pair2");
      ObjectCreate(gs_duettofx_292 + "Pair2", OBJ_TEXT, gi_196, TimeCurrent(), gda_172[0]);
      ObjectSetText(gs_duettofx_292 + "Pair2", "                           " + instrument_2 + ": " + DoubleToStr(gda_172[0], 2), text_size, text_font, Red);
      ObjectDelete(gs_duettofx_292 + "Spread");
      ObjectCreate(gs_duettofx_292 + "Spread", OBJ_TEXT, gi_196, TimeCurrent(), gda_176[0]);
      ObjectSetText(gs_duettofx_292 + "Spread", "                         SPREAD: " + DoubleToStr(gda_176[0] - 50.0, 2), text_size, text_font, Yellow);
   }
   return (0);
}

int f0_2(string as_0, int ai_8, int ai_12, int ai_16) {
   double ld_20;
   double ld_28 = ai_16 + ai_12;
   while (ai_16 < ld_28) {
      ld_20 += MathPow(10, Digits - 1) * (iHigh(as_0, ai_8, ai_16) - iLow(as_0, ai_8, ai_16));
      ai_16++;
   }
   return (MathRound(ld_20 / ai_12));
}

double f0_3(string as_0, string as_8, int ai_16, int ai_20) {
   double ld_24 = iMA(as_8, ai_20, ai_16, 0, MODE_SMA, PRICE_CLOSE, 0);
   double ld_32 = iMA(as_0, ai_20, ai_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 <= ai_16; li_64++) {
      ld_40 += (iClose(as_8, ai_20, li_64) - ld_24) * (iClose(as_0, ai_20, li_64) - ld_32);
      ld_48 += MathPow(iClose(as_8, ai_20, li_64) - ld_24, 2);
      ld_56 += MathPow(iClose(as_0, ai_20, li_64) - ld_32, 2);
   }
   ld_48 = MathSqrt(ld_48);
   ld_56 = MathSqrt(ld_56);
   double ld_68 = ld_48 * ld_56;
   double ld_76 = ld_40 / ld_68;
   return (ld_76);
}

int f0_1(int ai_0, int ai_4, int ai_8) {
   int li_12 = MathMin(255, MathAbs(ai_0)) + 256.0 * MathMin(255, MathAbs(ai_4)) + 65536.0 * MathMin(255, MathAbs(ai_8));
   return (li_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_1(ai_0 - ld_56 * ad_40, ai_4 - ld_64 * ad_40, ai_8 - ld_72 * ad_40));
}