KVNLinearRegression
Download scriptThis script is designed to work as a visual tool on your trading chart, providing a mathematical "best-fit" line based on historical price movements. Instead of simply following the most recent price like a standard moving average, this logic attempts to identify the underlying trend by calculating a linear regression.
How it Works
The core objective of this tool is to determine the general direction of the market over a specific period. It looks at a set number of previous price points (defined by the user as a lookback period) and performs a statistical calculation to find a straight line that minimizes the distance between itself and all those data points.
The Mathematical Logic
To achieve this, the script follows these logical steps:
- Data Collection: It gathers the closing prices for the specified number of previous periods.
- Trend Calculation (The Slope): It calculates a value representing the "angle" or steepness of the trend. This tells the tool how fast the price is moving up or down over time.
- Positioning (The Offset): It then determines the starting point or vertical position of the line to ensure it sits correctly in the middle of the historical data.
- Projection: Finally, it combines these two pieces of information to plot a predicted "average" price point for the current moment.
What it Shows You
On your chart, this results in a blue line that represents a smoothed-out version of the market's trajectory. Because it uses regression logic, the line is less "choppy" than traditional indicators; it doesn't react instantly to every tiny price flicker. Instead, it provides a clearer picture of the overarching trend, helping you see where the price should be heading based on its recent historical behavior.
//+------------------------------------------------------------------+
//| KVNLinearRegression.mq4 |
//| Copyright © 2008, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "KVN"
#property link "http://www.ktu.aknet.kg"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- input parameters
extern int nn=21;
//---- buffers
double LR1[];
int n,n1=1,n2;
double ssm1,ssm2,ssm3,ssm4,a,b,LR;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,LR1);
//----
return(0);
}
//+------------------------------------------------------------------+
//| ïîëüçîâàòåëüñêèé èíäèêàòîð
//Èíäèêàòîð ñòðîèòñÿ ïî ôîðìóëå:LR = at+b
//ãäå LR - ïðîãíîçèðóåìàÿ "ñðåäíÿÿ" öåíà çàêðûòèÿ,
//t - ìîìåíò âðåìåíè,Pt - öåíû çàêðûòèÿ çà n ïîñëåäíèõ ïåðèîäîâ.
//a = (n*ÑÓÌÌÀ (t*Pt) - ÑÓÌÌÀ(t)*CÓÌÌÀ(Pt))/(n*ÑÓÌÌÀ(t^2) - (ÑÓÌÌÀ(t))^2) - òàíãåíñ óãëà íàêëîíà ëèíèè ðåãðåññèè,
//b = 1/n*(ÑÓÌÌÀ(Pt) - a*ÑÓÌÌÀ(t)), - ñìåùåíèå ïî ãîðèçîíòàëè}
//+------------------------------------------------------------------+
int start()
{
n=0;
while(n<100){
ssm1=0; ssm2=0; ssm3=0; ssm4=0; n1=1;
while(n1<=nn) {
n2=n+n1-1;
ssm1=ssm1+n1*Close[n2]; ssm2=ssm2+n1;
ssm3=ssm3+Close[n2]; ssm4=ssm4+n1*n1; n1++;
}
a=(nn*ssm1-ssm2*ssm3)/(nn*ssm4-ssm2*ssm2);
b=(1.0/nn)*(ssm3-a*ssm2);
LR=a*nn+b; LR1[n]=LR; n++;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments