HMA_Russian_Color_sep

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

This technical script is designed to analyze financial market trends by utilizing a specialized moving average known as the Hull Moving Average (HMA). Its primary objective is to smooth out market noise while maintaining high responsiveness, allowing traders to identify the direction of a trend more clearly than standard moving averages.

Core Logic and Functionality

The script operates through a multi-layered mathematical process to determine price direction:

1. Data Smoothing and Weighting Instead of simply averaging prices over a set period, the logic performs a series of calculations involving Weighted Moving Averages (WMA). It first calculates the difference between two weighted averages—one representing half of the user-defined period and the other representing the full period. This specific calculation is designed to reduce "lag," which is the common delay found in most traditional indicators where the line follows the price too slowly.

2. Secondary Trend Filtering Once the initial smoothing is complete, the script applies a second layer of calculation. It takes the results from the first step and runs another moving average over them. This "averaging of averages" helps to create a much smoother curve that highlights the underlying momentum of the market without being distracted by minor, temporary price fluctuations.

3. Trend Identification and Visualization The script then analyzes the direction of this final smoothed curve to determine whether the market is in an "Up" or "Down" state:

  • Uptrend: When the current value of the smoothed curve is higher than the previous value, the logic identifies an upward movement. It highlights these points specifically to show bullish momentum.
  • Downtrend: When the value of the curve drops compared to the previous point, the logic identifies a downward movement, highlighting bearish momentum.

User Experience

For the user, this results in a visual representation where the trend is clearly separated into two distinct components. By calculating these transitions automatically, the script helps in identifying exactly when a trend has shifted from positive to negative (or vice versa), providing a cleaner view of market cycles than raw price data alone.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
15 Views
0 Downloads
0 Favorites
HMA_Russian_Color_sep
//+------------------------------------------------------------------+ 
//| HMA.mq4 
//| Copyright © 2006 WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104 
//| wizardserg@mail.ru 
//|                         Revised by IgorAD,igorad2003@yahoo.co.uk |   
//|                                        http://www.forex-tsd.com/ |                                      
//+------------------------------------------------------------------+
#property copyright "MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104" 
#property link      "wizardserg@mail.ru" 

#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_color1 Aqua 
#property indicator_color2 Tomato 
//---- input parameters 
extern int       period=21; 
extern int       method=0;                         // 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("Hull Moving Average("+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