#property copyright "Copyright © 2008, MyAQAsuite"
#property link      "www.myaqasuite.com"

#property indicator_separate_window
#property indicator_minimum 1.0
#property indicator_maximum 400.0

extern color Chrt_BackgColor = C'0x28,0x28,0x28';
extern color Chrt_CaptionColor = clrLime;
extern color MyBarsColor = clrBlue;
extern color RequoteColor = clrRed;
extern bool UseSound_Alert_by_Requotes = TRUE;
extern string AlertRequoteSound = "expert.wav";
int G_datetime_104;
int G_datetime_112;
int G_datetime_116;
int G_datetime_128;
int Gi_unused_132 = 1;
bool Gi_136 = FALSE;
double Gda_140[140];
double Gda_144[140];
double Gda_148[140];
double Gda_152[140];
double Gda_156[70];
double Gda_160[70];
double Gda_164[70];
double Gda_168[70];
int Gi_172 = 1000;
double Gda_176[1];
double Gda_180[1];
double Gda_184[1];
double Gda_188[1];
double Gd_192 = 10.0;
int G_window_200 = 1;
int G_spread_204;
int G_spread_208;
int G_spread_212;
bool Gi_unused_216 = FALSE;

int init() {
   IndicatorShortName("DC_Checker");
   G_datetime_104 = iTime(NULL, PERIOD_M1, 0);
   G_datetime_112 = iTime(NULL, PERIOD_M30, 0);
   G_datetime_116 = iTime(NULL, PERIOD_H4, 0);
   G_datetime_128 = iTime(NULL, 0, 1);
   ArrayResize(Gda_176, Gi_172);
   ArrayResize(Gda_180, Gi_172);
   ArrayResize(Gda_184, Gi_172);
   ArrayResize(Gda_188, Gi_172);
   FillUpQuotes();
   for (int index_0 = 0; index_0 <= ArraySize(Gda_148) - 1; index_0++) {
      Gda_148[index_0] = 0;
      Gda_152[index_0] = 0;
      Gda_156[index_0] = 0;
      Gda_160[index_0] = 0;
      Gda_164[index_0] = 0;
      Gda_168[index_0] = 0;
   }
   int Li_unused_4 = 1;
   int Li_unused_8 = 50;
 
   return (0);
}

int deinit() {
   string name_12;
   int objs_total_8 = ObjectsTotal();
   for (int count_4 = 0; count_4 <= 10; count_4++) {
      for (int Li_0 = 0; Li_0 <= objs_total_8; Li_0++) {
         name_12 = ObjectName(Li_0);
         if (StringFind(name_12, "HBarBody") > -1) ObjectDelete(name_12);
         if (StringFind(name_12, "HBarHL") > -1) ObjectDelete(name_12);
      }
   }
  
   return (0);
}

int start() {
   G_window_200 = WindowFind("DC_Checker");
   DrawAllHystoryBars();
   int Li_0 = 1;
   int Li_4 = 150;
   Update_iTickSpread(Gda_140);
   DrawCharts(Gda_140, Gda_144, 150, ArraySize(Gda_140), Li_0, Gd_192, "Tick", "Tick spread hystory");
   double spread_8 = (MarketInfo(Symbol(), MODE_SPREAD))/10;
   TextInfoObject("ActualSpread", "Actual Spread: " + DoubleToStr(spread_8, 0), G_window_200, Red, 80, 10, 20, "Lucida Console");
   if (spread_8 > G_spread_204) G_spread_204 = spread_8;
   if (spread_8 > G_spread_208) G_spread_208 = spread_8;
   if (spread_8 > G_spread_212) G_spread_212 = spread_8;
   if (iTime(NULL, 0, 1) != G_datetime_128) {
      UpdateiHystoryBars();
      G_datetime_128 = iTime(NULL, 0, 1);
      DrawAllHystoryBars();
      CheckBrokersReqwortes();
   }
   if (G_datetime_104 != iTime(NULL, PERIOD_M1, 0)) {
      Update_iSpread(G_spread_204, Gda_148, Gda_152);
      G_spread_204 = 1;
      DrawCharts(Gda_148, Gda_152, 150, ArraySize(Gda_148), Li_0, 180, "M1", "Tick Spread M1 max");
      G_datetime_104 = iTime(NULL, PERIOD_M1, 0);
   }
   if (G_datetime_112 != iTime(NULL, PERIOD_M30, 0)) {
      Update_iSpread(G_spread_208, Gda_156, Gda_160);
      G_spread_208 = 1;
      DrawCharts(Gda_156, Gda_160, 150, ArraySize(Gda_156), Li_0 + Li_4, 10, "M30", "Tick Spread M30 max");
      G_datetime_112 = iTime(NULL, PERIOD_M30, 0);
   }
   if (G_datetime_116 != iTime(NULL, PERIOD_H4, 0)) {
      Update_iSpread(G_spread_212, Gda_164, Gda_168);
      G_spread_212 = 1;
      DrawCharts(Gda_164, Gda_168, 150, ArraySize(Gda_164), Li_0 + Li_4, 180, "H4", "Tick Spread H4 Max");
      G_datetime_116 = iTime(NULL, PERIOD_H4, 0);
   }
   return (0);
}

double GetAverageValue(double Ada_0[]) {
   double Ld_4 = 0;
   for (int Li_12 = ArraySize(Ada_0) - 1; Li_12 > 0; Li_12--) Ld_4 += Ada_0[Li_12];
   return (Ld_4 / ArraySize(Ada_0));
}

void Update_iTickSpread(double &Ada_0[140]) {
   for (int Li_4 = ArraySize(Ada_0) - 1; Li_4 > 0; Li_4--) {
      Ada_0[Li_4] = Ada_0[Li_4 - 1];
      Gda_144[Li_4] = Gda_144[Li_4 - 1];
   }
   Ada_0[0] = MarketInfo(Symbol(), MODE_SPREAD);
   Gda_144[0] = GetAverageValue(Ada_0);
}

void Update_iSpread(int Ai_0, double &Ada_4[140], double &Ada_8[140]) {
   for (int Li_12 = ArraySize(Ada_4) - 1; Li_12 > 0; Li_12--) {
      Ada_4[Li_12] = Ada_4[Li_12 - 1];
      Ada_8[Li_12] = Ada_8[Li_12 - 1];
   }
   Ada_4[0] = Ai_0;
   Ada_8[0] = GetAverageValue(Ada_4);
}

void UpdateiHystoryBars() {
   for (int Li_0 = Gi_172 - 1; Li_0 > 1; Li_0--) {
      Gda_176[Li_0] = Gda_176[Li_0 - 1];
      Gda_180[Li_0] = Gda_180[Li_0 - 1];
      Gda_184[Li_0] = Gda_184[Li_0 - 1];
      Gda_188[Li_0] = Gda_188[Li_0 - 1];
   }
   Gda_176[1] = NormalizeDouble(iHigh(NULL, 0, 1), Digits);
   Gda_180[1] = NormalizeDouble(iLow(NULL, 0, 1), Digits);
   Gda_184[1] = NormalizeDouble(iOpen(NULL, 0, 1), Digits);
   Gda_188[1] = NormalizeDouble(iClose(NULL, 0, 1), Digits);
}

int DrawVertLine(string A_name_0, int A_datetime_8, double A_price_12, double A_price_20, color A_color_28, int A_width_32, int A_window_36) {
   if (ObjectCreate(A_name_0, OBJ_TREND, A_window_36, A_datetime_8, A_price_12, A_datetime_8, A_price_20)) {
      ObjectSet(A_name_0, OBJPROP_COLOR, A_color_28);
      ObjectSet(A_name_0, OBJPROP_RAY, FALSE);
      ObjectSet(A_name_0, OBJPROP_BACK, TRUE);
      ObjectSet(A_name_0, OBJPROP_WIDTH, A_width_32);
      return (1);
   }
   if (GetLastError() == 4200/* OBJECT_ALREADY_EXISTS */) {
      ObjectSet(A_name_0, OBJPROP_TIME1, A_datetime_8);
      ObjectSet(A_name_0, OBJPROP_TIME2, A_datetime_8);
      ObjectSet(A_name_0, OBJPROP_PRICE1, A_price_20);
      ObjectSet(A_name_0, OBJPROP_PRICE2, A_price_12);
      ObjectSet(A_name_0, OBJPROP_COLOR, A_color_28);
      ObjectSet(A_name_0, OBJPROP_WIDTH, A_width_32);
   } else {
      Print("DrawVertLine error = ", GetLastError());
      return (0);
   }
   return (0);
}

