
#property copyright "Copyright © 2006-2010, Sergey Kravchuk. http://forextools.com.ua"
#property link      "http://forextools.com.ua"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Black
#property indicator_color2 Black
#property indicator_color3 Black
#property indicator_color4 Black
#property indicator_color5 Black
#property indicator_color6 Black

extern int RoundTo = 10;
extern int MaxTicksCnt = 1000;
extern color ColorTicksBid = DodgerBlue;
extern color ColorTicksBidRounded = LightBlue;
extern color ColorTicksAsk = DeepPink;
extern color ColorTicksAskRounded = Pink;
extern color ColorOpenPriceLine = Green;
extern color ColorLimitPriceLine = Gray;
extern bool WriteHistoryToFile = FALSE;
extern bool BarMode = FALSE;
int G_file_116 = -1;
int Gi_120 = 0;
int Gi_128;
string Gs_132;
double Gd_140 = 0.0;
bool Gi_148 = FALSE;
string Gs_152 = "ft.Ticker: ";
string Gsa_160[7] = {"Buy", "Sell", "BuyLimit", "SellLimit", "BuyStop", "SellStop", "Balance",
   "Credit"};
double G_ibuf_164[];
double G_ibuf_168[];
double G_ibuf_172[];
double G_ibuf_176[];
double G_ibuf_180[];
double G_ibuf_184[];
string Gs_tickbid_188 = "TickBid";
int G_digits_196;
int Gi_200;
double G_point_204;
color G_color_212;
string Gsa_216[256];
int G_acc_number_220 = 0;
int G_timeframe_224 = 0;
int Gi_228 = -1;
int Gi_unused_232 = 0;
string Gs_unused_236 = "";
string Gsa_244[256];

void Clear() {
   for (int Li_0 = ObjectsTotal() - 1; Li_0 >= 0; Li_0--)
      if (StringFind(ObjectName(Li_0), Gs_152) == 0) ObjectDelete(ObjectName(Li_0));
}

