/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "ForexScanner Quantum Elite"
#property link      "http://www.forexscanners.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red

string gs_unused_76 = "Last Modified: 2009.06.26 18:45";
double g_ibuf_84[];
double g_ibuf_88[];
extern bool AlertOn = TRUE;
int gi_unused_96 = 0;
double gd_100 = 0.0;
double gd_108 = 0.0;
double gd_116 = 0.0;
double gd_124 = 0.0;
double gd_132 = 0.0;
double gd_140 = 0.0;
double gd_148 = 0.0;
double gd_156 = 0.0;
double g_point_164 = 0.0001;
int gi_unused_172 = -1;
int g_period_176 = 50;
double gd_unused_180 = 0.15;
int gi_188 = 2;
int gi_192 = 0;
int gi_196 = 15;
int gi_unused_200 = 5000;
//int gi_204 = D'25.08.2039 13:00';
//string gs_208 = "Your trail/subscription period is over!";
int g_bars_216 = 0;
int g_bars_220 = 0;
int g_bars_224 = 0;
int g_bars_228 = 0;
int gi_232 = 500;

int init() {
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexLabel(0, "BUY");
   SetIndexLabel(1, "SELL");
   SetIndexBuffer(0, g_ibuf_88);
   SetIndexBuffer(1, g_ibuf_84);
   SetIndexArrow(1, 234);
   SetIndexArrow(0, 233);
   if (Point == 0.00001) g_point_164 = 0.0001;
   else {
      if (Point == 0.001) g_point_164 = 0.01;
      else g_point_164 = Point;
   }
   Draw_Pivots();
   return (0);
}

int deinit() {
   DeletePrints();
   DrawFlat();
   DeletePivots();
   return (0);
}

int start() {

   int li_20;
   string ls_24;
   /*
   if (TimeCurrent() >= gi_204) {
      Alert(gs_208);
      return (0);
   }
   */
   int li_0 = 0;
   double ld_4 = 0;
   li_0 = GetTrend(0, ld_4);
   if (li_0 == 1) {
      DeletePrints();
      DrawBuy();
   }
   if (li_0 == 2) {
      DeletePrints();
      DrawSell();
   }
   if (li_0 == 0) {
      DeletePrints();
      DrawFlat();
   }
   DisplayAlerts();
   Draw_Pivots();
   int li_unused_12 = 0;
   for (int li_16 = 5000; li_16 >= 1; li_16--) {
      li_20 = GetSignal(li_16);
      g_ibuf_88[li_16] = 0;
      g_ibuf_84[li_16] = 0;
      if (li_20 == 1) g_ibuf_88[li_16] = Low[li_16] - 5.0 * g_point_164;
      if (li_20 == 2) g_ibuf_84[li_16] = High[li_16] + 5.0 * g_point_164;
   }
   if (AlertOn) {
      ls_24 = "";
      if (g_ibuf_88[1] > 0.0 && g_ibuf_88[2] == 0.0) {
         ls_24 = "ForexScanner Quantum Elite" + " " + Symbol() + " " + Period() + " Min -  Green Arrow UP - Possible Buy Entry";
         AlertOnce(ls_24, 1);
      }
      if (g_ibuf_84[1] > 0.0 && g_ibuf_84[2] == 0.0) {
         ls_24 = "ForexScanner Quantum Elite" + " " + Symbol() + " " + Period() + " Min -  Red Arrow DOWN - Possible Sell Entry";
         AlertOnce(ls_24, 2);
      }
   }
   return (0);
}

int GetSignal(int ai_0) {
   double l_ima_4 = 0;
   double l_ima_12 = 0;
   double l_ima_20 = 0;
   double l_ima_28 = 0;
   l_ima_4 = iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, ai_0);
   l_ima_12 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_CLOSE, ai_0);
   l_ima_20 = iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, ai_0 + 1);
   l_ima_28 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_CLOSE, ai_0 + 1);
   double l_ima_36 = 0;
   double l_ima_44 = 0;
   double l_ima_52 = 0;
   l_ima_36 = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, ai_0);
   l_ima_44 = iMA(NULL, 0, 21, 0, MODE_EMA, PRICE_CLOSE, ai_0);
   l_ima_52 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, ai_0);
   if (l_ima_4 > l_ima_12 && l_ima_20 < l_ima_28 && l_ima_52 > l_ima_44 && l_ima_44 > l_ima_36) return (1);
   if (l_ima_4 < l_ima_12 && l_ima_20 > l_ima_28 && l_ima_36 > l_ima_44 && l_ima_44 > l_ima_52) return (2);
   return (0);
}