int DrawLine(string A_name_0, int A_datetime_8, double A_price_12, int A_datetime_20, double A_price_24, color A_color_32) {
   if (ObjectCreate(A_name_0, OBJ_TREND, G_window_200, A_datetime_8, A_price_12, A_datetime_20, A_price_24)) {
      ObjectSet(A_name_0, OBJPROP_COLOR, A_color_32);
      ObjectSet(A_name_0, OBJPROP_RAY, FALSE);
      ObjectSet(A_name_0, OBJPROP_BACK, TRUE);
      return (1);
   }
   if (GetLastError() == 4200/* OBJECT_ALREADY_EXISTS */) {
      ObjectSet(A_name_0, OBJPROP_COLOR, A_color_32);
      ObjectSet(A_name_0, OBJPROP_TIME1, A_datetime_8);
      ObjectSet(A_name_0, OBJPROP_TIME2, A_datetime_20);
      ObjectSet(A_name_0, OBJPROP_PRICE1, A_price_24);
      ObjectSet(A_name_0, OBJPROP_PRICE2, A_price_12);
   } else {
      Print("DrawLine error = ", GetLastError());
      return (0);
   }
   return (0);
}

int DrawLineMain(string A_name_0, int A_datetime_8, double A_price_12, int A_datetime_20, double A_price_24, color A_color_32, int A_width_36) {
   if (ObjectCreate(A_name_0, OBJ_TREND, 0, A_datetime_8, A_price_12, A_datetime_20, A_price_24)) {
      ObjectSet(A_name_0, OBJPROP_COLOR, A_color_32);
      ObjectSet(A_name_0, OBJPROP_RAY, FALSE);
      ObjectSet(A_name_0, OBJPROP_BACK, TRUE);
      ObjectSet(A_name_0, OBJPROP_WIDTH, A_width_36);
      ObjectSet(A_name_0, OBJPROP_TIME1, A_datetime_8);
      ObjectSet(A_name_0, OBJPROP_PRICE1, A_price_12);
      ObjectSet(A_name_0, OBJPROP_TIME2, A_datetime_20);
      ObjectSet(A_name_0, OBJPROP_PRICE2, A_price_24);
      ObjectSet(A_name_0, OBJPROP_WIDTH, A_width_36);
      return (1);
   }
   if (GetLastError() == 4200/* OBJECT_ALREADY_EXISTS */) {
      ObjectSet(A_name_0, OBJPROP_TIME1, A_datetime_8);
      ObjectSet(A_name_0, OBJPROP_PRICE1, A_price_12);
      ObjectSet(A_name_0, OBJPROP_TIME2, A_datetime_20);
      ObjectSet(A_name_0, OBJPROP_PRICE2, A_price_24);
      ObjectSet(A_name_0, OBJPROP_WIDTH, A_width_36);
      ObjectSet(A_name_0, OBJPROP_COLOR, A_color_32);
      return (1);
   }
   return (0);
}

double GetMaxArray(double Ada_0[]) {
   double Ld_ret_8;
   int arr_size_4 = ArraySize(Ada_0);
   for (int index_16 = 0; index_16 < arr_size_4 - 1; index_16++)
      if (Ada_0[index_16] < Ada_0[index_16 + 1]) Ld_ret_8 = Ada_0[index_16 + 1];
   return (Ld_ret_8);
}

