//+------------------------------------------------------------------+
//|                                Jim Sloman's natural market combo |
//|                   with Standard Deviation and historical extreme |
//|                                                                  |
//|                                            ocn nmc & SD hist.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  SeaGreen
#property indicator_color3  DimGray
#property indicator_color4  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT
#property indicator_style3  STYLE_DOT

//
//
//
//
//

extern int    NMCPeriod             = 40;
extern int    NMCPrice              = PRICE_CLOSE;
extern int    TEMAPeriod            = 10;
extern bool   ShowStandardDeviation = true;
extern int    SDPeriod              = 30;
extern double SDUp                  = 2.0;
extern double SDDown                = 2.0;
extern bool   ShowHistoricalExtreme = true;
extern int    HistoryLookBack       = 15;

//
//
//
//
//

double nmc[];
double zeroLine[];
double sd[];
double hist[];
double tBuffer[][4];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,hist); SetIndexDrawBegin(1,NMCPeriod+SDPeriod);
   SetIndexBuffer(2,sd);   SetIndexDrawBegin(2,NMCPeriod+SDPeriod);
   SetIndexBuffer(3,nmc);  SetIndexDrawBegin(3,NMCPeriod);
   
   //
   //
   //
   //
   //
   
   string PriceType;
      switch(NMCPrice)
      {
         case PRICE_CLOSE:    PriceType = "Close";    break;  // 0
         case PRICE_OPEN:     PriceType = "Open";     break;  // 1
         case PRICE_HIGH:     PriceType = "High";     break;  // 2
         case PRICE_LOW:      PriceType = "Low";      break;  // 3
         case PRICE_MEDIAN:   PriceType = "Median";   break;  // 4
         case PRICE_TYPICAL:  PriceType = "Typical";  break;  // 5
         case PRICE_WEIGHTED: PriceType = "Weighted"; break;  // 6
      }      
      TEMAPeriod = MathMax(TEMAPeriod,1);
      NMCPeriod  = MathMax(NMCPeriod ,1);
           alpha = 2.0 /(1.0 + TEMAPeriod);
   
   //
   //
   //
   //
   //

   IndicatorShortName ("nmc & SD hist ("+NMCPeriod+","+PriceType+","+TEMAPeriod+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iPrc 3

//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    i,k,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;
         if (ArrayRange(tBuffer,0) != Bars) ArrayResize(tBuffer,Bars);

   //
   //
   //
   //
   //

   for(i=limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      double rawPrice  = iMA(NULL,0,1,0,MODE_SMA,NMCPrice,i);
      double currPrice = iTema(rawPrice,r);
         if (currPrice > 0)
               tBuffer[r][iPrc] = MathLog(currPrice);
         else  tBuffer[r][iPrc] = 0.00;
      
         //
         //
         //
         //
         //
         
         nmc[i]      = 0;
         zeroLine[i] = 0;
            double nmm = iNmmFunction(1,i);
            double nmr = iNmrFunction(1,i);
            double tmp = (MathAbs(nmm)*nmr+MathAbs(nmr)*nmm)/2.00;
               if (tmp > 0) nmc[i] =  MathSqrt( tmp);
               if (tmp < 0) nmc[i] = -MathSqrt(-tmp);
         
         //
         //
         //
         //
         //

         if (ShowStandardDeviation)
         {         
            sd[i] = 0;
            double avg = iSma(nmc,SDPeriod,i);
            double deviation = iDeviation(nmc,avg,SDPeriod,i);
               if (nmc[i] > 0) sd[i] = avg+deviation*SDUp;
               if (nmc[i] < 0) sd[i] = avg-deviation*SDDown;
         }
         
         //
         //
         //
         //
         //
         
         if (ShowHistoricalExtreme)
         {
            hist[i]  = hist[i+1];
               if (nmc[i] >  0) hist[i] = MathMax(0,nmc[ArrayMaximum(nmc,HistoryLookBack,i)]);
               if (nmc[i] <  0) hist[i] = MathMin(0,nmc[ArrayMinimum(nmc,HistoryLookBack,i)]);
               if (nmc[i] == 0) hist[i] = 0;
         }               
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//    natural market mirror
//
//
//

double iNmmFunction(int startPeriod, int tPos)
{
   int    endPeriod = startPeriod*NMCPeriod;
   int    pos       = Bars-tPos-1;
   double sum       = 0.00;
   double currPrice = tBuffer[pos][iPrc];
      
   for (int i= startPeriod; i <= endPeriod; i += startPeriod)
           sum += (currPrice-tBuffer[pos-i][iPrc])/MathSqrt(i);
   return((sum/NMCPeriod)*1000.00);
}

//
//
//
//    natural market river
//
//
//

double iNmrFunction(int startPeriod, int tPos)
{
   int    endPeriod = startPeriod*NMCPeriod;
   int    pos       = Bars-tPos-1;
   double sum       = 0.00;
      
   for (int i=startPeriod; i <= endPeriod; i+=startPeriod)
          sum += (tBuffer[pos-i+startPeriod][iPrc]-tBuffer[pos-i][iPrc])*(MathSqrt(i)-MathSqrt(i-startPeriod));
   return(sum*1000.00);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

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);
}

//
//
//
//
//

double iTema(double price,int i)
{
   if (i < 1)
      {
         tBuffer[i][0] = price;
         tBuffer[i][1] = price;
         tBuffer[i][2] = price;
      }
   else
      {
         tBuffer[i][0] = tBuffer[i-1][0]+alpha*(price        -tBuffer[i-1][0]);
         tBuffer[i][1] = tBuffer[i-1][1]+alpha*(tBuffer[i][0]-tBuffer[i-1][1]);
         tBuffer[i][2] = tBuffer[i-1][2]+alpha*(tBuffer[i][1]-tBuffer[i-1][2]);
      }
   return(3*tBuffer[i][0] - 3*tBuffer[i][1] + tBuffer[i][2]);
}