
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Chocolate
#property indicator_color2 Chocolate
#property indicator_color3 Chocolate
#property indicator_color4 Chocolate
#property indicator_color5 Chocolate
#property indicator_color6 Chocolate

extern bool Show.Gauge = TRUE;
extern int myChartX = 10;
extern int myChartY = 100;
extern int myCorner = 1;
extern string myFont = "Courier New";
extern int myFontSize = 12;
extern bool Show.PriceBox = TRUE;
extern int myLineWidth = 3;
extern int myBoxWidth = 3;
extern int BandsPeriod = 25;
extern int BandsShift = 0;
extern double BandsDeviations = 2.0;
extern color colorHead = Orange;
extern color colorTop = Chocolate;
extern color colorMid = Chocolate;
extern color colorBot = Chocolate;
extern color colorUP = Lime;
extern color colorEQ = Yellow;
extern color colorDN = Red;
extern color colorUPfill = DarkGreen;
extern color colorDNfill = Maroon;
double g_ibuf_168[];
double g_ibuf_172[];
double g_ibuf_176[];
double g_ibuf_180[];
double g_ibuf_184[];
double g_ibuf_188[];
string gs_dummy_192;
string g_name_200;
string g_name_208;
string g_name_216;
string gs_dummy_224;
string gs_dummy_232;
string gs_dummy_240;
string gs_dummy_248;
string gs_dummy_256;
string gs_dummy_264;
string gs_dummy_272;
int gi_280 = 11;
int gi_unused_284;
int gi_288;
int gi_292;
int gi_296;
int gi_unused_300;
int gi_unused_304;
int gi_unused_308;
string g_symbol_312;
string gs_320;
string gs_328;
string gs_336;
int gi_348;
int g_timeframe_352;
int gi_unused_356;
string gsa_360[15];
string gsa_364[15];
int gi_368;
int g_index_372;
string gs_dummy_376;
string gs_dummy_384;
string gs_dummy_392;
string gs_dummy_400;
string gs_dummy_408;
string gs_dummy_416;
string gs_dummy_424;
string gs_dummy_432;
string gs_dummy_440;
double g_iclose_448;
double gd_456;
double gd_464;
double gd_472;
int g_color_496 = Blue;

int init() {
   SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, myLineWidth, colorUPfill);
   SetIndexBuffer(0, g_ibuf_180);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, myLineWidth, colorTop);
   SetIndexBuffer(1, g_ibuf_168);
   SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, myLineWidth, colorDNfill);
   SetIndexBuffer(2, g_ibuf_184);
   SetIndexStyle(3, DRAW_LINE, STYLE_SOLID, myLineWidth, colorBot);
   SetIndexBuffer(3, g_ibuf_176);
   SetIndexStyle(4, DRAW_HISTOGRAM, STYLE_SOLID, myLineWidth, colorUPfill);
   SetIndexBuffer(4, g_ibuf_188);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID, myLineWidth, colorMid);
   SetIndexBuffer(5, g_ibuf_172);
   SetIndexDrawBegin(0, BandsPeriod + BandsShift);
   SetIndexDrawBegin(1, BandsPeriod + BandsShift);
   SetIndexDrawBegin(2, BandsPeriod + BandsShift);
   g_name_200 = "bb002";
   g_name_208 = "bb003";
   g_name_216 = "bb004";
   g_timeframe_352 = Period();
   gs_320 = TimeFrameToString(g_timeframe_352);
   g_symbol_312 = Symbol();
   gi_348 = Digits;
   gd_472 = Point;
   gs_336 = "bb" + g_symbol_312 + g_timeframe_352;
   gi_unused_356 = 0;
   if (gi_348 == 5 || gi_348 == 3) {
      gi_348--;
      gd_472 = 10.0 * gd_472;
   }
   gs_328 = "bb";
   ObDeleteObjectsByPrefix(gs_328);
   if (myCorner == 0 || myCorner == 2) {
      gi_unused_284 = 0;
      gi_288 = 0;
      gi_292 = 90;
      gi_296 = 150;
      gi_unused_300 = 100;
      gi_unused_304 = 90;
      gi_unused_308 = 90;
      gi_280 = 20;
   } else {
      gi_unused_284 = 0;
      gi_288 = 10;
      gi_292 = 60;
      gi_296 = 0;
      gi_unused_300 = 10;
      gi_unused_304 = 10;
      gi_unused_308 = 10;
      gi_280 = 23;
   }
   return (0);
}

void ObDeleteObjectsByPrefix(string as_0) {
   string l_name_16;
   int l_str_len_8 = StringLen(as_0);
   int li_12 = 0;
   while (li_12 < ObjectsTotal()) {
      l_name_16 = ObjectName(li_12);
      if (StringSubstr(l_name_16, 0, l_str_len_8) != as_0) {
         li_12++;
         continue;
      }
      ObjectDelete(l_name_16);
   }
}

int deinit() {
   ObDeleteObjectsByPrefix("bb");
   TRO();
   return (0);
}

