//+-------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-station.com"
//+-------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  clrDarkSlateGray
#property indicator_color2  clrDarkSlateGray
#property indicator_color3  clrDarkSlateGray
#property indicator_color4  clrLimeGreen
#property indicator_color5  clrOrange
#property indicator_color6  clrOrange
#property indicator_color7  clrRed
#property indicator_width4  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_style1  STYLE_DOT
#property indicator_style2  STYLE_DOT
#property indicator_style3  STYLE_DOT
#property indicator_style7  STYLE_DASH

//
//
//
//
//

#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

//
//
//
//
//

enum enPrices
{
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_haclose,    // Heiken ashi close
   pr_haopen ,    // Heiken ashi open
   pr_hahigh,     // Heiken ashi high
   pr_halow,      // Heiken ashi low
   pr_hamedian,   // Heiken ashi median
   pr_hatypical,  // Heiken ashi typical
   pr_haweighted, // Heiken ashi weighted
   pr_haaverage,  // Heiken ashi average
   pr_hamedianb,  // Heiken ashi median body
   pr_hatbiased   // Heiken ashi trend biased price
};

enum colorOn
{
   clrOnSlope, //Color change on slope change
   clrOnZero,  // Color change on zero cross
   clrOnSign   // Color change on signal cross
};

extern enPrices Price                  = pr_close;       // Price
extern int      PriceShift             = 0;              // Price shift
extern int      EmaPeriod              = 32;             // Ema Period
extern int      HighLowPeriod          = 100;            // High Low period
extern int      LwmaPeriod             = 6;              // Lwma Period
extern double   OmaLength              = 26;             // OMA period for signal
extern double   OmaSpeed               = 1.0;            // OMA speed for signal
extern bool     OmaAdaptive            = true;           // Should the signal be adaptive?
extern int      DzLookBackBars         = 35;             // Dynamic zone look back
extern double   DzStartBuyProbability  = 0.05;           // Dynamic zone buy probability
extern double   DzStartSellProbability = 0.05;           // Dynamic zone sell probability
extern colorOn  ColorChangeOn          = clrOnSlope;     // Color change on?


