//----
#property indicator_separate_window
#property indicator_buffers 3

#property indicator_width1 1
#property indicator_color1 Yellow
#property indicator_width2 2
#property indicator_color2 DodgerBlue
#property indicator_width3 2
#property indicator_color3 Tomato



#property indicator_level1 0
#property indicator_level2 1
#property indicator_level3 2
#property indicator_level4 -1
#property indicator_level5 -2

extern int per=50;
extern bool ShowStats=true;



double   PiOsc[], PiOscUp[], PiOscDn[], Up[], Dn[];



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {



   IndicatorDigits(Digits+1);
   IndicatorBuffers(3);
   
   SetIndexBuffer(0,PiOsc);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,PiOscUp);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,PiOscDn);
   SetIndexStyle(2,DRAW_HISTOGRAM);      
   
//----

   IndicatorShortName("PiOsc("+IntegerToString(per)+")");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
  
  int counted_bars=IndicatorCounted();
  if (counted_bars>0) counted_bars--;
  int maxbars = Bars-counted_bars;
  
  
  double path=0, crow=0; 

  int totbars=0, trendbars=0, strtrendbars=0;
   
  for (int i=maxbars-1; i>=1; i--)   {
     totbars++;
     path+=MathAbs(Close[i]-Close[i+1])-MathAbs(Close[i+per]-Close[i+1+per]);  
     crow=2*3.141592654*(Close[i]-Close[i+per]);
     PiOscUp[i]=0; PiOscDn[i]=0;
     if (path>0) PiOsc[i]=crow/path;  
     if (PiOsc[i]>=0) PiOscUp[i]=PiOsc[i];
      else PiOscDn[i]=PiOsc[i]; 
     if (MathAbs(PiOsc[i])>1 && MathAbs(PiOsc[i])<2) trendbars++;
     if (MathAbs(PiOsc[i])>=2) strtrendbars++;                
  }   

   if (ShowStats==true) Comment("Total Bars: "+IntegerToString(totbars)+"\n"+"Trend Bars: "+DoubleToStr(100.0*trendbars/(1.0*totbars),2)+" %\n"+"Strong Trend Bars: "+DoubleToStr(100.0*strtrendbars/(1.0*totbars),2)+" %");
      else Comment("");
   return(0);
  }
//+------------------------------------------------------------------+