#property copyright "Copyright © 2010.01.17, SwingMan"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 DodgerBlue

extern int maxBars = 500;
extern int Entry_Offset = 3;
extern string Parameters_NonLagDot = "_______________";
extern int Price = 0;
extern int Length = 20;
extern int Displace = 0;
extern int Filter = 0;
extern int Color = 1;
extern int ColorBarBack = 0;
extern double Deviation = 0.0;
extern bool EmailON = FALSE;
double gd_unused_124 = 4.0;
double g_ibuf_132[];
double g_ibuf_136[];
double gda_140[];
double gda_144[];
double gd_148;
bool gi_156;
bool gi_160;
int gi_164;

int init() {
   ArraySetAsSeries(gda_140, TRUE);
   ArraySetAsSeries(gda_144, TRUE);
   SetIndexBuffer(0, g_ibuf_132);
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, SYMBOL_ARROWUP);
   SetIndexLabel(0, "UP signal");
   SetIndexBuffer(1, g_ibuf_136);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, SYMBOL_ARROWDOWN);
   SetIndexLabel(1, "DOWN signal");
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   gi_164 = MarketInfo(Symbol(), MODE_DIGITS);
   if (gi_164 == 5 || gi_164 == 3) gi_164--;
   IndicatorDigits(gi_164);
   gd_148 = Entry_Offset * (1.0 / MathPow(10, gi_164));
   Delete_NameObjects("price_");
   return (0);
}

int deinit() {
   Delete_NameObjects("price_");
   return (0);
}

int start() {
   int li_0;
   ArrayResize(gda_140, Bars);
   ArrayResize(gda_144, Bars);
   int li_4 = IndicatorCounted();
   if (li_4 < 0) return (-1);
   if (li_4 > 0) li_4--;
   if (li_4 == 0) li_0 = maxBars;
   else li_0 = Bars - li_4;
   if (li_0 <= 2) li_0 = 0;
   for (int li_8 = li_0; li_8 >= 0; li_8--) Get_Indicators(li_8);
   return (0);
}

