/*
   
*/


#property indicator_chart_window

int gi_76 = 20;
int g_count_80 = 0;
int g_count_84 = 0;
int gi_88 = 0;
int gi_92 = 0;
int gi_96 = 0;
int gi_100 = 0;
int gi_104 = 0;
int gi_108 = 12;
extern int Numberbars = 5000;
extern bool History = FALSE;
extern bool ShowData = FALSE;
bool gi_124 = TRUE;
extern bool ShowInfo = FALSE;
extern bool ShowTD = TRUE;
string gs_unused_136 = "2015.12.20";
string g_name_144 = "TD_History";
int gi_unused_152 = 16711680;
int gi_156 = 20;

int init() {
   if (gi_124) {
      if (ObjectFind(g_name_144) == -1 && History) {
         ObjectCreate(g_name_144, OBJ_ARROW, 0, Time[gi_156], Low[gi_156] - 100.0 * Point);
         ObjectSet(g_name_144, OBJPROP_COLOR, Blue);
         ObjectSet(g_name_144, OBJPROP_ARROWCODE, 233);
         ObjectSet(g_name_144, OBJPROP_WIDTH, 3);
      }
   }
   if (!gi_124) {
      if (ObjectFind(g_name_144) == -1 && History) {
         ObjectCreate(g_name_144, OBJ_ARROW, 0, Time[gi_156], High[gi_156] + 100.0 * Point);
         ObjectSet(g_name_144, OBJPROP_COLOR, Red);
         ObjectSet(g_name_144, OBJPROP_ARROWCODE, 234);
         ObjectSet(g_name_144, OBJPROP_WIDTH, 3);
      }
   }
   Comment("");
   return (0);
}

int deinit() {
   Comment("");
   ObjectsDeleteAll();
   return (0);
}

