Author: Copyright � 2006, Nick Bilak
HMAenv

This script is designed to display an "envelope" on a trading chart. Think of an envelope as two lines drawn around a central line, indicating potential areas of price fluctuation. Here's how it works:

  1. Input Parameters: The script starts by allowing the user to customize its behavior through a few key settings.

    • _maPeriod: This determines the length of the period used to calculate the main moving average, influencing the responsiveness of the envelope to price changes. A larger number smooths out the average, while a smaller number makes it more sensitive.
    • deviation: This setting dictates how far away the upper and lower lines of the envelope are from the central line, expressed as a percentage. A larger deviation creates a wider envelope.
    • price1 and price2: These parameters decide which price data (e.g., open, close, high, low) the script uses for calculations.
  2. Hull Moving Average (HMA) Calculation:

    • The script uses a variation of the moving average called the Hull Moving Average (HMA). The HMA aims to reduce lag compared to traditional moving averages, making it more responsive to recent price movements.
    • It calculates two WMAs which are then used to calculate 2 more HMA to calculate the bands
  3. Envelope Creation:

    • The script calculates the "middle line" of the envelope using HMA. This line represents the central tendency of the price over the specified period.
    • It then calculates the upper and lower bands by adding and subtracting the specified deviation percentage from the "middle line". This creates the visual "envelope" around the price action.
  4. Visual Display: The script draws the upper and lower bands on the chart as lines. These lines visually represent the envelope around the price, providing a quick visual reference for potential price ranges.

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
1 Downloads
0 Favorites
HMAenv
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                 HMA envelope.mq4 |
//|                                  Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Nick Bilak"
#property link      "http://www.mql4.info"

#property indicator_chart_window
#property indicator_buffers 2

#property indicator_color1 Red
#property indicator_color2 Red

//---- External parameters
extern int _maPeriod=20;
extern double deviation = 0.1;
extern int price1 = PRICE_OPEN;
int price1shift = 0;
int price2shift = 0;
extern int price2 = PRICE_OPEN;
/*
PRICE_CLOSE 0 Close price. 
PRICE_OPEN 1 Open price. 
PRICE_HIGH 2 High price. 
PRICE_LOW 3 Low price. 
PRICE_MEDIAN 4 Median price, (high+low)/2. 
PRICE_TYPICAL 5 Typical price, (high+low+close)/3. 
PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4. 
*/

//---- indicator buffers
double _hma[],_wma[],hma[],wma[],b1[],b2[];

//----
int ExtCountedBars=0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
   if (price1!=PRICE_OPEN) price1shift = 1;
   if (price2!=PRICE_OPEN) price2shift = 1;

	int    draw_begin;
	string short_name;
	IndicatorBuffers(6);

	//---- indicator buffers mapping
	SetIndexBuffer(0, b1);
	SetIndexStyle(0, DRAW_LINE);
	SetIndexEmptyValue(0, 0.0);
	SetIndexBuffer(1, b2);
	SetIndexStyle(1, DRAW_LINE);
	SetIndexEmptyValue(1, 0.0);
	SetIndexBuffer(2, _hma);
	SetIndexEmptyValue(2, 0.0);
	SetIndexBuffer(3, _wma);
	SetIndexEmptyValue(3, 0.0);
	SetIndexBuffer(4, hma);
	SetIndexEmptyValue(4, 0.0);
	SetIndexBuffer(5, wma);
	SetIndexEmptyValue(5, 0.0);
	
	IndicatorDigits(Digits);

	//---- initialization done
	return(0);
}

int start() {
	int i, shift, countedBars=IndicatorCounted();
	int maxBars=_maPeriod*2;
	int period=_maPeriod;
	double sqrtPeriod = MathSqrt(period*1.00);
	int halfPeriod=period/2;

	if(Bars<_maPeriod) return(-1);
   if(countedBars == 0) countedBars = maxBars;
	int limit=Bars-countedBars+maxBars;
	//---- moving average
	double wma1,_wma1;
	double wma2,_wma2;
	for(i=limit; i>=0; i--) {
	  _wma1 = iMA(Symbol(), 0, period, 0, MODE_LWMA, price1, i+price1shift);
	  _wma2 = iMA(Symbol(), 0, halfPeriod, 0, MODE_LWMA, price1, i+price1shift);
	  _wma[i] = 2.0*_wma2-_wma1;
	  wma1 = iMA(Symbol(), 0, period, 0, MODE_LWMA, price2, i+price2shift);
	  wma2 = iMA(Symbol(), 0, halfPeriod, 0, MODE_LWMA, price2, i+price2shift);
	  wma[i] = 2.0*wma2-wma1;
	} 
	
	for(i=limit; i>=0; i--) {
	  _hma[i]=iMAOnArray(_wma, 0, sqrtPeriod, 0, MODE_LWMA, i);
	  hma[i]=iMAOnArray(wma, 0, sqrtPeriod, 0, MODE_LWMA, i);
	  b1[i]=_hma[i]+_hma[i]*deviation/100.0;
	  b2[i]=hma[i]-hma[i]*deviation/100.0;
	} 

	return(0);
}

Comments