/*
   Generated by EX4-TO-MQ4 decompiler V4.0.220.2c []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2008, Dean Malone"
#property link      "www.compassfx.com"

#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_buffers 1
#property indicator_color1 DarkGray
/*
#import "as2_updater.dll"
   int isExeRunning();
   int runAutoUpdater();
   int isInstallerRunning();
   int runAutoInstaller();
#import "Synergy.dll"
   string gGrab(string a0, string a1);
#import
*/
int gi_76 = -1;
double g_ibuf_80[];
double gda_84[10];
string gsa_88[10];
string gsa_92[10];
int g_file_96;
extern string Custom_Indicator = "Volatility ";
extern string Copyright = "© 2008, Dean Malone ";
extern string Web_Address = "www.compassfx.com";
extern string label = "--Login Information--";
extern string EMail = "";
extern string Password = "";
extern double MyLevel = 0.0;
extern string MyLevel_Help = "If MyLevel = 0.0 - defaults used (M1=0.0001,M5=0.0002,M15=0.0004,M30=0.0006,H1=0.0008,H4=0.0014,D1=0.005)";
double gd_164;
double g_ibuf_172[];
double g_period_176 = 7.0;
int g_file_184;
bool gi_188 = FALSE;
string gs_192;
bool gi_200 = FALSE;
int g_datetime_204;
string gs_208;
string gs_216;
string gs_224;
string gs_232;
int g_str2int_240;
int g_global_var_244;
int g_datetime_248;
bool gi_252 = FALSE;
int g_str2int_256 = 80;
string gs_260 = "http://www.synergyforex.com/server/server2/";
string gs_268 = "http://www.compassfx.com/server/";
string gs_276;
int gi_284 = -1;

int doLoginCheck(string as_0) {
   double lda_16[10];
   string lsa_20[10];
   string ls_unused_24;
   bool li_8 = FALSE;
   int l_file_12 = FileOpen("indicators_debug.txt", FILE_CSV|FILE_READ, ",");
   if (l_file_12 != -1) {
      if (FileSize(l_file_12) > 100000) {
         FileClose(l_file_12);
         FileDelete("indicators_debug.txt");
      } else FileClose(l_file_12);
   }
   int l_index_44 = 0;
   bool li_48 = FALSE;
   ArrayInitialize(lda_16, 2.01);
   fillGlobalBuffers(as_0);
   int li_40 = openOutputFile();
   lda_16[0] = iCustom(NULL, 0, "Synergy_TradeSignal", "verInfo_AS", 0, 0);
   lsa_20[0] = "Synergy_TradeSignal";
   lda_16[1] = iCustom(NULL, 0, "Synergy_Continuation", "verInfo_AS", 0, 0);
   lsa_20[1] = "Synergy_Continuation";
   lda_16[2] = iCustom(NULL, 0, "Synergy_Volatility", "verInfo_AS", 0, 0);
   lsa_20[2] = "Synergy_Volatility";
   lda_16[3] = iCustom(NULL, 0, "Synergy_DSR", "verInfo_AS", 0, 0);
   lsa_20[3] = "Synergy_DSR";
   lda_16[4] = iCustom(NULL, 0, "Synergy_TDI", "verInfo_AS", 0, 0);
   lsa_20[4] = "Synergy_TDI";
   lda_16[5] = iCustom(NULL, 0, "Synergy_HeikenAshi", "verInfo_AS", 0, 0);
   lsa_20[5] = "Synergy_HeikenAshi";
   lda_16[6] = iCustom(NULL, 0, "Synergy_RangeFactor", "verInfo_AS", 0, 0);
   lsa_20[6] = "Synergy_RangeFactor";
   lda_16[7] = iCustom(NULL, 0, "Synergy_MTF", "verInfo_AS", 0, 0);
   lsa_20[7] = "Synergy_MTF";
   lda_16[8] = iCustom(NULL, 0, "Synergy_TradeTargets", "verInfo_AS", 0, 0);
   lsa_20[8] = "Synergy_TradeTargets";
   for (int l_index_32 = 0; l_index_32 < gi_76; l_index_32++) {
      for (int l_index_36 = 0; l_index_36 < 9; l_index_36++) {
         if (gsa_88[l_index_32] == lsa_20[l_index_36]) {
            li_8 = TRUE;
            Print("Indicator : ", lsa_20[l_index_36], " Value : ", lda_16[l_index_36], " Latest Version : ", gda_84[l_index_32]);
            if (lda_16[l_index_36] < gda_84[l_index_32]) {
               gsa_92[l_index_44] = gsa_88[l_index_32];
               l_index_44++;
               if (MathAbs(lda_16[l_index_36] - gda_84[l_index_32]) >= 0.05) {
                  li_48 = TRUE;
                  if (!GlobalVariableCheck("rA_synergy")) GlobalVariableSet("rA_synergy", 1);
               }
            }
         }
      }
      if (!li_8) {
         gsa_92[l_index_44] = gsa_88[l_index_32];
         l_index_44++;
      }
      li_8 = FALSE;
   }
   Print("-- Advanced Synergy Version Information --");
   Print("Files that need to be updated: ", l_index_44);
   if (l_index_44 > 0) Print("Executing AutoUpdater.exe now...");
   writeOutputFile(li_40, l_index_44);
   closeOutputFile(li_40, l_index_44);
   if (l_index_44 == 0) return (0);
   int li_52 = runAutoUpdater();
   if (li_48 == FALSE) return (0);
   return (1);
}

