!LinRegrBuf

Download script
Author: Copyright � 2008 signalsbg.com
!LinRegrBuf

The Linear Regression Channel is a technical analysis tool designed to help traders visualize price trends and identify potential areas of market reversal. By applying mathematical modeling to historical price data, this indicator provides a clear picture of where the market has been and where it might be heading next.

How it Works

The script operates by analyzing a specific number of previous candles (customizable by the user) to determine the "line of best fit" for the current price movement. This is achieved through several distinct logical steps:

1. Trend Identification (The Regression Line) The tool looks at a set window of recent price action—specifically focusing on closing prices—to calculate a linear path. It determines the general direction of the market, whether it is moving upward, downward, or sideways. This resulting "Regression Line" acts as the central axis of the trend, showing the core momentum of the asset.

2. Measuring Market Volatility (The Deviation) Once the central trend line is established, the tool measures how far the price has drifted away from that center point. It looks for the maximum "peaks" and "troughs" within your chosen timeframe to see how wide the price fluctuations have been. This measurement defines the boundaries of the market's current behavior.

3. Defining Support and Resistance (The Channel) Using the data from the first two steps, the script projects two outer boundaries parallel to the central trend line:

  • Resistance: The upper boundary where the price has historically faced difficulty breaking through.
  • Support: The lower boundary where the price has historically found a "floor" and bounced back up.

Key Features & Customization

  • Adjustable Lookback Period: Users can define exactly how many past bars the tool should analyze. A larger window provides a broader, more stable trend view, while a smaller window makes the tool more responsive to immediate price changes.
  • Price Source Selection: The logic allows for flexibility in what data it tracks. You can choose to base the calculations on "Closing Prices" (for a smoother trend analysis) or "High/Low Prices" (to capture the full range of market volatility).
  • Visual Guidance: By plotting these three lines—the center trend, the upper resistance, and the lower support—the tool creates a visual "tunnel." Traders can use this to identify overextended price movements; when the price touches the outer edges of the channel, it may indicate that the market is reaching an extreme and could be due for a correction or reversal.
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
15 Views
0 Downloads
0 Favorites
!LinRegrBuf
//+------------------------------------------------------------------+
//|                                      LinearRegressionChannel.mq4 |
//|                                                           dimicr |
//|                                         http://www.signalsbg.com |
//|      SignalsBG.com - development of trading tools for Metatrader |
//////////////////////////////////////////////////////////////////////
#property copyright "Copyright © 2008 signalsbg.com"
#property link      "http://www.signalsbg.com"
#property indicator_chart_window

#property indicator_buffers 3
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_color1 Gold
#property indicator_color2 Aqua
#property indicator_color3 OrangeRed

extern bool UseClose = true;
extern int  barsToCount=50;

double LR_line[];
double Sup_line[];
double Res_line[];

//////////////////////////////////////////////////////////////////////
int init()
{
   //IndicatorShortName("LinearRegressionChannel:"+barsToCount);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,LR_line);
   SetIndexEmptyValue(0,0.0);
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Sup_line);
   SetIndexEmptyValue(1,0.0);
   
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,Res_line);
   SetIndexEmptyValue(2,0.0);
   
   
   return(0);
}

//////////////////////////////////////////////////////////////////////
int deinit()
{  
   return(0);
}

//////////////////////////////////////////////////////////////////////
int start()
{
   // variables
   double a,b,c,
          sumy=0.0,
          sumx=0.0,
          sumxy=0.0,
          sumx2=0.0,
          h=0.0,l=0.0;   
   int x;
   
   // calculate linear regression
   
   for(int i=0; i<barsToCount; i++)
   {
      sumy+=Close[i];
      sumxy+=Close[i]*i;
      sumx+=i;
      sumx2+=i*i;
   }
   
   c=sumx2*barsToCount-sumx*sumx;
   
   if(c==0.0)
   {
      Alert("Error in linear regression!");
      return;
   }
   
   
   // Line equation    
   b=(sumxy*barsToCount-sumx*sumy)/c;
   a=(sumy-sumx*b)/barsToCount;
   
   // Linear regression line in buffer
   for(x=0;x<barsToCount;x++)
      LR_line[x]=a+b*x;
   
   

   
   // Use PRICE_CLOSE for support-resistance
   if (UseClose)
     for(x=0;x<barsToCount;x++)
     {
       if(Close[x]-LR_line[x] > h) h = Close[x]-LR_line[x];
       if(LR_line[x] - Close[x]> l) l = LR_line[x] - Close[x];
     }  
   
   // Use HIGH - LOW
   else
     for(x=0;x<barsToCount;x++)
     {
       if(High[x]-LR_line[x] > h) h = High[x]-LR_line[x];
       if(LR_line[x] - Low[x]> l) l = LR_line[x] - Low[x];
     }
   
   // Drawing support - resistance lines   
   if (h>l)
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-h+b*x;
       Res_line[x]=a+h+b*x;
     } 
   }
   else
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-l+b*x;
       Res_line[x]=a+l+b*x;
     }
   }
   
   LR_line[x]  = 0.0;
   Sup_line[x] = 0.0;
   Res_line[x] = 0.0;

   return(0);
}

Comments