#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Black
#property indicator_color2 Blue
#property indicator_color3 Red

extern int period = 15;
double buf_1[];
double buf_2[];
double buf_3[];

int init() {
   SetIndexStyle(0, DRAW_NONE);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   IndicatorDigits(Digits + 1);
   SetIndexBuffer(0, buf_1);
   SetIndexBuffer(1, buf_2);
   SetIndexBuffer(2, buf_3);
   IndicatorShortName("SS2009_B");
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL);
   return (0);
}

int start() {
   double histogramm_1;
   double histogramm_0;
   double medium;
   int indicator_counted = IndicatorCounted();
   double maxmin = 0;
   double maxmin2 = 0;
   double ld_unused_48 = 0;
   double ld_unused_56 = 0;
   double result1 = 0;
   double ld_unused_72 = 0;
   double low = 0;
   double high = 0;
   if (indicator_counted > 0) indicator_counted--;
   int li_0 = Bars - indicator_counted;
   for (int i = 0; i < Bars; i++) {
      high = High[iHighest(NULL, 0, MODE_HIGH, period, i)];
      low = Low[iLowest(NULL, 0, MODE_LOW, period, i)];
      medium = (High[i] + Low[i]) / 2.0;
      if (high - low == 0.0) maxmin = 0.67 * maxmin2 + (-0.33);
      else maxmin = 0.66 * ((medium - low) / (high - low) - 0.5) + 0.67 * maxmin2;
      maxmin = MathMin(MathMax(maxmin, -0.999), 0.999);
      if (1 - maxmin == 0.0) buf_1[i] = result1 / 2.0 + 0.5;
      else buf_1[i] = MathLog((maxmin + 1.0) / (1 - maxmin)) / 2.0 + result1 / 2.0;
      maxmin2 = maxmin;
      result1 = buf_1[i];
   }
   bool revers = TRUE;
   for (i = Bars; i >= 0; i--) {
      histogramm_0 = buf_1[i];
      histogramm_1 = buf_1[i + 1];
      if ((histogramm_0 < 0.0 && histogramm_1 > 0.0) || histogramm_0 < 0.0) revers = FALSE;
      if ((histogramm_0 > 0.0 && histogramm_1 < 0.0) || histogramm_0 > 0.0) revers = TRUE;
      if (!revers) {
         buf_3[i] = histogramm_0;
         buf_2[i] = 0.0;
      } else {
         buf_2[i] = histogramm_0;
         buf_3[i] = 0.0;
      }
   }
   return (0);
}