Bollinger MA Price
Download scriptThis custom indicator for the MetaTrader platform is designed to provide traders with a visual representation of market volatility and price trends by combining two classic technical analysis tools: Moving Averages and Bollinger Bands.
Core Functionality
The primary purpose of this script is to plot three distinct lines on your trading chart. These lines help identify where the current market price sits in relation to its recent average behavior, allowing for a clearer understanding of "normal" price ranges versus extreme price movements.
How the Logic Works
The indicator operates through a three-step calculation process:
1. Establishing the Baseline (Moving Average) First, the script calculates a central moving average line. You can customize how this average is calculated by choosing between different smoothing methods (such as Simple, Exponential, Smoothed, or Linear Weighted). It also allows you to choose which price data point to prioritize—whether it be the closing price, opening price, highs, lows, or a variety of other mathematical averages of the candle. This line acts as the "equilibrium" for the current market trend.
2. Measuring Volatility (Standard Deviation) Once the baseline is established, the script looks backward over a specific period (the number of candles you define) to measure how much the price has fluctuated away from that baseline. It calculates the difference between the actual price at each point and the moving average, squares those differences to ensure they are treated as positive values, and then finds the average of those squares.
3. Creating the Boundary Lines (Bollinger Bands) Finally, the script applies a "deviation" multiplier to that volatility measurement. It adds this value to the moving average to create an Upper Band and subtracts it to create a Lower Band.
What This Means for the Trader
- The Center Line: Represents the smoothed trend of the market.
- The Outer Bands: Create a "channel" or "tunnel." When the price stays within these bands, it is considered to be moving within a standard range of volatility.
- Breakouts: When the price pushes outside of these upper or lower bands, it indicates that the market is experiencing a period of unusual activity or high volatility, which can often signal a significant trend shift or an overextended move.
Customization Options
Users can fine-tune the indicator to suit different trading styles by adjusting:
- Period: How many past candles should be used for the calculation (larger periods result in smoother lines).
- Deviations: How "wide" the outer bands should be (higher numbers create a wider channel).
- MA Method & Applied Price: The specific mathematical rules used to determine the center of the trend.
//+------------------------------------------------------------------+
//| Bollinger MA Price.mq4 |
//| Paladin80 |
//| forevex@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Paladin80"
#property link "forevex@mail.ru"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
//---- indicator parameters
extern int BandsPeriod=20;
extern int BandsShift=0;
extern double BandsDeviations=2.0;
extern int MA_method=0;
extern int Applied_price=0;
bool error=false;
/* MA_method: 0 - Simple moving average,
1 - Exponential moving average,
2 - Smoothed moving average,
3 - Linear weighted moving average.
Applied_price: 0 - Close price,
1 - Open price,
2 - High price,
3 - Low price,
4 - Median price,
5 - Typical price,
6 - Weighted close price, */
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MovingBuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,UpperBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,LowerBuffer);
//----
SetIndexDrawBegin(0,BandsPeriod+BandsShift);
SetIndexDrawBegin(1,BandsPeriod+BandsShift);
SetIndexDrawBegin(2,BandsPeriod+BandsShift);
//----
if (MA_method<0 || MA_method>3)
{ error=true;
Alert("Please select correct MA_method (0-3) for indicator Bollinger MA Price");
}
if (Applied_price<0 || Applied_price>6)
{ error=true;
Alert("Please select correct Applied_price (0-6) for indicator Bollinger MA Price");
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int start()
{
//----
if (error==true) return(0);
//----
int i,k,counted_bars=IndicatorCounted();
double deviation;
double sum,oldval,newres;
//----
if(Bars<=BandsPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=BandsPeriod;i++)
{
MovingBuffer[Bars-i]=EMPTY_VALUE;
UpperBuffer[Bars-i]=EMPTY_VALUE;
LowerBuffer[Bars-i]=EMPTY_VALUE;
}
//----
int limit=Bars-counted_bars;
if(counted_bars>0) limit++;
for(i=0; i<limit; i++)
MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MA_method,Applied_price,i);
//----
i=Bars-BandsPeriod+1;
if(counted_bars>BandsPeriod-1) i=Bars-counted_bars-1;
while(i>=0)
{
sum=0.0;
k=i+BandsPeriod-1;
oldval=MovingBuffer[i];
while(k>=i)
{
//----
switch (Applied_price)
{
case 0: newres=Close[k]-oldval; break;
case 1: newres=Open[k]-oldval; break;
case 2: newres=High[k]-oldval; break;
case 3: newres=Low[k]-oldval; break;
case 4: newres=(High[k]+Low[k])/2-oldval; break;
case 5: newres=(High[k]+Low[k]+Close[k])/3-oldval; break;
case 6: newres=(High[k]+Low[k]+Close[k]+Close[k])/4-oldval; break;
}
//----
sum+=newres*newres;
k--;
}
deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
UpperBuffer[i]=oldval+deviation;
LowerBuffer[i]=oldval-deviation;
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments