BandsLSMA_v1
Download scriptThis technical tool is designed to help traders visualize price trends and volatility by creating a dynamic "channel" on their trading charts. It combines two distinct mathematical concepts—Linear Regression and Standard Deviation—to provide a clearer picture of where the price is currently heading versus its historical average.
Core Logic Overview
The indicator functions by calculating three distinct lines that appear on your chart: a central moving average and two outer boundaries (upper and lower bands). Here is the step-by-step logic used to generate these visuals:
1. The Central Trend Line (Least Squares Moving Average)
Unlike a standard moving average, which simply averages past prices, this tool uses a Least Squares Moving Average (LSMA). This approach acts like a "trendline of best fit."
Instead of just looking at the average price over a set period, it calculates a linear regression. This means it tries to find the most accurate straight line that passes through the recent price data points. By doing this, the central line is more responsive to current price direction and less "laggy" than traditional indicators, allowing it to stay closer to the actual price action during fast movements.
2. Measuring Market Volatility (Standard Deviation)
Once the tool establishes the central trend line, it looks at how much the price has fluctuated around that line over the same period. It calculates the "deviation"—essentially measuring the distance between the current price and the average trend.
3. Constructing the Outer Bands
The tool then projects two outer boundaries based on the volatility calculated in the previous step:
- Upper Band: The tool adds a user-defined multiplier (the Deviation) to the central trend line. This represents the "ceiling" of normal price movement.
- Lower Band: The tool subtracts that same multiplier from the central trend line, representing the "floor."
How to Interpret the Results
The resulting visual is a channel that breathes with the market:
- Stability: When the market is calm and moving steadily, the bands will stay relatively narrow.
- Volatility: When the market becomes erratic or moves sharply in one direction, the bands will widen automatically to accommodate the increased price swings.
- Trend Identification: The central LSMA line identifies the primary direction of the trend, while the outer bands help identify "overextended" prices. If the price pushes significantly outside these bands, it often suggests that the market is stretched and may be due for a correction or a reversal toward the mean.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Bands.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#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;
//---- 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);
//----
return(0);
}
//+------------------------------------------------------------------------+
//| LSMA - Least Squares Moving Average function calculation |
//| LSMA_In_Color Indicator plots the end of the linear regression line |
//+------------------------------------------------------------------------+
double LSMA(int Rperiod, int shift)
{
int i;
double sum;
int length;
double lengthvar;
double tmp;
double wt;
length = Rperiod;
sum = 0;
for(i = length; i >= 1 ; i--)
{
lengthvar = length + 1;
lengthvar /= 3;
tmp = 0;
tmp = ( i - lengthvar)*Close[length-i+shift];
sum+=tmp;
}
wt = sum*6/(length*(length+1));
return(wt);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
int start()
{
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]=LSMA(BandsPeriod,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)
{
newres=Close[k]-oldval;
sum+=newres*newres;
k--;
}
deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
UpperBuffer[i]=oldval+deviation;
LowerBuffer[i]=oldval-deviation;
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments