/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2009, Forex Impact LLC."
#property link      "http://www.foreximpact.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green

#import "FXI.dll"
   string gGrab(string a0, string a1);
#import "corr_upd.dll"
   string returnReg(string a0, string a1);
   void runUpdater();
#import

extern string Copyright = "Copyright 2009 ForexImpact.com";
extern string Indicator = "FXI-CorrLines - 2 Currency Lines";
extern string CorrPair = "GBPUSD";
extern string UseTimeFrame = "D1";
extern int Mode = 0;
extern string modelbl = "0 - Close; 1 - Open; 2 - High; 3 - Low";
extern color DistanceLineColor = White;
extern color LabelColor = White;
extern color AlertColor = Red;
extern int DistanceForAlert = 20;
extern int DistanceLineWidth = 2;
int g_str2dbl_140 = 7;
double g_ibuf_144[];
double g_ibuf_148[];
int gi_unused_152 = 0;
double g_open_156;
double g_iopen_164;
double gd_172 = 0.0;
double gd_180 = 0.0;
bool gi_unused_188 = TRUE;
int gi_192;
int g_time_196 = -1;
int gi_200 = EMPTY;
double g_point_204;
int g_file_220;
bool gi_224 = FALSE;
double gda_unused_228[];
string gs_232;
double gd_240 = 1.3;

string StrToUpper(string as_0) {
   int li_8;
   for (int li_12 = 0; li_12 < StringLen(as_0); li_12++) {
      li_8 = StringGetChar(as_0, li_12);
      if (li_8 >= 'a' && li_8 <= 'z') as_0 = StringSetChar(as_0, li_12, li_8 - 32);
   }
   return (as_0);
}

int isPair(string a_symbol_0) {
   double l_bid_8 = MarketInfo(a_symbol_0, MODE_BID);
   double l_ask_16 = MarketInfo(a_symbol_0, MODE_ASK);
   double l_point_24 = MarketInfo(a_symbol_0, MODE_POINT);
   int l_digits_32 = MarketInfo(a_symbol_0, MODE_DIGITS);
   int l_spread_36 = MarketInfo(a_symbol_0, MODE_SPREAD);
   if (l_bid_8 <= 0.0 && l_ask_16 <= 0.0 && l_point_24 <= 0.0 && l_digits_32 <= 0 && l_spread_36 <= 0) return (0);
   return (1);
}

int loginStart() {
   int l_str2int_0;
   bool li_4;
   int li_8;
   int li_16;
   string ls_20;
   double l_str2dbl_28;
   g_file_220 = FileOpen("fxi_c.bin", FILE_CSV|FILE_READ);
   if (g_file_220 < 1) li_4 = FALSE;
   else {
      l_str2int_0 = StrToInteger(FileReadString(g_file_220));
      FileClose(g_file_220);
      li_4 = TRUE;
   }
   if (TimeLocal() - l_str2int_0 >= 259200 || li_4 == FALSE) {
      li_8 = doLogin();
      switch (li_8) {
      case 0:
         Alert("The Correlation Code -- Logged In");
         g_file_220 = FileOpen("fxi_c.bin", FILE_WRITE, 8);
         if (g_file_220 < 1) {
            Print("Cannot open password cache!");
            return (0);
         }
         FileWrite(g_file_220, TimeLocal());
         FileClose(g_file_220);
         /*
         li_16 = StringFind(gs_232, "-", 0);
         if (li_16 == -1) return (0);
         ls_20 = StringSubstr(gs_232, li_16 + 1);
         l_str2dbl_28 = StrToDouble(ls_20);
         Print("Global Version -- ", gd_240);
         Print("Retrieved Version -- ", l_str2dbl_28);
         if (gd_240 != l_str2dbl_28) dlVersions();
         */
         break;
      case 1:
         Alert("Invalid key provided!! Please re-install the software with the correct key.");
         gi_224 = TRUE;
         break;
      case 4:
         Alert("Your account has been disabled! Please contact support@fximpact.com");
         gi_224 = TRUE;
         break;
      case 5:
         Alert("Server error!! Please make sure you are connected to the Internet and try again.");
         gi_224 = TRUE;
         break;
      case 6:
         Alert("No key found in your registry (bad install)! Please re-install the product.");
         gi_224 = TRUE;
      }
   }
   return (0);
}

int doLogin() {
/*
   string ls_unused_0;
   string ls_unused_8;
   string ls_unused_16;
   string ls_24 = returnReg("Software\\FXI\\Correlation", "key");
   if (ls_24 == "") return (6);
   string ls_32 = "key=" + ls_24;
   string ls_40 = gGrab("http://www.foreximpact.com/correlation/c_login.php", ls_32);
   if (StringSubstr(ls_40, 0, 1) == "0") {
      gs_232 = ls_40;
      return (0);
   }
   if (StringSubstr(ls_40, 0, 1) == "1") return (1);
   if (StringSubstr(ls_40, 0, 1) == "4") return (4);
   return (5);
   */
   return(0);
}
/*
void dlVersions() {
   string ls_0 = "http://www.foreximpact.com/correlation-upd/versions.txt";
   string ls_8 = gGrab(ls_0, "");
   int l_file_16 = FileOpen("corr_update.txt", FILE_CSV|FILE_WRITE, ",");
   if (l_file_16 < 1) Print("Can NOT load the versions.txt file! Not updating indicators...");
   FileWrite(l_file_16, ls_8);
   FileClose(l_file_16);
   runUpdater();
}
*/

int init() {
   loginStart();
   if (gi_224) return (0);
   IndicatorShortName("Correlation Lines -- " + Symbol() + " / " + CorrPair);
   SetIndexBuffer(0, g_ibuf_144);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(1, g_ibuf_148);
   SetIndexStyle(1, DRAW_LINE);
   if (!isPair(CorrPair)) Print("Invalid Pair! -- " + CorrPair);
   setTimeFrame(UseTimeFrame);
   if (g_str2dbl_140 < 1 || g_str2dbl_140 > 10) {
      Alert("Timeframe must be between 1 and 10");
      gi_unused_188 = FALSE;
      return (0);
   }
   if (Period() == PERIOD_M5 && g_str2dbl_140 <= 1) {
      Alert("Timeframe must be above 1 on a 5M chart -- Defaulting To \'2\'");
      g_str2dbl_140 = 2;
   }
   if (Period() == PERIOD_M15 && g_str2dbl_140 <= 2) {
      Alert("Timeframe must be above 2 on a 15M chart -- Defaulting To \'3\'");
      g_str2dbl_140 = 3;
   }
   if (Period() == PERIOD_M30 && g_str2dbl_140 <= 3) {
      Alert("Timeframe must be above 3 on a 30M chart -- Defaulting To \'4\'");
      g_str2dbl_140 = 4;
   }
   if (Period() == PERIOD_H1 && g_str2dbl_140 <= 4) {
      Alert("Timeframe must be above 4 on a 1H chart -- Defaulting To \'5\'");
      g_str2dbl_140 = 5;
   }
   if (Period() == PERIOD_H4 && g_str2dbl_140 <= 5) {
      Alert("Timeframe must be above 5 on a 4H chart -- Defaulting To \'6\'");
      g_str2dbl_140 = 6;
   }
   if (Period() == PERIOD_D1 && g_str2dbl_140 <= 6) {
      Alert("Timeframe must be above 6 on a Daily chart -- Defaulting To \'7\'");
      g_str2dbl_140 = 7;
   }
   if (Period() == PERIOD_W1 && g_str2dbl_140 <= 7) {
      Alert("Timeframe must be above 7 on a Weekly chart -- Defaulting To \'8\'");
      g_str2dbl_140 = 8;
   }
   if (Period() == PERIOD_MN1 && g_str2dbl_140 <= 8) {
      Alert("Timeframe must be above 8 on a Monthly chart -- Defaulting To \'9\'");
      g_str2dbl_140 = 9;
   }
   if (g_str2dbl_140 == 1) gi_192 = 1;
   if (g_str2dbl_140 == 2) gi_192 = 5;
   if (g_str2dbl_140 == 3) gi_192 = 15;
   if (g_str2dbl_140 == 4) gi_192 = 30;
   if (g_str2dbl_140 == 5) gi_192 = 60;
   if (g_str2dbl_140 == 6) gi_192 = 240;
   if (g_str2dbl_140 == 7) gi_192 = 1440;
   if (g_str2dbl_140 == 8) gi_192 = 10080;
   if (g_str2dbl_140 == 9) gi_192 = 43200;
   if (g_str2dbl_140 == 10) gi_192 = 518400;
   return (0);
}

