//+------------------------------------------------------------------+ //| 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); }