Author: Written by Rosh
TDI-2_v1

The TDI-2 script is a technical analysis tool designed to help traders visualize and interpret market trends by analyzing price momentum. Unlike standard indicators that simply show the current price direction, this script processes historical data to smooth out "noise," helping users identify the underlying strength and direction of a currency pair or asset.

Core Logic Overview

The logic operates through a multi-layered calculation process. It transforms raw price data into three distinct visual components:

1. Momentum Analysis The foundation of the script begins by calculating the difference between the current closing price and the closing price from a specific number of periods ago (defined by the user as the "TDI Period"). By measuring this gap, the script determines how much the price has moved over a set timeframe.

2. Directional Filtering The script then evaluates the magnitude of these momentum movements. It takes the average of these fluctuations to establish a baseline for market activity. This creates a "Direction" line, which helps traders see whether the prevailing movement is trending upward, downward, or remaining stagnant.

3. Trend Index Calculation The final layer of the logic focuses on the "Trend Direction Index." To achieve this, the script calculates the sum of absolute momentum values over two different scales:

  • It measures the average movement over the primary period.
  • It simultaneously measures the average movement over a doubled period.

By subtracting the larger, slower cycle from the smaller, faster cycle, the script isolates the current trend's acceleration. This results in a visual line that reflects the "heartbeat" of the market—showing not just where the price is going, but the velocity and consistency of that movement.

Visual Representation

The indicator displays these calculations in a separate window below the main price chart:

  • Trend Direction Index (TDI): A primary line used to identify the overall trend strength.
  • Direction Line: A secondary reference line that helps confirm the trajectory of the market movement.

By using these three layers—raw momentum, directional averaging, and multi-period trend subtraction—the script provides a comprehensive view of market dynamics, allowing users to distinguish between temporary price spikes and sustained trading trends.

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

//+------------------------------------------------------------------+
//|                                                        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