TDI-2_v2
Download scriptThis script is designed to function as a visual trend-following tool for the MetaTrader platform. Its primary purpose is to help traders identify the strength and direction of market momentum by analyzing price changes over a specific period of time.
How the Logic Works
1. Momentum Foundation The logic begins by measuring the "Momentum." It looks at the current closing price and compares it to the closing price from a set number of bars ago (defined by the user). By calculating this difference, the script determines how much the price has moved in a specific direction over that timeframe.
2. Smoothing the Data Raw market data can be "noisy" and jump around unpredictably. To make the information useful for human eyes, the script performs several layers of smoothing:
- It calculates the average movement (Simple Moving Average) of the momentum.
- It then analyzes the absolute values of these movements to understand the sheer magnitude of price action, regardless of whether it was moving up or down.
3. Trend Direction Calculation The core "intelligence" of this script lies in its multi-step mathematical progression. Instead of just showing a simple line, it combines several different averages of momentum and absolute momentum. By subtracting and layering these different types of averages against each other, the script filters out minor price fluctuations and highlights the underlying trend.
4. Visual Representation The final result is presented to the user as two distinct visual components:
- Trend Direction Index: A primary line that represents the smoothed momentum and provides a clearer picture of the market's "flow."
- Direction Buffer: A supporting indicator used to confirm the trend strength.
Summary for the User
In simple terms, this script acts like a filter. It takes the raw, messy movements of the Forex market, strips away the minor "hiccups" in price, and calculates a smoothed path that shows you whether the market is gaining strength or losing steam. This allows you to see the broader trend more clearly than you might be able to by looking at the price candles alone.
//+------------------------------------------------------------------+
//| 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