//+------------------------------------------------------------------+
//|                 Jim Sloman's natural market slope with ocean mas |
//|                                            ocn nmc & ocn mas.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  Green
#property indicator_color3  Green
#property indicator_color4  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_style3  STYLE_DOT

//
//
//
//
//

extern int   NMCPeriod   = 40;
extern int   NMCPrice    = PRICE_CLOSE;
extern int   TEMAPeriod  =  1;

//
//
//
//
//

double nmc[];
double nma[];
double nmf[];
double zeroLine[];
double tBuffer[][9];
double alpha;
double oneFifth;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,nma);      SetIndexLabel(1,"NMA"); SetIndexDrawBegin(1,NMCPeriod);
   SetIndexBuffer(2,nmf);      SetIndexLabel(2,"NMF"); SetIndexDrawBegin(2,NMCPeriod);
   SetIndexBuffer(3,nmc);      SetIndexLabel(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);
           oneFifth = MathMax(MathRound(NMCPeriod/5),1);
           alpha    = 2.0 /(1.0 + TEMAPeriod);
           
   IndicatorShortName ("nmc & nmas ("+NMCPeriod+","+PriceType+","+TEMAPeriod+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iPrc 6
#define iPrp 7
#define iMom 8

//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    i,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,i);
         if (currPrice > 0)
               tBuffer[r][iPrc] = MathLog(currPrice);
         else  tBuffer[r][iPrc] = 0.00;
      
         //
         //
         //
         //
         //
         
            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);
            else   nmc[i] = -MathSqrt(-tmp);
            tBuffer[r][iMom] = nmc[i]-nmc[i+1];
            nma[i]           = nma[i+1]+iNmaRatio(r,NMCPeriod)                  *(nmc[i]-nma[i+1]);
            nmf[i]           = nmf[i+1]+iNmaRatio(r,iNmfPeriodLen(1,oneFifth,i))*(nmc[i]-nmf[i+1]);
         
         //
         //
         //
         //
         //
         
         zeroLine[i] = 0.00;
   }
   
   //
   //
   //
   //
   //

   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);
}

//
//
//
//
//

int iNmfPeriodLen(int startPeriod, int minPeriod,int tPos)
{
   int    endPeriod = startPeriod*NMCPeriod;
   int    pos       = Bars-tPos-1;
   int    maxlookb  = 0;
   double maxmomen  = 0.00;
   double currPrice = iTema(iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,tPos),tPos,3);
      
   
   tBuffer[pos][iPrp] = currPrice;

      for (int i= startPeriod; i <= endPeriod; i++)
      {
         double tMomentum  = MathAbs((currPrice-tBuffer[pos-i][iPrp])/MathSqrt(i));
            if (tMomentum > maxmomen)
            {
               maxmomen = tMomentum;
               maxlookb = i;
            }
         i += (startPeriod-1);
      }

   return(MathMax(maxlookb,minPeriod));
}

//
//
//
//
//

double iNmaRatio(int r,int period)
{
   double momRatio = 0.00;
   double sumMomen = 0.00;
   double ratio    = 0.00;

   //
   //
   //
   //
   //
   
      for (int k = 0; k<period; k++)
      {
         sumMomen += MathAbs(tBuffer[r-k][iMom]);
         momRatio +=         tBuffer[r-k][iMom]*(MathSqrt(k+1)-MathSqrt(k));
      }
   if (sumMomen != 0) ratio = MathAbs(momRatio)/sumMomen;
   return(ratio);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iTema(double price,int pos,int sbuf=0)
{
   int i  = Bars-pos-1;
   int ia = sbuf+0;
   int ib = sbuf+1;
   int ic = sbuf+2;
   
   if (i < 1)
      {
         tBuffer[i][ia] = price;
         tBuffer[i][ib] = price;
         tBuffer[i][ic] = price;
      }
   else
      {
         tBuffer[i][ia] = tBuffer[i-1][ia]+alpha*(price         -tBuffer[i-1][ia]);
         tBuffer[i][ib] = tBuffer[i-1][ib]+alpha*(tBuffer[i][ia]-tBuffer[i-1][ib]);
         tBuffer[i][ic] = tBuffer[i-1][ic]+alpha*(tBuffer[i][ib]-tBuffer[i-1][ic]);
      }
   return(3*tBuffer[i][ia] - 3*tBuffer[i][ib] + tBuffer[i][ic]);
}