void DrawCharts(double Ada_0[140], double Ada_4[140], int Ai_8, int Ai_12, int Ai_16, int Ai_20, string As_24, string A_text_32) {
   int datetime_96;
   int datetime_100;
   double Ld_124;
   double Ld_132;
   double Ld_140;
   double Ld_148;
   double Ld_156;
   double Ld_164;
   double Ld_172;
   double Ld_180;
   string Ls_188;
   string Ls_196;
   double Ld_240;
   double Ld_40 = 1;
   double Ld_52 = Gd_192;
   if (GetMaxArray(Ada_4) > Gd_192) Ld_52 = GetMaxArray(Ada_4);
   double Ld_60 = 1;
   int Li_76 = 55295;
   double price_80 = 999999;
   double price_88 = 0;
   int Li_104 = WindowBarsPerChart();
   int datetime_108 = iTime(NULL, 0, 0);
   if (Gi_136) Print("bars_count=", Li_104, " bar=", datetime_108, " X=", Ai_16);
   double Ld_112 = Ld_52 - Ld_60;
   double Ld_68 = (Ai_8 - Ai_8 / 10.0) / Ld_112;
   if (Gi_136) Print("SymbolRage :", Ld_40, " ChartWideHigh:", Ld_52, " ChartWideLow:", Ld_60, " ChartHeight :", Ld_112, " my_aspect:", Ld_68);
   for (int count_120 = 0; count_120 < Ai_12; count_120++) {
      Ld_124 = Ada_0[Ai_12 - count_120];
      Ld_132 = 0;
      Ld_140 = Ada_4[Ai_12 - count_120];
      Ld_148 = 0;
      datetime_96 = iTime(NULL, 0, Ai_12 + 0 - count_120 + Ai_16);
      datetime_100 = iTime(NULL, 0, Ai_12 + 0 - (count_120 + 1) + Ai_16);
      Ld_156 = (Ld_124 - Ld_60) * Ld_68 + Ai_20;
      Ld_164 = (Ld_132 - Ld_60) * Ld_68 + Ai_20;
      Ld_172 = (Ld_140 - Ld_60) * Ld_68 + Ai_20;
      Ld_180 = (Ld_148 - Ld_60) * Ld_68 + Ai_20;
      if (Ld_156 > Ai_8 + Ai_20) Ld_156 = Ai_8 + Ai_20;
      if (Ld_156 < Ai_20) Ld_156 = Ai_20;
      if (Ld_164 < Ai_20) Ld_164 = Ai_20;
      if (Ld_164 > Ai_8 + Ai_20) Ld_164 = Ai_8 + Ai_20;
      if (Ld_172 > Ai_8 + Ai_20) Ld_172 = Ai_8 + Ai_20;
      if (Ld_172 < Ai_20) Ld_172 = Ai_20;
      if (Ld_180 < Ai_20) Ld_180 = Ai_20;
      if (Ld_180 > Ai_8 + Ai_20) Ld_180 = Ai_8 + Ai_20;
      Ls_188 = "Hysto_L1" + As_24 + DoubleToStr(count_120, 0);
      Ls_196 = "Hysto_L2" + As_24 + DoubleToStr(count_120, 0);
      DrawVertLine(Ls_196, datetime_96, Ld_180, Ld_172, Red, 1, G_window_200);
      DrawVertLine(Ls_188, datetime_96, Ld_164, Ld_156, Li_76, 4, G_window_200);
   }
   string name_204 = "Bk_Hysto" + As_24;
   string name_212 = "TF_Hysto" + As_24;
   double Ld_220 = (Gd_192 - Ld_60) * Ld_68 + Ai_20;
   int datetime_228 = iTime(NULL, 0, Ai_16);
   int datetime_232 = iTime(NULL, 0, Ai_12 + Ai_16);
   if (datetime_228 == 0) datetime_228 = iTime(NULL, 0, 0);
   if (datetime_232 == 0) datetime_232 = iTime(NULL, 0, 0);
   for (int count_236 = 0; count_236 < Gd_192; count_236++) {
      Ld_240 = count_236 * Ld_68 + Ai_20;
      DrawLine("Hline" + As_24 + DoubleToStr(count_236, 0), datetime_232, Ld_240, datetime_228, Ld_240, Gray);
   }
   if (ObjectFind(name_204) == -1) {
      if (ObjectCreate(name_204, OBJ_RECTANGLE, G_window_200, datetime_232, price_88, datetime_228, price_80) || ObjectCreate(name_212, OBJ_TEXT, G_window_200, iTime(NULL,
         0, Ai_16 + 4), Ai_8 + Ai_20)) {
         ObjectSet(name_204, OBJPROP_COLOR, Chrt_BackgColor);
         ObjectSetText(name_212, A_text_32, 8, "Lucida Console", Chrt_CaptionColor);
         ObjectSet(name_204, OBJPROP_TIME1, datetime_232);
         ObjectSet(name_204, OBJPROP_TIME2, datetime_228);
         ObjectSet(name_204, OBJPROP_PRICE1, Ai_8 + Ai_20);
         ObjectSet(name_204, OBJPROP_PRICE2, Ai_20);
         ObjectSet(name_204, OBJPROP_COLOR, Chrt_BackgColor);
         ObjectSet(name_212, OBJPROP_TIME1, iTime(NULL, 0, Ai_16 + Ai_12 / 2.0));
         ObjectSet(name_212, OBJPROP_PRICE1, Ai_8 + Ai_20 - 1);
         return;
      }
      Print("Unable create an object ", GetLastError());
      return;
   }
   ObjectSet(name_204, OBJPROP_TIME1, datetime_232);
   ObjectSet(name_204, OBJPROP_TIME2, datetime_228);
   ObjectSet(name_204, OBJPROP_PRICE1, Ai_8 + Ai_20);
   ObjectSet(name_204, OBJPROP_PRICE2, Ai_20);
   ObjectSet(name_204, OBJPROP_COLOR, Chrt_BackgColor);
   ObjectSetText(name_212, A_text_32, 8, "Lucida Console", Chrt_CaptionColor);
   ObjectSet(name_212, OBJPROP_TIME1, iTime(NULL, 0, Ai_16 + Ai_12 / 2.0));
   ObjectSet(name_212, OBJPROP_PRICE1, Ai_8 + Ai_20 - 1);
}