int AlertOnce(string as_0, int ai_8) {
   switch (ai_8) {
   case 1:
      if (g_bars_216 == 0 || g_bars_216 < Bars) {
         Alert(as_0);
         g_bars_216 = Bars;
         return (1);
      }
      break;
   case 2:
      if (g_bars_220 == 0 || g_bars_220 < Bars) {
         Alert(as_0);
         g_bars_220 = Bars;
         return (1);
      }
      break;
   case 3:
      if (g_bars_224 == 0 || g_bars_224 < Bars) {
         Alert(as_0);
         g_bars_224 = Bars;
         return (1);
      }
      break;
   case 4:
      if (g_bars_228 == 0 || g_bars_228 < Bars) {
         Alert(as_0);
         g_bars_228 = Bars;
         return (1);
      }
      break;
   }
   return (0);
}

void Draw_Pivots() {
   double lda_0[1][6];
   double ld_4;
   double ld_12;
   double ld_20;
   ArrayCopyRates(lda_0, Symbol(), PERIOD_D1);
   if (DayOfWeek() == 1) {
      if (TimeDayOfWeek(iTime(Symbol(), PERIOD_D1, 1)) == 5) {
         ld_4 = lda_0[1][4];
         ld_12 = lda_0[1][3];
         ld_20 = lda_0[1][2];
      } else {
         for (int li_28 = 5; li_28 >= 0; li_28--) {
            if (TimeDayOfWeek(iTime(Symbol(), PERIOD_D1, li_28)) == 5) {
               ld_4 = lda_0[li_28][4];
               ld_12 = lda_0[li_28][3];
               ld_20 = lda_0[li_28][2];
            }
         }
      }
   } else {
      ld_4 = lda_0[1][4];
      ld_12 = lda_0[1][3];
      ld_20 = lda_0[1][2];
   }
   gd_100 = ld_12 - ld_20;
   gd_108 = (ld_12 + ld_20 + ld_4) / 3.0;
   gd_116 = ld_12 + 2.0 * (gd_108 - ld_20);
   gd_124 = gd_108 + (2.0 * gd_108 - ld_20 - (2.0 * gd_108 - ld_12));
   gd_132 = 2.0 * gd_108 - ld_20;
   gd_140 = 2.0 * gd_108 - ld_12;
   gd_148 = gd_108 - (2.0 * gd_108 - ld_20 - (2.0 * gd_108 - ld_12));
   gd_156 = ld_20 - 2.0 * (ld_12 - gd_108);
   drawLine(gd_116, "R3", DarkGreen, 0, 0);
   drawLabel("Resistance 3", gd_116, DarkGreen);
   drawLine(gd_124, "R2", DarkGreen, 0, 0);
   drawLabel("Resistance 2", gd_124, DarkGreen);
   drawLine(gd_132, "R1", DarkGreen, 0, 0);
   drawLabel("Resistance 1", gd_132, DarkGreen);
   drawLine(gd_108, "PIVIOT", DimGray, 1, 1);
   drawLabel("Piviot level", gd_108, DimGray);
   drawLine(gd_140, "S1", Maroon, 0, 0);
   drawLabel("Support 1", gd_140, Maroon);
   drawLine(gd_148, "S2", Maroon, 0, 0);
   drawLabel("Support 2", gd_148, Maroon);
   drawLine(gd_156, "S3", Maroon, 0, 0);
   drawLabel("Support 3", gd_156, Maroon);
}

void drawLabel(string a_name_0, double a_price_8, color a_color_16) {
   if (ObjectFind(a_name_0) != 0) {
      ObjectCreate(a_name_0, OBJ_TEXT, 0, Time[10], a_price_8);
      ObjectSetText(a_name_0, a_name_0, 8, "Arial", CLR_NONE);
      ObjectSet(a_name_0, OBJPROP_COLOR, a_color_16);
      return;
   }
   ObjectMove(a_name_0, 0, Time[10], a_price_8);
}

void drawLine(double a_price_0, string a_name_8, color a_color_16, int ai_20, int a_width_24) {
   if (ObjectFind(a_name_8) == -1) {
      ObjectCreate(a_name_8, OBJ_HLINE, 0, Time[0], a_price_0, Time[0], a_price_0);
      if (ai_20 == 1) ObjectSet(a_name_8, OBJPROP_STYLE, STYLE_SOLID);
      else ObjectSet(a_name_8, OBJPROP_STYLE, STYLE_DOT);
      ObjectSet(a_name_8, OBJPROP_COLOR, a_color_16);
      ObjectSet(a_name_8, OBJPROP_WIDTH, a_width_24);
   } else {
      ObjectDelete(a_name_8);
      ObjectCreate(a_name_8, OBJ_HLINE, 0, Time[0], a_price_0, Time[0], a_price_0);
      if (ai_20 == 1) ObjectSet(a_name_8, OBJPROP_STYLE, STYLE_SOLID);
      else ObjectSet(a_name_8, OBJPROP_STYLE, STYLE_DOT);
      ObjectSet(a_name_8, OBJPROP_COLOR, a_color_16);
      ObjectSet(a_name_8, OBJPROP_WIDTH, a_width_24);
   }
   ObjectsRedraw();
}

