BB_MACD
Download scriptThis custom indicator is designed to provide traders with a visual tool that combines two well-known technical analysis methods: the MACD (Moving Average Convergence Divergence) and Bollinger Bands. By blending these two concepts, the script aims to provide a clearer picture of market momentum and volatility than either indicator could offer on its own.
Core Logic & Functionality
The indicator operates through a multi-step mathematical process to generate visual data on your trading chart:
1. Momentum Calculation (The MACD Base) First, the script calculates the difference between two types of moving averages—a "fast" average and a "slow" average. This creates the primary MACD line. This line represents the momentum of the market; it shows whether the price is accelerating in one direction or the other.
2. Dynamic Range Filtering (The Bollinger Bands) Instead of applying Bollinger Bands directly to the price, this script applies them specifically to the MACD values. It calculates a moving average of the MACD line and then determines the standard deviation (volatility). Based on these calculations, it creates two boundary lines:
- Upper Band: Represents the high-end momentum limit.
- Lower Band: Represents the low-end momentum limit.
3. Visual Signals and Trends The script is programmed to display specific visual cues based on the movement of the MACD line:
- Trend Identification: The indicator monitors whether the current MACD value is rising or falling compared to the previous point.
- Dynamic Arrows: It uses a logic gate to toggle visual markers (arrows). If the momentum is increasing, it highlights specific data points; if it is decreasing, it clears those markers. This helps the user quickly identify the direction of the trend at a glance.
Summary of Visual Output
When viewing the indicator on your platform, you will see:
- The MACD Line: The primary moving line showing current momentum.
- Upper and Lower Bands: A "channel" around the MACD line that helps identify when momentum is reaching extreme levels (overbought or oversold conditions relative to its own average).
- Directional Markers: Visual indicators that react automatically as the momentum shifts from bullish to bearish, helping to filter out noise and focus on active price movements.
//+------------------------------------------------------------------+
//| Custom BB_MACD.mq4 |
//| Copyright © 2005, adoleh2000 |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, adoleh2000"
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Lime //bbMacd up
#property indicator_color2 Red //bbMacd up
#property indicator_color3 Blue //Upperband
#property indicator_color4 Red //Lowerband
//---- indicator parameters
extern int FastLen=9;
extern int SlowLen=18;
extern int Length=10;
extern double StDv=2.5;
int loopbegin;
int shift;
double zeroline;
//---- indicator buffers
//---- buffers
double ExtMapBuffer1[]; // bbMacd
double ExtMapBuffer2[]; // bbMacd
double ExtMapBuffer3[]; // Upperband Line
double ExtMapBuffer4[]; // Lowerband Line
double bbMacd[];
double bbMacdline;
double Upperband[];
double Lowerband[];
double avg[];
double sDev;
double mean;
double sumSqr;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 6 additional buffers are used for counting.
IndicatorBuffers(8);
//---- drawing settings
SetIndexBuffer(0,ExtMapBuffer1);//bbMacd line
SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1);
SetIndexArrow(0,159);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexBuffer(1,ExtMapBuffer2);//bbMacd line
SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
SetIndexArrow(1,159);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexBuffer(2,ExtMapBuffer3); //Upperband line
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexBuffer(3,ExtMapBuffer4); //Lowerband line
SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
SetIndexBuffer(4,bbMacd);
SetIndexBuffer(5,Upperband);
SetIndexBuffer(6,Lowerband);
SetIndexBuffer(7,avg);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("BB MACD ("+FastLen+","+SlowLen+","+Length+")");
SetIndexLabel(0,"bbMacd");
SetIndexLabel(1,"Upperband");
SetIndexLabel(2,"Lowerband");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
// --------------------------
//----
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//loopbegin = Bars-1;
//for(int i = loopbegin; i >= 0; i--)
for(int i=0; i<limit; i++)
bbMacd[i]=iMA(NULL,0,FastLen,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowLen,0,MODE_EMA,PRICE_CLOSE,i);
for(i=0; i<limit; i++)
{
avg[i]=iMAOnArray(bbMacd,0,Length,0,MODE_EMA,i);
//Comment("avg[i]=",avg[i]," bbMacd[i+1]=",bbMacd[i+1]," bbMacd[i]=",bbMacd[i]," Length=",Length," sDev=",sDev," Upperband=",Upperband[i]," Lowerband=",Lowerband[i]);
sDev = iStdDevOnArray(bbMacd,0,Length,MODE_EMA,0,i);
Upperband[i] = avg[i] + (StDv * sDev);
Lowerband[i] = avg[i] - (StDv * sDev);
ExtMapBuffer1[i]=bbMacd[i]; //Uptrend bbMacd
ExtMapBuffer2[i]=bbMacd[i]; // downtrend bbMacd
ExtMapBuffer3[i]=Upperband[i];// Upperband
ExtMapBuffer4[i]=Lowerband[i];//Lowerband
if (bbMacd[i]>bbMacd[i+1])
{
ExtMapBuffer2[i]=EMPTY_VALUE;
}
if (bbMacd[i]<bbMacd[i+1])
{
ExtMapBuffer1[i]=EMPTY_VALUE;
}
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments