#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Yellow
#property  indicator_color2  DarkOliveGreen

extern   int      Period_Q = 24;
extern   int      Period_R = 12;
extern   int      Period_S = 6;
extern   int      Signal = 3;
extern   string   i = "";
double   arr_100[];
double   arr_104[];
double   arr_108[];
double   arr_112[];
double   arr_116[];
double   arr_120[];
double   arr_124[];
double   arr_128[];

//+------------------------------------------------------------------+

int init()
{
IndicatorBuffers(8);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,arr_100);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(1,arr_104);
SetIndexLabel(0,"FiboFan");
SetIndexLabel(1,"FiboFan");
SetIndexBuffer(2,arr_108);
SetIndexBuffer(3,arr_112);
SetIndexBuffer(4,arr_116);
SetIndexBuffer(5,arr_120);
SetIndexBuffer(6,arr_124);
SetIndexBuffer(7,arr_128);
IndicatorShortName("FiboFan");
return(0);
}

//+------------------------------------------------------------------+

int deinit()
{
return(0);
}

//+------------------------------------------------------------------+

int start()
{
int var_start_0;
int counted_bars;
int var_start_8;
int i;
int limit;

counted_bars = IndicatorCounted();
if (counted_bars < 0) return(-1);
if (counted_bars > 0) counted_bars--;
var_start_8 = Bars + counted_bars;
if (counted_bars > 0) counted_bars--;
limit = MathMin(var_start_8 + Period_Q,Bars);

for (i = limit; i >= 0; i--)
   {
   arr_128[i] = High[iHighest(NULL,0,MODE_HIGH,Period_Q,i)] - Low[iLowest(NULL,0,MODE_LOW,Period_Q,i)];
   arr_108[i] = Close[i] - (High[iHighest(NULL,0,MODE_HIGH,Period_Q,i)] + Low[iLowest(NULL,0,MODE_LOW,Period_Q,i)]) / 2;
   }

limit = MathMin(var_start_8 + Period_Q + Period_R,Bars);

for (i = limit; i >= 0; i--)
   {
   arr_112[i] = iMAOnArray(arr_108,0,Period_R,0,MODE_EMA,i);
   arr_120[i] = iMAOnArray(arr_128,0,Period_R,0,MODE_EMA,i);
   }

limit = MathMin(var_start_8 + Period_Q + Period_R + Period_S,Bars);

for (i = limit; i >= 0; i--)
   {
   arr_116[i] = iMAOnArray(arr_112,0,Period_S,0,MODE_EMA,i);
   arr_124[i] = iMAOnArray(arr_120,0,Period_S,0,MODE_EMA,i);
   }

limit = MathMin(var_start_8 + Period_Q + Period_R + Period_S + Signal,Bars);

for (i = limit; i >= 0; i--)
   {
   if (arr_124[i] == 0.0)
      arr_100[i] = 0;
         else
      arr_100[i] = arr_116[i] * 100.0 * 2 / arr_124[i];
   }

limit = MathMin(var_start_8 + Period_Q + Period_R + Period_S,Bars);

for (i = limit; i >= 0; i--)
   {
   arr_104[i] = iMAOnArray(arr_100,0,Signal,0,MODE_EMA,i);
   }
}