void CPrint(int ai_0, int ai_4, string a_text_8, int a_fontsize_16 = 10, color a_color_20 = 65535, int ai_24 = 5, int a_y_28 = 20, int ai_32 = 80, int ai_36 = 15) {
   bool li_40 = FALSE;
   string ls_44 = "CPrint_Line" + ai_0 + "_" + ai_4;
   for (int l_objs_total_52 = ObjectsTotal(); l_objs_total_52 >= 0; l_objs_total_52--)
      if (StringFind(ObjectName(l_objs_total_52), ls_44, 0) > -1) li_40 = TRUE;
   if (li_40) {
      ObjectSetText(ls_44, a_text_8, a_fontsize_16, "Arial Bold", a_color_20);
      ObjectsRedraw();
      return;
   }
   ObjectCreate(ls_44, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(ls_44, a_text_8, a_fontsize_16, "Arial Bold", a_color_20);
   ObjectSet(ls_44, OBJPROP_XDISTANCE, ai_24 + (ai_4 - 1) * ai_32);
   if (ai_0 == 1) {
      ObjectSet(ls_44, OBJPROP_YDISTANCE, a_y_28);
      return;
   }
   ObjectSet(ls_44, OBJPROP_YDISTANCE, a_y_28 + ai_36 * (ai_0 - 1));
}

void DeletePivots() {
   ObjectDelete("S1");
   ObjectDelete("S2");
   ObjectDelete("S3");
   ObjectDelete("R1");
   ObjectDelete("R2");
   ObjectDelete("R3");
   ObjectDelete("PIVIOT");
   ObjectDelete("Support 1");
   ObjectDelete("Support 2");
   ObjectDelete("Support 3");
   ObjectDelete("Piviot level");
   ObjectDelete("Resistance 1");
   ObjectDelete("Resistance 2");
   ObjectDelete("Resistance 3");
}

void DrawBuy() {
   CPrint(2, 1, "Trend Direction = ", 18, Green, 5, 10);
   DrawArrow(1, 220, 8, 20, ".", 32768);
}

void DrawSell() {
   CPrint(2, 1, "Trend Direction = ", 18, Red, 5, 10);
   DrawArrow(2, 220, 5, 20, ".", 255);
}

void DrawFlat() {
   CPrint(2, 1, "Trend Direction = ", 18, White, 5, 10);
   CPrint(2, 3, "FLAT", 18, Silver, 5, 10, 105);
}

void DisplayAlerts() {
   CPrint2(3, 1, "Average Daily Range: " + CalcDailyRange() + " pips", 10, White, 5, 25);
   CPrint2(5, 1, "ForexScanner Quantum Elite", 8, White, 5, 25);
   CPrint2(6, 1, "http://www.forexscanners.com", 8, White, 5, 25);
}

void CPrint2(int ai_0, int ai_4, string a_text_8, int a_fontsize_16 = 10, color a_color_20 = 65535, int ai_24 = 5, int a_y_28 = 20, int ai_32 = 80, int ai_36 = 15) {
   bool li_40 = FALSE;
   string ls_44 = "CPrint_Line" + ai_0 + "_" + ai_4;
   for (int l_objs_total_52 = ObjectsTotal(); l_objs_total_52 >= 0; l_objs_total_52--)
      if (StringFind(ObjectName(l_objs_total_52), ls_44, 0) > -1) li_40 = TRUE;
   if (li_40) {
      ObjectSetText(ls_44, a_text_8, a_fontsize_16, "Arial", a_color_20);
      ObjectsRedraw();
      return;
   }
   ObjectCreate(ls_44, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(ls_44, a_text_8, a_fontsize_16, "Arial", a_color_20);
   ObjectSet(ls_44, OBJPROP_XDISTANCE, ai_24 + (ai_4 - 1) * ai_32);
   if (ai_0 == 1) {
      ObjectSet(ls_44, OBJPROP_YDISTANCE, a_y_28);
      return;
   }
   ObjectSet(ls_44, OBJPROP_YDISTANCE, a_y_28 + ai_36 * (ai_0 - 1));
}

int CalcDailyRange() {
   int li_0 = 0;
   int li_ret_4 = 0;
   RefreshRates();
   for (li_0 = 1; li_0 <= 10; li_0++) {
      RefreshRates();
      li_ret_4 = li_ret_4 + (iHigh(NULL, PERIOD_D1, li_0) - iLow(NULL, PERIOD_D1, li_0)) / g_point_164;
   }
   li_ret_4 /= 10;
   return (li_ret_4);
}

void DeletePrints() {
   string l_name_4;
   int l_objs_total_0 = ObjectsTotal();
   for (int li_12 = ObjectsTotal() - 1; li_12 >= 0; li_12--) {
      l_name_4 = ObjectName(li_12);
      if (StringFind(l_name_4, "CPrint_", 0) > -1) ObjectDelete(l_name_4);
      if (StringFind(l_name_4, "L_", 0) > -1) ObjectDelete(l_name_4);
      ObjectsRedraw();
   }
}

void DrawArrow(int ai_0 = 0, int ai_4 = 5, int ai_8 = 5, int ai_12 = 20, string as_16 = ".", int ai_24 = 16711680) {
   if (ai_0 == 1) {
      Label("A_" + gi_232, ai_4, ai_8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 1, ai_8 + 2, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 1, ai_8 + 2, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 2, ai_8 + 4, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4, ai_8 + 4, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 2, ai_8 + 4, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 3, ai_8 + 6, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 2, ai_8 + 6, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4, ai_8 + 6, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 2, ai_8 + 6, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 3, ai_8 + 6, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 4, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 3, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 - 2, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 2, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 3, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4 + 4, ai_8 + 8, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4, ai_8 + 10, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4, ai_8 + 12, as_16, ai_12, ai_24);
      gi_232++;
      Label("A_" + gi_232, ai_4, ai_8 + 14, as_16, ai_12, ai_24);
      gi_232++;
      return;
   }
   Label("A_" + gi_232, ai_4, ai_8 + 4, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4, ai_8 + 6, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4, ai_8 + 8, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 4, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 3, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 2, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 2, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 3, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 4, ai_8 + 10, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 3, ai_8 + 12, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 2, ai_8 + 12, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4, ai_8 + 12, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 2, ai_8 + 12, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 3, ai_8 + 12, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 2, ai_8 + 14, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4, ai_8 + 14, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 2, ai_8 + 14, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 - 1, ai_8 + 16, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4 + 1, ai_8 + 16, as_16, ai_12, ai_24);
   gi_232++;
   Label("A_" + gi_232, ai_4, ai_8 + 18, as_16, ai_12, ai_24);
   gi_232++;
}

void Label(string as_0, int a_x_8, int a_y_12, string a_text_16 = "-", int a_fontsize_24 = 42, color a_color_28 = 65535, string a_fontname_32 = "Arial") {
   ObjectCreate("L_" + as_0, OBJ_LABEL, 0, 0, 0);
   ObjectSet("L_" + as_0, OBJPROP_COLOR, a_color_28);
   ObjectSet("L_" + as_0, OBJPROP_XDISTANCE, a_x_8);
   ObjectSet("L_" + as_0, OBJPROP_YDISTANCE, a_y_12);
   ObjectSetText("L_" + as_0, a_text_16, a_fontsize_24, a_fontname_32, a_color_28);
   ObjectsRedraw();
}

int GetTrend(int ai_0, double &ad_4) {
   bool li_12 = TRUE;
   bool li_16 = TRUE;
   for (int li_20 = ai_0; li_20 <= gi_196 + ai_0; li_20++) {
      if (GetSMAAngle(li_20) >= 0.0) li_16 = FALSE;
      if (GetSMAAngle(li_20) <= 0.0) li_12 = FALSE;
   }
   ad_4 = GetSMAAngle(ai_0);
   if (li_12 == TRUE && li_16 == FALSE) return (1);
   if (li_12 == FALSE && li_16 == TRUE) return (2);
   return (0);
}

double GetSMAAngle(int ai_0) {
   if (gi_192 >= gi_188) {
      Print("Error: EndEMAShift >= StartEMAShift");
      gi_188 = 6;
      gi_192 = 0;
   }
   int li_28 = IndicatorCounted();
   if (li_28 < 0) return (-1);
   if (li_28 > 0) li_28--;
   int li_32 = Bars - li_28;
   double ld_unused_36 = 0.0349065556;
   double ld_44 = 10000.0;
   string ls_52 = StringSubstr(Symbol(), 3, 3);
   if (ls_52 == "JPY") ld_44 = 100.0;
   int li_60 = gi_188 - gi_192;
   ld_44 /= li_60;
   double l_ima_4 = iMA(NULL, 0, g_period_176, 0, MODE_SMA, PRICE_MEDIAN, ai_0 + gi_192);
   double l_ima_12 = iMA(NULL, 0, g_period_176, 0, MODE_SMA, PRICE_MEDIAN, ai_0 + gi_188);
   double ld_ret_20 = ld_44 * (l_ima_4 - l_ima_12) / 2.0;
   return (ld_ret_20);
}