//+------------------------------------------------------------------+
//|                                                          FVE.mq4 |
//|                               Copyright © 2014, Gehtsoft USA LLC |
//|                                            http://fxcodebase.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  clrGreen
#property indicator_color2  clrRed
#property indicator_color3  clrDeepSkyBlue
#property indicator_color4  clrDeepSkyBlue
#property indicator_color5  clrPaleVioletRed
#property indicator_color6  clrPaleVioletRed
#property indicator_color7  clrPeru
#property indicator_style4  STYLE_DOT
#property indicator_style5  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

//
//
//
//
//

extern int            Length1                 = 4;
extern ENUM_MA_METHOD MA1_Method              = 0;
extern int            Length2                 = 13;
extern ENUM_MA_METHOD MA2_Method              = 0;
extern bool           ShowMiddleLine          = true;
extern int            DzLookBackBars          = 49;
extern double         DzStartBuyProbability1  = 0.10;
extern double         DzStartBuyProbability2  = 0.25;
extern double         DzStartSellProbability1 = 0.10;
extern double         DzStartSellProbability2 = 0.25;

double FVE[], Signal[];
double Intra[], Inter[], VE[], Vol[];
double bl1[];
double bl2[];
double sl1[];
double sl2[];
double zli[];

//
//
//
//
//

int init()
{
   IndicatorBuffers(11);
   SetIndexBuffer(0,FVE);
   SetIndexBuffer(1,Signal);
   SetIndexBuffer(2,bl1);
   SetIndexBuffer(3,bl2);
   SetIndexBuffer(4,sl2);
   SetIndexBuffer(5,sl1);
   SetIndexBuffer(6,zli);
   SetIndexBuffer(7,Intra);
   SetIndexBuffer(8,Inter);
   SetIndexBuffer(9,VE);
   SetIndexBuffer(10,Vol);
      IndicatorShortName("Dz Finite Volume Elements");
      return(0);
}
int deinit() { return(0); }
int start()
{
   int i,counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-3);


   //
   //
   //
   //
   //
   
      for(i=limit; i>=0; i--)
      {
         Intra[i] = MathLog(High[i])-MathLog(Low[i]);
         Inter[i] = MathLog((High[i]+Low[i]+Close[i])/3.)-MathLog((High[i+1]+Low[i+1]+Close[i+1])/3.);
         Vol[i]   = Volume[i];
      }         
      for(i=limit; i>=0; i--)
      {
            double Vintra = iStdDevOnArray(Intra, 0, Length1, 0, MODE_SMA, i);
            double Vinter = iStdDevOnArray(Inter, 0, Length1, 0, MODE_SMA, i);
            double Cutoff = 0.1*(Vintra+Vinter)*Close[i];
            double MF=Close[i]-(High[i]+Low[i])/2.+High[i]+Low[i]+Close[i]-High[i+1]-Low[i+1]-Close[i+1];
  
            if (MF>Cutoff) VE[i]=Volume[i];
            else
               if (MF<-Cutoff)
                     VE[i]=-Volume[i];
               else  VE[i]=0.;
      }               
      for(i=limit; i>=0; i--)
      {
         double MA_VE = iMAOnArray(VE, 0, Length1, 0, MODE_SMA, i);
         double MA1   = iMAOnArray(Vol, 0, Length1, 0, MA1_Method, i);
         if (MA1!=0.)
               FVE[i]=100.*MA_VE/(MA1*_Point);
         else  FVE[i]=0.;
         if (DzStartBuyProbability1 >0) bl1[i] = dzBuyP (FVE, DzStartBuyProbability1,  DzLookBackBars, Bars, i, 0.0001);
         if (DzStartBuyProbability2 >0) bl2[i] = dzBuyP (FVE, DzStartBuyProbability2,  DzLookBackBars, Bars, i, 0.0001);
         if (DzStartSellProbability1>0) sl1[i] = dzSellP(FVE, DzStartSellProbability1, DzLookBackBars, Bars, i, 0.0001);
         if (DzStartSellProbability2>0) sl2[i] = dzSellP(FVE, DzStartSellProbability2, DzLookBackBars, Bars, i, 0.0001);
         if (ShowMiddleLine)            zli[i] = dzSellP(FVE, 0.5                    , DzLookBackBars, Bars, i, 0.0001);
      }
      for(i=limit; i>=0; i--) Signal[i]=iMAOnArray(FVE, 0, Length2, 0, MA2_Method, i);
   return(0);
}