
#property copyright "Copyright © 2008, 2hrfx.com"
#property link      "http://www.2hrfx.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 ForestGreen
#property indicator_color3 Red

extern int Price = 0;
extern int Length = 20;
extern int Displace = 0;
extern int Filter = 0;
extern int Color = 1;
extern int ColorBarBack = 1;
extern double Deviation = 0.0;
double g_ibuf_108[];
double g_ibuf_112[];
double g_ibuf_116[];
double g_ibuf_120[];
double gda_124[];
int g_index_128;
int gi_132;
int gi_136;
int gi_140 = 4;
double gd_144;
double gd_152;
double gd_160;
double gd_168;
double gd_176;
double gd_184;
double gd_192 = 3.1415926535;

int init() {
   IndicatorBuffers(4);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, g_ibuf_108);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, g_ibuf_112);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, g_ibuf_116);
   SetIndexBuffer(3, g_ibuf_120);
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS));
   string ls_0 = "SaneFX(" + Length + ")";
   IndicatorShortName(ls_0);
   SetIndexLabel(0, "SaneFX");
   SetIndexLabel(1, "Up");
   SetIndexLabel(2, "Dn");
   SetIndexShift(0, Displace);
   SetIndexShift(1, Displace);
   SetIndexShift(2, Displace);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexEmptyValue(2, EMPTY_VALUE);
   SetIndexDrawBegin(0, Length * gi_140 + Length);
   SetIndexDrawBegin(1, Length * gi_140 + Length);
   SetIndexDrawBegin(2, Length * gi_140 + Length);
   gd_144 = 3.0 * gd_192;
   gi_132 = Length - 1;
   gi_136 = Length * gi_140 + gi_132;
   ArrayResize(gda_124, gi_136);
   gd_176 = 0;
   for (g_index_128 = 0; g_index_128 < gi_136 - 1; g_index_128++) {
      if (g_index_128 <= gi_132 - 1) gd_160 = 1.0 * g_index_128 / (gi_132 - 1);
      else gd_160 = (g_index_128 - gi_132 + 1) * (2.0 * gi_140 - 1.0) / (gi_140 * Length - 1.0) + 1.0;
      gd_152 = MathCos(gd_192 * gd_160);
      gd_184 = 1.0 / (gd_144 * gd_160 + 1.0);
      if (gd_160 <= 0.5) gd_184 = 1;
      gda_124[g_index_128] = gd_184 * gd_152;
      gd_176 += gda_124[g_index_128];
   }
   return (0);
}

int start() {
   int li_12;
   double ld_16;
   int l_ind_counted_8 = IndicatorCounted();
   if (l_ind_counted_8 > 0) li_12 = Bars - l_ind_counted_8;
   if (l_ind_counted_8 < 0) return (0);
   if (l_ind_counted_8 == 0) li_12 = Bars - gi_136 - 1;
   if (l_ind_counted_8 < 1) {
      for (int l_index_0 = 1; l_index_0 < Length * gi_140 + Length; l_index_0++) {
         g_ibuf_108[Bars - l_index_0] = 0;
         g_ibuf_112[Bars - l_index_0] = 0;
         g_ibuf_116[Bars - l_index_0] = 0;
      }
   }
   for (int li_4 = li_12; li_4 >= 0; li_4--) {
      gd_168 = 0;
      for (l_index_0 = 0; l_index_0 <= gi_136 - 1; l_index_0++) {
         if (Price == 0) ld_16 = Close[li_4 + l_index_0];
         else {
            if (Price == 1) ld_16 = Open[li_4 + l_index_0];
            else {
               if (Price == 2) ld_16 = High[li_4 + l_index_0];
               else {
                  if (Price == 3) ld_16 = Low[li_4 + l_index_0];
                  else {
                     if (Price == 4) ld_16 = (High[li_4 + l_index_0] + (Low[li_4 + l_index_0])) / 2.0;
                     else {
                        if (Price == 5) ld_16 = (High[li_4 + l_index_0] + (Low[li_4 + l_index_0]) + (Close[li_4 + l_index_0])) / 3.0;
                        else
                           if (Price == 6) ld_16 = (High[li_4 + l_index_0] + (Low[li_4 + l_index_0]) + 2.0 * (Close[li_4 + l_index_0])) / 4.0;
                     }
                  }
               }
            }
         }
         gd_168 += gda_124[l_index_0] * ld_16;
      }
      if (gd_176 > 0.0) g_ibuf_108[li_4] = (Deviation / 100.0 + 1.0) * gd_168 / gd_176;
      if (Filter > 0)
         if (MathAbs(g_ibuf_108[li_4] - (g_ibuf_108[li_4 + 1])) < Filter * Point) g_ibuf_108[li_4] = g_ibuf_108[li_4 + 1];
      if (Color > 0) {
         g_ibuf_120[li_4] = g_ibuf_120[li_4 + 1];
         if (g_ibuf_108[li_4] - (g_ibuf_108[li_4 + 1]) > Filter * Point) g_ibuf_120[li_4] = 1;
         if (g_ibuf_108[li_4 + 1] - g_ibuf_108[li_4] > Filter * Point) g_ibuf_120[li_4] = -1;
         if (g_ibuf_120[li_4] > 0.0) {
            g_ibuf_112[li_4] = g_ibuf_108[li_4];
            if (g_ibuf_120[li_4 + ColorBarBack] < 0.0) g_ibuf_112[li_4 + ColorBarBack] = g_ibuf_108[li_4 + ColorBarBack];
            g_ibuf_116[li_4] = EMPTY_VALUE;
         }
         if (g_ibuf_120[li_4] < 0.0) {
            g_ibuf_116[li_4] = g_ibuf_108[li_4];
            if (g_ibuf_120[li_4 + ColorBarBack] > 0.0) g_ibuf_116[li_4 + ColorBarBack] = g_ibuf_108[li_4 + ColorBarBack];
            g_ibuf_112[li_4] = EMPTY_VALUE;
         }
      }
   }
   return (0);
}