int TextInfoObject(string A_name_0, string A_text_8, int A_window_16, color A_color_20, int A_x_24, int A_y_28, int A_fontsize_32, string A_fontname_36) {
   if ((!ObjectCreate(A_name_0, OBJ_LABEL, A_window_16, 0, 0)) || (!ObjectSetText(A_name_0, A_text_8, A_fontsize_32, A_fontname_36, A_color_20)) || (!ObjectSet(A_name_0,
      OBJPROP_CORNER, 0)) || (!ObjectSet(A_name_0, OBJPROP_XDISTANCE, A_x_24)) || (!ObjectSet(A_name_0, OBJPROP_YDISTANCE, A_y_28))) return (0);
   return (1);
}

void DrawAllHystoryBars() {
   string Ls_4;
   string Ls_12;
   for (int Li_0 = 1; Li_0 <= Gi_172 - 1; Li_0++) {
      Ls_4 = "HBarBody" + DoubleToStr(Li_0, 0);
      Ls_12 = "HBarHL" + DoubleToStr(Li_0, 0);
      DrawLineMain(Ls_4, iTime(NULL, 0, Li_0), Gda_184[Li_0], iTime(NULL, 0, Li_0), Gda_188[Li_0], MyBarsColor, 6);
      DrawLineMain(Ls_12, iTime(NULL, 0, Li_0), Gda_176[Li_0], iTime(NULL, 0, Li_0), Gda_180[Li_0], MyBarsColor, 2);
   }
}

void FillUpQuotes() {
   double Ld_4;
   double Ld_12;
   double Ld_20;
   double Ld_28;
   for (int Li_0 = 1; Li_0 <= Gi_172 - 1; Li_0++) {
      Ld_4 = NormalizeDouble(iHigh(NULL, 0, Li_0), Digits);
      Ld_12 = NormalizeDouble(iLow(NULL, 0, Li_0), Digits);
      Ld_20 = NormalizeDouble(iOpen(NULL, 0, Li_0), Digits);
      Ld_28 = NormalizeDouble(iClose(NULL, 0, Li_0), Digits);
      Gda_176[Li_0] = Ld_4;
      Gda_180[Li_0] = Ld_12;
      Gda_184[Li_0] = Ld_20;
      Gda_188[Li_0] = Ld_28;
   }
   Print("vHigh: ", Gda_176[1], " vLow:", Gda_180[1], " vOpen:", Gda_184[1], " vClose:", Gda_188[1]);
}

void CheckBrokersReqwortes() {
   double Ld_0;
   double Ld_8;
   double Ld_16;
   double Ld_24;
   for (int Li_32 = 1; Li_32 <= Gi_172 - 1; Li_32++) {
      Ld_0 = NormalizeDouble(iHigh(NULL, 0, Li_32), Digits);
      Ld_8 = NormalizeDouble(iLow(NULL, 0, Li_32), Digits);
      Ld_16 = NormalizeDouble(iOpen(NULL, 0, Li_32), Digits);
      Ld_24 = NormalizeDouble(iClose(NULL, 0, Li_32), Digits);
      if (Gda_176[Li_32] != Ld_0 || Gda_180[Li_32] != Ld_8 || Gda_184[Li_32] != Ld_16 || Gda_188[Li_32] != Ld_24) {
         DrawRequotedBar(Li_32);
         if (UseSound_Alert_by_Requotes) PlaySound(AlertRequoteSound);
      }
   }
}

void DrawRequotedBar(int Ai_0) {
   string Ls_4 = "HBarBody" + DoubleToStr(Ai_0, 0);
   string Ls_12 = "HBarHL" + DoubleToStr(Ai_0, 0);
   DrawLineMain(Ls_4, iTime(NULL, 0, Ai_0), Gda_184[Ai_0], iTime(NULL, 0, Ai_0), Gda_188[Ai_0], RequoteColor, 6);
   DrawLineMain(Ls_12, iTime(NULL, 0, Ai_0), Gda_176[Ai_0], iTime(NULL, 0, Ai_0), Gda_180[Ai_0], RequoteColor, 2);
}
