#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Aquamarine
#property indicator_color4 Gold
#property indicator_color5 Blue
#property indicator_color6 Red

extern int Length = 20;
extern int Deviation = 1;
extern double MoneyRisk = 1.0;
extern int Signal = 1;
extern int Line = 1;
extern int Nbars = 1000;
double G_ibuf_104[];
double G_ibuf_108[];
double G_ibuf_112[];
double G_ibuf_116[];
double G_ibuf_120[];
double G_ibuf_124[];
extern bool SoundON = FALSE;
bool GI_132 = FALSE;
bool GI_136 = FALSE;

int init() {
   SetIndexBuffer(0, G_ibuf_104);
   SetIndexBuffer(1, G_ibuf_108);
   SetIndexBuffer(2, G_ibuf_112);
   SetIndexBuffer(3, G_ibuf_116);
   SetIndexBuffer(4, G_ibuf_120);
   SetIndexBuffer(5, G_ibuf_124);
   SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, 1);
   SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, 1);
   SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 1);
   SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 1);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexArrow(0, 159);
   SetIndexArrow(1, 159);
   SetIndexArrow(2, 108);
   SetIndexArrow(3, 108);
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
   string ls_0 = "BBands Stop(" + Length + "," + Deviation + ")";
   IndicatorShortName(ls_0);
   SetIndexLabel(0, "UpTrend Stop");
   SetIndexLabel(1, "DownTrend Stop");
   SetIndexLabel(2, "UpTrend Signal");
   SetIndexLabel(3, "DownTrend Signal");
   SetIndexLabel(4, "UpTrend Line");
   SetIndexLabel(5, "DownTrend Line");
   SetIndexDrawBegin(0, Length);
   SetIndexDrawBegin(1, Length);
   SetIndexDrawBegin(2, Length);
   SetIndexDrawBegin(3, Length);
   SetIndexDrawBegin(4, Length);
   SetIndexDrawBegin(5, Length);
   return (0);
}

int start() {
   int Li_8;
   double ldA_12[25000];
   double ldA_16[25000];
   double ldA_20[25000];
   double ldA_24[25000];
   for (int L_shift_4 = Nbars; L_shift_4 >= 0; L_shift_4--) {
      G_ibuf_104[L_shift_4] = 0;
      G_ibuf_108[L_shift_4] = 0;
      G_ibuf_112[L_shift_4] = 0;
      G_ibuf_116[L_shift_4] = 0;
      G_ibuf_120[L_shift_4] = EMPTY_VALUE;
      G_ibuf_124[L_shift_4] = EMPTY_VALUE;
   }
   for (L_shift_4 = Nbars - Length - 1; L_shift_4 >= 0; L_shift_4--) {
      ldA_12[L_shift_4] = iBands(NULL, 0, Length, Deviation, 0, PRICE_CLOSE, MODE_UPPER, L_shift_4);
      ldA_16[L_shift_4] = iBands(NULL, 0, Length, Deviation, 0, PRICE_CLOSE, MODE_LOWER, L_shift_4);
      if (Close[L_shift_4] > ldA_12[L_shift_4 + 1]) Li_8 = 1;
      if (Close[L_shift_4] < ldA_16[L_shift_4 + 1]) Li_8 = -1;
      if (Li_8 > 0 && ldA_16[L_shift_4] < ldA_16[L_shift_4 + 1]) ldA_16[L_shift_4] = ldA_16[L_shift_4 + 1];
      if (Li_8 < 0 && ldA_12[L_shift_4] > ldA_12[L_shift_4 + 1]) ldA_12[L_shift_4] = ldA_12[L_shift_4 + 1];
      ldA_20[L_shift_4] = ldA_12[L_shift_4] + (MoneyRisk - 1.0) / 2.0 * (ldA_12[L_shift_4] - ldA_16[L_shift_4]);
      ldA_24[L_shift_4] = ldA_16[L_shift_4] - (MoneyRisk - 1.0) / 2.0 * (ldA_12[L_shift_4] - ldA_16[L_shift_4]);
      if (Li_8 > 0 && ldA_24[L_shift_4] < ldA_24[L_shift_4 + 1]) ldA_24[L_shift_4] = ldA_24[L_shift_4 + 1];
      if (Li_8 < 0 && ldA_20[L_shift_4] > ldA_20[L_shift_4 + 1]) ldA_20[L_shift_4] = ldA_20[L_shift_4 + 1];
      if (Li_8 > 0) {
         if (Signal > 0 && G_ibuf_104[L_shift_4 + 1] == -1.0) {
            G_ibuf_112[L_shift_4] = ldA_24[L_shift_4];
            G_ibuf_104[L_shift_4] = ldA_24[L_shift_4];
            if (Line > 0) G_ibuf_120[L_shift_4] = ldA_24[L_shift_4];
            if (SoundON == TRUE && L_shift_4 == 0 && !GI_132) {
                Alert("BERUBAH", Symbol(), Period());
                GI_132 = TRUE;
               GI_136 = FALSE;
            }
         } else {
            G_ibuf_104[L_shift_4] = ldA_24[L_shift_4];
            if (Line > 0) G_ibuf_120[L_shift_4] = ldA_24[L_shift_4];
            G_ibuf_112[L_shift_4] = -1;
         }
         if (Signal == 2) G_ibuf_104[L_shift_4] = 0;
         G_ibuf_116[L_shift_4] = -1;
         G_ibuf_108[L_shift_4] = -1.0;
         G_ibuf_124[L_shift_4] = EMPTY_VALUE;
      }
      if (Li_8 < 0) {
         if (Signal > 0 && G_ibuf_108[L_shift_4 + 1] == -1.0) {
            G_ibuf_116[L_shift_4] = ldA_20[L_shift_4];
            G_ibuf_108[L_shift_4] = ldA_20[L_shift_4];
            if (Line > 0) G_ibuf_124[L_shift_4] = ldA_20[L_shift_4];
            if (SoundON == TRUE && L_shift_4 == 0 && !GI_136) {
               Alert("BERUBAH", Symbol(), Period());
               GI_136 = TRUE;
               GI_132 = FALSE;
            }
         } else {
            G_ibuf_108[L_shift_4] = ldA_20[L_shift_4];
            if (Line > 0) G_ibuf_124[L_shift_4] = ldA_20[L_shift_4];
            G_ibuf_116[L_shift_4] = -1;
         }
         if (Signal == 2) G_ibuf_108[L_shift_4] = 0;
         G_ibuf_112[L_shift_4] = -1;
         G_ibuf_104[L_shift_4] = -1.0;
         G_ibuf_120[L_shift_4] = EMPTY_VALUE;
      }
   }
   return (0);
}