int openOutputFile() {
   bool li_ret_4;
   FileDelete("as2_update.txt");
   int l_file_0 = FileOpen("as2_update.txt", FILE_WRITE);
   if (l_file_0 < 1) {
      Alert("Can not open as2_update.txt for writing!");
      li_ret_4 = TRUE;
   } else li_ret_4 = FALSE;
   if (li_ret_4 == FALSE) return (l_file_0);
   else return (li_ret_4);
}

void writeOutputFile(int ai_0, int ai_4) {
   FileWrite(ai_0, ai_4);
   for (int l_index_12 = 0; l_index_12 < ai_4; l_index_12++) FileWrite(ai_0, gsa_92[l_index_12] + ".ex4");
}

void closeOutputFile(int a_file_0, int ai_unused_4) {
   FileClose(a_file_0);
}

void fillGlobalBuffers(string as_0) {
   string ls_8;
   string ls_16;
   string ls_24;
   int li_32 = -1;
   int l_index_40 = 0;
   string ls_44 = "";//gGrab(as_0 + "update.php", 0);
   FileDelete("temp.txt");
   int l_file_36 = FileOpen("temp.txt", FILE_WRITE|FILE_READ);
   if (l_file_36 < 1) {
      Alert("Can not open temp text file!");
      return;
   }
   Print(ls_44);
   FileWrite(l_file_36, ls_44);
   FileSeek(l_file_36, 0, SEEK_SET);
   gi_76 = StrToDouble(FileReadString(l_file_36));
   while (true) {
      ls_24 = FileReadString(l_file_36);
      if (StringFind(ls_24, "DONE") != -1) break;
      li_32 = StringFind(ls_24, ",", 0);
      ls_8 = StringSubstr(ls_24, 0, li_32);
      ls_16 = StringSubstr(ls_24, li_32 + 1, StringLen(ls_24) - li_32);
      gda_84[l_index_40] = StrToDouble(ls_16);
      gsa_88[l_index_40] = ls_8;
      l_index_40++;
      if (!(FileIsEnding(l_file_36))) continue;
      break;
   }
   FileClose(l_file_36);
}

void logResult(string as_0, int ai_8, string as_12) {
   int l_file_20 = FileOpen("indicators_debug.txt", FILE_CSV|FILE_WRITE|FILE_READ, ",");
   if (l_file_20 < 1) {
      Print("Cannot open indicators_debug.txt for appendage!");
      return;
   }
   if (FileSeek(l_file_20, 0, SEEK_END) == FALSE) {
      Print("Problem seeking to the end of the file! Aborting...");
      return;
   }
   FileWrite(l_file_20, as_0, TimeCurrent(), ai_8, as_12);
   FileClose(l_file_20);
}

