Author: Written by Rosh
TDI

This script is designed to function as a visual technical indicator for the MetaTrader 4 trading platform. Its primary purpose is to help traders identify market trends and momentum by calculating and displaying the Trend Direction Index (TDI).

Core Logic and Functionality

The indicator works by processing historical price data—specifically closing prices—to filter out "market noise" and highlight the underlying direction of the market. It achieves this through a multi-step mathematical progression:

1. Momentum Calculation The script begins by measuring the raw momentum of the market. It calculates the difference between the current closing price and the closing price from a specific number of bars ago (defined by the user-adjustable period). This tells the tool how much the price has moved over a set timeframe.

2. Smoothing and Normalization To ensure the data is readable and not overly volatile, the script applies a "Simple Moving Average" to those momentum values. It takes the average movement over the specified period and scales it. This step transforms raw price fluctuations into a smoother curve that represents the general "flow" of the market.

3. Directional Analysis The tool distinguishes between positive and negative momentum. By calculating the absolute value (the magnitude) of these movements, it can better understand the strength of a trend regardless of whether the price is moving up or down.

4. Final Trend Index Generation The final visual output is produced by comparing different averages of these smoothed values. By subtracting a secondary, weighted average from the primary average, the script isolates the most significant trend signals.

Visual Representation

The logic results in two distinct visual components displayed in a separate window below the main price chart:

  • Trend Direction Index: This is the primary line that moves across the screen. It reflects the smoothed, filtered momentum of the asset.
  • Direction Indicator: This provides a secondary reference point to help traders see the intensity and orientation of the market flow.

In summary, rather than looking at every tiny tick in price movement, this script aggregates historical data into a smoothed trend line, making it easier for traders to visualize whether the current market sentiment is leaning bullish, bearish, orsideways.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
TDI
//+------------------------------------------------------------------+
//|                                                          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
//---- 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