void Get_Indicators(int ai_0) {
   double ld_4;
   double l_icustom_12 = iCustom(Symbol(), Period(), "smNonlagDot", Price, Length, Displace, Filter, Color, ColorBarBack, Deviation, 1, ai_0);
   double l_icustom_20 = iCustom(Symbol(), Period(), "smNonlagDot", Price, Length, Displace, Filter, Color, ColorBarBack, Deviation, 2, ai_0);
   gda_140[ai_0] = 0;
   if (l_icustom_12 != EMPTY_VALUE && l_icustom_20 == EMPTY_VALUE) gda_140[ai_0] = 1;
   else
      if (l_icustom_12 == EMPTY_VALUE && l_icustom_20 != EMPTY_VALUE) gda_140[ai_0] = -1;
   double l_icustom_28 = iCustom(Symbol(), Period(), "smSupertrend1", 0, 0, ai_0);
   double l_icustom_36 = iCustom(Symbol(), Period(), "smSupertrend1", 0, 1, ai_0);
   gda_144[ai_0] = 0;
   if (l_icustom_28 != EMPTY_VALUE && l_icustom_36 == EMPTY_VALUE) gda_144[ai_0] = 1;
   else
      if (l_icustom_28 == EMPTY_VALUE && l_icustom_36 != EMPTY_VALUE) gda_144[ai_0] = -1;
   if (l_icustom_28 != EMPTY_VALUE && l_icustom_36 != EMPTY_VALUE) gda_144[ai_0] = gda_144[ai_0 + 1];
   double l_iopen_44 = iOpen(Symbol(), Period(), ai_0);
   double l_iclose_52 = iClose(Symbol(), Period(), ai_0);
   int li_60 = 0;
   if (l_iclose_52 > l_iopen_44) li_60 = 1;
   else
      if (l_iclose_52 < l_iopen_44) li_60 = -1;
   double ld_64 = iATR(Symbol(), Period(), 10, ai_0) / 2.0;
   if (gda_144[ai_0] == 1.0) {
      if (gi_156 == FALSE) {
         if (gda_140[ai_0] == 1.0) {
            if (l_icustom_12 > l_icustom_28) {
               if (li_60 == -1) {
                  g_ibuf_132[ai_0] = Low[ai_0] - ld_64;
                  g_ibuf_136[ai_0] = EMPTY_VALUE;
                  gi_156 = TRUE;
                  gi_160 = FALSE;
                  ld_4 = High[ai_0] + gd_148;
                  Alert("Andrew System:  ", Symbol(), "  ", "TF", Period(), " Buy at break of level ", ld_4);
                  if(EmailON)SendMail("Andrew System:  ", Symbol() + "  " + "TF" + Period() + " Buy at break of level " + ld_4);
                  Draw_PriceValue(ai_0, ld_4, LimeGreen);
               } else {
                  if (li_60 == 1) {
                     g_ibuf_132[ai_0] = EMPTY_VALUE;
                     gi_156 = FALSE;
                     ObjectDelete("price_" + TimeToStr(Time[ai_0]));
                  }
               }
            } else {
               if (l_icustom_12 < l_icustom_28) {
                  g_ibuf_132[ai_0] = EMPTY_VALUE;
                  gi_156 = FALSE;
                  ObjectDelete("price_" + TimeToStr(Time[ai_0]));
               }
            }
         }
      } else {
         if (gi_156 == TRUE) {
            if (gda_140[ai_0] == -1.0 && l_icustom_20 < l_icustom_28) gi_156 = FALSE;
            if (g_ibuf_132[ai_0] != EMPTY_VALUE) g_ibuf_132[ai_0] = Low[ai_0] - ld_64;
         }
      }
   } else {
      if (gda_144[ai_0] == -1.0) {
         if (gi_160 == FALSE) {
            if (gda_140[ai_0] == -1.0) {
               if (l_icustom_20 < l_icustom_36) {
                  if (li_60 == 1) {
                     g_ibuf_132[ai_0] = EMPTY_VALUE;
                     g_ibuf_136[ai_0] = High[ai_0] + ld_64;
                     gi_156 = FALSE;
                     gi_160 = TRUE;
                     ld_4 = Low[ai_0] - gd_148;
                     Alert("Andrew System:  ", Symbol(), "  ", "TF", Period(), " Sell at break of level ", ld_4);
                     if(EmailON){SendMail("Andrew System:  ", Symbol()+ "  " + "TF" + Period() + " Sell at break of level " + ld_4);}
                     Draw_PriceValue(ai_0, ld_4, DodgerBlue);
                  } else {
                     if (li_60 == -1) {
                        g_ibuf_136[ai_0] = EMPTY_VALUE;
                        gi_160 = FALSE;
                        ObjectDelete("price_" + TimeToStr(Time[ai_0]));
                     }
                  }
               } else {
                  if (l_icustom_20 > l_icustom_36) {
                     g_ibuf_136[ai_0] = EMPTY_VALUE;
                     gi_160 = FALSE;
                     ObjectDelete("price_" + TimeToStr(Time[ai_0]));
                  }
               }
            }
         } else {
            if (gi_160 == TRUE) {
               if (gda_140[ai_0] == 1.0 && l_icustom_12 > l_icustom_36) gi_160 = FALSE;
               if (g_ibuf_136[ai_0] != EMPTY_VALUE) g_ibuf_136[ai_0] = High[ai_0] + ld_64;
            }
         }
      }
   }
}

void Draw_PriceValue(int ai_0, double a_price_4, color a_color_12) {
   string l_name_16 = "price_" + TimeToStr(Time[ai_0]);
   int l_width_24 = 2;
   a_price_4 = NormalizeDouble(a_price_4, gi_164);
   ObjectCreate(l_name_16, OBJ_ARROW, 0, Time[ai_0], a_price_4);
   ObjectSet(l_name_16, OBJPROP_ARROWCODE, SYMBOL_LEFTPRICE);
   ObjectSet(l_name_16, OBJPROP_WIDTH, l_width_24);
   ObjectSet(l_name_16, OBJPROP_COLOR, a_color_12);
   ObjectSet(l_name_16, OBJPROP_BACK, FALSE);
}

void Delete_NameObjects(string as_unused_0) {
   ObjectsDeleteAll();
}