int start() {
   int li_4;
   double ld_12;
   double ld_20;
   double ld_28;
   double ld_36;
   int l_ind_counted_8 = IndicatorCounted();
   if (Bars <= BandsPeriod) return (0);
   if (l_ind_counted_8 < 1) {
      for (int li_0 = 1; li_0 <= BandsPeriod; li_0++) {
         g_ibuf_168[Bars - li_0] = EMPTY_VALUE;
         g_ibuf_172[Bars - li_0] = EMPTY_VALUE;
         g_ibuf_176[Bars - li_0] = EMPTY_VALUE;
      }
   }
   int li_44 = Bars - l_ind_counted_8;
   if (l_ind_counted_8 > 0) li_44++;
   for (li_0 = 0; li_0 < li_44; li_0++) g_ibuf_168[li_0] = iMA(NULL, 0, BandsPeriod, BandsShift, MODE_SMA, PRICE_CLOSE, li_0);
   li_0 = Bars - BandsPeriod + 1;
   if (l_ind_counted_8 > BandsPeriod - 1) li_0 = Bars - l_ind_counted_8 - 1;
   while (li_0 >= 0) {
      ld_20 = 0.0;
      li_4 = li_0 + BandsPeriod - 1;
      ld_28 = g_ibuf_168[li_0];
      while (li_4 >= li_0) {
         ld_36 = Close[li_4] - ld_28;
         ld_20 += ld_36 * ld_36;
         li_4--;
      }
      ld_12 = BandsDeviations * MathSqrt(ld_20 / BandsPeriod);
      g_ibuf_172[li_0] = ld_28 + ld_12;
      g_ibuf_176[li_0] = ld_28 - ld_12;
      g_ibuf_180[li_0] = g_ibuf_172[li_0];
      g_ibuf_188[li_0] = g_ibuf_168[li_0];
      g_ibuf_184[li_0] = g_ibuf_168[li_0];
      li_0--;
   }
   if (Show.PriceBox) {
      if (ObjectFind(g_name_200) != 0) {
         ObjectCreate(g_name_200, OBJ_ARROW, 0, Time[0], g_ibuf_172[0]);
         ObjectSet(g_name_200, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
         ObjectSet(g_name_200, OBJPROP_COLOR, colorTop);
         ObjectSet(g_name_200, OBJPROP_WIDTH, myBoxWidth);
      } else {
         ObjectMove(g_name_200, 0, Time[0], g_ibuf_172[0]);
         ObjectSet(g_name_200, OBJPROP_COLOR, colorTop);
      }
      if (ObjectFind(g_name_208) != 0) {
         ObjectCreate(g_name_208, OBJ_ARROW, 0, Time[0], g_ibuf_176[0]);
         ObjectSet(g_name_208, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
         ObjectSet(g_name_208, OBJPROP_COLOR, colorBot);
         ObjectSet(g_name_208, OBJPROP_WIDTH, myBoxWidth);
      } else {
         ObjectMove(g_name_208, 0, Time[0], g_ibuf_176[0]);
         ObjectSet(g_name_208, OBJPROP_COLOR, colorBot);
      }
      if (ObjectFind(g_name_216) != 0) {
         ObjectCreate(g_name_216, OBJ_ARROW, 0, Time[0], g_ibuf_168[0]);
         ObjectSet(g_name_216, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
         ObjectSet(g_name_216, OBJPROP_COLOR, colorMid);
         ObjectSet(g_name_216, OBJPROP_WIDTH, myBoxWidth);
      } else {
         ObjectMove(g_name_216, 0, Time[0], g_ibuf_168[0]);
         ObjectSet(g_name_216, OBJPROP_COLOR, colorMid);
      }
   }
   if (Show.Gauge) DoShowGauge();
   ObjectsRedraw();
   return (0);
}

void DoShowGauge() {
   gi_368 = 10;
   g_index_372 = 0;
   g_iclose_448 = iClose(g_symbol_312, g_timeframe_352, 0);
   gsa_360[g_index_372] = gs_336 + g_index_372;
   gsa_364[g_index_372] = fFill(gs_320 + " Bollinger(" + BandsPeriod + "," + DoubleToStr(BandsDeviations, 1) + ")", gi_280);
   DoShow(g_index_372, colorHead);
   g_index_372++;
   gsa_360[g_index_372] = gs_336 + g_index_372;
   gd_464 = g_ibuf_172[0];
   gd_456 = (g_iclose_448 - gd_464) / gd_472;
   DoColor(gd_456, 0);
   gsa_364[g_index_372] = fFill("Upper BB", gi_280);
   DoShow(g_index_372, g_color_496);
   g_index_372++;
   gsa_360[g_index_372] = gs_336 + g_index_372;
   gd_464 = g_ibuf_168[0];
   gd_456 = (g_iclose_448 - gd_464) / gd_472;
   DoColor(gd_456, 0);
   gsa_364[g_index_372] = fFill("Middle BB", gi_280);
   DoShow(g_index_372, g_color_496);
   g_index_372++;
   gsa_360[g_index_372] = gs_336 + g_index_372;
   gd_464 = g_ibuf_176[0];
   gd_456 = (g_iclose_448 - gd_464) / gd_472;
   DoColor(gd_456, 0);
   gsa_364[g_index_372] = fFill("Lower BB", gi_280);
   DoShow(g_index_372, g_color_496);
   g_index_372++;
   gsa_360[g_index_372] = gs_336 + g_index_372;
   gd_464 = (g_ibuf_172[0] - g_ibuf_176[0]) / gd_472;
   gd_456 = 100.0 * (g_iclose_448 - g_ibuf_176[0]) / (g_ibuf_172[0] - g_ibuf_176[0]);
   DoColor(gd_456, 50);
   gsa_364[g_index_372] = fFill("Range| %", gi_280);
   DoShow(g_index_372, g_color_496);
}

void DoShow(int ai_0, color a_color_4) {
   string l_name_8;
   string l_text_16;
   string l_name_24;
   string l_text_32;
   ObjectCreate(gsa_360[ai_0], OBJ_LABEL, 0, Time[0], 0);
   ObjectSet(gsa_360[ai_0], OBJPROP_CORNER, myCorner);
   ObjectSet(gsa_360[ai_0], OBJPROP_XDISTANCE, gi_288 + myChartX);
   ObjectSet(gsa_360[ai_0], OBJPROP_YDISTANCE, gi_368 + myChartY);
   ObjectSetText(gsa_360[ai_0], gsa_364[ai_0], myFontSize, myFont, a_color_4);
   if (ai_0 > 0) {
      l_name_8 = gsa_360[ai_0] + "val";
      l_text_16 = fFill(DoubleToStr(gd_464, gi_348), 7);
      ObjectCreate(l_name_8, OBJ_LABEL, 0, Time[0], 0);
      ObjectSet(l_name_8, OBJPROP_CORNER, myCorner);
      ObjectSet(l_name_8, OBJPROP_XDISTANCE, gi_292 + myChartX);
      ObjectSet(l_name_8, OBJPROP_YDISTANCE, gi_368 + myChartY);
      ObjectSetText(l_name_8, l_text_16, myFontSize, myFont, a_color_4);
      l_name_24 = gsa_360[ai_0] + "dif";
      l_text_32 = rtadjust(DoubleToStr(gd_456, 0));
      ObjectCreate(l_name_24, OBJ_LABEL, 0, Time[0], 0);
      ObjectSet(l_name_24, OBJPROP_CORNER, myCorner);
      ObjectSet(l_name_24, OBJPROP_XDISTANCE, gi_296 + myChartX);
      ObjectSet(l_name_24, OBJPROP_YDISTANCE, gi_368 + myChartY);
      ObjectSetText(l_name_24, l_text_32, myFontSize, myFont, a_color_4);
   }
   gi_368 += 20;
}

void DoColor(double ad_0, double ad_8) {
   if (ad_0 > ad_8) {
      g_color_496 = colorUP;
      return;
   }
   if (ad_0 < ad_8) {
      g_color_496 = colorDN;
      return;
   }
   g_color_496 = colorEQ;
}

string TimeFrameToString(int ai_0) {
   string ls_ret_4;
   switch (ai_0) {
   case 1:
      ls_ret_4 = "M1";
      break;
   case 5:
      ls_ret_4 = "M5";
      break;
   case 15:
      ls_ret_4 = "M15";
      break;
   case 30:
      ls_ret_4 = "M30";
      break;
   case 60:
      ls_ret_4 = "H1";
      break;
   case 240:
      ls_ret_4 = "H4";
      break;
   case 1440:
      ls_ret_4 = "D1";
      break;
   case 10080:
      ls_ret_4 = "W1";
      break;
   case 43200:
      ls_ret_4 = "MN";
   }
   return (ls_ret_4);
}

string fFill(string as_0, int ai_8) {
   string ls_unused_12;
   return (StringSubstr(as_0 + "                                         ", 0, ai_8));
}

string rtadjust(string as_0) {
   int l_str_len_8 = StringLen(as_0);
   if (l_str_len_8 != 5) {
      if (l_str_len_8 == 4) as_0 = " " + as_0;
      else {
         if (l_str_len_8 == 3) as_0 = "  " + as_0;
         else {
            if (l_str_len_8 == 2) as_0 = "   " + as_0;
            else
               if (l_str_len_8 == 1) as_0 = "    " + as_0;
         }
      }
   }
   return (as_0);
}

void TRO() {
   string l_name_0 = "TROTAG";
   ObjectCreate(l_name_0, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(l_name_0, CharToStr(78), 12, "Wingdings", DimGray);
   ObjectSet(l_name_0, OBJPROP_CORNER, 3);
   ObjectSet(l_name_0, OBJPROP_XDISTANCE, 5);
   ObjectSet(l_name_0, OBJPROP_YDISTANCE, 5);
}