int init() {
   if (Custom_Indicator == "verInfo_AS") {
      IndicatorBuffers(1);
      SetIndexBuffer(0, g_ibuf_80);
      return (0);
   }
   if (Copyright != "98w34988suesdfe") {
      if (!IsDllsAllowed()) {
         Alert("WARNING: Must allow DLL imports. Go to Tools > Options > Expert Advisors and check \"Allow DLL imports\".");
         gi_188 = TRUE;
         return (0);
      }
      switch (Period()) {
      case PERIOD_MN1:
         gd_164 = 0.005;
         break;
      case PERIOD_W1:
         gd_164 = 0.005;
         break;
      case PERIOD_D1:
         gd_164 = 0.005;
         break;
      case PERIOD_H4:
         gd_164 = 0.0014;
         break;
      case PERIOD_H1:
         gd_164 = 0.0008;
         break;
      case PERIOD_M30:
         gd_164 = 0.0006;
         break;
      case PERIOD_M15:
         gd_164 = 0.0004;
         break;
      case PERIOD_M5:
         gd_164 = 0.0002;
         break;
      case PERIOD_M1:
         gd_164 = 0.0001;
      }
      if (Digits < 4) gd_164 = 100.0 * gd_164;
      if (MyLevel != 0.0) gd_164 = MyLevel;
      IndicatorShortName("Volatility | www.compassfx.com  ");
      SetIndexBuffer(0, g_ibuf_172);
      SetIndexStyle(0, DRAW_LINE);
      SetIndexLabel(0, "Volatility");
      SetLevelValue(0, gd_164);
      SetLevelStyle(0, 0, Red);
      gi_188 = FALSE;
      g_file_184 = FileOpen("advsynergy.bin", FILE_CSV|FILE_READ);
      if (g_file_184 < 1) {
         gi_252 = FALSE;
         if (isInstallerRunning() != 1) {
            gi_188 = TRUE;
            return (0);
         }
         runAutoInstaller();
         gi_188 = TRUE;
         return (0);
      }
      gs_208 = FileReadString(g_file_184);
      gs_216 = FileReadString(g_file_184);
      g_str2int_240 = StrToInteger(FileReadString(g_file_184));
      gs_276 = FileReadString(g_file_184);
      if (gs_276 == "") gs_276 = "x";
      FileClose(g_file_184);
      gi_252 = TRUE;
      gs_224 = gs_208;
      gs_232 = gs_216;
      if (GlobalVariableCheck("aSyn")) {
         g_global_var_244 = GlobalVariableGet("aSyn");
         g_datetime_248 = TimeLocal();
         if (g_global_var_244 > g_datetime_248) {
            gi_188 = mylogin(gs_208, gs_216);
            if (gi_284 == 1) {
               gi_188 = mylogin(gs_208, gs_216);
               gi_284 = -1;
            }
            if (gi_188) return (0);
         }
         if (g_datetime_248 - g_global_var_244 >= 1800) {
            gi_188 = mylogin(gs_208, gs_216);
            if (gi_284 == 1) {
               gi_188 = mylogin(gs_208, gs_216);
               gi_284 = -1;
            }
            if (gi_188) return (0);
         }
         if (g_datetime_248 - g_global_var_244 <= g_str2int_256) return (0);
         gi_188 = mykeepalive();
         if (gi_284 == 1) {
            gi_188 = mykeepalive();
            gi_284 = -1;
         }
         if (!(gi_188)) return (0);
         return (0);
      }
      gi_188 = mylogin(gs_208, gs_216);
      if (gi_284 == 1) {
         gi_188 = mylogin(gs_208, gs_216);
         gi_284 = -1;
      }
      return (0);
   }
   SetIndexBuffer(0, g_ibuf_172);
   return (0);
}

int start() {
   double ld_20;
   double ld_28;
   double l_ima_36;
  // if (gi_188) return (0);
   if (Custom_Indicator == "verInfo_AS") {
      g_ibuf_80[0] = 2.02;
      return (0);
   }
   if (Copyright == "98w34988suesdfe") {
      g_ibuf_172[0] = 14325.112;
      return (0);
   }
   if (Copyright != "98w34988suesdfe" && !gi_200) {
      gi_200 = TRUE;
      gs_192 = "Synergy_Volatility";
      if (checkFilename() == -1) {
         gi_188 = TRUE;
         Alert("Filename has been changed -- Indicator haulting");
         return (-1);
      }
   }
   if (!gi_188) {
      if (GlobalVariableCheck("aSyn")) {
         g_global_var_244 = GlobalVariableGet("aSyn");
         g_datetime_248 = TimeLocal();
         if (g_global_var_244 > g_datetime_248) {
            gi_188 = mylogin(gs_208, gs_216);
            if (gi_284 == 1) {
               gi_188 = mylogin(gs_208, gs_216);
               gi_284 = -1;
            }
            if (gi_188) return (0);
         }
         if (g_datetime_248 - g_global_var_244 >= 1800) {
            gi_188 = mylogin(gs_208, gs_216);
            if (gi_284 == 1) {
               gi_188 = mylogin(gs_208, gs_216);
               gi_284 = -1;
            }
            if (gi_188) return (0);
         }
         if (g_datetime_248 - g_global_var_244 > g_str2int_256) {
            gi_188 = mykeepalive();
            if (gi_284 == 1) {
               gi_188 = mykeepalive();
               gi_284 = -1;
            }
            if (gi_188) return (0);
         }
      } else {
         resetIndy();
         gi_188 = TRUE;
         return (0);
      }
   }
   if (Bars <= g_period_176) return (0);
   int l_ind_counted_16 = IndicatorCounted();
   int li_4 = Bars - g_period_176 - 1.0;
   if (l_ind_counted_16 > g_period_176) li_4 = Bars - l_ind_counted_16;
   if (Copyright == "isCalledViaMTF" && li_4 > 20) li_4 = 20;
   while (li_4 >= 0) {
      ld_28 = 0.0;
      l_ima_36 = iMA(NULL, 0, g_period_176, 0, MODE_SMA, PRICE_CLOSE, li_4);
      for (int l_count_8 = 0; l_count_8 < g_period_176; l_count_8++) {
         ld_20 = Close[li_4 + l_count_8];
         ld_28 += (ld_20 - l_ima_36) * (ld_20 - l_ima_36);
      }
      g_ibuf_172[li_4] = MathSqrt(ld_28 / g_period_176);
      li_4--;
   }
   return (0);
}

