BB_MACD3
Download scriptThis script is a custom financial analysis tool designed to combine two well-known trading methodologies: the MACD (Moving Average Convergence Divergence) and Bollinger Bands. By merging these indicators, the tool aims to provide a clearer picture of market momentum and volatility simultaneously.
Core Logic Overview
The logic operates in three primary stages to process market data into visual signals:
1. Momentum Calculation (The Foundation)
The script starts by calculating a standard MACD value. It does this by measuring the distance between two different "Exponential Moving Averages" (EMAs)—one that reacts quickly to price changes and one that reacts more slowly. The difference between these two averages tells the script whether the market's current momentum is accelerating upward or pulling downward.
2. Volatility Mapping (The Bands)
Once the momentum values are established, the script applies Bollinger Band logic to those specific values. Instead of applying bands to the price itself, it creates "bands" around the MACD momentum line.
- Average Line: It calculates a smoothed average of the momentum.
- Standard Deviation: It measures how much the momentum typically fluctuates (volatility).
- Upper and Lower Bands: It projects boundaries above and below the momentum average based on a user-defined multiplier. This helps identify when momentum is reaching an extreme or "overextended" state.
3. Trend Directional Signaling
Finally, the script interprets the movement of the momentum line to provide visual cues for trend direction:
- Upward Momentum: If the current momentum value is higher than the previous one, the tool marks this as an upward trend.
- Downward Momentum: If the current momentum value is lower than the previous one, it identifies a downward trend.
Visual Representation
The script translates these calculations into a separate window on your chart for easy viewing:
- Lines: It draws the Upper and Lower boundaries as solid lines, allowing you to see the "range" of the current momentum.
- Arrows/Markers: It uses distinct markers to highlight whether the momentum is currently trending up (Lime) or down (Magenta), helping you distinguish between the direction of price movement at a glance.
Summary for the User
Essentially, this tool allows you to see not just if the price is moving, but how strongly it is moving and whether that movement is becoming extreme. It filters the "noise" of price action by focusing on the underlying momentum, providing a more refined view of market trends.
//+------------------------------------------------------------------+
//| 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 = 10;
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
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
IndicatorShortName("BB MACD("+FastLen+","+SlowLen+")["+Length+"]");
//----
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