int start() {
   double ld_40;
   double ld_48;
   double l_low_60;
   double l_high_68;
   double ld_76;
   int li_84;
   int li_88;
   double ld_96;
   double ld_104;
   double ld_112;
   double ld_120;
   double ld_128;
   double ld_136;
   double ld_144;
   int li_152;
   int l_shift_156;
   int l_count_0 = 0;
   int l_count_4 = 0;
   double l_low_8 = 0.0;
   double l_high_16 = 500.0;
   
   if (ShowInfo) info_TF();
   gi_100 = FALSE;
   gi_96 = FALSE;
   double ld_24 = gi_108 * Point * MathSqrt(Period());
   if (Period() == PERIOD_M1) ld_24 = Point * gi_108;
   if (ShowTD) {
      for (gi_104 = Numberbars; gi_104 >= 0; gi_104--) {
         ObjectDelete("Buy" + gi_104);
         ObjectDelete("Sell" + gi_104);
         if (Close[gi_104] < Close[gi_104 + 4] && Close[gi_104 + 1] >= Close[gi_104 + 5] && g_count_80 == 0) {
            ObjectCreate("Buy" + gi_104, OBJ_TREND, 0, Time[gi_104], High[gi_104], Time[gi_104 - gi_76], High[gi_104]);
            ObjectSet("Buy" + gi_104, OBJPROP_RAY, FALSE);
            ObjectSet("Buy" + gi_104, OBJPROP_STYLE, STYLE_DOT);
            ObjectSet("Buy" + gi_104, OBJPROP_COLOR, Red);
            ObjectSet("Buy" + gi_104, OBJPROP_BACK, TRUE);
            g_count_80 = 1;
            gi_88 = gi_104;
            gi_100 = FALSE;
            ObjectsRedraw();
         }
         if (Low[gi_104] <= l_low_8) l_count_4 = 0;
         if (Close[gi_104] < Close[gi_104 + 4] && g_count_80 != 0 && ObjectFind("Buy" + gi_104) == -1 && gi_88 != gi_104) {
            g_count_80++;
            if (g_count_80 == 9) {
               if (Low[gi_104 + 2] > Low[gi_104 + 3]) l_low_60 = Low[gi_104 + 3];
               else l_low_60 = Low[gi_104 + 2];
               if (Low[gi_104] < l_low_60 || Low[gi_104 + 1] < l_low_60) {
                  ObjectCreate("Buy" + gi_104, OBJ_TEXT, 0, Time[gi_104], High[gi_104] + ld_24);
                  ObjectSetText("Buy" + gi_104, "" + g_count_80, 8, "Arial", Red);
                  ObjectsRedraw();
               } else {
                  ObjectCreate("Buy" + gi_104, OBJ_TEXT, 0, Time[gi_104], High[gi_104] + ld_24);
                  ObjectSetText("Buy" + gi_104, "" + g_count_80, 8, "Arial", Red);
                  ObjectsRedraw();
               }
               l_high_16 = High[gi_104 + 8];
               l_count_0++;
               ObjectCreate("BuyUp" + gi_104, OBJ_TEXT, 0, Time[gi_104], High[gi_104 + 8], Time[gi_104 - gi_76], High[gi_104 + 8]);
               ObjectSetText("BuyUp" + gi_104, "" + l_count_0, 16, "Arial", Red);
               g_count_80 = 0;
               gi_88 = FALSE;
               if (gi_96 == FALSE) gi_96 = TRUE;
               ld_40 = ObjectGet("Buy" + ((gi_104 + 8)), OBJPROP_PRICE1);
            } else {
               ObjectCreate("Buy" + gi_104, OBJ_TEXT, 0, Time[gi_104], High[gi_104] + ld_24);
               ObjectSetText("Buy" + gi_104, "" + g_count_80, 8, "Arial", Red);
               ObjectsRedraw();
            }
         } else {
            if (Close[gi_104] >= Close[gi_104 + 4]) {
               for (int li_56 = gi_104 + 1; li_56 <= gi_104 + g_count_80; li_56++) ObjectDelete("Buy" + li_56);
               g_count_80 = 0;
            }
         }
         if (Close[gi_104] > Close[gi_104 + 4] && Close[gi_104 + 1] <= Close[gi_104 + 5] && g_count_84 == 0) {
            ObjectCreate("Sell" + gi_104, OBJ_TREND, 0, Time[gi_104], Low[gi_104], Time[gi_104 - gi_76], Low[gi_104]);
            ObjectSet("Sell" + gi_104, OBJPROP_RAY, FALSE);
            ObjectSet("Sell" + gi_104, OBJPROP_STYLE, STYLE_DOT);
            ObjectSet("Sell" + gi_104, OBJPROP_COLOR, Green);
            ObjectSet("Sell" + gi_104, OBJPROP_BACK, TRUE);
            ObjectsRedraw();
            g_count_84 = 1;
            gi_92 = gi_104;
            gi_96 = FALSE;
         }
         if (High[gi_104] >= l_high_16) l_count_0 = 0;
         if (Close[gi_104] > Close[gi_104 + 4] && g_count_84 != 0 && ObjectFind("Sell" + gi_104) == -1 && gi_92 != gi_104) {
            g_count_84++;
            if (g_count_84 == 9) {
               if (High[gi_104 + 2] > High[gi_104 + 3]) l_high_68 = High[gi_104 + 2];
               else l_high_68 = High[gi_104 + 3];
               if (High[gi_104] > l_high_68 || High[gi_104 + 1] > l_high_68) {
                  ObjectCreate("Sell" + gi_104, OBJ_TEXT, 0, Time[gi_104], Low[gi_104] - ld_24);
                  ObjectSetText("Sell" + gi_104, "" + g_count_84, 8, "Arial", Green);
                  ObjectsRedraw();
               } else {
                  ObjectCreate("Sell" + gi_104, OBJ_TEXT, 0, Time[gi_104], Low[gi_104] - ld_24);
                  ObjectSetText("Sell" + gi_104, "" + g_count_84, 8, "Arial", Green);
                  ObjectsRedraw();
               }
               l_low_8 = Low[gi_104 + 8];
               l_count_4++;
               ObjectCreate("SellUp" + gi_104, OBJ_TEXT, 0, Time[gi_104], Low[gi_104 + 8], Time[gi_104 - gi_76], Low[gi_104 + 8]);
               ObjectSetText("SellUp" + gi_104, "" + l_count_4, 16, "Arial", Green);
               g_count_84 = 0;
               gi_92 = FALSE;
               if (gi_100 == FALSE) gi_100 = TRUE;
               ld_48 = ObjectGet("Sell" + ((gi_104 + 8)), OBJPROP_PRICE1);
            } else {
               ObjectCreate("Sell" + gi_104, OBJ_TEXT, 0, Time[gi_104], Low[gi_104] - ld_24);
               ObjectSetText("Sell" + gi_104, "" + g_count_84, 8, "Arial", Green);
               ObjectsRedraw();
            }
         } else {
            if (Close[gi_104] <= Close[gi_104 + 4]) {
               for (li_56 = gi_104 + 1; li_56 <= gi_104 + g_count_84; li_56++) ObjectDelete("Sell" + li_56);
               g_count_84 = 0;
               ObjectsRedraw();
            }
         }
      }
   }
   if (ShowData) {
      li_84 = Time[0] + 60 * Period() - TimeCurrent();
      ld_76 = li_84 / 60.0;
      li_88 = li_84 % 60;
      li_84 = (li_84 - li_84 % 60) / 60;
      l_high_68 = NormalizeDouble(iHigh(Symbol(), Period(), 1) + 1.0 * Point + MarketInfo(Symbol(), MODE_SPREAD) * Point, Digits);
      l_low_60 = NormalizeDouble(iLow(Symbol(), Period(), 1) - 1.0 * Point, Digits);
      ld_96 = NormalizeDouble(MathAbs(iOpen(Symbol(), Period(), 1) - iClose(Symbol(), Period(), 1)), Digits);
      ld_104 = l_low_60 - ld_96;
      ld_112 = l_high_68 + ld_96;
      ld_120 = NormalizeDouble((l_high_68 - ld_104) / Point, 0);
      ld_128 = NormalizeDouble((ld_112 - l_low_60) / Point, 0);
      ld_136 = NormalizeDouble(AccountEquity() / 100.0 / (ld_120 * MarketInfo(Symbol(), MODE_TICKVALUE)), 1);
      ObjectCreate("Price_Tag_6", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_6", OBJPROP_CORNER, 1);
      ObjectSet("Price_Tag_6", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_6", OBJPROP_YDISTANCE, 260);
      ObjectSetText("Price_Tag_6", "Max Lot Allow  " + DoubleToStr(ld_136, 1) + " on Bar " + TimeToStr(iTime(Symbol(), Period(), 1), TIME_MINUTES), 10, "Arial", DarkBlue);
      ObjectCreate("Price_Tag_7", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_7", OBJPROP_CORNER, 1);
      ObjectSet("Price_Tag_7", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_7", OBJPROP_YDISTANCE, 290);
      ObjectSetText("Price_Tag_7", "Current Bar " + TimeToStr(iTime(Symbol(), Period(), 0), TIME_MINUTES) + " has " + DoubleToStr(li_84, -3) + " MIN " + DoubleToStr(li_88, -3) +
         " SEC", 10, "Arial", Blue);
      if (ld_40 != 0.0) {
         ObjectCreate("Price_Tag_8", OBJ_LABEL, 0, 0, 0);
         ObjectSet("Price_Tag_8", OBJPROP_CORNER, 1);
         ObjectSet("Price_Tag_8", OBJPROP_XDISTANCE, 10);
         ObjectSet("Price_Tag_8", OBJPROP_YDISTANCE, 320);
         ObjectSetText("Price_Tag_8", "Last Red Line " + DoubleToStr(ld_40, Digits), 10, "Arial", Red);
      }
      if (ld_48 != 0.0) {
         ObjectCreate("Price_Tag_9", OBJ_LABEL, 0, 0, 0);
         ObjectSet("Price_Tag_9", OBJPROP_CORNER, 1);
         ObjectSet("Price_Tag_9", OBJPROP_XDISTANCE, 10);
         ObjectSet("Price_Tag_9", OBJPROP_YDISTANCE, 350);
         ObjectSetText("Price_Tag_9", "Last Blue Line  " + DoubleToStr(ld_48, Digits), 10, "Arial", Blue);
      }
      ObjectCreate("Price_Tag", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag", OBJPROP_CORNER, 1);
      ObjectSet("Price_Tag", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag", OBJPROP_YDISTANCE, 15);
      ObjectSetText("Price_Tag", "Buy at " + DoubleToStr(l_high_68, Digits), 10, "Arial", DarkBlue);
      ObjectCreate("Price_Tag_1", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_1", OBJPROP_CORNER, 1);
      ObjectSet("Price_Tag_1", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_1", OBJPROP_YDISTANCE, 35);
      ObjectSetText("Price_Tag_1", "Stoploss at " + DoubleToStr(ld_104, Digits), 10, "Arial", DarkBlue);
      ObjectCreate("Price_Tag_2", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_2", OBJPROP_CORNER, 1);
      ObjectSet("Price_Tag_2", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_2", OBJPROP_YDISTANCE, 55);
      ObjectSetText("Price_Tag_2", "Risk is  " + DoubleToStr(ld_120, 0), 10, "Arial", DarkBlue);
      ObjectCreate("Price_Tag_3", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_3", OBJPROP_CORNER, 3);
      ObjectSet("Price_Tag_3", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_3", OBJPROP_YDISTANCE, 55);
      ObjectSetText("Price_Tag_3", "Sell at " + DoubleToStr(l_low_60, Digits), 10, "Arial", Red);
      ObjectCreate("Price_Tag_4", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_4", OBJPROP_CORNER, 3);
      ObjectSet("Price_Tag_4", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_4", OBJPROP_YDISTANCE, 35);
      ObjectSetText("Price_Tag_4", "Stoploss at " + DoubleToStr(ld_112, Digits), 10, "Arial", Red);
      ObjectCreate("Price_Tag_5", OBJ_LABEL, 0, 0, 0);
      ObjectSet("Price_Tag_5", OBJPROP_CORNER, 3);
      ObjectSet("Price_Tag_5", OBJPROP_XDISTANCE, 10);
      ObjectSet("Price_Tag_5", OBJPROP_YDISTANCE, 15);
      ObjectSetText("Price_Tag_5", "Risk is  " + DoubleToStr(ld_128, 0), 10, "Arial", Red);
   }
   if (History) {
      if (ObjectFind(g_name_144) == -1) {
         ObjectDelete("TD_History_Order");
         ObjectDelete("TD_History_Order_1");
         return (0);
      }
      li_152 = ObjectGet(g_name_144, OBJPROP_TIME1);
      l_shift_156 = iBarShift(Symbol(), Period(), li_152);
      l_high_68 = NormalizeDouble(iHigh(Symbol(), Period(), l_shift_156) + 1.0 * Point + MarketInfo(Symbol(), MODE_SPREAD) * Point, Digits);
      l_low_60 = NormalizeDouble(iLow(Symbol(), Period(), l_shift_156) - 1.0 * Point, Digits);
      ld_96 = NormalizeDouble(MathAbs(iOpen(Symbol(), Period(), l_shift_156) - iClose(Symbol(), Period(), l_shift_156)), Digits);
      ld_144 = NormalizeDouble(MathAbs(iHigh(Symbol(), Period(), l_shift_156) - iLow(Symbol(), Period(), l_shift_156)), Digits);
      ld_104 = l_low_60 - ld_96;
      ld_112 = l_high_68 + ld_96;
      ld_120 = NormalizeDouble((l_high_68 - ld_104) / Point, 0);
      ld_128 = NormalizeDouble((ld_112 - l_low_60) / Point, 0);
      ld_136 = NormalizeDouble(AccountEquity() / 100.0 / (ld_120 * MarketInfo(Symbol(), MODE_TICKVALUE)), 1);
      ObjectDelete("TD_History_Order");
      ObjectDelete("TD_History_Order_1");
      ObjectCreate("TD_History_Order", OBJ_TREND, 0, Time[l_shift_156], High[l_shift_156] + 1.0 * Point, Time[0], High[l_shift_156] + 1.0 * Point, 0);
      ObjectSet("TD_History_Order", OBJPROP_COLOR, Blue);
      ObjectSet("Sell" + gi_104, OBJPROP_RAY, FALSE);
      ObjectSet("TD_History_Order", OBJPROP_STYLE, STYLE_DASHDOTDOT);
      ObjectCreate("TD_History_Order_1", OBJ_TREND, 0, Time[l_shift_156], Low[l_shift_156] - 1.0 * Point, Time[0], Low[l_shift_156] - 1.0 * Point, 0);
      ObjectSet("TD_History_Order_1", OBJPROP_COLOR, Red);
      ObjectSet("Sell" + gi_104, OBJPROP_RAY, FALSE);
      ObjectSet("TD_History_Order_1", OBJPROP_STYLE, STYLE_DASHDOTDOT);
      ObjectCreate("TD_History_6", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_6", OBJPROP_CORNER, 0);
      ObjectSet("TD_History_6", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_6", OBJPROP_YDISTANCE, 105);
      ObjectSetText("TD_History_6", "History Bar " + TimeToStr(iTime(Symbol(), Period(), l_shift_156), TIME_MINUTES), 10, "Arial", DarkBlue);
      ObjectCreate("TD_History_7", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_7", OBJPROP_CORNER, 0);
      ObjectSet("TD_History_7", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_7", OBJPROP_YDISTANCE, 125);
      ObjectSetText("TD_History_7", "Bar Size : " + DoubleToStr(ld_144 / Point, 0) + "     Body Size : " + DoubleToStr(ld_96 / Point, 0), 10, "Arial", Red);
      ObjectCreate("TD_History_0", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_0", OBJPROP_CORNER, 0);
      ObjectSet("TD_History_0", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_0", OBJPROP_YDISTANCE, 35);
      ObjectSetText("TD_History_0", "Buy at " + DoubleToStr(l_high_68, Digits) + " Target at  " + DoubleToStr(l_high_68 + 3.0 * ld_120 * Point, Digits), 10, "Arial", DarkBlue);
      ObjectCreate("TD_History_1", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_1", OBJPROP_CORNER, 0);
      ObjectSet("TD_History_1", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_1", OBJPROP_YDISTANCE, 55);
      ObjectSetText("TD_History_1", "Stoploss at " + DoubleToStr(ld_104, Digits), 10, "Arial", DarkBlue);
      ObjectCreate("TD_History_2", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_2", OBJPROP_CORNER, 0);
      ObjectSet("TD_History_2", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_2", OBJPROP_YDISTANCE, 75);
      ObjectSetText("TD_History_2", "Risk is  " + DoubleToStr(ld_120, 0), 10, "Arial", DarkBlue);
      ObjectCreate("TD_History_3", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_3", OBJPROP_CORNER, 2);
      ObjectSet("TD_History_3", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_3", OBJPROP_YDISTANCE, 55);
      ObjectSetText("TD_History_3", "Sell at " + DoubleToStr(l_low_60, Digits) + " Target at  " + DoubleToStr(l_low_60 - 3.0 * ld_128 * Point, Digits), 10, "Arial", Red);
      ObjectCreate("TD_History_4", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_4", OBJPROP_CORNER, 2);
      ObjectSet("TD_History_4", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_4", OBJPROP_YDISTANCE, 35);
      ObjectSetText("TD_History_4", "Stoploss at " + DoubleToStr(ld_112, Digits), 10, "Arial", Red);
      ObjectCreate("TD_History_5", OBJ_LABEL, 0, 0, 0);
      ObjectSet("TD_History_5", OBJPROP_CORNER, 2);
      ObjectSet("TD_History_5", OBJPROP_XDISTANCE, 10);
      ObjectSet("TD_History_5", OBJPROP_YDISTANCE, 15);
      ObjectSetText("TD_History_5", "Risk is  " + DoubleToStr(ld_128, 0), 10, "Arial", Red);
   }
   return (0);
}

void info_TF() {
   string ls_8;
   int li_20;
   double lda_24[] = {0.0, 0.0, 0.0, 0.0, 0.0};
   double lda_28[] = {0.0, 0.0, 0.0, 0.0, 0.0};
   double lda_32[] = {0.0, 0.0, 0.0, 0.0, 0.0};
   double lda_36[] = {0.0, 0.0, 0.0, 0.0, 0.0};
   double ld_unused_40 = 0;
   string lsa_48[] = {"MN", "W1", "D1", "H4", "H1"};
   lda_24[0] = iOpen(NULL, PERIOD_MN1, 0);
   lda_28[0] = iClose(NULL, PERIOD_MN1, 0);
   lda_32[0] = iLow(NULL, PERIOD_MN1, 0);
   lda_36[0] = iHigh(NULL, PERIOD_MN1, 0);
   lda_24[1] = iOpen(NULL, PERIOD_W1, 0);
   lda_28[1] = iClose(NULL, PERIOD_W1, 0);
   lda_32[1] = iLow(NULL, PERIOD_W1, 0);
   lda_36[1] = iHigh(NULL, PERIOD_W1, 0);
   lda_24[2] = iOpen(NULL, PERIOD_D1, 0);
   lda_28[2] = iClose(NULL, PERIOD_D1, 0);
   lda_32[2] = iLow(NULL, PERIOD_D1, 0);
   lda_36[2] = iHigh(NULL, PERIOD_D1, 0);
   lda_24[3] = iOpen(NULL, PERIOD_H4, 0);
   lda_28[3] = iClose(NULL, PERIOD_H4, 0);
   lda_32[3] = iLow(NULL, PERIOD_H4, 0);
   lda_36[3] = iHigh(NULL, PERIOD_H4, 0);
   lda_24[4] = iOpen(NULL, PERIOD_H1, 0);
   lda_28[4] = iClose(NULL, PERIOD_H1, 0);
   lda_32[4] = iLow(NULL, PERIOD_H1, 0);
   lda_36[4] = iHigh(NULL, PERIOD_H1, 0);
   string ls_0 = "|  ";
   for (int l_index_16 = 0; l_index_16 < 5; l_index_16++) {
      li_20 = (lda_36[l_index_16] - lda_32[l_index_16]) / Point;
      if (li_20 > 0) {
         if (lda_24[l_index_16] > lda_28[l_index_16]) ls_8 = " < ";
         else {
            if (lda_24[l_index_16] == lda_28[l_index_16]) ls_8 = " = ";
            else
               if (lda_24[l_index_16] < lda_28[l_index_16]) ls_8 = " > ";
         }
         ls_0 = ls_0 + lsa_48[l_index_16] + ls_8 + DoubleToStr(li_20, 0) + "   " + DoubleToStr((lda_28[l_index_16] - lda_32[l_index_16]) / (li_20 * Point), 3) + " |  ";
      }
   }
   Comment(ls_0);
   ld_unused_40 = Close[0];
}