linreg

This script is designed as a visual market analysis tool that helps traders identify trends by calculating the mathematical "line of best fit" for price action. Instead of simply following the current price, it uses a regression logic to smooth out market noise and determine the underlying direction of the movement.

Core Logic and Functionality

1. Linear Regression Calculation The primary engine of this script is a linear regression model. It looks at a specific set of recent historical candles (defined by the user-set period) and calculates a mathematical trend line that minimizes the distance between that line and the actual price points. By doing this, it creates a "smoothed" path that represents where the price "should" be based on its recent momentum.

2. Dynamic Trend Tracking Rather than showing just one static line, the script continuously recalculates this regression as new price data enters the chart. It identifies:

  • The Slope: How steeply the price is moving up or down.
  • The Intercept: The baseline position of the price.
  • The Resultant Value: A calculated coordinate that represents the current trend's trajectory.

3. Visual Trend Identification (Color Coding) The script translates these complex mathematical calculations into a clear, visual format on your chart using a multi-colored line system:

  • Directional Shifts: The tool compares the current calculated trend value against the previous one.
  • Color Logic: By detecting whether the regression value is increasing or decreasing compared to the previous candle, the script dynamically changes which color "activates" on the chart.
  • Visual Clarity: It effectively hides colors that do not represent the current trend direction, allowing the trader to see a continuous, clean line that shifts colors to signal whether the momentum is currently bullish (upward) or bearish (downward).

Summary for the User

Think of this tool as a "noise filter." While standard price charts can be jagged and jumpy, this script looks at the recent history to draw a smooth mathematical path. It then paints that path in different colors to give you an immediate visual cue of whether the current market momentum is trending up or down, helping you stay on the right side of the market movement.

Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
linreg
//+------------------------------------------------------------------+
//|                                                        |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright © 2005, FX Sniper "
#property  link      "http://www.metaquotes.net/"

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 3
#property indicator_color1 Yellow      
#property indicator_color2 Green
#property indicator_color3 Red

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
int width;

extern int Rperiod = 3;
extern int Draw4HowLongg = 1500;
int Draw4HowLong;
int shift;
int i;
int loopbegin;
double sumx[];
double sumy[],sumxy[],sumxsq[],sumysq[];
int length;
double lengthvar;
double sqsumx[], sqsumy[];
double tmp ;
double wt[],reglin[], slopem[], interceptb[], refr[], unk[];
int c;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(5);
   
//---- drawing settings
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer3);
   SetIndexBuffer(3,sumx);
   SetIndexBuffer(4,wt);
   
   
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

//---- initialization done
   return(0);
  }


int start()

  {   Draw4HowLong = Bars-Rperiod - 5;
      length = Rperiod;
      loopbegin = Draw4HowLong - length - 1;
 
      for(shift = loopbegin; shift >= 0; shift--)
      { 
        sumx[1] = 0;
	     sumy[1] = 0;
	     sumxy[1] = 0;
	     sumxsq[1] = 0;
	     sumysq[1] = 0;
         for(i = length; i >= 1  ; i--)
         {
         tmp = 0;
//         tmp = ( i - lengthvar)*Close[length-i+shift];
	 tmp=(length-i+1)*Close[length-i+shift];
         sumxy[1]+=tmp;
	 sumy[1]+=Close[shift];
	 sumysq[1]+=Close[shift]*Close[shift];
 	 sumxsq[1]+=i*i;
 	 sumx[1]+=i;


         }
	 sqsumx[shift]=sumx[1]*sumx[1];
 	 sqsumy[shift]=sumy[1]*sumy[1];
	 slopem[shift]=(length*sumxy[1]-sumx[1]*sumy[1])/(length*sumxsq[1]-sqsumx[shift]);
	 interceptb[shift]=(sumy[1]-slopem[shift]*sumx[1])/length;
//	 refr[shift]=(length*sumxy[1]-sumx[1]*sumy[1])/sqrt((length*sumxsq[1]-sqsumx[shift])*(length*sumysq[1]-sqsumy[shift]));
	 reglin[shift]=length*slopem[shift]+interceptb[shift];
	 wt[shift] = reglin[shift];
	
         
//========== COLOR CODING ===========================================               
        
       ExtMapBuffer3[shift] = wt[shift]; //red 
       ExtMapBuffer2[shift] = wt[shift]; //green
       ExtMapBuffer1[shift] = wt[shift]; //yellow
       
       //  for(c=loopbegin;c==shift;c++)
       // {
        if (wt[shift+1] > wt[shift])
        {
        ExtMapBuffer2[shift+1] = EMPTY_VALUE;
  // ObjectCreate("smiley_face", OBJ_ARROW, 0, Time[shift], Low[shift]-Point*20);
  // Print("time=  ",Time[shift]);
  // ObjectSet("smiley_face", OBJPROP_ARROWCODE, 242);
  // ObjectSet("smiley_face", OBJPROP_COLOR , Red);
  // ObjectSet("smiley_face", OBJPROP_WIDTH  , 1);
  // ObjectsRedraw();

        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
       else if (wt[shift+1] < wt[shift]) 
        {
        ExtMapBuffer1[shift+1] = EMPTY_VALUE; //-1 red/greem tight
       //ExtMapBuffer3[shift+1] = EMPTY_VALUE;
        
        }
         else 
         {
         
         ExtMapBuffer1[shift+1]=CLR_NONE;//EMPTY_VALUE;
         ExtMapBuffer2[shift+1]=CLR_NONE;//EMPTY_VALUE;
         }
        
      }
    
      return(0);
  }
//+------------------------------------------------------------------+




Comments