//+------------------------------------------------------------------+
//|                   Jim Sloman's natural market mirror ocean index |
//|                                                                  |
//|                                                                  |
//|                                          ocn nmm ocean undex.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DimGray
#property indicator_color2  LimeGreen
#property indicator_color3  Green
#property indicator_color4  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT
#property indicator_width4  2

//
//
//
//
//

extern int    NMM_Period             = 21;
extern int    NMM_Price              = PRICE_CLOSE;
extern bool   Show_StandardDeviation = true;
extern int    SD_Period              = 30;
extern double SD_Up                  = 2.0;
extern double SD_Down                = 2.0;
extern bool   Show_HistoricalExtreme = true;
extern int    History_LookBack       = 15;

//
//
//
//
//

double nmm[];
double zeroLine[];
double sd[];
double hist[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,hist); SetIndexDrawBegin(1,NMM_Period+SD_Period);
   SetIndexBuffer(2,sd);   SetIndexDrawBegin(2,NMM_Period+SD_Period);
   SetIndexBuffer(3,nmm);  SetIndexDrawBegin(3,NMM_Period);
   
   //
   //
   //
   //
   //
   
   NMM_Period  = MathMax(NMM_Period ,1);

   IndicatorShortName ("NMM ocean index");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   double coef = 1000/MathSqrt(NMM_Period);
   int    counted_bars=IndicatorCounted();
   int    i,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;

   //
   //
   //
   //
   //

   for(i=limit, r=Bars-i-1; i >= 0; i--,r++)
   {
         double rawPrice1 = iMA(NULL,0,1,0,MODE_SMA,NMM_Price,i);
         double rawPrice2 = iMA(NULL,0,1,0,MODE_SMA,NMM_Price,i+NMM_Period);
         double price1    = 0.0;
         double price2    = 0.0;
               if (rawPrice1 >0) price1 = MathLog(rawPrice1);
               if (rawPrice2 >0) price2 = MathLog(rawPrice2);
         nmm[i]      = (price1-price2)*coef;
         zeroLine[i] = 0;
         
         //
         //
         //
         //
         //

         if (Show_StandardDeviation)
         {         
            sd[i] = 0;
            double avg = iSma(nmm,SD_Period,i);
            double deviation = iDeviation(nmm,avg,SD_Period,i);
               if (nmm[i] > 0) sd[i] = avg+deviation*SD_Up;
               if (nmm[i] < 0) sd[i] = avg-deviation*SD_Down;
         }
         
         //
         //
         //
         //
         //
         
         if (Show_HistoricalExtreme)
         {
            hist[i]  = hist[i+1];
               if (nmm[i] >  0) hist[i] = MathMax(0,nmm[ArrayMaximum(nmm,History_LookBack,i)]);
               if (nmm[i] <  0) hist[i] = MathMin(0,nmm[ArrayMinimum(nmm,History_LookBack,i)]);
               if (nmm[i] == 0) hist[i] = 0;
         }               
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iDeviation(double& array[],double dMA, int period, int pos)
{
   double dSum = 0;
         for(int i=0; i<period; i++,pos++) dSum += (array[pos]-dMA)*(array[pos]-dMA);
   return(MathSqrt(dSum/period));
}

//
//
//
//
//

double iSma(double& array[], int period, int pos)
{
   double sum = 0;
         for(int i=0; i<period; i++,pos++) sum += array[pos];
   return(sum/period);
}