int checkFilename() {
   double l_icustom_0 = iCustom(NULL, 0, gs_192, "", "98w34988suesdfe", "", "", "", "", 0, "", 0, 0);
   if (l_icustom_0 != 14325.112) return (-1);
   else return (1);
}

int mylogin(string as_0, string as_8) {
   string ls_unused_16;
   int li_24;
   int li_28;
   int li_40;
   if (GlobalVariableCheck("rA_synergy")) {
      if (isExeRunning() != 1) {
         gi_188 = TRUE;
         return (1);
      }
      GlobalVariableDel("rA_synergy");
   }
   int li_32 = -1;
   string l_str_concat_44 = StringConcatenate(gs_260, "login_test.php");
   string l_str_concat_52 = StringConcatenate("user=", as_0, "&pass=", as_8, "&ver=2%2E1");
   string ls_60 = "";//gGrab(l_str_concat_44, l_str_concat_52);
   if (StringSubstr(ls_60, 0, 1) == "0") li_32 = 0;
   if (StringSubstr(ls_60, 0, 1) == "2") li_32 = 2;
   if (StringSubstr(ls_60, 0, 1) == "3") li_32 = 3;
   if (StringSubstr(ls_60, 0, 1) == "4") li_32 = 4;
   if (StringSubstr(ls_60, 0, 1) == "6") li_32 = 6;
   if (StringSubstr(ls_60, 0, 1) == "7") li_32 = 7;
   GlobalVariableDel("advSynSW");
   logResult("Volatility", li_32, " LOGIN");
   switch (li_32) {
   case 0:
      gi_188 = doLoginCheck(gs_260);
      if (gi_188) return (1);
      li_24 = StringFind(ls_60, "SVR=");
      li_28 = StringFind(ls_60, "DONE");
      li_40 = StringFind(ls_60, "zZD1");
      if (li_24 == -1) gs_276 = "x";
      else {
         if (li_28 == -1) gs_276 = "x";
         gs_276 = StringSubstr(ls_60, li_24 + 4, li_28 - li_24 - 4);
      }
      g_str2int_256 = StrToInteger(StringSubstr(ls_60, 4, li_40 - 4));
      if (g_str2int_256 <= 0) g_str2int_256 = 180;
      if (g_str2int_256 >= 600) g_str2int_256 = 600;
      g_datetime_204 = TimeLocal();
      gs_224 = as_0;
      gs_232 = as_8;
      GlobalVariableSet("aSyn", TimeLocal());
      g_file_184 = FileOpen("advsynergy.bin", FILE_WRITE, 8);
      if (g_file_184 < 1) {
         Print("Cannot open password cache!");
         return (0);
      }
      FileWrite(g_file_184, gs_208);
      FileWrite(g_file_184, gs_216);
      FileWrite(g_file_184, TimeLocal());
      FileWrite(g_file_184, gs_276);
      FileClose(g_file_184);
      return (0);
   case 7:
      Alert(StringSubstr(ls_60, 4, StringLen(ls_60)));
      g_datetime_204 = TimeLocal();
      gs_224 = gs_208;
      GlobalVariableSet("aSyn", TimeLocal());
      g_file_184 = FileOpen("advsynergy.bin", FILE_WRITE, 8);
      if (g_file_184 < 1) {
         Print("Cannot open password cache!");
         return (0);
      }
      FileWrite(g_file_184, gs_208);
      FileWrite(g_file_184, gs_216);
      FileWrite(g_file_184, TimeLocal());
      FileClose(g_file_184);
      return (0);
   case 6:
      Alert(StringSubstr(ls_60, 4, StringLen(ls_60)));
      if (gi_252) FileDelete("advsynergy.bin");
      return (1);
   case 2:
      Alert("Incorrect EMail -- Please check your EMail address spelling.");
      if (gi_252) FileDelete("advsynergy.bin");
      return (1);
   case 3:
      Alert("Email OK -- Incorrect Password!\n Please check your password spelling\n and make sure Caps Lock is NOT on.");
      if (gi_252) FileDelete("advsynergy.bin");
      return (1);
   case 4:
      Alert("Your account has been disabled!\n Please contact support@compassfx.com");
      if (gi_252) FileDelete("advsynergy.bin");
      return (1);
   case -1:
      if (gs_276 != "x" && gs_276 != "" && gs_268 != gs_276) gs_268 = gs_276;
      if (gs_260 == gs_268) {
         if (IsConnected()) {
            Alert("Connection Error!\n An error connecting to the Internet has occurred.\n Please close MetaTrader and re-open.");
            resetIndy();
            return (1);
         }
         g_file_96 = FileOpen("advsynergy.bin", FILE_CSV|FILE_READ);
         if (g_file_96 < 1) return (1);
         FileClose(g_file_96);
         gs_224 = as_0;
         gs_232 = as_8;
         GlobalVariableSet("aSyn", TimeLocal());
         return (0);
      }
      gs_260 = gs_268;
      gi_284 = 1;
      return (1);
   }
   return (0);
}

