Navigation´╝ÜHome > Content >

T3_Dtm.mq4

Time: 2014-12-17 | Download file:T3_Dtm.mq4

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

#property indicator_separate_window
#property indicator_buffers    6
#property indicator_color1     LimeGreen      
#property indicator_color2     Red
#property indicator_color3     Red
#property indicator_color4     Green      
#property indicator_color5     Crimson
#property indicator_color6     Crimson
#property indicator_width1     2      
#property indicator_width2     2
#property indicator_width3     2         
#property indicator_width4     2      
#property indicator_width5     2
#property indicator_width6     2 
#property indicator_level1     0
#property indicator_levelcolor DarkSlateGray
#property indicator_levelstyle STYLE_DOT        

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    T3Period        = 4;
extern double T3Hot           = 1.0;
extern bool   T3Original      = false;
extern bool   MultiColor      = true;

extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double stlm[]; 
double stlmDa[]; 
double stlmDb[];
double ftlm[]; 
double ftlmDa[]; 
double ftlmDb[];
double trend[]; 
double trene[]; 

//
//
//
//
//

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

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

int init() 
{ 
   IndicatorBuffers(8);
   SetIndexBuffer(0,stlm); 
   SetIndexBuffer(1,stlmDa);
   SetIndexBuffer(2,stlmDb); 
   SetIndexBuffer(3,ftlm); 
   SetIndexBuffer(4,ftlmDa);
   SetIndexBuffer(5,ftlmDb); 
   SetIndexBuffer(6,trend);
   SetIndexBuffer(7,trene);    
   
   //
   //
   //
   //
   //
   
   indicatorFileName = WindowExpertName();
   calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
   returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
   timeFrame         = stringToTimeFrame(TimeFrame);
   
   //
   //
   //
   //
   //
   
   IndicatorShortName(timeFrameToString(timeFrame)+"  t3 dtm");
   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) { stlm[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if(calculateValue || timeFrame==Period())
   {
   if(MultiColor && !calculateValue && trend[limit]==-1) CleanPoint(limit,stlmDa,stlmDb);
   if(MultiColor && !calculateValue && trene[limit]==-1) CleanPoint(limit,ftlmDa,ftlmDb);
   
   for(i=limit; i>=0; i--)
   {
    double value1 = 
0.112952*Close[i+0] 
+0.111644*Close[i+1] 
+0.109058*Close[i+2] 
+0.105254*Close[i+3] 
+0.100320*Close[i+4] 
+0.094368*Close[i+5] 
+0.087532*Close[i+6] 
+0.079966*Close[i+7] 
+0.071834*Close[i+8] 
+0.063313*Close[i+9] 
+0.054575*Close[i+10] 
+0.045801*Close[i+11] 
+0.037159*Close[i+12] 
+0.028809*Close[i+13] 
+0.020898*Close[i+14] 
+0.013548*Close[i+15] 
+0.006870*Close[i+16] 
+0.000944*Close[i+17] 
-0.004166*Close[i+18] 
-0.008428*Close[i+19] 
-0.011828*Close[i+20] 
-0.014378*Close[i+21] 
-0.016112*Close[i+22] 
-0.017080*Close[i+23] 
-0.017348*Close[i+24] 
-0.016998*Close[i+25] 
-0.016118*Close[i+26] 
-0.014806*Close[i+27] 
-0.013156*Close[i+28] 
-0.011270*Close[i+29] 
-0.009238*Close[i+30] 
-0.007152*Close[i+31] 
-0.005090*Close[i+32] 
-0.003120*Close[i+33] 
-0.001302*Close[i+34] 
+0.000322*Close[i+35] 
+0.001714*Close[i+36] 
+0.002858*Close[i+37] 
+0.003744*Close[i+38] 
+0.004372*Close[i+39] 
+0.004754*Close[i+40] 
+0.004910*Close[i+41] 
+0.004864*Close[i+42] 
+0.004646*Close[i+43] 
+0.004288*Close[i+44] 
+0.003824*Close[i+45] 
+0.003288*Close[i+46] 
+0.002712*Close[i+47] 
+0.002124*Close[i+48] 
+0.001548*Close[i+49] 
+0.001010*Close[i+50] 
+0.000522*Close[i+51] 
+0.000098*Close[i+52] 
-0.000254*Close[i+53] 
-0.000530*Close[i+54] 
-0.000734*Close[i+55] 
-0.000866*Close[i+56] 
-0.000934*Close[i+57] 
-0.000948*Close[i+58] 
-0.000914*Close[i+59] 
-0.000844*Close[i+60] 
-0.000750*Close[i+61] 
-0.000638*Close[i+62] 
-0.000520*Close[i+63] 
-0.000922*Close[i+64]; 

    double value2 = 
0.038953*Close[i+0] 
+0.042502*Close[i+1] 
+0.045773*Close[i+2] 
+0.048702*Close[i+3] 
+0.051237*Close[i+4] 
+0.053326*Close[i+5] 
+0.054931*Close[i+6] 
+0.056018*Close[i+7] 
+0.056566*Close[i+8] 
+0.056566*Close[i+9] 
+0.056018*Close[i+10] 
+0.054931*Close[i+11] 
+0.053326*Close[i+12] 
+0.051237*Close[i+13] 
+0.048702*Close[i+14] 
+0.045773*Close[i+15] 
+0.042502*Close[i+16] 
+0.038953*Close[i+17] 
+0.035190*Close[i+18] 
+0.031281*Close[i+19] 
+0.027295*Close[i+20] 
+0.023298*Close[i+21] 
+0.019357*Close[i+22] 
+0.015532*Close[i+23] 
+0.011880*Close[i+24] 
+0.008451*Close[i+25] 
+0.005287*Close[i+26] 
+0.002424*Close[i+27] 
-0.000112*Close[i+28] 
-0.002305*Close[i+29] 
-0.004142*Close[i+30] 
-0.005624*Close[i+31] 
-0.006758*Close[i+32] 
-0.007558*Close[i+33] 
-0.008042*Close[i+34] 
-0.008240*Close[i+35] 
-0.008177*Close[i+36] 
-0.007890*Close[i+37] 
-0.007415*Close[i+38] 
-0.006786*Close[i+39] 
-0.006041*Close[i+40] 
-0.005219*Close[i+41] 
-0.004349*Close[i+42] 
-0.003466*Close[i+43] 
-0.002596*Close[i+44] 
-0.001764*Close[i+45] 
-0.000991*Close[i+46] 
-0.000294*Close[i+47] 
+0.000318*Close[i+48] 
+0.000835*Close[i+49] 
+0.001254*Close[i+50] 
+0.001575*Close[i+51] 
+0.001801*Close[i+52] 
+0.001937*Close[i+53] 
+0.001993*Close[i+54] 
+0.001978*Close[i+55] 
+0.001902*Close[i+56] 
+0.001777*Close[i+57] 
+0.001616*Close[i+58] 
+0.001428*Close[i+59] 
+0.001228*Close[i+60] 
+0.001020*Close[i+61] 
+0.000816*Close[i+62] 
+0.000622*Close[i+63] 
+0.000445*Close[i+64] 
+0.000287*Close[i+65] 
+0.000154*Close[i+66] 
+0.000045*Close[i+67] 
-0.000040*Close[i+68] 
-0.000100*Close[i+69] 
-0.000139*Close[i+70] 
-0.000159*Close[i+71] 
-0.000161*Close[i+72] 
-0.000672*Close[i+73]; 


         double value3 =
 0.0982862174*Close[i+0]
+0.0975682269*Close[i+1]
+0.0961401078*Close[i+2]
+0.0940230544*Close[i+3]
+0.0912437090*Close[i+4]
+0.0878391006*Close[i+5]
+0.0838544303*Close[i+6]
+0.0793406350*Close[i+7]
+0.0743569346*Close[i+8]
+0.0689666682*Close[i+9]
+0.0632381578*Close[i+10]
+0.0572428925*Close[i+11]
+0.0510534242*Close[i+12]
+0.0447468229*Close[i+13]
+0.0383959950*Close[i+14]
+0.0320735368*Close[i+15]
+0.0258537721*Close[i+16]
+0.0198005183*Close[i+17]
+0.0139807863*Close[i+18]
+0.0084512448*Close[i+19]
+0.0032639979*Close[i+20]
-0.0015350359*Close[i+21]
-0.0059060082*Close[i+22]
-0.0098190256*Close[i+23]
-0.0132507215*Close[i+24]
-0.0161875265*Close[i+25]
-0.0186164872*Close[i+26]
-0.0205446727*Close[i+27]
-0.0219739146*Close[i+28]
-0.0229204861*Close[i+29]
-0.0234080863*Close[i+30]
-0.0234566315*Close[i+31]
-0.0231017777*Close[i+32]
-0.0223796900*Close[i+33]
-0.0213300463*Close[i+34]
-0.0199924534*Close[i+35]
-0.0184126992*Close[i+36]
-0.0166377699*Close[i+37]
-0.0147139428*Close[i+38]
-0.0126796776*Close[i+39]
-0.0105938331*Close[i+40]
-0.0084736770*Close[i+41]
-0.0063841850*Close[i+42]
-0.0043466731*Close[i+43]
-0.0023956944*Close[i+44]
-0.0005535180*Close[i+45]
+0.0011421469*Close[i+46]
+0.0026845693*Close[i+47]
+0.0040471369*Close[i+48]
+0.0052380201*Close[i+49]
+0.0062194591*Close[i+50]
+0.0070340085*Close[i+51]
+0.0076266453*Close[i+52]
+0.0080376628*Close[i+53]
+0.0083037666*Close[i+54]
+0.0083694798*Close[i+55]
+0.0082901022*Close[i+56]
+0.0080741359*Close[i+57]
+0.0077543820*Close[i+58]
+0.0073260526*Close[i+59]
+0.0068163569*Close[i+60]
+0.0062325477*Close[i+61]
+0.0056078229*Close[i+62]
+0.0049516078*Close[i+63]
+0.0161380976*Close[i+64];

         double value4 =
-0.0074151919*Close[i+0]
-0.0060698985*Close[i+1]
-0.0044979052*Close[i+2]
-0.0027054278*Close[i+3]
-0.0007031702*Close[i+4]
+0.0014951741*Close[i+5]
+0.0038713513*Close[i+6]
+0.0064043271*Close[i+7]
+0.0090702334*Close[i+8]
+0.0118431116*Close[i+9]
+0.0146922652*Close[i+10]
+0.0175884606*Close[i+11]
+0.0204976517*Close[i+12]
+0.0233865835*Close[i+13]
+0.0262218588*Close[i+14]
+0.0289681736*Close[i+15]
+0.0315922931*Close[i+16]
+0.0340614696*Close[i+17]
+0.0363444061*Close[i+18]
+0.0384120882*Close[i+19]
+0.0402373884*Close[i+20]
+0.0417969735*Close[i+21]
+0.0430701377*Close[i+22]
+0.0440399188*Close[i+23]
+0.0446941124*Close[i+24]
+0.0450230100*Close[i+25]
+0.0450230100*Close[i+26]
+0.0446941124*Close[i+27]
+0.0440399188*Close[i+28]
+0.0430701377*Close[i+29]
+0.0417969735*Close[i+30]
+0.0402373884*Close[i+31]
+0.0384120882*Close[i+32]
+0.0363444061*Close[i+33]
+0.0340614696*Close[i+34]
+0.0315922931*Close[i+35]
+0.0289681736*Close[i+36]
+0.0262218588*Close[i+37]
+0.0233865835*Close[i+38]
+0.0204976517*Close[i+39]
+0.0175884606*Close[i+40]
+0.0146922652*Close[i+41]
+0.0118431116*Close[i+42]
+0.0090702334*Close[i+43]
+0.0064043271*Close[i+44]
+0.0038713513*Close[i+45]
+0.0014951741*Close[i+46]
-0.0007031702*Close[i+47]
-0.0027054278*Close[i+48]
-0.0044979052*Close[i+49]
-0.0060698985*Close[i+50]
-0.0074151919*Close[i+51]
-0.0085278517*Close[i+52]
-0.0094111161*Close[i+53]
-0.0100658241*Close[i+54]
-0.0104994302*Close[i+55]
-0.0107227904*Close[i+56]
-0.0107450280*Close[i+57]
-0.0105824763*Close[i+58]
-0.0102517019*Close[i+59]
-0.0097708805*Close[i+60]
-0.0091581551*Close[i+61]
-0.0084345004*Close[i+62]
-0.0076214397*Close[i+63]
-0.0067401718*Close[i+64]
-0.0058083144*Close[i+65]
-0.0048528295*Close[i+66]
-0.0038816271*Close[i+67]
-0.0029244713*Close[i+68]
-0.0019911267*Close[i+69]
-0.0010974211*Close[i+70]
-0.0002535559*Close[i+71]
+0.0005231953*Close[i+72]
+0.0012297491*Close[i+73]
+0.0018539149*Close[i+74]
+0.0023994354*Close[i+75]
+0.0028490136*Close[i+76]
+0.0032221429*Close[i+77]
+0.0034936183*Close[i+78]
+0.0036818974*Close[i+79]
+0.0038037944*Close[i+80]
+0.0038338964*Close[i+81]
+0.0037975350*Close[i+82]
+0.0036986051*Close[i+83]
+0.0035521320*Close[i+84]
+0.0033559226*Close[i+85]
+0.0031224409*Close[i+86]
+0.0028550092*Close[i+87]
+0.0025688349*Close[i+88]
+0.0022682355*Close[i+89]
+0.0073925495*Close[i+90];

    stlm[i]   = iT3(value3-value4,T3Period,T3Hot,T3Original,i,0);
    ftlm[i]   = iT3(value1-value2,T3Period,T3Hot,T3Original,i,1);
    stlmDa[i] = EMPTY_VALUE;
    stlmDb[i] = EMPTY_VALUE;
    ftlmDa[i] = EMPTY_VALUE;
    ftlmDb[i] = EMPTY_VALUE;
    trend[i]  = trend[i+1];
    trene[i]  = trene[i+1];
    
    if(stlm[i] > stlm[i+1]) trend[i] =  1;
    if(stlm[i] < stlm[i+1]) trend[i] = -1;
    if(ftlm[i] > ftlm[i+1]) trene[i] =  1;
    if(ftlm[i] < ftlm[i+1]) trene[i] = -1;
    
    if (MultiColor && !calculateValue && trend[i]==-1) PlotPoint(i,stlmDa,stlmDb,stlm);    
    if (MultiColor && !calculateValue && trene[i]==-1) PlotPoint(i,ftlmDa,ftlmDb,ftlm);     
        
    }
        
    manageAlerts();           
    return(0);
    }
    
    //
    //
    //
    //
    //
    
    limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
    if (MultiColor && trend[limit]==-1) CleanPoint(limit,stlmDa,stlmDb);
    if (MultiColor && trene[limit]==-1) CleanPoint(limit,ftlmDa,ftlmDb);
    for(i=limit; i>=0; i--)
    {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         stlm[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Original,0,y);
         ftlm[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Original,3,y);
         trend[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Original,6,y);
         trene[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",T3Period,T3Hot,T3Original,7,y);
         stlmDa[i] = EMPTY_VALUE;
         stlmDb[i] = EMPTY_VALUE;
         ftlmDa[i] = EMPTY_VALUE;
         ftlmDb[i] = EMPTY_VALUE;
         
    }
    
    if (MultiColor) for (i=limit;i>=0;i--) if (trend[i]==-1) PlotPoint(i,stlmDa,stlmDb,stlm);
    if (MultiColor) for (i=limit;i>=0;i--) if (trene[i]==-1) PlotPoint(i,ftlmDa,ftlmDb,ftlm);
   
   manageAlerts();
   return(0);
}


//+------------------------------------------------------------------+


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



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

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      
      //
      //
      //
      //
      //
      
         if (trend[whichBar] != trend[whichBar+1] && trene[whichBar] != trene[whichBar+1])
         {
            if (trend[whichBar] == 1 && trene[whichBar] == 1) doAlert(whichBar,"strong upmove");
            if (trend[whichBar] ==-1 && trene[whichBar] == 1) doAlert(whichBar,"upmove weakening");
            if (trend[whichBar] ==-1 && trene[whichBar] ==-1) doAlert(whichBar,"strong downmove");
            if (trend[whichBar] == 1 && trene[whichBar] ==-1) doAlert(whichBar,"downmove weakening");

         }                 
    }   
 }
 
