Author: Written by Rosh
TDI_v2

The Trend Direction Index (TDI) is a technical analysis tool designed to help traders visualize the underlying momentum and direction of a market trend. Rather than looking at raw price action, this script processes price data to smooth out "noise," making it easier to identify whether the current market sentiment is bullish, bearish, or consolidating.

Core Logic and Workflow

The tool operates by calculating three primary layers of data:

1. Momentum Analysis The script begins by measuring the change in price over a specific look-back period (defined by the user as the TDI Period). By subtracting the previous price from the current price, it identifies the basic movement. To ensure the data is useful for long-term analysis, the tool then takes the "absolute value" of these movements. This allows the script to recognize the strength of a move regardless of whether it was moving up or down.

2. Trend Direction Identification Once the momentum is established, the script applies a moving average to these values. This step acts as a filter; it averages out the short-term fluctuations to reveal a clearer "trend line." This helps distinguish between a temporary price spike and a sustained shift in market direction. The tool then plots this trend information as a visual indicator on your chart.

3. Trend Strength and Exhaustion The final calculation provides a more nuanced view of the market's health. It compares different averages of the momentum data against one another. By subtracting a secondary moving average from a primary one, the script highlights:

  • Trend Intensity: How aggressively the price is moving in its current direction.
  • Potential Reversals: When the distance between these calculated averages shrinks or shifts significantly, it can signal that the current momentum is losing steam and the market might be preparing for a change in direction.

Summary of Visual Output

For the user, this means the indicator provides a visual representation of "market energy." Instead of guessing based on volatile candles, you can see a smoother representation of where the price wants to go next, helping you identify when a trend is strong enough to join and when it is becoming too weak to sustain.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
TDI_v2
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                          TDI.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
// Èäåÿ èç ForexMagazin #58 
#property copyright "Written by Rosh"
#property link      "http://www.metaquotes.net"

#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[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TDI_Buffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Direction_Buffer);
   SetIndexLabel(0,"Trend Direction Index");
   SetIndexLabel(1,"Direction");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double MomBuffer[];
   double MomAbsBuffer[];
   double MomSumBuffer[];
   double MomSumAbsBuffer[];
   double MomAbsSumBuffer[];
   double MomAbsSum2Buffer[];
   double temp;
   bool First=true;
   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) counted_bars--;
   if (First){limit=Bars-PeriodTDI; First=false;} else limit=Bars-counted_bars;
//   limit=Bars-PeriodTDI;
   ArrayResize(MomBuffer,limit);
   ArrayResize(MomAbsBuffer,limit);
   ArrayResize(MomSumBuffer,limit);
   ArrayResize(MomSumAbsBuffer,limit);
   ArrayResize(MomAbsSumBuffer,limit);
   ArrayResize(MomAbsSum2Buffer,limit);
   ArraySetAsSeries(MomBuffer,true);
   ArraySetAsSeries(MomAbsBuffer,true);
   ArraySetAsSeries(MomSumBuffer,true);
   ArraySetAsSeries(MomSumAbsBuffer,true);
   ArraySetAsSeries(MomAbsSumBuffer,true);
   ArraySetAsSeries(MomAbsSum2Buffer,true);
   
//---- TODO: add your code here
   for(i=0; i<limit; i++)
      {
      MomBuffer[i]=Close[i]-Close[i+PeriodTDI];
      MomAbsBuffer[i]=MathAbs(MomBuffer[i]);
      }
   for(i=limit; i>=0; i--)
      {
      temp=0;
      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