//+------------------------------------------------------------------+
//|                Jim Sloman's natural market mirror with ocean mas |
//|                                            ocn nmm & ocn mas.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1  DimGray
#property indicator_color2  LimeGreen
#property indicator_color3  Red
#property indicator_color4  Green
#property indicator_color5  LimeGreen
#property indicator_color6  DeepSkyBlue
#property indicator_style1  STYLE_DOT
#property indicator_style5  STYLE_DOT
#property indicator_width6  2


//
//
//
//
//

#import "dynamicZone.dll"
   double dzBuyP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precision);
   double dzSellP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precision);
#import

//
//
//
//
//

extern int    NMMPeriod              = 40;
extern int    NMMPrice               = PRICE_CLOSE;
extern int    TEMAPeriod             = 10;
extern int    ZoneOn                 = 0;
extern int    DzLookBackBars         = 70;
extern double DzStartBuyProbability  = 0.10;
extern double DzStartSellProbability = 0.10;

//
//
//
//
//

double nmm[];
double nma[];
double nmf[];
double bli[];
double sli[];
double zeroLine[];
double tBuffer[][9];
double alpha;
double oneFifth;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,zeroLine); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,bli);      SetIndexLabel(1,NULL);   
   SetIndexBuffer(2,sli);      SetIndexLabel(2,NULL);   
   SetIndexBuffer(3,nma);      SetIndexLabel(3,"nma");      SetIndexDrawBegin(3,NMMPeriod);
   SetIndexBuffer(4,nmf);      SetIndexLabel(4,"nma fast"); SetIndexDrawBegin(4,NMMPeriod);
   SetIndexBuffer(5,nmm);      SetIndexLabel(5,"nmr");      SetIndexDrawBegin(5,NMMPeriod);
 
   //
   //
   //
   //
   //

      ZoneOn        = MathMax(MathMin(ZoneOn,2),0);
      TEMAPeriod    = MathMax(TEMAPeriod,1);
      NMMPeriod     = MathMax(NMMPeriod ,1);
           oneFifth = MathMax(MathRound(NMMPeriod/5),1);
           alpha    = 2.0 /(1.0 + TEMAPeriod);

   
   //
   //
   //
   //
   //

   string PriceType;
      switch(NMMPrice)
      {
         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
      }      
   string ZoneName = "";
      switch(ZoneOn)
      {
         case 0 : ZoneName = "nmm"; break;
         case 1 : ZoneName = "nms"; break;
         case 2 : ZoneName = "nmf"; break;
      }                 
   IndicatorShortName("dynamic zone nmm & nmas (zone on "+ZoneName+") ("+NMMPeriod+","+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 = MathMin(Bars-counted_bars,Bars-1);
         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,NMMPrice,i);
      double currPrice = iTema(rawPrice,i);
         if (currPrice > 0)
               tBuffer[r][iPrc] = MathLog(currPrice);
         else  tBuffer[r][iPrc] = 0.00;
      
         //
         //
         //
         //
         //
         
            nmm[i]           = iNmmFunction(1,i);
            tBuffer[r][iMom] = nmm[i]-nmm[i+1];
            nma[i]           = nma[i+1]+iNmaRatio(r,NMMPeriod)                  *(nmm[i]-nma[i+1]);
            nmf[i]           = nmf[i+1]+iNmaRatio(r,iNmfPeriodLen(1,oneFifth,i))*(nmm[i]-nmf[i+1]);
         
         //
         //
         //
         //
         //
         
         switch(ZoneOn)
         {
            case 0: 
               bli[i]      = dzBuyP (nmm, DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.0001);
               sli[i]      = dzSellP(nmm, DzStartSellProbability, DzLookBackBars, Bars, i, 0.0001);
               zeroLine[i] = dzSellP(nmm, 0.5,                    DzLookBackBars, Bars, i, 0.0001);
               break;
            case 1: 
               bli[i]      = dzBuyP (nma, DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.0001);
               sli[i]      = dzSellP(nma, DzStartSellProbability, DzLookBackBars, Bars, i, 0.0001);
               zeroLine[i] = dzSellP(nma, 0.5,                    DzLookBackBars, Bars, i, 0.0001);
               break;
            case 2: 
               bli[i]      = dzBuyP (nmf, DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.0001);
               sli[i]      = dzSellP(nmf, DzStartSellProbability, DzLookBackBars, Bars, i, 0.0001);
               zeroLine[i] = dzSellP(nmf, 0.5,                    DzLookBackBars, Bars, i, 0.0001);
               break;
         }               
   }
   
   //
   //
   //
   //
   //

   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//    natural market mirror
//
//
//

double iNmmFunction(int startPeriod, int tPos)
{
   int    endPeriod = startPeriod*NMMPeriod;
   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/NMMPeriod)*1000.00);
}

//
//
//
//
//

int iNmfPeriodLen(int startPeriod, int minPeriod,int tPos)
{
   int    endPeriod = startPeriod*NMMPeriod;
   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]);
}