double beh[];
double behDa[];
double behDb[];
double bli[];
double sli[];
double cen[];
double sig[];
double trend[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,bli);
   SetIndexBuffer(1,cen);
   SetIndexBuffer(2,sli);
   SetIndexBuffer(3,beh);
   SetIndexBuffer(4,behDa);
   SetIndexBuffer(5,behDb);
   SetIndexBuffer(6,sig);
   SetIndexBuffer(7,trend);
   
   OmaLength = MathMax(OmaLength,1);
   OmaSpeed  = MathMax(OmaSpeed,-1.5);
   IndicatorShortName(" dz Behgozin Strength Finder ("+(string)PriceShift+","+(string)EmaPeriod+","+(string)HighLowPeriod+","+(string)LwmaPeriod+")");
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double work[];
int start()
{
   int i,r,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            int limit=Bars-counted_bars;
            if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);

   //
   //
   //
   //
   //
   
   if (trend[limit]==-1) CleanPoint(limit,behDa,behDb);    
   for(i=limit, r=Bars-i-1; i>=0; i--,r++)
   {
      if ((i+PriceShift)>=0)
      {
         double price = getPrice(Price,Open,Close,High,Low,i+PriceShift);
         double ema   = iEma(price,EmaPeriod,r);
            if (ema != 0)
                  work[r] = 100.0*(price-ema)/ema;
            else  work[r] = 0;    
         double max = work[r];
         double min = work[r];
         for (int k=1; k<HighLowPeriod && (r-k)>=0; k++)
         {
            max = MathMax(max,work[r-k]);
            min = MathMin(min,work[r-k]);
         }
         beh[i] = iLwma(work[r]*(max-min),LwmaPeriod,r);
         sig[i] = iAverage(beh[i],OmaLength,OmaSpeed,OmaAdaptive,i,0);
         bli[i] = dzBuyP (beh, DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.00001);
         sli[i] = dzSellP(beh, DzStartSellProbability, DzLookBackBars, Bars, i, 0.00001);
         cen[i] = dzSellP(beh, 0.5,                    DzLookBackBars, Bars, i, 0.00001);
         
         if (i>=Bars-1) continue;
            
         //
         //
         //
         //
         //
            
         behDa[i] = EMPTY_VALUE;
         behDb[i] = EMPTY_VALUE;             
         trend[i] = trend[i+1];
         switch (ColorChangeOn)
         {
            case clrOnSlope: 
               if (beh[i]>beh[i+1]) trend[i] =  1;
               if (beh[i]<beh[i+1]) trend[i] = -1;
            break;
            case clrOnZero: 
               if (beh[i]>cen[i])   trend[i] =  1;
               if (beh[i]<cen[i])   trend[i] = -1;
            break;
            default : 
               if (beh[i]>sig[i])   trend[i] =  1;
               if (beh[i]<sig[i])   trend[i] = -1;
            break;
         }    
            if (trend[i] == -1) PlotPoint(i,behDa,behDb,beh);  
      }
   }
   return(0);         
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workEma[][1];
double iEma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workEma,0)!= Bars) ArrayResize(workEma,Bars);

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workLwma[][1];
double iLwma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workLwma,0)!= Bars) ArrayResize(workLwma,Bars);
   
   //
   //
   //
   //
   //
   
   workLwma[r][instanceNo] = price;
      double sumw = period;
      double sum  = period*price;

      for(int k=1; k<period && (r-k)>=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define omaInstances 1
double workOma[][omaInstances*7];
#define E1  0
#define E2  1
#define E3  2
#define E4  3
#define E5  4
#define E6  5
#define res 6

//
//
//
//
//

double iAverage(double price, double averagePeriod, double tconst, bool adaptive, int i, int instanceNo=0)
{
   if (averagePeriod <=1) return(price); int r = Bars-i-1; instanceNo *= 7;
   if (ArrayRange(workOma,0)!=Bars) ArrayResize(workOma,Bars);
   
   double e1=workOma[(int)MathMax(r-1,0)][instanceNo+E1];  double e2=workOma[(int)MathMax(r-1,0)][instanceNo+E2];
   double e3=workOma[(int)MathMax(r-1,0)][instanceNo+E3];  double e4=workOma[(int)MathMax(r-1,0)][instanceNo+E4];
   double e5=workOma[(int)MathMax(r-1,0)][instanceNo+E5];  double e6=workOma[(int)MathMax(r-1,0)][instanceNo+E6];

   //
   //
   //
   //
   //

      if (adaptive && (averagePeriod > 1))
      {
         double minPeriod = averagePeriod/2.0;
         double maxPeriod = minPeriod*5.0;
         int    endPeriod = (int)MathCeil(maxPeriod);
         double signal    = MathAbs((price-workOma[(int)MathMax(r-endPeriod,0)][instanceNo+res]));
         double noise     = 0.00000000001;

            for(int k=1; k<endPeriod && r-k>=0; k++) noise=noise+MathAbs(price-workOma[r-k][instanceNo+res]);

         averagePeriod = ((signal/noise)*(maxPeriod-minPeriod))+minPeriod;
      }
      
      //
      //
      //
      //
      //
      
      double alpha = (2.0+tconst)/(1.0+tconst+averagePeriod);

      e1 = e1 + alpha*(price-e1); e2 = e2 + alpha*(e1-e2); double v1 = 1.5 * e1 - 0.5 * e2;
      e3 = e3 + alpha*(v1   -e3); e4 = e4 + alpha*(e3-e4); double v2 = 1.5 * e3 - 0.5 * e4;
      e5 = e5 + alpha*(v2   -e5); e6 = e6 + alpha*(e5-e6); double v3 = 1.5 * e5 - 0.5 * e6;

   //
   //
   //
   //
   //

   workOma[r][instanceNo+E1]  = e1;  workOma[r][instanceNo+E2] = e2;
   workOma[r][instanceNo+E3]  = e3;  workOma[r][instanceNo+E4] = e4;
   workOma[r][instanceNo+E5]  = e5;  workOma[r][instanceNo+E6] = e6;
   workOma[r][instanceNo+res] = price;
   return(v3);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//
//

double workHa[][4];
double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
  if (price>=pr_haclose && price<=pr_hatbiased)
   {
      if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars);
         int r = Bars-i-1;
         
         //
         //
         //
         //
         //
         
         double haOpen;
         if (r>0)
                haOpen  = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
         else   haOpen  = (open[i]+close[i])/2;
         double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
         double haHigh  = MathMax(high[i], MathMax(haOpen,haClose));
         double haLow   = MathMin(low[i] , MathMin(haOpen,haClose));

         if(haOpen  <haClose) { workHa[r][instanceNo+0] = haLow;  workHa[r][instanceNo+1] = haHigh; } 
         else                 { workHa[r][instanceNo+0] = haHigh; workHa[r][instanceNo+1] = haLow;  } 
                                workHa[r][instanceNo+2] = haOpen;
                                workHa[r][instanceNo+3] = haClose;
         //
         //
         //
         //
         //
         
         switch (price)
         {
            case pr_haclose:     return(haClose);
            case pr_haopen:      return(haOpen);
            case pr_hahigh:      return(haHigh);
            case pr_halow:       return(haLow);
            case pr_hamedian:    return((haHigh+haLow)/2.0);
            case pr_hamedianb:   return((haOpen+haClose)/2.0);
            case pr_hatypical:   return((haHigh+haLow+haClose)/3.0);
            case pr_haweighted:  return((haHigh+haLow+haClose+haClose)/4.0);
            case pr_haaverage:   return((haHigh+haLow+haClose+haOpen)/4.0);
            case pr_hatbiased:
               if (haClose>haOpen)
                     return((haHigh+haClose)/2.0);
               else  return((haLow+haClose)/2.0);        
         }
   }
   
   //
   //
   //
   //
   //
   
   switch (price)
   {
      case pr_close:     return(close[i]);
      case pr_open:      return(open[i]);
      case pr_high:      return(high[i]);
      case pr_low:       return(low[i]);
      case pr_median:    return((high[i]+low[i])/2.0);
      case pr_medianb:   return((open[i]+close[i])/2.0);
      case pr_typical:   return((high[i]+low[i]+close[i])/3.0);
      case pr_weighted:  return((high[i]+low[i]+close[i]+close[i])/4.0);
      case pr_average:   return((high[i]+low[i]+close[i]+open[i])/4.0);
      case pr_tbiased:   
               if (close[i]>open[i])
                     return((high[i]+close[i])/2.0);
               else  return((low[i]+close[i])/2.0);        
   }
   return(0);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i];  first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] =  from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                           second[i] = EMPTY_VALUE; }
}


