//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                     Copyright © 2010, Isoft LTD. |
//|                                                        gigel.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Isoft LTD."
#property link      "gigel.net"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
double m=1.0,
       a,
       b,
       c,
       d;
   for (int i=1;i<=Bars;i++)
{ExtMapBuffer1[i]=i;}                             //the main ideea is that the moving averages show when a trend is ending, and from
a=iMA(0,0,50,0,MODE_EMA,PRICE_CLOSE,i);           // the crossover the indicator determines when to count another trend
b=iMA(0,0,21,0,MODE_EMA,PRICE_CLOSE,i+1);
c=iMA(0,0,50,0,MODE_EMA,PRICE_CLOSE,i+2);
d=iMA(0,0,21,0,MODE_EMA,PRICE_CLOSE,i+3);

if(a>b&&c>d) {q++;                        //q counts the bars for the speed of the trend   
              j=Close[m]-Close[i];        //j is the differece of the closing prices. 
             
             }
   else if(a>b&&c<d) {
                      s[k]=q;             //
                      n[k]=j;             //
                      m=i-1;
                      q=0;
                      k++;                //k counts the trends
                      } 
if(a<b&&c<d) {
              q++;
              j=Close[m]-Close[i];
     
             }
   else if(a<b&&c>d) { 
                      s[k]=q;
                      n[k]=j;
                      m=i-1;
                      q=0;
                      k++;
                     }   
}

for(int o=1;o<=k;o++)
{suma_t+=n[s];                            //the sum of trend lenghts
suma_b+=s[o];                             //the sum of bars 
}

media_t=suma_t/k;                        //the average trend lenght
media_b=suma_b/k;                        //the average bar number


//ExtMapBuffer1[i]=  //bufer care sa afiseze media 
             
   //----
   return(0);
  }
//+------------------------------------------------------------------+