BB_SqueezeLRSm_4c
Download scriptThis tool is designed to help traders identify market momentum and recognize specific "squeezed" conditions where volatility might be about to break out. It operates by analyzing the relationship between price trends, historical averages, and current market fluctuations.
Core Logic Overview
The script functions based on three primary pillars of analysis:
1. Trend Direction and Strength (Linear Regression) Instead of just looking at where the price is right now, the tool calculates a "Linear Regression Slope." Think of this as drawing a straight trend line through a specific period of recent price data to see the overall direction.
- Positive Slope: Indicates an upward trend.
- Negative Slope: Indicates a downward trend. The script distinguishes between these trends and marks them visually, allowing you to see if the current momentum is accelerating or slowing down compared to the previous period.
2. Volatility Comparison (The "Squeeze" Mechanic) The tool looks for a condition where market activity becomes unusually tight. It does this by comparing two different types of measurements:
- Average True Range (ATR): This measures how much the price is moving on average over a set timeframe.
- Standard Deviation: This measures how much the price is deviating from its recent average.
By dividing the standard deviation by the average range, the tool calculates a "Squeeze" value. When this value drops below a specific threshold, it indicates that the market is becoming compressed—similar to a spring being pushed down. The logic assumes that when volatility "squeezes," a significant move in price is likely to follow.
3. Visual Signaling The tool translates these complex calculations into clear visual cues on your chart:
- Trend Histograms: These provide a continuous look at the current slope of the market, helping you stay aligned with the dominant direction.
- Alert Signals: The script identifies specific moments where the "Squeeze" condition is met and places an arrow or marker on the chart. This acts as a warning for traders that the market may be preparing for a breakout, providing a potential entry point or a signal to watch for high-momentum movement.
Summary for the User
Essentially, this script filters out the "noise" of daily price fluctuations to show you two things: which way the market is leaning (Trend) and when the market is getting "too quiet" before a potential big move (Squeeze). It helps traders stay on the right side of the trend while identifying high-probability zones for volatility expansion.
//+------------------------------------------------------------------+
//|BB_SqueezeLRSm_4c basedon ml code bbsqueeze.mq4 |
//|2007fxtsd ki Copyright © 2005, Nick Bilak, beluck[AT]gmail.com |
//| enhanced a little bit by CJ Rivas, carlos[AT]vealo.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Nick Bilak" //
#property link "http://metatrader.50webs.com/"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 DarkGreen
#property indicator_color4 Maroon
#property indicator_color5 Yellow
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
//---- input parameters
extern int LinRegrPeriod = 13;
extern int Price =6;
extern int bolPrd = 20;
extern double bolDev = 2.0;
extern int keltPrd = 20;
extern double keltFactor= 1.5;
extern int MaxBarsToCount = 1500;
extern bool ValuesDisplayOff = true;
extern string note_Price = "COHLMTW: C0,O1,H2,L3,Md4,Tp5,6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
//---- buffers
double upB[];
double upB2[];
double loB[];
double loB2[];
double upK[];
double SumSqrBars;
double SumBars;
double Num2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY);
SetIndexBuffer(0,upB);
SetIndexEmptyValue(0,EMPTY_VALUE);
SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY);
SetIndexBuffer(1,loB);
SetIndexEmptyValue(1,EMPTY_VALUE);
SetIndexStyle(4,DRAW_ARROW,EMPTY);
SetIndexBuffer(4,upK);
SetIndexEmptyValue(4,EMPTY_VALUE);
SetIndexArrow(4,159);
SetIndexStyle(2,DRAW_HISTOGRAM,EMPTY);
SetIndexEmptyValue(2,EMPTY_VALUE);
SetIndexBuffer(2,upB2);
SetIndexStyle(3,DRAW_HISTOGRAM,EMPTY);
SetIndexEmptyValue(3,EMPTY_VALUE);
SetIndexBuffer(3,loB2);
SetIndexLabel(0,"upB");
SetIndexLabel(1,"loB");
SetIndexLabel(2,"upB2");
SetIndexLabel(3,"loB2");
SetIndexLabel(4,"upK");
if (ValuesDisplayOff)
{
SetIndexLabel(0,"");
SetIndexLabel(1,"");
SetIndexLabel(2,"");
SetIndexLabel(3,"");
SetIndexLabel(4,"");
}
IndicatorShortName("BB_Sqz ("+LinRegrPeriod+") "+Price+" |");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int shift, limit=IndicatorCounted();
double diff,d,dPrev, std,bbs;
if (limit<0) return(-1);
if (limit>0) limit--;
limit=MathMin(Bars-limit,MaxBarsToCount);
for (shift=limit;shift>=0;shift--)
{
SumBars = LinRegrPeriod * (LinRegrPeriod-1) * 0.5;
SumSqrBars = LinRegrPeriod* (LinRegrPeriod-1) * (2 * LinRegrPeriod - 1)/6;
Num2 = MathPow(SumBars,2) - LinRegrPeriod * SumSqrBars;
d=LinearRegressionSlope(LinRegrPeriod,shift);
dPrev=LinearRegressionSlope(LinRegrPeriod,shift+1);
if(d>0) {
if ((dPrev>0) && (dPrev > d))
{ upB2[shift]=d; upB[shift] = 0; }
else { upB[shift]= d; upB2[shift] = 0; }
loB[shift]=0;
loB2[shift]=0;
} else {
if ((dPrev<0) && (dPrev < d))
{ loB2[shift]=d; loB[shift] = 0; }
else { loB[shift]= d; loB2[shift] = 0; }
upB[shift]=0;
upB2[shift]=0;
}
diff = iATR(NULL,0,keltPrd,shift)*keltFactor;
std = iStdDev(NULL,0,bolPrd,MODE_SMA,0,PRICE_CLOSE,shift);
bbs = bolDev * std / diff;
if(bbs<1) {
upK[shift]=0;
} else {
upK[shift]=EMPTY_VALUE;
}
}
return(0);
}
//+------------------------------------------------------------------+
double LinearRegressionSlope(int Len,int shift)
{
double LinearRegSlope;
double SumY = 0;
double Sum1 = 0;
double Num1;
int i;
for (i=0; i<Len; i++) {
Sum1 += i*iMA(NULL,0,1,0,0,Price,i+shift);
SumY += iMA(NULL,0,1,0,0,Price,i+shift);
}
Num1 = Len * Sum1 - SumBars * SumY;
if( Num2 != 0 )
LinearRegSlope = 100*Num1/Num2;
else LinearRegSlope = 0;
return (LinearRegSlope);
}
Comments