|
TDI |
|
TDI |
|
TDI |
|
TDI |
|
TDI |
|
TDI |
|
TDI |
|
TDI |
The Trend Detection Index - TDI was introduced by M. H. Pee, this hybrid indicator is developed to assist traders in their ability to decipher and monitor market conditions, respectively indicator is used to detect market trend. The trend detection index will signal a trend if it shows a positive value and a consolidation if it shows a negative value. As a trend-follower, the position should be entered in the direction of the trend when the Trend Detection Index - TDI is positive. If the blue line is above zero; the trend is up. If the blue line is below zero; the trend is down. You will also see the red line that gives the current direction of the trend. To calculate the 20-day trend detection index, first find the value of the momentum indicator. After the market closes, calculate today's 20-day momentum by subtracting the close 20 days ago from that of today. Next, find the 20-day absolute momentum, which is defined as the absolute value of today's 20-day momentum. Mathematical Formula:
20-day Trend Detection Index - TDI = (AV20) - {(∑ AM40) - (∑ AM20)}
AV20 = Absolute value of the sum of 20-day momenta of the last 20 days
∑ AM40 = Sum of 20-day absolute momenta of the last 40 days
∑ AM20 = Sum of 20-day absolute momenta of the last 20 days
Trend Detection Index - TDI
MQ4 Code Base (Copy Code)
//+------------------------------------------------------------------+
//| TDI.mq4 |
//| http://plusforex.blogspot.com/ |
//| http://forexsystems.ru/phpBB/index.php |
//+------------------------------------------------------------------+
// Forex Indicators Blog, plusforex.blogspot.com
#property copyright "Written by Rosh"
#property link "http://forexsystems.ru/phpBB/index.php"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DarkBlue
#property indicator_color2 Red
#property indicator_level1 0.0
//---- input parameters
extern int PeriodTDI=20;
//---- buffers
double TDI_Buffer[];
double Direction_Buffer[];
// ---- temporary buffers
double MomBuffer[];
double MomAbsBuffer[];
double MomSumBuffer[];
double MomSumAbsBuffer[];
double MomAbsSumBuffer[];
double MomAbsSum2Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(8);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,TDI_Buffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,Direction_Buffer);
SetIndexLabel(0,"Trend Direction Index");
SetIndexLabel(1,"Direction");
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexBuffer(2,MomBuffer);
SetIndexBuffer(3,MomAbsBuffer);
SetIndexBuffer(4,MomSumBuffer);
SetIndexBuffer(5,MomSumAbsBuffer);
SetIndexBuffer(6,MomAbsSumBuffer);
SetIndexBuffer(7,MomAbsSum2Buffer);
IndicatorShortName("TDI"+"("+PeriodTDI+")");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit,i;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) limit=Bars-counted_bars;
if (counted_bars==0)
{
limit=Bars-PeriodTDI;
for (i=Bars-1;i>limit;i--) {MomBuffer[i]=0.0;MomAbsBuffer[i]=0.0;}
}
limit--;
//---- TODO: add your code here
for(i=limit; i>=0; i--)
{
MomBuffer[i]=Close[i]-Close[i+PeriodTDI];
MomAbsBuffer[i]=MathAbs(MomBuffer[i]);
}
for(i=limit; i>=0; i--)
{
MomSumBuffer[i]=iMAOnArray(MomBuffer,0,PeriodTDI,0,MODE_SMA,i)*PeriodTDI;
MomSumAbsBuffer[i]=MathAbs(MomSumBuffer[i]);
Direction_Buffer[i]=MomSumBuffer[i];
}
for(int l=limit; l>=0; l--)
{
MomAbsSumBuffer[l]=iMAOnArray(MomAbsBuffer,0,PeriodTDI,0,MODE_SMA,l)*PeriodTDI;
MomAbsSum2Buffer[l]=iMAOnArray(MomAbsBuffer,0,2*PeriodTDI,0,MODE_SMA,l)*2*PeriodTDI;
TDI_Buffer[l]=MomSumAbsBuffer[l]-(MomAbsSum2Buffer[l]-MomAbsSumBuffer[l]);
}
//---- done
//----
return(0);
}
//+------------------------------------------------------------------+