Navigation:Home > Content >

Fractal_dimesion_index_alb_-_jppoton.mq4

Time: 2015-03-09 | Download file:Fractal_dimesion_index_alb_-_jppoton.mq4

//+------------------------------------------------------------------+
//|                                       Fractal dimesion index.mq4 |
//|                                                           mladen |
//|                                                                  |
//| Modification of original FDI made after jppoton version          |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 7
#property indicator_color1  DeepSkyBlue
#property indicator_color2  PaleVioletRed
#property indicator_color3  DeepSkyBlue
#property indicator_color4  PaleVioletRed
#property indicator_color5  PaleVioletRed
#property indicator_color6  DimGray
#property indicator_color7  DimGray
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2

//
//
//
//
//

extern int    SwingCount         = 30;
extern double Speed              = 0.5;
extern int    MinimalLength      = 10;
extern int    FDIPrice           = PRICE_CLOSE;
extern double FDLevel            = 1.5;
extern bool   alertsOn           = false;
extern bool   alertsOnEveryAlign = true;
extern bool   alertsOnCurrent    = true;
extern bool   alertsOnAlignUp    = true;
extern bool   alertsOnAlignDown  = true;
extern bool   alertsMessage      = true;
extern bool   alertsSound        = false;
extern bool   alertsEmail        = false;

//
//
//
//
//

double FDIBuffer[];
double FDIBufferda[];
double FDIBufferdb[];
double FDIBufferu[];
double FDIBufferd[];
double FDIBufferhu[];
double FDIBufferhd[];
double PriceBuffer[];

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

int init()
{
   IndicatorBuffers(8);
      SetIndexBuffer(0,FDIBufferhu); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,FDIBufferhd); SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,FDIBuffer);
      SetIndexBuffer(3,FDIBufferda);
      SetIndexBuffer(4,FDIBufferdb);
      SetIndexBuffer(5,FDIBufferu);
      SetIndexBuffer(6,FDIBufferd);
      SetIndexBuffer(7,PriceBuffer);
         SetLevelValue(0,0);
   return(0);
}
int deinit()
{
   return(0);
}

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

double trends[][4];

//
//
//
//
//

int start()
{
   int i,k,n,r,limit,counted_bars=IndicatorCounted();
   
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (ArrayRange(trends,0)!=Bars) ArrayResize(trends,Bars);

   //
   //
   //
   //
   //

   if (trends[Bars-limit-1][0]==-1) CleanPoint(limit,FDIBufferda,FDIBufferdb);
   for (i=limit, r=Bars-i-1; i>=0; i--,r++)
   {
      PriceBuffer[i] = iMA(NULL,0,1,0,MODE_SMA,FDIPrice,i);
      int swing = 0;
         if (High[i]>High[i+1] && High[i+1]>High[i+2] && Low[i+2] < Low[i+3] && Low[i+3] < Low[i+4]) swing = -1;
         if (Low[i] < Low[i+1] && Low[i+1] < Low[i+2] && High[i+2]>High[i+3] && High[i+3]>High[i+4]) swing =  1;
      
      trends[r][2] = swing; for (k=r, n=0; (k>=0) && (n0) trends[r][0] =  1;
               if (FDIBuffer[i]<0) trends[r][0] = -1;
               if (trends[r][0]==-1) PlotPoint(i,FDIBufferda,FDIBufferdb,FDIBuffer);
   }         
   for (i=limit, r=Bars-i-1; i>=0; i--,r++)
   {
      double stddev = iStdDevOnArray(FDIBuffer,0,trends[r][3],0,MODE_SMA,i);
      
         FDIBufferu[i]  = FDIBuffer[i]+stddev;
         FDIBufferd[i]  = FDIBuffer[i]-stddev;
         FDIBufferhu[i] = MathMax(FDIBufferd[i],0);
         FDIBufferhd[i] = MathMin(FDIBufferu[i],0);

        if (alertsOnEveryAlign)
               trends[r][1] = 0;
        else   trends[r][1] = trends[r-1][1];
        if (FDIBufferd[i]>0) trends[r][1] =  1;
        if (FDIBufferu[i]<0) trends[r][1] = -1;

   }
   manageAlerts();
   return(0);
}



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

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; 
      if (trends[Bars-whichBar-1][1] != trends[Bars-whichBar-2][1])
      {
         if (alertsOnAlignUp   && trends[Bars-whichBar-1][1] ==  1) doAlert(whichBar,"up");
         if (alertsOnAlignDown && trends[Bars-whichBar-1][1] == -1) doAlert(whichBar,"down");
      }
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," FDI all values aligned : ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"FDI"),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}


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

void CleanPoint(int i,double& first[],double& second[])
{
   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 (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;
      }
}

Recommend