Author: Written by Rosh
TDI_v1

This tool is designed to help traders visualize and identify market trends by analyzing price momentum. Rather than looking at the raw price of an asset, it focuses on the "speed" and "strength" of price movements over a specific period, smoothing out the noise to reveal the underlying direction.

How the Logic Works

The script processes data through several layers of calculation to arrive at the final visual output:

1. Measuring Raw Momentum First, the tool looks at the price difference between the current closing price and the price from a set number of bars ago (the "Period"). This creates a raw momentum value. If the price is moving up, the value is positive; if it is moving down, it is negative.

2. Establishing Trend Direction The script then takes these raw momentum values and calculates an average. This helps identify the general "flow" of the market. It creates a dedicated line that represents whether the current trend is leaning bullish (upward) or bearish (downward), effectively filtering out minor price fluctuations.

3. Calculating Strength and Volatility To make the data more useful, the tool looks at the absolute strength of these movements—essentially measuring how "hard" the market is pushing in any given direction, regardless of whether it is up or down.

4. Final Trend Index Calculation Finally, the tool combines these elements into a single visual indicator. It compares the current smoothed momentum against a weighted average of past volatility. By subtracting the adjusted historical volatility from the current smoothed sum, it produces a refined "Trend Direction Index."

What this means for the user

The resulting lines on your chart provide a clearer picture of the market's health:

  • Directional Awareness: It helps you see if the primary trend is moving with strength or losing steam.
  • Smoothing: Because it uses multiple layers of averaging, it reduces "市場雜訊" (market noise), allowing you to see the broader trend more clearly than by looking at raw price candles alone.
  • Visual Clarity: By separating the general trend from its directional component, the tool allows you to distinguish between a market that is simply moving and a market that is moving with significant momentum.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
TDI_v1
//+------------------------------------------------------------------+
//|                                                          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