//+------------------------------------------------------------------+
//|                                Jim Sloman's natural market river |
//|                                                      ocn nmr.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  DimGray
#property indicator_color2  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_width2  2

//
//
//
//
//

extern int NMR_Period  = 40;
extern int NMR_Price   = PRICE_CLOSE;
extern int TEMA_Period =  1;

//
//
//
//
//

double nmr[];
double zeroLine[];
double tBuffer[][4];
double alpha;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine);
   SetIndexBuffer(1,nmr);

   //
   //
   //
   //
   //

   string PriceType;
      switch(NMR_Price)
      {
         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
      }      
   
      //
      //
      //
      //
      //
   
      TEMA_Period = MathMax(TEMA_Period,1);
      NMR_Period  = MathMax(NMR_Period ,1);
           alpha = 2.0 /(1.0 + TEMA_Period);
           
   IndicatorShortName ("nmr ("+NMR_Period+","+PriceType+","+TEMA_Period+")");
   return(0);
}
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iPrc 3

//
//
//
//
//

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,NMR_Price,i);
      double currPrice = iTema(rawPrice,i);
         if (currPrice > 0)
               tBuffer[r][iPrc] = MathLog(currPrice);
         else  tBuffer[r][iPrc] = 0.00;
      
         //
         //
         //
         //
         //
         
         nmr[i]      = iNmrFunction(1,i);
         zeroLine[i] = 0.00;
   }
   
   //
   //
   //
   //
   //
   
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//    natural market river
//
//
//

double iNmrFunction(int startPeriod, int tPos)
{
   int    endPeriod = startPeriod*NMR_Period;
   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 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]);
}

