indicator error - initialization

 

Hi everybody,

I was trying to realize where is the problem on this indicator formula. When I insert it on the graphic, it's plot two horizontal lines, so, I have to open the code and click again on "Compile" button. Another problem is: When a new bar appears, the lines change dramatically for a second.


Here is the code:


//+------------------------------------------------------------------+
//|                                        Flat Market Indicator.mq4 |
//|                                 Copyright © 2010, wabbit.com.au. |
//|                                        http://www.wabbit.com.au  |
//|                                                                  |
//|                   {FLAT MARKET INDICATOR (FMI) by Piotr Wojdy?o} |
//|                                     {indicator of "flat market"} |
//|       http://trader.online.pl/MSZ/e-w-Flat_Market_Indicator.html |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, wabbit.com.au"
#property link      "http://www.wabbit.com.au"

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

extern int periods = 17;

double FMI[];
double media[];
double MMA[];
double SMMA[];
double TDF[];

extern int periodo=5;

int init()
{
   IndicatorBuffers(5) ;
   IndicatorDigits(Digits);

   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexBuffer(0,FMI);
   SetIndexDrawBegin(0, 6*periods);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,media);
   SetIndexDrawBegin(1, 6*periods);

   SetIndexBuffer(2,MMA);
   SetIndexBuffer(3,SMMA);
   SetIndexBuffer(4,TDF);

   return(0);
}

int deinit()
{
   return(0);
}

int start()
{
   int i;
   double IMPETMMA,IMPETSMMA,DIVMA,AVERIMPET,HHV,resultado;
   
   int counted_bars = IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars-1;
   
   for(i = limit; i >= 0; i--)
   {
      MMA[i] = iMA(NULL,0,periods,0,MODE_EMA,PRICE_CLOSE,i);
   }
   
   for(i = limit-1; i >= 0; i--)
   {
      SMMA[i] = iMAOnArray(MMA,0,periods,0,MODE_EMA,i);

      IMPETMMA = MMA[i] - MMA[i+1];
      IMPETSMMA = SMMA[i] - SMMA[i+1];
      
      DIVMA = MathAbs(MMA[i] - SMMA[i]);
      AVERIMPET= (IMPETMMA + IMPETSMMA)/2.0;
      TDF[i] = MathAbs(DIVMA * MathPow(AVERIMPET,3));

      HHV = TDF[ArrayMaximum(TDF,periods*3,i)];
      if(HHV!=0.0) FMI[i] = TDF[i]/HHV; else FMI[i] = 0.0;
      
      media[i]=iMAOnArray(FMI,0,periodo,0,MODE_EMA,i)*0.2;
    }

   return(0);
}
 

Anyone knows where is the problem?

Reason: