LSMA_Channel

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
LSMA_Channel

This script is designed to generate a visual Regression Channel on your trading chart. Rather than simply following the price like a standard moving average, it uses mathematical regression to identify the "line of best fit" for a specific period of historical data.

Here is a breakdown of how the logic functions:

Core Methodology

The script looks back at a user-defined number of candles (the "Period") and calculates a linear trend line that minimizes the distance between itself and the actual price points. It allows you to choose which price data to analyze—specifically the Closing price, the High, or the Low.

Visual Components

Once the central regression line is established, the script automatically generates several supporting boundaries around it, creating a "channel" effect. These are divided into three distinct zones based on statistical deviation:

  • The Mid Line: This represents the primary trend of the selected period. It serves as the baseline for the price action.
  • Inside Zone (Green): These lines are drawn very close to the center line using a small standard deviation. They represent the "core" expected path of the price.
  • Outside Zone (Brown): These lines are pushed further out from the center. They represent a wider area of normal volatility where the price is still considered within a standard expected range.
  • Outside 2 Zone (Red): These are the outermost boundaries, calculated using a much higher deviation. These act as extreme limits; when price moves beyond these lines, it indicates an unusual deviation from the established trend.

How it Works in Real-Time

  1. Data Collection: The script gathers all price data for the chosen period (e.g., the last 28 candles).
  2. Linear Calculation: It performs a mathematical calculation to find the exact slope and starting point of a straight line that best matches those candles.
  3. Deviation Measurement: It calculates the "Standard Deviation"—essentially measuring how much the price typically "wiggles" away from that center line.
  4. Dynamic Mapping: Based on those wiggles, it plots the four sets of boundary lines (Inside, Outside, and Outside 2) automatically.

Summary for Traders

This tool provides a visual map of market equilibrium. By looking at where the current price sits relative to these lines, traders can identify whether the market is behaving normally (inside the green/brown zones), stretching toward an extreme (approaching the red lines), or potentially breaking out of its current trend entirely.

6 Views
0 Downloads
0 Favorites
LSMA_Channel
//+------------------------------------------------------------------+
//|                                                    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  MidColor = Red;
extern int    LineWeight   = 1;
extern int    PriceVal = 0;         // 0 = Close, 1 = Low, 2 = High
extern double StDevOutside2 = 2.55;
extern color Outside2 = Red;
extern double StDevOutside = 1.618;
extern color Outside = Brown;
extern double StDevInside = 0.809;
extern color Inside = Green;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----
  ObjectCreate("Reg_line"+IdNum, OBJ_TREND, 0, 0,0, 0,0);
	ObjectSet("Reg_line"+IdNum, OBJPROP_COLOR, MidColor);
 	ObjectSet("Reg_line"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_line"+IdNum, OBJPROP_WIDTH, LineWeight);
  ObjectCreate("Reg_Outside_upper" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_COLOR, Outside);
 	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_WIDTH, LineWeight);
  ObjectCreate("Reg_Outside_lower" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_COLOR, Outside);
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_WIDTH, LineWeight);
  ObjectCreate("Reg_Inside_upper" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_COLOR, Inside);
 	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_WIDTH, LineWeight);
  ObjectCreate("Reg_Inside_lower" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_COLOR, Inside);
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_WIDTH, LineWeight);
  ObjectCreate("Reg_Outside2_upper" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_COLOR, Outside2);
 	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_WIDTH, LineWeight);
  ObjectCreate("Reg_Outside2_lower" +IdNum, OBJ_TREND, 0, 0,0, 0,0);
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_COLOR, Outside2);
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_STYLE, STYLE_SOLID);
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_WIDTH, LineWeight);

  Comment("Regression channel");
   return(0);
  }

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit() {
  ObjectDelete("Reg_line"+IdNum);
  ObjectDelete("Reg_Outside_upper" +IdNum);
  ObjectDelete("Reg_Outside_lower" +IdNum);
  ObjectDelete("Reg_Inside_upper" +IdNum);
  ObjectDelete("Reg_Inside_lower" +IdNum);
  ObjectDelete("Reg_Outside2_upper" +IdNum);
  ObjectDelete("Reg_Outside2_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_upOut, x_1_upOut, x_n_downOut, x_1_downOut;
   double x2_n_upOut, x2_1_upOut, x2_n_downOut, x2_1_downOut;
   double x_n_upIn, x_1_upIn, x_n_downIn, x_1_downIn;
   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_upOut = y1 + StDevOutside*stddiv;
  x_1_upOut = y2 + StDevOutside*stddiv;

  x_n_downOut = y1 - StDevOutside*stddiv;
  x_1_downOut = y2 - StDevOutside*stddiv;

  x_n_upIn = y1 + StDevInside*stddiv;
  x_1_upIn = y2 + StDevInside*stddiv;

  x_n_downIn = y1 - StDevInside*stddiv;
  x_1_downIn = y2 - StDevInside*stddiv;

  x2_n_upOut = y1 + StDevOutside2*stddiv;
  x2_1_upOut = y2 + StDevOutside2*stddiv;

  x2_n_downOut = y1 - StDevOutside2*stddiv;
  x2_1_downOut = y2 - StDevOutside2*stddiv;
  
// OUTSIDE	
  //upper
 	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
 	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_TIME2, Time[0]);
 	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_PRICE1, x_n_upOut);
 	ObjectSet("Reg_Outside_upper"+IdNum, OBJPROP_PRICE2, x_1_upOut);
  //lower
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_TIME2, Time[0]);
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_PRICE1, x_n_downOut);
 	ObjectSet("Reg_Outside_lower"+IdNum, OBJPROP_PRICE2, x_1_downOut);

// OUTSIDE2	
  //upper
 	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
 	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_TIME2, Time[0]);
 	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_PRICE1, x2_n_upOut);
 	ObjectSet("Reg_Outside2_upper"+IdNum, OBJPROP_PRICE2, x2_1_upOut);
  //lower
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_TIME2, Time[0]);
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_PRICE1, x2_n_downOut);
 	ObjectSet("Reg_Outside2_lower"+IdNum, OBJPROP_PRICE2, x2_1_downOut);

//INSIDE
  //upper
 	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
 	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_TIME2, Time[0]);
 	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_PRICE1, x_n_upIn);
 	ObjectSet("Reg_Inside_upper"+IdNum, OBJPROP_PRICE2, x_1_upIn);
  //lower
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_TIME1, Time[RPeriod]);
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_TIME2, Time[0]);
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_PRICE1, x_n_downIn);
 	ObjectSet("Reg_Inside_lower"+IdNum, OBJPROP_PRICE2, x_1_downIn);
   return(0);
  }
//+------------------------------------------------------------------+

Comments