BB_MACD3_v1
Download scriptThis script is a technical analysis tool designed to help traders identify price momentum and volatility trends by combining two popular trading methodologies: the Moving Average Convergence Divergence (MACD) and Bollinger Bands.
How the Logic Works
The indicator operates by processing market data through three distinct layers of calculation:
1. Momentum Foundation (MACD) First, the script calculates the difference between two Exponential Moving Averages (EMAs). By subtracting a slower moving average from a faster one, the tool isolates the "velocity" of the price. This tells the user whether the market is accelerating in one direction or slowing down.
2. Volatility Envelope (Bollinger Bands) Instead of applying standard Bollinger Bands directly to the price chart, this script applies them to the momentum data calculated in the previous step. It takes the average of that momentum and creates an "upper" and "lower" boundary based on a specific standard deviation. This creates a visual corridor:
- Upper Band: Represents an overextended upward move in momentum.
- Lower Band: Represents an overextended downward move in momentum.
3. Visual Trend Detection The script translates these complex calculations into easy-to-read visual signals on your chart:
- Directional Arrows: The tool monitors the movement of the MACD value from one bar to the next. If the momentum is rising, it places an upward signal (Lime color). If the momentum is falling, it places a downward signal (Magenta color).
- The Momentum Corridor: By plotting the Upper and Lower bands in solid lines, it provides a visual "channel." When the momentum signals move toward or outside of these bands, it can help traders identify periods of extreme exhaustion or high-intensity trends.
Summary for the Trader
Essentially, this tool doesn't just tell you if the price is going up or down; it tells you how aggressively it is moving. By looking at the relationship between the directional arrows and the volatility bands, users can better visualize whether a current trend is healthy, overextended, or beginning to lose steam.
//+------------------------------------------------------------------+
//| 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 Magenta //bbMacd down
#property indicator_color3 Blue //Upperband
#property indicator_color4 Red //Lowerband
//---- indicator parameters
extern int FastLen = 12;
extern int SlowLen = 26;
extern int Length = 9;
extern double StDv = 2.5;
extern int BarsAmount = 200;
//---- indicator buffers
double ExtMapBuffer1[]; // bbMacd up
double ExtMapBuffer2[]; // bbMacd down
double ExtMapBuffer3[]; // Upperband Line
double ExtMapBuffer4[]; // Lowerband Line
//---- buffers
double bbMacd[];
double avg;
double sDev;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 1 additional buffers are used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexBuffer(0, ExtMapBuffer1); // bbMacd up line
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 159);
IndicatorDigits(Digits + 1);
//----
SetIndexBuffer(1, ExtMapBuffer2); // bbMacd down line
SetIndexStyle(1, DRAW_ARROW);
SetIndexArrow(1, 159);
IndicatorDigits(Digits + 1);
//----
SetIndexBuffer(2, ExtMapBuffer3); // Upperband line
SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 1);
IndicatorDigits(Digits + 1);
//----
SetIndexBuffer(3, ExtMapBuffer4); // Lowerband line
SetIndexStyle(3, DRAW_LINE, STYLE_SOLID, 1);
IndicatorDigits(Digits + 1);
//----
SetIndexBuffer(4, bbMacd);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("BB MACD(" + FastLen + "," + SlowLen + "," + Length+")");
SetIndexLabel(0, "bbMacdUp");
SetIndexLabel(1, "bbMacdDn");
SetIndexLabel(2, "Upperband");
SetIndexLabel(3, "Lowerband");
//----
SetIndexDrawBegin(0, Bars - BarsAmount - 1);
SetIndexDrawBegin(1, Bars - BarsAmount - 1);
SetIndexDrawBegin(2, Bars - BarsAmount - 1);
SetIndexDrawBegin(3, Bars - BarsAmount - 1);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom BB_MACD |
//+------------------------------------------------------------------+
int start()
{
int limit;
if(Bars < MathMax(FastLen, MathMax(SlowLen, Length)))
return(-1);
if(Bars < BarsAmount)
BarsAmount = Bars;
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;
//----
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 < BarsAmount; i++)
{
avg = iMAOnArray(bbMacd, 0, Length, 0, MODE_EMA, i);
sDev = iStdDevOnArray(bbMacd, 0, Length, 0, MODE_EMA, i);
ExtMapBuffer3[i] = avg + (StDv * sDev); // Upperband
ExtMapBuffer4[i] = avg - (StDv * sDev); // Lowerband
//----
if(bbMacd[i] > bbMacd[i+1])
{
ExtMapBuffer1[i]=bbMacd[i]; // Uptrend bbMacd
ExtMapBuffer2[i] = EMPTY_VALUE;
}
//----
if(bbMacd[i] < bbMacd[i+1])
{
ExtMapBuffer2[i]=bbMacd[i]; // Downtrend bbMacd
ExtMapBuffer1[i] = EMPTY_VALUE;
}
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments