LSMA_Line_v2
Download scriptThis script is a technical analysis tool designed to automatically project a trend corridor onto your trading chart. It uses mathematical regression to determine the underlying direction of price movement over a specific period, creating a visual "channel" that helps traders identify the prevailing momentum of the market.
How it Works
1. Data Selection and Period Tracking The script begins by looking at a fixed window of historical data (defined by the user as the "Regression Period"). Within this timeframe, it monitors specific price points—such as where the candles closed, their highest points, or their lowest points—depending on your preference.
2. Calculating the Core Trend (The Median Line) Instead of simply connecting dots, the script uses a regression calculation to find the "line of best fit." It mathematically analyzes every price point in the chosen period to determine the overall trajectory. This results in a central trend line that represents the equilibrium of the current price movement.
3. Establishing the Volatility Boundaries (The Channel) To give you a clearer picture of the market's range, the script doesn't just show a single line; it calculates the "Standard Deviation." This is a measure of how much the price typically fluctuates away from the central trend line.
Using this deviation, the script generates two outer boundaries:
- The Upper Boundary: Projects the upper limit of normal price movement.
- The Lower Boundary: Projects the lower limit of normal price movement.
4. Visual Output The final result is a three-line channel drawn directly on your chart. The center line shows you where the "average" trend is heading, while the upper and lower lines create a corridor. This allows you to see at a glance whether the current price is behaving normally within the established trend or if it is pushing toward an extreme boundary, potentially signaling a reversal or a breakout.
Key Features
- Dynamic Updating: The script recalculates these positions automatically as new price data arrives, ensuring the channel stays relevant to recent market activity.
- Customizable Sensitivity: You can adjust the length of the history analyzed and the width of the corridor (the standard deviation) to suit different trading styles, ranging from wide, stable channels to tight, responsive ones.
- Multi-Line Clarity: The tool organizes these lines clearly on your screen, providing a clean visual aid for identifying support and resistance levels within a trending market.
//+------------------------------------------------------------------+
//| LSMA_Line.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
//---- indicator parameters
extern int IdNum =1;
extern int RPeriod=28;
extern color LineColor=Red;
extern int LineWeight =1;
extern int PriceVal=0; // 0 = Close, 1 = Low, 2 = High
extern double StDev=1.618;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ObjectCreate("Reg_line"+IdNum, OBJ_TREND, 0, 0,0, 0,0);
ObjectSet("Reg_line"+IdNum, OBJPROP_COLOR, LineColor);
ObjectSet("Reg_line"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Reg_line"+IdNum, OBJPROP_WIDTH, LineWeight);
ObjectCreate("Reg_upper" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
ObjectSet("Reg_upper"+IdNum, OBJPROP_COLOR, LineColor);
ObjectSet("Reg_upper"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Reg_upper"+IdNum, OBJPROP_WIDTH, LineWeight);
ObjectCreate("Reg_lower" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
ObjectSet("Reg_lower"+IdNum, OBJPROP_COLOR, LineColor);
ObjectSet("Reg_lower"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("Reg_lower"+IdNum, OBJPROP_WIDTH, LineWeight);
//----
Comment("Auto Regression channel");
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
void deinit()
{
ObjectDelete("Reg_line"+IdNum);
ObjectDelete("Reg_upper" +IdNum);
ObjectDelete("Reg_lower" +IdNum);
Comment("");
}
//+------------------------------------------------------------------+
//| Regression Line |
//+------------------------------------------------------------------+
int start()
{
double y1,y2, price;
double a1, a2, a3, b1, a, b;
double stddiv, tmp_div;
double x_n_up, x_1_up, x_n_down, x_1_down;
int shift, n;
//----
if(Bars<=RPeriod) return(0);
a1=0;
a2=0;
a3=0;
b1=0;
a=0;
b=0;
y1=0;
y2=0;
tmp_div=0;
n=RPeriod;
for(shift=RPeriod;shift>0;shift--)
{
switch(PriceVal)
{
case 0: price=Close[shift];
break;
case 1: price=Low[shift];
break;
case 2: price=High[shift];
break;
}
a1=a1 + shift*price;
a2=a2 + shift;
a3=a3 + price;
b1=b1 + shift*shift;
}
b=(n*a1 - a2*a3)/(n*b1 - a2*a2);
a=(a3 - b*a2)/n;
y1=a + b*n;
y2=a + b;
//----
ObjectSet("Reg_line"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
ObjectSet("Reg_line"+IdNum, OBJPROP_TIME2, Time[0]);
ObjectSet("Reg_line"+IdNum, OBJPROP_PRICE1, y1);
ObjectSet("Reg_line"+IdNum, OBJPROP_PRICE2, y2);
//----
for(shift=RPeriod; shift>0; shift--)
{
switch(PriceVal)
{
case 0: price=Close[shift];
break;
case 1: price=Low[shift];
break;
case 2: price=High[shift];
break;
}
tmp_div=tmp_div + (price - (a + b*shift))*(price - (a + b*shift));
}
stddiv=MathSqrt(tmp_div/n);
//----
x_n_up=y1 + StDev*stddiv;
x_1_up=y2 + StDev*stddiv;
x_n_down=y1 - StDev*stddiv;
x_1_down=y2 - StDev*stddiv;
//upper
ObjectSet("Reg_upper"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
ObjectSet("Reg_upper"+IdNum, OBJPROP_TIME2, Time[0]);
ObjectSet("Reg_upper"+IdNum, OBJPROP_PRICE1, x_n_up);
ObjectSet("Reg_upper"+IdNum, OBJPROP_PRICE2, x_1_up);
//lower
ObjectSet("Reg_lower"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
ObjectSet("Reg_lower"+IdNum, OBJPROP_TIME2, Time[0]);
ObjectSet("Reg_lower"+IdNum, OBJPROP_PRICE1, x_n_down);
ObjectSet("Reg_lower"+IdNum, OBJPROP_PRICE2, x_1_down);
return(0);
}
//+------------------------------------------------------------------+
Comments