int mykeepalive() {
   int li_0 = -1;
   string l_str_concat_4 = StringConcatenate(gs_260, "keepalive_new.php");
   string l_str_concat_12 = StringConcatenate("user=", gs_224, "&pass=", gs_232);
   string ls_20 = "";//gGrab(l_str_concat_4, l_str_concat_12);
   if (StringSubstr(ls_20, 0, 1) == "0") li_0 = 0;
   if (StringSubstr(ls_20, 0, 1) == "1") li_0 = 1;
   if (StringSubstr(ls_20, 0, 1) == "2") li_0 = 2;
   if (StringSubstr(ls_20, 0, 1) == "3") li_0 = 3;
   if (StringSubstr(ls_20, 0, 1) == "4") li_0 = 4;
   if (StringSubstr(ls_20, 0, 1) == "5") li_0 = 5;
   if (li_0 != -1)
      if (GlobalVariableCheck("advSynSW")) GlobalVariableDel("advSynSW");
   logResult("Volatility", li_0, DoubleToStr(GlobalVariableCheck("advSynSW"), 0) + " -- KEEPALIVE");
   switch (li_0) {
   case 1:
      gi_188 = mylogin(gs_224, gs_232);
      if (gi_284 == 1) {
         gi_188 = mylogin(gs_224, gs_232);
         gi_284 = -1;
      }
      return (0);
   case 2:
      Alert("Your account has been logged into from a different computer.\n  This connection has been terminated.");
      resetIndy();
      return (1);
   case 3:
      Alert("Your username was not found in our database of logged in users.\n Please close indicators and re-open\n them to re-login.");
      resetIndy();
      return (1);
   case 4:
      Alert(StringSubstr(ls_20, 4, StringLen(ls_20)));
      GlobalVariableSet("aSyn", TimeLocal());
      break;
   case 5:
      Alert(StringSubstr(ls_20, 4, StringLen(ls_20)));
      resetIndy();
      return (1);
   case -1:
      if (gs_276 != "x" && gs_276 != "" && gs_276 != gs_268) gs_268 = gs_276;
      if (gs_260 == gs_268) {
         if (GlobalVariableCheck("advSynSW")) {
            if (TimeLocal() - GlobalVariableGet("advSynSW") <= 1200.0) break;
            if (IsConnected()) {
               Alert("Connection Error!\n An error connecting to the Internet has occurred.\n Please close MetaTrader and re-open.");
               resetIndy();
               return (1);
            }
            g_file_96 = FileOpen("advsynergy.bin", FILE_CSV|FILE_READ);
            if (g_file_96 < 1) return (1);
            FileClose(g_file_96);
            return (0);
         }
         GlobalVariableSet("advSynSW", TimeLocal());
      } else {
         gs_260 = gs_268;
         gi_284 = 1;
         return (1);
      }
      break;
   }
   GlobalVariableSet("aSyn", TimeLocal());
   return (0);
}

void resetIndy() {
   ArrayInitialize(g_ibuf_172, EMPTY_VALUE);
   ArrayInitialize(gd_164, EMPTY_VALUE);
   GlobalVariableDel("aSyn");
   GlobalVariableDel("advSynSW");
}
int isInstallerRunning(){
return(0);
}
void runAutoInstaller(){
}
int runAutoUpdater(){return(0);}
int isExeRunning(){return(0);}