Author: MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104
MegaTrend

This technical tool, known as the MegaTrend HMA, is designed to help traders visualize and identify the overall direction of a financial market (the "trend") with greater clarity than standard moving averages. Rather than just showing a simple average price, it uses a multi-layered mathematical approach to smooth out minor price fluctuations and highlight significant momentum shifts.

How the Logic Works

The script processes market data through three distinct logical stages to determine the trend:

1. Velocity Calculation Instead of looking directly at the price, the tool first calculates a "vector." It does this by comparing two different weighted moving averages—one based on a full period and another based on half that period. By calculating the difference between these two, the script identifies the current "speed" or velocity of the price movement. This helps the tool distinguish between a steady trend and stagnant price action.

2. Signal Smoothing The raw velocity data can often be "noisy" or jumpy. To solve this, the script applies an additional smoothing calculation (an HMA-style transformation) to that velocity vector. This creates a secondary map of the market’s momentum, filtering out insignificant market "hiccups" while retaining the core direction of the price movement.

3. Trend Identification and Visualization Finally, the script analyzes the direction of this smoothed momentum map:

  • Uptrend: When the momentum map is moving upward, the tool identifies an active uptrend. It renders a lime-colored line on your chart to signify bullish sentiment.
  • Downtrend: When the momentum map shifts downward, the tool identifies a downtrend and switches to a red line to signal bearish sentiment.

Key Features for the Trader

  • Clean Visualization: The tool automatically manages the lines so that you only see the relevant trend. When the market switches from up to down, it clearly marks the transition points.
  • Reduced Lag: By using weighted averages and velocity vectors, the logic attempts to react to price changes faster than a traditional moving average would, while still providing a smooth visual experience.
  • Customizable Sensitivity: Users can adjust the "Period" setting to determine how aggressively the tool reacts to new data. A lower period makes the trend lines more sensitive to quick price changes, while a higher period creates smoother, more stable trend lines suitable for long-term analysis.
Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
MegaTrend
//+------------------------------------------------------------------+ 
//|  MegaTrend   HMA.mq4 
//| Copyright © 2006 WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104 
//| wizardserg@mail.ru 
//| Revised by IgorAD,igorad2003@yahoo.co.uk |   
//|                                         
//| Personalized by iGoR for the Trend Slope Trading method (T_S_T) 
//| Link: http://www.strategybuilderfx.com/forums/showthread.php?t=16507
//| contact: igor@igor.cc                                 
//+------------------------------------------------------------------+
#property copyright "MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104" 
#property link      "wizardserg@mail.ru" 

#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_color1 Lime 
#property indicator_color2 Red 
//---- input parameters 
extern int       period=15; 
extern int       method=3;                         // MODE_SMA 
extern int       price=0;                          // PRICE_CLOSE 
//---- buffers 
double Uptrend[];
double Dntrend[];
double ExtMapBuffer[]; 


//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
{ 
    IndicatorBuffers(3);  
    SetIndexBuffer(0, Uptrend); 
    //ArraySetAsSeries(Uptrend, true); 
    SetIndexBuffer(1, Dntrend); 
    //ArraySetAsSeries(Dntrend, true); 
    SetIndexBuffer(2, ExtMapBuffer); 
    ArraySetAsSeries(ExtMapBuffer, true); 
    
    SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
    SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
    
    IndicatorShortName("MegaTrend("+period+")"); 
    return(0); 
} 

//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
{ 
    // ???? ????? ?????? ?????? 
    return(0); 
} 

//+------------------------------------------------------------------+ 
//| ?????????? ???????                                               | 
//+------------------------------------------------------------------+ 
double WMA(int x, int p) 
{ 
    return(iMA(NULL, 0, p, 0, method, price, x));    
} 

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() 
{ 
    int counted_bars = IndicatorCounted(); 
    
    if(counted_bars < 0) 
        return(-1); 
                  
    int x = 0; 
    int p = MathSqrt(period);              
    int e = Bars - counted_bars + period + 1; 
    
    double vect[], trend[]; 
    
    if(e > Bars) 
        e = Bars;    

    ArrayResize(vect, e); 
    ArraySetAsSeries(vect, true);
    ArrayResize(trend, e); 
    ArraySetAsSeries(trend, true); 
    
    for(x = 0; x < e; x++) 
    { 
        vect[x] = 2*WMA(x, period/2) - WMA(x, period);        
 //       Print("Bar date/time: ", TimeToStr(Time[x]), " close: ", Close[x], " vect[", x, "] = ", vect[x], " 2*WMA(p/2) = ", 2*WMA(x, period/2), " WMA(p) = ",  WMA(x, period)); 
    } 

    for(x = 0; x < e-period; x++)
     
        ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);        
    
    for(x = e-period; x >= 0; x--)
    {     
        trend[x] = trend[x+1];
        if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
        if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;
    
    if (trend[x]>0)
    { Uptrend[x] = ExtMapBuffer[x]; 
      if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
      Dntrend[x] = EMPTY_VALUE;
    }
    else              
    if (trend[x]<0)
    { 
      Dntrend[x] = ExtMapBuffer[x]; 
      if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
      Uptrend[x] = EMPTY_VALUE;
    }              
    
    //Print( " trend=",trend[x]);
    }
    
    return(0); 
} 
//+------------------------------------------------------------------+ 

Comments