int deinit() {
   ObjectsDeleteAll(gi_200);
   return (0);
}

int start() {
   int l_shift_0;
   int li_32;
   if (gi_224) return (0);
   if (gi_200 == EMPTY) gi_200 = WindowFind("Correlation Lines -- " + Symbol() + " / " + CorrPair);
   int l_ind_counted_4 = IndicatorCounted();
   int li_8 = Bars - l_ind_counted_4;
   if (li_8 > 0) li_8--;
   if (li_8 < 0) return (-1);
   double ld_16 = 1;
   double ld_24 = 1;
   g_point_204 = Point;
   if (MathAbs(MarketInfo(CorrPair, MODE_DIGITS) - MarketInfo(Symbol(), MODE_DIGITS)) != 0.0) {
      li_32 = MathAbs(MarketInfo(CorrPair, MODE_DIGITS) - MarketInfo(Symbol(), MODE_DIGITS));
      if (MarketInfo(CorrPair, MODE_DIGITS) > MarketInfo(Symbol(), MODE_DIGITS)) {
         if (li_32 == 1) ld_24 = 0.1;
         else {
            if (li_32 == 2) ld_24 = 0.01;
            else {
               if (li_32 == 3) ld_24 = 0.001;
               else {
                  if (li_32 == 4) ld_24 = 0.0001;
                  else {
                     if (li_32 == 5) ld_24 = 0.0001;
                     else
                        if (li_32 == 6) ld_24 = 0.00001;
                  }
               }
            }
         }
         g_point_204 = MarketInfo(CorrPair, MODE_POINT);
      } else {
         if (li_32 == 1) ld_16 = 0.1;
         else {
            if (li_32 == 2) ld_16 = 0.01;
            else {
               if (li_32 == 3) ld_16 = 0.001;
               else {
                  if (li_32 == 4) ld_16 = 0.0001;
                  else {
                     if (li_32 == 5) ld_16 = 0.0001;
                     else
                        if (li_32 == 6) ld_16 = 0.00001;
                  }
               }
            }
         }
         g_point_204 = MarketInfo(Symbol(), MODE_POINT);
      }
   }
   for (int li_12 = li_8; li_12 >= 0; li_12--) {
      if (g_time_196 == -1) {
         if (!((Period() < PERIOD_D1 && TimeHour(Time[li_12]) == 0 && TimeHour(Time[li_12 + 1]) != 0) || (Period() >= PERIOD_D1 && TimeDay(Time[li_12]) < TimeDay(Time[li_12 +
            1])))) continue;
         g_time_196 = Time[li_12];
      }
      l_shift_0 = iBarShift(CorrPair, 0, Time[li_12], TRUE);
      if (l_shift_0 > -1) {
         if (Time[li_12] >= g_time_196 + 60 * gi_192) {
            g_time_196 = Time[li_12];
            gd_180 = Close[li_12];
            g_iopen_164 = iOpen(CorrPair, 0, l_shift_0);
            g_open_156 = Open[li_12];
         }
         if (Mode == 0) {
            gd_172 = Close[li_12] - g_open_156;
            gd_180 = iClose(CorrPair, 0, l_shift_0) - g_iopen_164;
         } else {
            if (Mode == 1) {
               gd_172 = Open[li_12] - g_open_156;
               gd_180 = iOpen(CorrPair, 0, l_shift_0) - g_iopen_164;
            } else {
               if (Mode == 2) {
                  gd_172 = High[li_12] - g_open_156;
                  gd_180 = iHigh(CorrPair, 0, l_shift_0) - g_iopen_164;
               } else {
                  if (Mode == 3) {
                     gd_172 = Low[li_12] - g_open_156;
                     gd_180 = iLow(CorrPair, 0, l_shift_0) - g_iopen_164;
                  }
               }
            }
         }
         g_ibuf_144[li_12] = gd_172 * ld_24;
         g_ibuf_148[li_12] = gd_180 * ld_16;
      }
   }
   if (ObjectFind("Separation_" + CorrPair) == -1) {
      ObjectCreate("Separation_" + CorrPair, OBJ_TREND, gi_200, Time[0], 0, Time[0], 0);
      ObjectSet("Separation_" + CorrPair, OBJPROP_RAY, FALSE);
      ObjectSet("Separation_" + CorrPair, OBJPROP_COLOR, DistanceLineColor);
      ObjectSet("Separation_" + CorrPair, OBJPROP_WIDTH, DistanceLineWidth);
   }
   if (g_ibuf_144[0] > g_ibuf_148[0]) {
      ObjectSet("Separation_" + CorrPair, OBJPROP_PRICE1, g_ibuf_144[0]);
      ObjectSet("Separation_" + CorrPair, OBJPROP_PRICE2, g_ibuf_148[0]);
   }
   if (g_ibuf_144[0] < g_ibuf_148[0]) {
      ObjectSet("Separation_" + CorrPair, OBJPROP_PRICE1, g_ibuf_148[0]);
      ObjectSet("Separation_" + CorrPair, OBJPROP_PRICE2, g_ibuf_144[0]);
   }
   if (g_ibuf_144[0] == g_ibuf_148[0]) {
      ObjectSet("Separation_" + CorrPair, OBJPROP_PRICE1, g_ibuf_148[0]);
      ObjectSet("Separation_" + CorrPair, OBJPROP_PRICE2, g_ibuf_148[0]);
   }
   ObjectSet("Separation_" + CorrPair, OBJPROP_TIME1, Time[0]);
   ObjectSet("Separation_" + CorrPair, OBJPROP_TIME2, Time[0]);
   if (MathAbs(g_ibuf_144[0] - g_ibuf_148[0]) / g_point_204 > DistanceForAlert) ObjectSet("Separation_" + CorrPair, OBJPROP_COLOR, AlertColor);
   else ObjectSet("Separation_" + CorrPair, OBJPROP_COLOR, DistanceLineColor);
   if (Digits == 3 || Digits == 5) {
      if (MathAbs(g_ibuf_144[0] - g_ibuf_148[0]) / g_point_204 / 10.0 > DistanceForAlert) ObjectSet("Separation_" + CorrPair, OBJPROP_COLOR, AlertColor);
      else ObjectSet("Separation_" + CorrPair, OBJPROP_COLOR, DistanceLineColor);
   }
   if (ObjectFind("DistanceLabel_" + CorrPair) == -1) {
      ObjectCreate("DistanceLabel_" + CorrPair, OBJ_LABEL, gi_200, 0, 0, 0, 0);
      ObjectSet("DistanceLabel_" + CorrPair, OBJPROP_XDISTANCE, 5);
      ObjectSet("DistanceLabel_" + CorrPair, OBJPROP_YDISTANCE, 15);
   }
   if (Digits == 3 || Digits == 5) ObjectSetText("DistanceLabel_" + CorrPair, "Distance Apart (In Pips) -- " + DoubleToStr(MathAbs(g_ibuf_148[0] - g_ibuf_144[0]) / g_point_204 / 10.0, 0), 8, "Tahoma", LabelColor);
   else ObjectSetText("DistanceLabel_" + CorrPair, "Distance Apart (In Pips) -- " + DoubleToStr(MathAbs(g_ibuf_148[0] - g_ibuf_144[0]) / g_point_204, 0), 8, "Tahoma", LabelColor);
   return (0);
}

