/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2010, Forex Impact LLC."
#property link      "http://www.foreximpact.com"

#property indicator_chart_window

#import "FXI.dll"
   string gGrab(string a0, string a1);
#import "corr_upd.dll"
   string returnReg(string a0, string a1);
#import

extern string lbl1 = "Copyright 2010, Forex Impact LLC.";
extern color OutsideColor = Red;
extern color ValidColor = Green;
extern int ADRDays = 5;
double gd_96;
bool gi_unused_104 = FALSE;
bool gi_108 = FALSE;
bool gi_112 = FALSE;
bool gi_116 = FALSE;
bool gi_120 = FALSE;
bool gi_124 = FALSE;
bool gi_128 = FALSE;
bool gi_132 = FALSE;
bool gi_136 = FALSE;
int gi_140 = 4;
double gda_unused_144[20];
double gda_148[500][2];
double gda_152[500][2];
double gda_156[500][2];
double gda_160[500][2];
double gda_164[500][2];
double gda_168[500][2];
double gda_172[500][2];
double gda_176[500][2];
int gi_unused_180 = -1;
int g_index_184 = 0;
double gda_188[5000];
int gi_224;
int gi_228;
int gi_232;
int gi_236;
int gi_240;
int gi_244;
int gi_248;
int gi_252;
int gi_256 = 500;
bool gi_260 = FALSE;
int g_day_264 = -1;
int g_file_268;
bool gi_272 = FALSE;
string gs_unused_276;

int loginStart() {
   int l_str2int_0;
   bool li_4;
   int li_8;
   g_file_268 = FileOpen("fxi_d.bin", FILE_CSV|FILE_READ);
   if (g_file_268 < 1) li_4 = FALSE;
   else {
      l_str2int_0 = StrToInteger(FileReadString(g_file_268));
      FileClose(g_file_268);
      li_4 = TRUE;
   }
   if (TimeLocal() - l_str2int_0 >= 259200 || li_4 == FALSE) {
      li_8 = doLogin();
      switch (li_8) {
      case 0:
         Alert("Delphi Scalping -- Logged In");
         g_file_268 = FileOpen("fxi_d.bin", FILE_WRITE, 8);
         if (g_file_268 < 1) {
            Print("Cannot open password cache!");
            return (0);
         }
         FileWrite(g_file_268, TimeLocal());
         FileClose(g_file_268);
         break;
      case 1:
         Alert("Invalid key provided!! Please re-install the software with the correct key.");
         gi_272 = TRUE;
         break;
      case 4:
         Alert("Your account has been disabled! Please contact support@fximpact.com");
         gi_272 = TRUE;
         break;
      case 5:
         Alert("Server error!! Please make sure you are connected to the Internet and try again.");
         gi_272 = TRUE;
         break;
      case 6:
         Alert("No key found in your registry (bad install)! Please re-install the product.");
         gi_272 = TRUE;
      }
   }
   return (0);
}

int doLogin() {
   string ls_unused_0;
   string ls_unused_8;
   string ls_unused_16;
   string ls_24 = returnReg("Software\\FXI\\Delphi", "key");
   if (ls_24 == "") return (6);
   string ls_32 = "key=" + ls_24;
   string ls_40 = gGrab("http://www.foreximpact.com/delphi/d_login.php", ls_32);
   if (StringSubstr(ls_40, 0, 1) == "0") {
      gs_unused_276 = ls_40;
      return (0);
   }
   if (StringSubstr(ls_40, 0, 1) == "1") return (1);
   if (StringSubstr(ls_40, 0, 1) == "4") return (4);
   return (5);
}

