TDI-2
Download scriptThe TDI-2 indicator is designed to provide traders with a clearer picture of market momentum and trend direction by smoothing out price fluctuations. Unlike standard indicators that might react too quickly to "noise" in the market, this tool uses a multi-layered calculation process to identify the underlying strength and orientation of a currency pair's movement.
How the Logic Works
The script processes market data through three primary stages to arrive at its final visual output:
1. Momentum Detection The logic begins by calculating the raw difference between the current price and the price from a specific number of bars back (determined by the user-defined period). This establishes the basic "momentum"—how much and in what direction the price has moved over that timeframe.
2. Smoothing and Directional Analysis Once the raw momentum is established, the script applies a Simple Moving Average (SMA) to those figures. This step serves two purposes:
- It filters out temporary price spikes to show a smoother trend.
- It helps identify the general direction of the market (upward or downward) by calculating the cumulative sum of these smoothed movements.
3. Trend Direction Index (TDI) Calculation The final, most sophisticated layer of the logic involves comparing the current smoothed momentum against previous historical averages. By analyzing the difference between the current moving average and a "double-period" moving average, the script calculates the velocity of the trend.
Visual Representation on the Chart
The result of these calculations is displayed in a separate window below your main price chart:
- Trend Direction Index (Blue Line): This represents the primary trend strength. When this line moves significantly above or below the zero level, it indicates the prevailing momentum of the market.
- Directional Buffer (Red Line): This provides a secondary visual cue to help traders distinguish between active price movement and mere volatility.
By combining these layers, the TDI-2 aims to help you stay on the right side of the trend, making it easier to identify when a move has enough strength to be sustained or when momentum is starting to fade.
//+------------------------------------------------------------------+
//| TDI-2.mq4 |
//| Ñèñòåìíàÿ òîðãîâëÿ íà ðûíêå Forex |
//| http://forexsystems.ru/phpBB/index.php |
//+------------------------------------------------------------------+
// Èäåÿ èç ForexMagazin #58 , âòîðàÿ âåðñèÿ
#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);
}
//+------------------------------------------------------------------+
Comments