int init() {
   string Ls_4;
   int Lia_12[13];
   G_digits_196 = MarketInfo(Symbol(), MODE_DIGITS);
   G_point_204 = MarketInfo(Symbol(), MODE_POINT);
   Gi_148 = MarketInfo(Symbol(), MODE_TIME) > Time[0] + 60 * Period();
   for (int index_0 = Bars - 1; index_0 >= 0; index_0--) {
      G_ibuf_164[index_0] = EMPTY_VALUE;
      G_ibuf_168[index_0] = EMPTY_VALUE;
      G_ibuf_172[index_0] = EMPTY_VALUE;
      G_ibuf_180[index_0] = EMPTY_VALUE;
      G_ibuf_176[index_0] = EMPTY_VALUE;
      G_ibuf_184[index_0] = EMPTY_VALUE;
   }
   for (index_0 = 0; index_0 < 256; index_0++) Gsa_216[index_0] = CharToStr(index_0);
   Print(Gsa_216[102] + Gsa_216[116] + Gsa_216[46] + Gsa_216[84] + Gsa_216[105] + Gsa_216[99] + Gsa_216[107] + Gsa_216[101] + Gsa_216[114] + Gsa_216[32] + Gsa_216[118] +
      Gsa_216[49] + Gsa_216[46] + Gsa_216[48] + Gsa_216[48] + Gsa_216[32] + Gsa_216[67] + Gsa_216[111] + Gsa_216[112] + Gsa_216[121] + Gsa_216[114] + Gsa_216[105] + Gsa_216[103] +
      Gsa_216[104] + Gsa_216[116] + Gsa_216[32] + Gsa_216[169] + Gsa_216[32] + Gsa_216[50] + Gsa_216[48] + Gsa_216[48] + Gsa_216[54] + Gsa_216[45] + Gsa_216[50] + Gsa_216[48] +
      Gsa_216[49] + Gsa_216[49] + Gsa_216[46] + Gsa_216[32] + Gsa_216[83] + Gsa_216[101] + Gsa_216[114] + Gsa_216[103] + Gsa_216[101] + Gsa_216[121] + Gsa_216[32] + Gsa_216[75] +
      Gsa_216[114] + Gsa_216[97] + Gsa_216[118] + Gsa_216[99] + Gsa_216[104] + Gsa_216[117] + Gsa_216[107] + Gsa_216[46] + Gsa_216[32] + Gsa_216[104] + Gsa_216[116] + Gsa_216[116] +
      Gsa_216[112] + Gsa_216[58] + Gsa_216[47] + Gsa_216[47] + Gsa_216[102] + Gsa_216[111] + Gsa_216[114] + Gsa_216[101] + Gsa_216[120] + Gsa_216[116] + Gsa_216[111] + Gsa_216[111] +
      Gsa_216[108] + Gsa_216[115] + Gsa_216[46] + Gsa_216[99] + Gsa_216[111] + Gsa_216[109] + Gsa_216[46] + Gsa_216[117] + Gsa_216[97]);
   IndicatorDigits(G_digits_196);
   SetIndexBuffer(4, G_ibuf_164);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(5, G_ibuf_168);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(2, G_ibuf_172);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(3, G_ibuf_176);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(0, G_ibuf_180);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexBuffer(1, G_ibuf_184);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   for (index_0 = 0; index_0 < 8; index_0++) SetIndexShift(index_0, 1);
   if (WriteHistoryToFile) {
      Gs_132 = Symbol();
      Ls_4 = "(C)opyright 2010, MetaQuotes Software Corp.";
      Print("Start writing history file \"" + Gs_132 + "1-ticks.hst\"");
      G_file_116 = FileOpenHistory(Gs_132 + "1-ticks.hst", FILE_BIN|FILE_WRITE);
      if (G_file_116 < 0) {
         Comment("Îøèáêà ïðè ñîçäàíèè ôàéëà èñòîðèè \'" + Gs_132 + "1-ticks.hst\'" + ".", "ÎØÈÁÊÀ", 16);
         return;
      }
      FileWriteInteger(G_file_116, 400, LONG_VALUE);
      FileWriteString(G_file_116, Ls_4, 64);
      FileWriteString(G_file_116, Gs_132, 12);
      FileWriteInteger(G_file_116, 1, LONG_VALUE);
      FileWriteInteger(G_file_116, Digits, LONG_VALUE);
      FileWriteInteger(G_file_116, 0, LONG_VALUE);
      FileWriteInteger(G_file_116, 0, LONG_VALUE);
      FileWriteArray(G_file_116, Lia_12, 0, 13);
      FileClose(G_file_116);
      Gi_128 = 0;
      Gd_140 = Close[0];
   }
   return (0);
}

int WriteTickToFile() {
   double close_0;
   double close_8;
   double close_16;
   double close_24;
   G_file_116 = FileOpenHistory(Gs_132 + "1-ticks.hst", FILE_BIN|FILE_WRITE|FILE_READ);
   FileSeek(G_file_116, 0, SEEK_END);
   if (G_file_116 < 0) {
      Comment("Îøèáêà ïðè îòêðûòèè ôàéëà èñòîðèè \'" + Gs_132 + "1-ticks.hst\'" + ".", "ÎØÈÁÊÀ", 16);
      return;
   }
   if (BarMode) {
      close_0 = Gd_140;
      close_24 = Close[0];
      if (Gd_140 <= Close[0]) {
         close_8 = Gd_140;
         close_16 = Close[0];
      } else {
         close_16 = Gd_140;
         close_8 = Close[0];
      }
   } else {
      close_0 = Close[0];
      close_8 = Close[0];
      close_16 = Close[0];
      close_24 = Close[0];
   }
   string time2str_40 = TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS);
   double str2dbl_48 = StrToDouble(StringSubstr(time2str_40, 5, 2) + StringSubstr(time2str_40, 8, 2) + StringSubstr(time2str_40, 11, 2) + StringSubstr(time2str_40, 14,
      2) + StringSubstr(time2str_40, 17, 2));
   Gi_120++;
   Gi_128 += 60;
   FileWriteInteger(G_file_116, Gi_128, LONG_VALUE);
   FileWriteDouble(G_file_116, close_0, DOUBLE_VALUE);
   FileWriteDouble(G_file_116, close_8, DOUBLE_VALUE);
   FileWriteDouble(G_file_116, close_16, DOUBLE_VALUE);
   FileWriteDouble(G_file_116, close_24, DOUBLE_VALUE);
   FileWriteDouble(G_file_116, str2dbl_48, DOUBLE_VALUE);
   Gd_140 = close_24;
   FileFlush(G_file_116);
   FileClose(G_file_116);
   return (0);
}

void start() {
   int Li_12;
   int Li_16;
   string Ls_20;
   string Ls_28;
   string Ls_36;
   string Ls_44;
   int Li_52;
   string name_60;
   int file_68;
   string Ls_72;
   int Li_88;
   double bid_92;
   double price_100;
   string Ls_128;
   string Ls_0 = "•";
   IndicatorShortName(Ls_0);
   int window_8 = WindowFind(Ls_0);
   if (G_acc_number_220 != AccountNumber()) {
      Gi_228 = 0;
      if (G_acc_number_220 != AccountNumber() || G_timeframe_224 != Period()) Gi_228 = -1;
      if (Gi_228 != 1 && Gi_228 != 0) {
         G_acc_number_220 = AccountNumber();
         G_timeframe_224 = Period();
         Li_12 = 40;
         Li_16 = 0;
         Ls_20 = "";
         Ls_28 = "";
         for (int index_56 = 0; index_56 < 256; index_56++) Gsa_244[index_56] = CharToStr(index_56);
         name_60 = Gsa_244[70] + Gsa_244[111] + Gsa_244[114] + Gsa_244[101] + Gsa_244[120] + Gsa_244[84] + Gsa_244[111] + Gsa_244[111] + Gsa_244[108] + Gsa_244[115] + Gsa_244[32] +
            Gsa_244[108] + Gsa_244[105] + Gsa_244[99] + Gsa_244[101] + Gsa_244[110] + Gsa_244[115] + Gsa_244[101] + Gsa_244[115] + Gsa_244[46] + Gsa_244[116] + Gsa_244[120] +
            Gsa_244[116];
         file_68 = FileOpen(name_60, FILE_CSV|FILE_READ);
         Gi_228 = 0;
         if (file_68 == -1 || Li_12 <= 0 || Li_12 > 200) Gi_228 = 0;
         else {
            while (!FileIsEnding(file_68)) {
               Ls_72 = FileReadString(file_68);
               Gi_228 = 0;
               if (StringFind(Ls_72, ":") < 0) continue;
               Li_52 = StringFind(Ls_72, ":") + 1;
               Ls_72 = StringTrimLeft(StringTrimRight(StringSubstr(Ls_72, Li_52)));
               if (StringLen(Ls_72) != 700) continue;
               for (string Ls_80 = StringSubstr(Ls_72, Li_12, 399 - Li_12); StringLen(Ls_80) < 192; Ls_80 = Ls_80 + Ls_80) {
               }
               Li_16 = 0;
               Ls_36 = StringSubstr(Ls_72, Li_12);
               for (Li_52 = 0; Li_52 < 32; Li_52++) {
                  Li_88 = StringGetChar(Ls_36, 0) - 48;
                  if (Li_88 == 0) break;
                  Li_16 = 10 * Li_16 + StrToInteger(StringSubstr(Ls_36, Li_88 + 3 + 2, 1));
                  Ls_36 = StringSubstr(Ls_36, Li_88 + 3);
               }
               Ls_36 = StringSubstr(Ls_72, 399, 192);
               Ls_36 = StringSubstr(Ls_36, StringGetChar(Ls_36, 0) - 48 + 1);
               Ls_20 = "";
               for (Li_52 = 0; Li_52 <= 64; Li_52++) {
                  Ls_44 = StringSubstr(Ls_36, 3 * Li_52, 3);
                  if (StrToInteger(Ls_44) <= 255) {
                     Ls_44 = CharToStr(StrToInteger(Ls_44) ^ StringGetChar(Ls_80, Li_52));
                     if (Ls_44 == CharToStr(3)) break;
                     Ls_20 = Ls_20 + Ls_44;
                  }
               }
               Ls_36 = StringSubstr(Ls_72, 619);
               Ls_36 = StringSubstr(Ls_36, StringGetChar(Ls_36, 0) - 48 + 1);
               Ls_28 = "";
               for (Li_52 = 0; Li_52 <= 64; Li_52++) {
                  Ls_44 = StringSubstr(Ls_36, 3 * Li_52, 3);
                  if (StrToInteger(Ls_44) <= 255) {
                     Ls_44 = CharToStr(StrToInteger(Ls_44) ^ StringGetChar(Ls_80, Li_52));
                     if (Ls_44 == CharToStr(3)) break;
                     Ls_28 = Ls_28 + Ls_44;
                  }
               }
               if ((Li_16 > 2 && Li_16 == AccountNumber()) || (Li_16 == 1 || Li_16 == 3 && IsDemo()) || (Li_16 == 2 && (!IsDemo())) && Ls_20 == "" || (Ls_20 != "" && Ls_20 == AccountServer()) &&
                  Ls_28 == "" || (Ls_28 != "" && TimeCurrent() < StrToTime(Ls_28) + 86400)) {
                  Gi_228 = 1;
                  break;
               }
            }
            FileClose(file_68);
         }
         if (Gi_228 != 1) {
            WriteHistoryToFile = FALSE;
            MaxTicksCnt = 100;
            Print(Gsa_216[68] + Gsa_216[69] + Gsa_216[77] + Gsa_216[79] + Gsa_216[32] + Gsa_216[77] + Gsa_216[79] + Gsa_216[68] + Gsa_216[69] + Gsa_216[46] + Gsa_216[32] + Gsa_216[78] +
               Gsa_216[111] + Gsa_216[32] + Gsa_216[72] + Gsa_216[83] + Gsa_216[84] + Gsa_216[45] + Gsa_216[102] + Gsa_216[105] + Gsa_216[108] + Gsa_216[101] + Gsa_216[32] + Gsa_216[119] +
               Gsa_216[114] + Gsa_216[105] + Gsa_216[116] + Gsa_216[105] + Gsa_216[110] + Gsa_216[103] + Gsa_216[32] + Gsa_216[97] + Gsa_216[110] + Gsa_216[100] + Gsa_216[32] + Gsa_216[77] +
               Gsa_216[97] + Gsa_216[120] + Gsa_216[84] + Gsa_216[105] + Gsa_216[99] + Gsa_216[107] + Gsa_216[115] + Gsa_216[67] + Gsa_216[110] + Gsa_216[116] + Gsa_216[32] + Gsa_216[102] +
               Gsa_216[105] + Gsa_216[120] + Gsa_216[101] + Gsa_216[100] + Gsa_216[32] + Gsa_216[116] + Gsa_216[111] + Gsa_216[32] + Gsa_216[49] + Gsa_216[48] + Gsa_216[48] + Gsa_216[46]);
         }
      }
   }
   if (Gi_148) {
      bid_92 = Close[0];
      price_100 = Close[0] + MarketInfo(Symbol(), MODE_SPREAD) * Point;
   } else {
      bid_92 = Bid;
      price_100 = Ask;
   }
   if (WriteHistoryToFile) WriteTickToFile();
   if (bid_92 <= 0.0 || price_100 <= 0.0) return;
   if (bid_92 == G_ibuf_164[2] && price_100 == G_ibuf_168[2] && Volume[0] == Gi_200) return;
   for (int pos_108 = MathMin(MaxTicksCnt, Bars - 2); pos_108 >= 1; pos_108--) {
      G_ibuf_164[pos_108 + 1] = G_ibuf_164[pos_108];
      G_ibuf_168[pos_108 + 1] = G_ibuf_168[pos_108];
      G_ibuf_172[pos_108 + 1] = G_ibuf_172[pos_108];
      G_ibuf_180[pos_108 + 1] = G_ibuf_180[pos_108];
      G_ibuf_176[pos_108 + 1] = G_ibuf_176[pos_108];
      G_ibuf_184[pos_108 + 1] = G_ibuf_184[pos_108];
   }
   for (pos_108 = MathMin(MaxTicksCnt, Bars - 2); pos_108 < Bars - 2; pos_108++) {
      G_ibuf_164[pos_108 + 1] = EMPTY_VALUE;
      G_ibuf_168[pos_108 + 1] = EMPTY_VALUE;
      G_ibuf_172[pos_108 + 1] = EMPTY_VALUE;
      G_ibuf_180[pos_108 + 1] = EMPTY_VALUE;
      G_ibuf_176[pos_108 + 1] = EMPTY_VALUE;
      G_ibuf_184[pos_108 + 1] = EMPTY_VALUE;
   }
   Gi_200 = Volume[0];
   G_ibuf_164[1] = bid_92;
   G_ibuf_168[1] = price_100;
   double Ld_112 = RoundTo * MarketInfo(Symbol(), MODE_POINT);
   G_ibuf_172[1] = MathCeil(bid_92 / Ld_112) * Ld_112;
   G_ibuf_176[1] = MathFloor(bid_92 / Ld_112) * Ld_112;
   G_ibuf_180[1] = MathCeil(price_100 / Ld_112) * Ld_112;
   G_ibuf_184[1] = MathFloor(price_100 / Ld_112) * Ld_112;
   IndicatorShortName("ft.Ticker: " + DoubleToStr(Volume[0], 0) + " tick at " + TimeToStr(TimeCurrent(), TIME_SECONDS) + " - Spread: " + DoubleToStr((price_100 - bid_92) / MarketInfo(Symbol(),
      MODE_POINT), 0) + ",  StopLevel: " + DoubleToStr(MarketInfo(Symbol(), MODE_STOPLEVEL), 0));
   Clear();
   int Li_120 = 12;
   int Li_124 = 12;
   for (pos_108 = 0; pos_108 < OrdersTotal(); pos_108++) {
      if (OrderSelect(pos_108, SELECT_BY_POS, MODE_TRADES) != FALSE) {
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
            Ls_128 = Gsa_160[OrderType()] + " " + DoubleToStr(OrderLots(), 2);
            if (OrderSymbol() != Symbol()) continue;
            Gs_tickbid_188 = Gs_152 + OrderTicket() + " ";
            ObjectCreate(Gs_tickbid_188, OBJ_HLINE, window_8, Time[0], OrderOpenPrice());
            ObjectSet(Gs_tickbid_188, OBJPROP_STYLE, STYLE_DOT);
            ObjectSet(Gs_tickbid_188, OBJPROP_COLOR, ColorOpenPriceLine);
            ObjectSet(Gs_tickbid_188, OBJPROP_WIDTH, 1);
            ObjectSetText(Gs_tickbid_188, Gs_tickbid_188 + Ls_128);
            if (OrderStopLoss() > 0.0) {
               Gs_tickbid_188 = Gs_152 + OrderTicket() + " SL ";
               if (OrderType() == OP_BUY) {
                  G_color_212 = ColorTicksBid;
                  Li_124 = 1;
               }
               if (OrderType() == OP_SELL) {
                  G_color_212 = ColorTicksAsk;
                  Li_120 = 1;
               }
               ObjectCreate(Gs_tickbid_188, OBJ_HLINE, window_8, Time[0], OrderStopLoss());
               ObjectSet(Gs_tickbid_188, OBJPROP_STYLE, STYLE_DASH);
               ObjectSet(Gs_tickbid_188, OBJPROP_COLOR, G_color_212);
               ObjectSet(Gs_tickbid_188, OBJPROP_WIDTH, 1);
               ObjectSetText(Gs_tickbid_188, Gs_tickbid_188 + Ls_128);
            }
            if (OrderTakeProfit() <= 0.0) continue;
            Gs_tickbid_188 = Gs_152 + OrderTicket() + " TP ";
            if (OrderType() == OP_BUY) {
               G_color_212 = ColorTicksBid;
               Li_124 = 1;
            }
            if (OrderType() == OP_SELL) {
               G_color_212 = ColorTicksAsk;
               Li_120 = 1;
            }
            ObjectCreate(Gs_tickbid_188, OBJ_HLINE, window_8, Time[0], OrderTakeProfit());
            ObjectSet(Gs_tickbid_188, OBJPROP_STYLE, STYLE_DASHDOT);
            ObjectSet(Gs_tickbid_188, OBJPROP_COLOR, G_color_212);
            ObjectSet(Gs_tickbid_188, OBJPROP_WIDTH, 1);
            ObjectSetText(Gs_tickbid_188, Gs_tickbid_188 + Ls_128);
            continue;
         }
         Li_120 = 1;
         Li_124 = 1;
         Gs_tickbid_188 = Gs_152 + OrderTicket() + " LIM";
         ObjectCreate(Gs_tickbid_188, OBJ_HLINE, window_8, Time[0], OrderOpenPrice());
         ObjectSet(Gs_tickbid_188, OBJPROP_STYLE, STYLE_DASH);
         ObjectSet(Gs_tickbid_188, OBJPROP_COLOR, ColorLimitPriceLine);
         ObjectSet(Gs_tickbid_188, OBJPROP_WIDTH, 1);
         ObjectSetText(Gs_tickbid_188, Gs_tickbid_188 + Ls_128);
      }
   }
   if (Li_120 == DRAW_NONE && Li_124 == DRAW_NONE) Li_124 = 1;
   Gs_tickbid_188 = "TickBid";
   ObjectDelete(Gs_tickbid_188);
   if (Li_124 != DRAW_NONE) {
      ObjectCreate(Gs_tickbid_188, OBJ_ARROW, window_8, Time[0], bid_92);
      ObjectSet(Gs_tickbid_188, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
      ObjectSet(Gs_tickbid_188, OBJPROP_COLOR, ColorTicksBid);
      ObjectSet(Gs_tickbid_188, OBJPROP_WIDTH, 1);
   }
   Gs_tickbid_188 = "TickAsk";
   ObjectDelete(Gs_tickbid_188);
   if (Li_120 != DRAW_NONE) {
      ObjectCreate(Gs_tickbid_188, OBJ_ARROW, window_8, Time[0], price_100);
      ObjectSet(Gs_tickbid_188, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
      ObjectSet(Gs_tickbid_188, OBJPROP_COLOR, ColorTicksAsk);
      ObjectSet(Gs_tickbid_188, OBJPROP_WIDTH, 1);
   }
   SetIndexStyle(4, Li_124, STYLE_SOLID, 1, ColorTicksBid);
   SetIndexStyle(5, Li_120, STYLE_SOLID, 1, ColorTicksAsk);
   if (Li_124 == DRAW_NONE || ColorTicksBidRounded == CLR_NONE) {
      SetIndexStyle(2, DRAW_NONE, STYLE_SOLID, 1, ColorTicksBidRounded);
      SetIndexStyle(3, DRAW_NONE, STYLE_SOLID, 1, ColorTicksBidRounded);
   } else {
      SetIndexStyle(2, DRAW_SECTION, STYLE_SOLID, 1, ColorTicksBidRounded);
      SetIndexStyle(3, DRAW_SECTION, STYLE_SOLID, 1, ColorTicksBidRounded);
   }
   if (Li_120 == DRAW_NONE || ColorTicksAskRounded == CLR_NONE) {
      SetIndexStyle(0, DRAW_NONE, STYLE_SOLID, 1, ColorTicksAskRounded);
      SetIndexStyle(1, DRAW_NONE, STYLE_SOLID, 1, ColorTicksAskRounded);
      return;
   }
   SetIndexStyle(0, DRAW_SECTION, STYLE_SOLID, 1, ColorTicksAskRounded);
   SetIndexStyle(1, DRAW_SECTION, STYLE_SOLID, 1, ColorTicksAskRounded);
}