Navigation´╝ÜHome > Content >

T3_bands_(3).mq4

Time: 2017-12-09 | Download file:T3_bands_(3).mq4

//+------------------------------------------------------------------+ 
//|                                                     T3 bands.mq4 | 
//+------------------------------------------------------------------+ 
#property copyright "mladen"
#property link      "[email protected]" 

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DimGray
#property indicator_color3 Red
#property indicator_style2 STYLE_DOT

//
//
//
//
//

extern string TimeFrame              = "Current time frame";
extern int    T3Period               = 14;
extern double T3Hot                  = 0.7;
extern int    T3Price                = PRICE_CLOSE; 
extern bool   T3Original             = false;
extern double Deviations             = 1;
extern bool   AllowNegativeDeviation = false;
extern bool   Interpolate            = true;

//
//
//
//
//

double bandU[];
double bandM[];
double bandD[];

//
//
//
//
//

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

double c1;
double c2;
double c3;
double c4;

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

int init()
{
   SetIndexBuffer(0, bandU);
   SetIndexBuffer(1, bandM);
   SetIndexBuffer(2, bandD);

      T3Hot = MathMax(MathMin(T3Hot,1),0.0001);
      double a  = T3Hot;
             c1 = -a*a*a;
             c2 =  3*(a*a+a*a*a);
             c3 = -3*(2*a*a+a+a*a*a);
             c4 = 1+3*a+a*a*a+3*a*a;

      //
      //
      //
      //
      //
         
      indicatorFileName = WindowExpertName();
      calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
      returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);

   //
   //
   //
   //
   //
      
   IndicatorShortName(timeFrameToString(timeFrame)+" T3 bands ("+T3Period+")");
   return(0);
}
//+------------------------------------------------------------------+  
//|                                                                  |
//+------------------------------------------------------------------+ 
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

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

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
      for(i=limit; i>=0; i--)
      {
         double price     = iMA(NULL,0,1,0,MODE_SMA,T3Price,i);
         double deviation = iT3Deviation(price,T3Period,i); if (!AllowNegativeDeviation) deviation = MathMax(deviation,0);
                bandM[i]  = iT3(price,T3Period,i, 0);
                bandU[i]  = bandM[i] + deviation*Deviations;
                bandD[i]  = bandM[i] - deviation*Deviations;
      }
      return(0);
   }

   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         bandU[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Price,T3Original,Deviations,AllowNegativeDeviation,0,y);
         bandM[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Price,T3Original,Deviations,AllowNegativeDeviation,1,y);
         bandD[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Price,T3Original,Deviations,AllowNegativeDeviation,2,y);

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

         //
         //
         //
         //
         //

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

   //
   //
   //
   //
   //
   
   return(0);
}

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

double t3Values[][42];
double iT3(double price,int period, int i,int s)
{
   if (ArrayRange(t3Values,0) != Bars) ArrayResize(t3Values,Bars);

   //
   //
   //
   //
   //
   
   int    r = Bars-i-1;
   double n = period; if (!T3Original) n = 1.0 + (n-1.0)/2.0;
   double alpha = 2.0/(1.0 + n);
   
   if (r < 2)
      {
         t3Values[r][s+0] = price;
         t3Values[r][s+1] = price;
         t3Values[r][s+2] = price;
         t3Values[r][s+3] = price;
         t3Values[r][s+4] = price;
         t3Values[r][s+5] = price;
      }
   else
      {
         t3Values[r][s+0] = t3Values[r-1][s+0]+alpha*(price           -t3Values[r-1][s+0]);
         t3Values[r][s+1] = t3Values[r-1][s+1]+alpha*(t3Values[r][s+0]-t3Values[r-1][s+1]);
         t3Values[r][s+2] = t3Values[r-1][s+2]+alpha*(t3Values[r][s+1]-t3Values[r-1][s+2]);
         t3Values[r][s+3] = t3Values[r-1][s+3]+alpha*(t3Values[r][s+2]-t3Values[r-1][s+3]);
         t3Values[r][s+4] = t3Values[r-1][s+4]+alpha*(t3Values[r][s+3]-t3Values[r-1][s+4]);
         t3Values[r][s+5] = t3Values[r-1][s+5]+alpha*(t3Values[r][s+4]-t3Values[r-1][s+5]);
      }
   return(c1*t3Values[r][s+5] + c2*t3Values[r][s+4] + c3*t3Values[r][s+3] + c4*t3Values[r][s+2]);
}

//
//
//
//
//

double t3Valued[][6];
double iT3Deviation(double price, int period, int i)
{
   if (ArrayRange(t3Valued,0) != Bars) ArrayResize(t3Valued,Bars);
   
   //
   //
   //
   //
   //
   
   int    r = Bars-i-1;
   double n = period; if (!T3Original) n = 1.0 + (n-1)/2.0;
   double alpha = 2.0/(1.0 + n);

   double temp = iT3(price         ,period,i, 6); t3Valued[r][0] = MathSqrt(alpha*(price         -temp)*(price         -temp)+(1.0-alpha)*t3Valued[r-1][0]*t3Valued[r-1][0]);
          temp = iT3(t3Valued[r][0],period,i,12); t3Valued[r][1] = MathSqrt(alpha*(t3Valued[r][0]-temp)*(t3Valued[r][0]-temp)+(1.0-alpha)*t3Valued[r-1][1]*t3Valued[r-1][1]);
          temp = iT3(t3Valued[r][1],period,i,18); t3Valued[r][2] = MathSqrt(alpha*(t3Valued[r][1]-temp)*(t3Valued[r][1]-temp)+(1.0-alpha)*t3Valued[r-1][2]*t3Valued[r-1][2]);
          temp = iT3(t3Valued[r][2],period,i,24); t3Valued[r][3] = MathSqrt(alpha*(t3Valued[r][2]-temp)*(t3Valued[r][2]-temp)+(1.0-alpha)*t3Valued[r-1][3]*t3Valued[r-1][3]);
          temp = iT3(t3Valued[r][3],period,i,30); t3Valued[r][4] = MathSqrt(alpha*(t3Valued[r][3]-temp)*(t3Valued[r][3]-temp)+(1.0-alpha)*t3Valued[r-1][4]*t3Valued[r-1][4]);
          temp = iT3(t3Valued[r][4],period,i,36); t3Valued[r][5] = MathSqrt(alpha*(t3Valued[r][4]-temp)*(t3Valued[r][4]-temp)+(1.0-alpha)*t3Valued[r-1][5]*t3Valued[r-1][5]);
   return(MathSqrt(n)*(c1*t3Valued[r][5] + c2*t3Valued[r][4] + c3*t3Valued[r][3] + c4*t3Valued[r][2]));
}


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

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