Navigation´╝ÜHome > Content >

Digital_filters_smoothed_-_separate_special.mq4

Time: 2017-05-01 | Download file:Digital_filters_smoothed_-_separate_special.mq4

//+------------------------------------------------------------------+  
//|                                                                  | 
//+------------------------------------------------------------------+ 
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  DimGray
#property indicator_color2  MediumVioletRed
#property indicator_width2  2

//
//
//
//
//

extern string TimeFrame     = "Current time frame";
extern int    AveragePeriod = 24; 
extern int    Price         = PRICE_CLOSE;
extern int    Length        = 5;
extern double Phase         = 0;
extern bool   Interpolate   = true;

//
//
//
//
//

double cycles[];
double values[];
double prices[];
double filter[];
int    filterSize;

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

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

int init()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0, cycles); SetIndexDrawBegin(0, filterSize+30);
   SetIndexBuffer(1, values); SetIndexDrawBegin(1, filterSize+30);
   SetIndexBuffer(2, prices);

   //
   //
   //
   //
   //

      fillArray(filter,filterSize);
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
   
   //
   //
   //
   //
   //
   
   IndicatorShortName(timeFrameToString(timeFrame)+" special");
   return(0); 
} 


//+------------------------------------------------------------------+  
//|                                                                  | 
//+------------------------------------------------------------------+ 
//
//
//
//
//
 
int start()
{
   int counted_bars=IndicatorCounted();
   int i,k,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { cycles[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i>=0; i--)
      {
         prices[i] = iSmooth(iMA(NULL,0,1,0,MODE_SMA,Price,i),Length,Phase,i);
         cycles[i] = 0;
         double sum = 0;
            for(k=0; k=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         cycles[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",AveragePeriod,Price,Length,Phase,0,y);
         values[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",AveragePeriod,Price,Length,Phase,1,y);

         //
         //
         //
         //
         //
      
            if (y==iBarShift(NULL,timeFrame,Time[i-1]) || !Interpolate) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(    k = 1; k < n; k++)
            {
               cycles[i+k] = cycles[i] + (cycles[i+n]-cycles[i])*k/n;
               values[i+k] = values[i] + (values[i+n]-values[i])*k/n;
            }               
   }
   return(0);
}


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

void fillArray(double& array[], int& size)
{

   size = 152;
   ArrayResize(array,size);
      array[  0] = +2.00000780; array[  1] = +1.75125900; array[  2] = +1.06749120; array[  3] = +0.12029670; array[  4] = -0.85308933;
      array[  5] = -1.60977530; array[  6] = -1.96240340; array[  7] = -1.82598400; array[  8] = -1.23855790; array[  9] = -0.35061930;
      array[ 10] = +0.61376122; array[ 11] = +1.41398890; array[ 12] = +1.85351760; array[ 13] = +1.82855630; array[ 14] = +1.35273310;
      array[ 15] = +0.55182167; array[ 16] = -0.36966849; array[ 17] = -1.18111680; array[ 18] = -1.68398630; array[ 19] = -1.76078420;
      array[ 20] = -1.40262020; array[ 21] = -0.70888019; array[ 22] = +0.14025891; array[ 23] = +0.93088008; array[ 24] = +1.46958860;
      array[ 25] = +1.63112670; array[ 26] = +1.38754400; array[ 27] = +0.81212728; array[ 28] = +0.05787859; array[ 29] = -0.68323419;
      array[ 30] = -1.22937520; array[ 31] = -1.45366700; array[ 32] = -1.31364400; array[ 33] = -0.85851239; array[ 34] = -0.21324996;
      array[ 35] = +0.45555038; array[ 36] = +0.98277978; array[ 37] = +1.24567860; array[ 38] = +1.19232940; array[ 39] = +0.85119877;
      array[ 40] = +0.32021601; array[ 41] = -0.26090387; array[ 42] = -0.74735238; array[ 43] = -1.02530630; array[ 44] = -1.03840640;
      array[ 45] = -0.79852787; array[ 46] = -0.37900381; array[ 47] = +0.10703017; array[ 48] = +0.53691574; array[ 49] = +0.80931602;
      array[ 50] = +0.86789419; array[ 51] = +0.71235816; array[ 52] = +0.39490641; array[ 53] = +0.00389350; array[ 54] = -0.36037873;
      array[ 55] = -0.61118714; array[ 56] = -0.69578145; array[ 57] = -0.60592414; array[ 58] = -0.37665124; array[ 59] = -0.07441554;
      array[ 60] = +0.22165210; array[ 61] = +0.44018182; array[ 62] = +0.53448851; array[ 63] = +0.49204567; array[ 64] = +0.33474789;
      array[ 65] = +0.11064559; array[ 66] = -0.12008666; array[ 67] = -0.30097405; array[ 68] = -0.39277060; array[ 69] = -0.38153230;
      array[ 70] = -0.27971375; array[ 71] = -0.12064786; array[ 72] = +0.05161002; array[ 73] = +0.19411643; array[ 74] = +0.27545316;
      array[ 75] = +0.28227126; array[ 76] = +0.22072259; array[ 77] = +0.11293620; array[ 78] = -0.01008024; array[ 79] = -0.11698050;
      array[ 80] = -0.18378936; array[ 81] = -0.19895618; array[ 82] = -0.16478702; array[ 83] = -0.09530024; array[ 84] = -0.01143985;
      array[ 85] = +0.06486303; array[ 86] = +0.11617244; array[ 87] = +0.13326572; array[ 88] = +0.11637531; array[ 89] = +0.07394659;
      array[ 90] = +0.01952701; array[ 91] = -0.03220630; array[ 92] = -0.06918372; array[ 93] = -0.08454655; array[ 94] = -0.07761825;
      array[ 95] = -0.05321953; array[ 96] = -0.01972164; array[ 97] = +0.01350451; array[ 98] = +0.03852422; array[ 99] = +0.05056984;
      array[100] = +0.04873674; array[101] = +0.03562211; array[102] = +0.01615310; array[103] = -0.00398154; array[104] = -0.01984449;
      array[105] = -0.02833847; array[106] = -0.02866536; array[107] = -0.02214149; array[108] = -0.01151981; array[109] = -0.00006966;
      array[110] = +0.00931487; array[111] = +0.01475926; array[112] = +0.01568670; array[113] = +0.01272408; array[114] = +0.00732568;
      array[115] = +0.00126015; array[116] = -0.00388495; array[117] = -0.00705940; array[118] = -0.00790361; array[119] = -0.00669924;
      array[120] = -0.00416909; array[121] = -0.00120701; array[122] = +0.00137964; array[123] = +0.00305176; array[124] = +0.00361415;
      array[125] = +0.00318790; array[126] = +0.00210889; array[127] = +0.00079465; array[128] = -0.00037928; array[129] = -0.00116359;
      array[130] = -0.00146728; array[131] = -0.00134155; array[132] = -0.00093105; array[133] = -0.00041319; array[134] = +0.00005561;
      array[135] = +0.00037430; array[136] = +0.00050805; array[137] = +0.00047880; array[138] = +0.00034344; array[139] = +0.00016879;
      array[140] = +0.00001114; array[141] = -0.00009543; array[142] = -0.00014124; array[143] = -0.00013579; array[144] = -0.00009895;
      array[145] = -0.00005197; array[146] = -0.00001114; array[147] = +0.00001506; array[148] = +0.00002558; array[149] = +0.00002429;
      array[150] = +0.00001694; array[151] = +0.00001010;
}

double wrk[][10];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i)
{
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[0][k]=price; for(; k<10; k++) wrk[0][k]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax];
      double del2   = price - wrk[r-1][bsmin];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty] = MathAbs(del2); 
         wrk[r][vsum] =	wrk[r-1][vsum] + (wrk[r][volty]-wrk[r-forBar][volty])*div;
   
         //
         //
         //
         //
         //
   
         wrk[r][avolty] = wrk[r-1][avolty]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum]-wrk[r-1][avolty]);
            if (wrk[r][avolty] > 0)
               double dVolty = wrk[r][volty]/wrk[r][avolty]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax] = price; else wrk[r][bsmax] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin] = price; else wrk[r][bsmin] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0] = price + alpha*(wrk[r-1][0]-price);
         wrk[r][1] = (price - wrk[r][0])*(1-beta) + beta*wrk[r-1][1];
         wrk[r][2] = (wrk[r][0] + R*wrk[r][1]);
         wrk[r][3] = (wrk[r][2] - wrk[r-1][4])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3];
         wrk[r][4] = (wrk[r-1][4] + wrk[r][3]); 

   //
   //
   //
   //
   //

   return(wrk[r][4]);
}

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

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

Recommend