int init() {
  // loginStart();
 //  if (gi_272) return (0);
   gi_unused_104 = FALSE;
   gi_108 = TRUE;
   gi_112 = TRUE;
   gi_116 = TRUE;
   gi_120 = TRUE;
   gi_124 = TRUE;
   gi_128 = TRUE;
   gi_132 = TRUE;
   gi_136 = TRUE;
   if (Period() >= PERIOD_M5) gi_108 = FALSE;
   if (Period() >= PERIOD_M15) gi_112 = FALSE;
   if (Period() >= PERIOD_M30) gi_116 = FALSE;
   if (Period() >= PERIOD_H1) gi_120 = FALSE;
   if (Period() >= PERIOD_H4) gi_124 = FALSE;
   if (Period() >= PERIOD_D1) gi_128 = FALSE;
   if (Period() >= PERIOD_W1) gi_132 = FALSE;
   if (Period() >= PERIOD_MN1) gi_136 = FALSE;
   ArrayInitialize(gda_148, 0.0);
   ArrayInitialize(gda_152, 0.0);
   ArrayInitialize(gda_156, 0.0);
   ArrayInitialize(gda_160, 0.0);
   ArrayInitialize(gda_164, 0.0);
   ArrayInitialize(gda_168, 0.0);
   ArrayInitialize(gda_172, 0.0);
   ArrayInitialize(gda_176, 0.0);
   if (gi_108) loadArray(PERIOD_M5, gda_148, 1);
   if (gi_112) loadArray(PERIOD_M15, gda_152, 1);
   if (gi_116) loadArray(PERIOD_M30, gda_156, 1);
   if (gi_120) loadArray(PERIOD_H1, gda_160, 1);
   if (gi_124) loadArray(PERIOD_H4, gda_164, 1);
   if (gi_128) loadArray(PERIOD_D1, gda_168, 1);
   if (gi_132) loadArray(PERIOD_W1, gda_172, 1);
   if (gi_136) loadArray(PERIOD_MN1, gda_176, 1);
   return (0);
}

int deinit() {
   ObjectsDeleteAll(0, OBJ_HLINE);
   return (0);
}

int start() {
 //  if (gi_272) return (0);
   int l_ind_counted_0 = IndicatorCounted();
   ArrayInitialize(gda_188, -1);
   g_index_184 = 0;
   if (gi_224 != iBars(Symbol(), PERIOD_M5) && gi_108) {
      loadArray(PERIOD_M5, gda_148, 0);
      gi_224 = iBars(Symbol(), PERIOD_M5);
   }
   if (gi_228 != iBars(Symbol(), PERIOD_M15) && gi_112) {
      loadArray(PERIOD_M15, gda_152, 0);
      gi_228 = iBars(Symbol(), PERIOD_M15);
   }
   if (gi_232 != iBars(Symbol(), PERIOD_M30) && gi_116) {
      loadArray(PERIOD_M30, gda_156, 0);
      gi_232 = iBars(Symbol(), PERIOD_M30);
   }
   if (gi_236 != iBars(Symbol(), PERIOD_H1) && gi_120) {
      loadArray(PERIOD_H1, gda_160, 0);
      gi_236 = iBars(Symbol(), PERIOD_H1);
   }
   if (gi_240 != iBars(Symbol(), PERIOD_H4) && gi_124) {
      loadArray(PERIOD_H4, gda_164, 0);
      gi_240 = iBars(Symbol(), PERIOD_H4);
   }
   if (gi_244 != iBars(Symbol(), PERIOD_D1) && gi_128) {
      loadArray(PERIOD_D1, gda_168, 0);
      gi_244 = iBars(Symbol(), PERIOD_D1);
   }
   if (gi_248 != iBars(Symbol(), PERIOD_W1) && gi_132) {
      loadArray(PERIOD_W1, gda_172, 0);
      gi_248 = iBars(Symbol(), PERIOD_W1);
   }
   if (gi_252 != iBars(Symbol(), PERIOD_MN1) && gi_136) {
      loadArray(PERIOD_MN1, gda_176, 0);
      gi_252 = iBars(Symbol(), PERIOD_MN1);
   }
   doTheRest();
   return (0);
}