void setTimeFrame(string as_0) {
   as_0 = StrToUpper(as_0);
   if (StringFind(as_0, "M1", 0) != -1) {
      g_str2dbl_140 = 1;
      return;
   }
   if (StringFind(as_0, "M5", 0) != -1) {
      g_str2dbl_140 = 2;
      return;
   }
   if (StringFind(as_0, "M15", 0) != -1) {
      g_str2dbl_140 = 3;
      return;
   }
   if (StringFind(as_0, "M30", 0) != -1) {
      g_str2dbl_140 = 4;
      return;
   }
   if (StringFind(as_0, "H1", 0) != -1) {
      g_str2dbl_140 = 5;
      return;
   }
   if (StringFind(as_0, "H4", 0) != -1) {
      g_str2dbl_140 = 6;
      return;
   }
   if (StringFind(as_0, "D", 0) != -1) {
      g_str2dbl_140 = 7;
      return;
   }
   if (StringFind(as_0, "W", 0) != -1) {
      g_str2dbl_140 = 8;
      return;
   }
   if (StringFind(as_0, "MN", 0) != -1) {
      g_str2dbl_140 = 9;
      return;
   }
   if (StringFind(as_0, "Y", 0) != -1) {
      g_str2dbl_140 = 10;
      return;
   }
   g_str2dbl_140 = StrToDouble(as_0);
}