ttm-trend
Download scriptThis tool is designed to help traders visualize market trends by identifying the momentum and direction of price movements on a trading chart. Instead of just looking at raw price points, it uses a specialized smoothing logic to filter out minor market noise and highlight the prevailing trend.
How the Logic Works
1. Price Smoothing (Heikin Ashi Style) The script begins by calculating "smoothed" prices. Rather than relying solely on the standard opening and closing prices of a candle, it averages them with the high and low points. This creates a more fluid representation of price action, making it easier to see the underlying trend without being distracted by sudden, erratic price spikes.
2. Trend Direction Detection The core logic determines the "color" or direction of the trend based on whether the smoothed closing price is higher or lower than the smoothed opening price.
- If the close is higher, it signals upward momentum.
- If the close is lower, it signals downward momentum.
3. Momentum Continuity (The Comparison Logic) To ensure the trend signal is stable and not flickering back and forth constantly, the script looks back at a specific number of previous bars (defined by the "CompBars" setting). It checks if the current price action is "overlapping" with the previous candles. If the current movement is nested within the range of those previous candles, the script maintains the previous trend color rather than switching it. This prevents "whipsaws"—where a tiny price move might trick the indicator into showing a trend change that isn't actually happening.
4. Visual Representation Finally, the script translates these calculations into a visual histogram on your chart:
- Bullish Trend: When the logic confirms upward momentum, the indicator highlights the high and low of the candle in a specific color (Blue).
- Bearish Trend: When the logic confirms downward momentum, it switches the highlighting to another color (Red).
Summary for the Trader
Essentially, this script acts as a "momentum filter." It smooths out the jagged movements of the market and looks backward at recent history to ensure that a trend change is significant before it updates the visual display. This helps you stay on the right side of the trend by providing a clearer, more stable picture of whether the market is currently pushing up or down.
//+------------------------------------------------------------------+
//| ttm-trend.mq4 |
//| Copyright © 2005, Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Nick Bilak"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color2 Red
#property indicator_color1 Blue
//---- input parameters
extern int CompBars=6;
//---- buffers
double buf1[];
double buf2[];
double haOpen[];
double haClose[];
double icolor[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(5);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,buf1);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(1,buf2);
SetIndexBuffer(2,haOpen);
SetIndexBuffer(3,haClose);
SetIndexBuffer(4,icolor);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int shift,limit,i;
int counted_bars=IndicatorCounted();
if (counted_bars<0) return(-1);
if (counted_bars>0) counted_bars--;
limit=Bars-4;
if(counted_bars>=1) limit=Bars-counted_bars-4;
double dip,dim,stm,sts,macdm,macds;
if (haOpen[limit+3]==0.0) haOpen[limit+3]=Open[limit+3];
haClose[limit+3] = (Open[limit+3]+High[limit+3]+Low[limit+3]+Close[limit+3])/4.0;
for(shift = limit+2; shift >= 0; shift--) {
haOpen[shift] = (haOpen[shift+1]+haClose[shift+1])/2.0;
haClose[shift] = (Open[shift]+High[shift]+Low[shift]+Close[shift])/4.0;
if (haClose[shift] > haOpen[shift]) icolor[shift] = 1;
else icolor[shift] = -1;
for (i=1; i<=CompBars; i++) {
if ( haOpen[shift] <= MathMax(haOpen[shift+i],haClose[shift+i]) &&
haOpen[shift] >= MathMin(haOpen[shift+i],haClose[shift+i]) &&
haClose[shift] <= MathMax(haOpen[shift+i],haClose[shift+i]) &&
haClose[shift] >= MathMin(haOpen[shift+i],haClose[shift+i]) )
icolor[shift] = icolor[shift+i];
}
if (icolor[shift]>0) {
buf1[shift]=High[shift];
buf2[shift]=Low[shift];
} else {
buf2[shift]=High[shift];
buf1[shift]=Low[shift];
}
}
}
//+------------------------------------------------------------------+
Comments