Bollinger %b
Download scriptThis tool is designed to provide traders with a visual representation of where the current price sits in relation to standard market volatility boundaries. By utilizing the principles of Bollinger Bands—a popular technical analysis framework—this script calculates a specific numerical value that identifies how far the price has stretched away from its recent average.
Core Logic and Functionality
The logic operates by measuring the distance between two key points:
- The Current Market Price: The closing price of the asset at any given moment.
- The Volatility Envelope: The space defined by the upper and lower limits of the Bollinger Bands (determined by a set period of time and a standard deviation).
Instead of simply showing you lines on a chart, this script converts that spatial relationship into a relative percentage or "position" value. It calculates where the price is currently located within the range between the high and low volatility boundaries.
How it Works for the User
- Contextual Positioning: The script looks at the historical data over a user-defined number of periods (the "BBPeriod"). It then determines the volatility of that period to establish a boundary.
- Relative Measurement: It calculates the current price's position relative to those boundaries. This allows you to see, at a glance, if the price is hugging the top of its range, sitting in the middle, or dropping toward the bottom.
- Visual Output: The result is displayed as a continuous line in a separate window on your trading platform. This allows for a cleaner analysis of "overbought" or "oversold" conditions without cluttering the main price chart.
Key Objectives
The primary goal of this logic is to help traders identify extremes in market momentum. By observing where the line sits relative to its own history, users can better understand if the current price movement is becoming stretched or if it is returning to a more "normal" equilibrium. It serves as a mathematical bridge between standard volatility indicators and actionable price positioning.
//+-------------------------------------------------------------------+
//| Bollinger Bands %b.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//| Modified by Alejandro Galindo |
//| |
//| You are free to use it |
//| |
//| If you want and if this work/modification is helpful to you |
//| you can send me a PayPal donation to ag@elcactus.com |
//+-------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int BBPeriod=20;
extern int StdDeviation=2;
//---- buffers
double BLGBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,BLGBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="BBpB("+BBPeriod+","+StdDeviation+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,BBPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Momentum |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=BBPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=BBPeriod;i++) BLGBuffer[Bars-i]=0.0;
//----
i=Bars-BBPeriod-1;
if(counted_bars>=BBPeriod) i=Bars-counted_bars-1;
while(i>=0)
{
BLGBuffer[i]=(Close[i]-iBands(NULL,0,BBPeriod,StdDeviation,0,0,MODE_HIGH,i))/(iBands(NULL,0,BBPeriod,StdDeviation,0,0,MODE_LOW,i)-iBands(NULL,0,BBPeriod,StdDeviation,0,0,MODE_HIGH,i));
i--;
}
return(0);
}
//+------------------------------------------------------------------+
Comments