//
//
//
//
//

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()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," dtm ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"dtm"),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

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

double workT3[][12];
double workT3Coeffs[][6];
#define _period 0
#define _c1     1
#define _c2     2
#define _c3     3
#define _c4     4
#define _alpha  5

//
//
//
//
//

double iT3(double price, double period, double hot, bool original, int i, int forInstance=0)
{
   if (ArrayRange(workT3,0) !=Bars)                  ArrayResize(workT3,Bars);
   if (ArrayRange(workT3Coeffs,0) < (forInstance+1)) ArrayResize(workT3Coeffs,forInstance+1);

   if (workT3Coeffs[forInstance][_period] != period)
   {
     workT3Coeffs[forInstance][_period] = period;
        double a = hot;
            workT3Coeffs[forInstance][_c1] = -a*a*a;
            workT3Coeffs[forInstance][_c2] = 3*a*a+3*a*a*a;
            workT3Coeffs[forInstance][_c3] = -6*a*a-3*a-3*a*a*a;
            workT3Coeffs[forInstance][_c4] = 1+3*a+a*a*a+3*a*a;
            if (original)
                 workT3Coeffs[forInstance][_alpha] = 2.0/(1.0 + period);
            else workT3Coeffs[forInstance][_alpha] = 2.0/(2.0 + (period-1.0)/2.0);
   }
   
   //
   //
   //
   //
   //
   
   int buffer = forInstance*6;
   int r = Bars-i-1;
   if (r == 0)
      {
         workT3[r][0+buffer] = price;
         workT3[r][1+buffer] = price;
         workT3[r][2+buffer] = price;
         workT3[r][3+buffer] = price;
         workT3[r][4+buffer] = price;
         workT3[r][5+buffer] = price;
      }
   else
      {
         workT3[r][0+buffer] = workT3[r-1][0+buffer]+workT3Coeffs[forInstance][_alpha]*(price              -workT3[r-1][0+buffer]);
         workT3[r][1+buffer] = workT3[r-1][1+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][0+buffer]-workT3[r-1][1+buffer]);
         workT3[r][2+buffer] = workT3[r-1][2+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][1+buffer]-workT3[r-1][2+buffer]);
         workT3[r][3+buffer] = workT3[r-1][3+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][2+buffer]-workT3[r-1][3+buffer]);
         workT3[r][4+buffer] = workT3[r-1][4+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][3+buffer]-workT3[r-1][4+buffer]);
         workT3[r][5+buffer] = workT3[r-1][5+buffer]+workT3Coeffs[forInstance][_alpha]*(workT3[r][4+buffer]-workT3[r-1][5+buffer]);
      }

   //
   //
   //
   //
   //
   
   return(workT3Coeffs[forInstance][_c1]*workT3[r][5+buffer] + 
          workT3Coeffs[forInstance][_c2]*workT3[r][4+buffer] + 
          workT3Coeffs[forInstance][_c3]*workT3[r][3+buffer] + 
          workT3Coeffs[forInstance][_c4]*workT3[r][2+buffer]);
}

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

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