Navigation´╝ÜHome > Content >

GL_Taotra_MTF.mq4

Time: 2015-02-25 | Download file:GL_Taotra_MTF.mq4

//+------------------------------------------------------------------+ 
//|                                                GL_Taotra_MTF.mq4 | 
//+------------------------------------------------------------------+ 
#property copyright ""
#property link      "" 

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Lime
#property indicator_color3 Gold
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_style1 2
#property indicator_style2 4
#property indicator_style3 1

extern string TimeFrame   = "Current time frame";
extern int    T3Period_1  = 21;
extern int    T3Period_2  = 100;
extern int    T3Period_3  = 300;
extern double T3Hot       = 0.01;
extern int    T3Price     = PRICE_CLOSE; 
extern bool   T3Original  = false;
extern bool   Interpolate = true;

double Ind_Buffer1[];
double Ind_Buffer2[];
double Ind_Buffer3[];

int    timeFrame;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
double c1;
double c2;
double c3;
double c4;

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

int init()
{
   SetIndexBuffer(0, Ind_Buffer1);
   SetIndexBuffer(1, Ind_Buffer2);
   SetIndexBuffer(2, Ind_Buffer3);

      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)+" GL_Taotra_MTF ("+T3Period_1+","+T3Period_2+","+T3Period_3+")");
   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) { Ind_Buffer1[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);
         Ind_Buffer1[i]  = iT3(price,T3Period_1,i, 0);
         Ind_Buffer2[i]  = iT3(price,T3Period_2,i, 6);
         Ind_Buffer3[i]  = iT3(price,T3Period_3,i,12);
      }
      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]);
         Ind_Buffer1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period_1,T3Period_2,T3Period_3,T3Hot,T3Price,T3Original,0,y);
         Ind_Buffer2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period_1,T3Period_2,T3Period_3,T3Hot,T3Price,T3Original,1,y);
         Ind_Buffer3[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period_1,T3Period_2,T3Period_3,T3Hot,T3Price,T3Original,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;	
            double factor = 1.0 / n;
            for(int k = 1; k < n; k++)
            {
               Ind_Buffer1[i+k] = k*factor*Ind_Buffer1[i+n] + (1.0-k*factor)*Ind_Buffer1[i];
               Ind_Buffer2[i+k] = k*factor*Ind_Buffer2[i+n] + (1.0-k*factor)*Ind_Buffer2[i];
               Ind_Buffer3[i+k] = k*factor*Ind_Buffer3[i+n] + (1.0-k*factor)*Ind_Buffer3[i];
            }               
   }

   //
   
   return(0);
}

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

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

   int    r = Bars-i-1;
   double alpha = 2.0/(2.0 + (period-1.0)/2.0);
            if (T3Original) alpha = 2.0/(1.0 + period);

   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]);
}

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

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 ch = StringGetChar(s, length);
         if((ch > 96 && ch < 123) || (ch > 223 && ch < 256))
                     s = StringSetChar(s, length, ch - 32);
         else if(ch > -33 && ch < 0)
                     s = StringSetChar(s, length, ch + 224);
   }
   return(s);
}

Recommend