void loadArray(int a_timeframe_0, double &ada_4[500][2], bool ai_8) {
   int l_datetime_20;
   string lsa_28[9999];
   string l_name_32;
   int li_12 = 0;
   int li_16 = 1;
   if (ai_8) {
      for (li_12 = gi_256; li_12 >= 0; li_12--) {
         if (iHighest(Symbol(), a_timeframe_0, MODE_HIGH, gi_140 * 2 + 2, li_12) == li_12 + gi_140 + 1) {
            ada_4[li_16][0] = iHigh(Symbol(), a_timeframe_0, li_12 + gi_140 + 1);
            ada_4[li_16][1] = iTime(Symbol(), a_timeframe_0, li_12 + gi_140 + 1);
            li_16++;
         }
         if (iLowest(Symbol(), a_timeframe_0, MODE_LOW, gi_140 * 2 + 2, li_12) == li_12 + gi_140 + 1) {
            ada_4[li_16][0] = iLow(Symbol(), a_timeframe_0, li_12 + gi_140 + 1);
            ada_4[li_16][1] = iTime(Symbol(), a_timeframe_0, li_12 + gi_140 + 1);
            li_16++;
         }
      }
      Print("Number of lines created for timefame " + a_timeframe_0 + " -- ", li_16 - 1);
      ada_4[0][0] = li_16;
   } else {
      li_16 = ada_4[0][0];
      l_datetime_20 = iTime(Symbol(), a_timeframe_0, gi_256);
      for (int li_24 = 1; li_24 < li_16; li_24++) {
         if (ada_4[li_24][1] < l_datetime_20) {
            Print("Deleting Line On Period " + a_timeframe_0 + " With Time " + TimeToStr(ada_4[li_24][1], TIME_DATE|TIME_MINUTES));
            removeAndShift(ada_4, li_24, li_16);
            li_16--;
            li_24--;
         }
      }
      if (iHighest(Symbol(), a_timeframe_0, MODE_HIGH, gi_140 * 2 + 2, 0) == gi_140 + 1) {
         ada_4[li_16][0] = iHigh(Symbol(), a_timeframe_0, gi_140 + 1);
         ada_4[li_16][1] = iTime(Symbol(), a_timeframe_0, gi_140 + 1);
         li_16++;
      }
      if (iLowest(Symbol(), a_timeframe_0, MODE_LOW, gi_140 * 2 + 2, 0) == gi_140 + 1) {
         ada_4[li_16][0] = iLow(Symbol(), a_timeframe_0, gi_140 + 1);
         ada_4[li_16][1] = iTime(Symbol(), a_timeframe_0, gi_140 + 1);
         li_16++;
      }
      ada_4[0][0] = li_16;
   }
   int l_index_40 = 0;
   for (int li_44 = 0; li_44 < ObjectsTotal(); li_44++) {
      l_name_32 = ObjectName(li_44);
      if (StringFind(l_name_32, "_" + a_timeframe_0 + "_") != -1) {
         lsa_28[l_index_40] = l_name_32;
         l_index_40++;
      }
   }
   for (li_44 = 0; li_44 < l_index_40; li_44++) ObjectDelete(lsa_28[li_44]);
   for (li_44 = 0; li_44 < ada_4[0][0]; li_44++) {
      if (!gi_260) {
         if (ObjectFind("ScalpLine_" + a_timeframe_0 + "_" + ada_4[li_44][1]) == -1) {
            ObjectCreate("ScalpLine_" + a_timeframe_0 + "_" + ada_4[li_44][1], OBJ_HLINE, 0, ada_4[li_44][1], ada_4[li_44][0]);
            ObjectSet("ScalpLine_" + a_timeframe_0 + "_" + ada_4[li_44][1], OBJPROP_COLOR, OutsideColor);
         }
      }
      gda_188[g_index_184] = ada_4[li_44][0];
      g_index_184++;
   }
}

void removeAndShift(double &ada_0[500][2], int ai_4, int ai_8) {
   for (int li_12 = ai_4; li_12 < ai_8; li_12++) {
      if (li_12 == ai_8 - 1) {
         ada_0[li_12][0] = 0;
         ada_0[li_12][1] = 0;
      } else {
         ada_0[li_12][0] = ada_0[li_12 + 1][0];
         ada_0[li_12][1] = ada_0[li_12 + 1][1];
      }
   }
}

void doTheRest() {
   string l_name_12;
   double ld_0 = 0;
   g_day_264 = TimeDay(Time[0]);
   for (int li_8 = 1; li_8 <= ADRDays; li_8++) ld_0 += iHigh(Symbol(), PERIOD_D1, li_8) - iLow(Symbol(), PERIOD_D1, li_8);
   gd_96 = ld_0 / ADRDays;
   if (!gi_260) {
      for (li_8 = 0; li_8 < ObjectsTotal(); li_8++) {
         l_name_12 = ObjectName(li_8);
         if (ObjectGet(l_name_12, OBJPROP_PRICE1) < iOpen(Symbol(), PERIOD_D1, 0) + gd_96 && ObjectGet(l_name_12, OBJPROP_PRICE1) > iOpen(Symbol(), PERIOD_D1, 0) - gd_96 &&
            ObjectType(l_name_12) == 1) ObjectSet(l_name_12, OBJPROP_COLOR, ValidColor);
      }
   }
}