//+------------------------------------------------------------------+
//|                                                     Lyanupov.mq4 |
//|                                         by jaguar1637@yahoo.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011 jaguar1637@yahoo.com"
#property link      "nope"
//----
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Crimson
//---- input parameters
extern int LyanupovPeriod = 525;
//---- buffers
double LyanupovBuffer[];
double ChaoticBuffer[];
double PredictableBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(3);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, LyanupovBuffer);
   //SetIndexStyle(1,DRAW_ARROW);
   //SetIndexArrow(1,233); //241 option for different arrow head
   SetIndexBuffer(1,ChaoticBuffer);
   //SetIndexArrow(2,234); //242 option for different arrow head
   //SetIndexStyle(2,DRAW_ARROW);
   SetIndexBuffer(2,PredictableBuffer); 
//---- indicator lines
   SetIndexEmptyValue(0,0.0);
   //SetIndexEmptyValue(1,0.0);
   //SetIndexEmptyValue(2,0.0);
//---- name for DataWindow
   SetIndexDrawBegin(0,LyanupovPeriod);   
   //SetIndexDrawBegin(1,LyanupovPeriod);
   //SetIndexDrawBegin(2,LyanupovPeriod);
   SetIndexLabel(0,"LyanupovLine");
   //SetIndexLabel(1,"Chaotic");
   //SetIndexLabel(2,"Predictable"); 
//---- name for DataWindow and indicator subwindow label
   short_name="Lyanupov" + LyanupovPeriod + ")";
   IndicatorShortName(short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Lyanupov Chaotic to Predictable market                                         |
//+------------------------------------------------------------------+
int start()
  {
   int    ii, k, counted_bars = IndicatorCounted();
   double price, sum, mul; 
   if(LyanupovPeriod <= 1)
       return(0);
   if(Bars <= LyanupovPeriod) 
       return(0);
//---- initial zero
  if(counted_bars < 1)
    {
      for(ii = 1; ii <= LyanupovPeriod; ii++) 
           LyanupovBuffer[Bars-ii] = 0.0;
      for(ii = 1; ii <= LyanupovPeriod; ii++) 
          ChaoticBuffer[Bars-ii] = 0.0;
      for(ii = 1; ii <= LyanupovPeriod; ii++) 
         PredictableBuffer[Bars-ii] =0.0;
    }
  int limit = Bars - counted_bars;
   if(counted_bars > 0) 
       limit++;
//---- moving average
   for(ii = 0; ii < limit; ii++)
   {
      //ChaoticBuffer[ii] = 0;
      //PredictableBuffer[ii] = 0;  
         
      LyanupovBuffer[ii]= MathLog(MathAbs( Close[ii+1]/Close[ii] ));
      //Comment("Close[ii+1]"+Close[ii+1]+"Close[ii]"+Close[ii],
      //      "\n Close[ii+1]/Close[ii]"+Close[ii+1]/Close[ii],
      //      "\nMathAbs(Close[ii+1]/Close[ii]"+MathAbs(Close[ii+1]/Close[ii]),
      //      "\nMathLog(MathAbs(Close[ii+1]/Close[ii]"+MathLog(MathAbs(Close[ii+1]/Close[ii]) ));
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+