TrendLinearReg
Download scriptThis indicator is designed to provide a visual representation of market momentum and trend direction by utilizing a mathematical method known as Linear Regression. Instead of just looking at where the price is right now, the tool analyzes a specific window of recent price history to determine the underlying "path" or slope of the market movement.
How the Logic Works
1. Data Analysis Window The tool looks back at a fixed number of previous candles (determined by the user) to calculate the trend. By examining this specific group of bars, it creates a mathematical "best-fit" line. This helps smooth out minor price fluctuations and reveals the primary direction in which the market is heading.
2. Calculating the Slope The core engine of this script focuses on the "slope" of that best-fit line. In simple terms, it measures how steeply the price is rising or falling over the selected period. The script converts this steepness into a numerical value. A positive slope indicates an upward trend, while a negative slope indicates a downward trend.
3. Trend Identification and Comparison To determine whether the market is currently gaining or losing strength, the tool compares the current calculated slope with the slope from the previous period:
- Strength Increase: If the current slope is higher than the previous one, the indicator recognizes an increase in upward momentum (or a decrease in downward momentum).
- Strength Decrease: If the current slope is lower than the previous one, it recognizes a decrease in upward momentum (or an increase in downward momentum).
4. Visual Representation The results are displayed in a separate window as a histogram:
- Lime Green Bars: These appear when the trend's momentum is increasing (the "slope" is moving upwards).
- Red Bars: These appear when the trend's momentum is decreasing (the "slope" is moving downwards).
Summary for the User
Essentially, this tool acts as a momentum tracker. It tells you not just where the price is, but how the speed and direction of that price movement are changing. By observing the switch between green and red histograms, you can visually identify shifts in market sentiment and the acceleration or deceleration of current trends.
//-------------------------------------------------------------------
// TrendLinearReg.mq4
// Copyright © 2008 Sergej Solujanov
//--------------------------------------------------------------------
#property copyright "Copyright © 2008 Sergej Solujanov"
#property link "irasol@bk.ru"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red
extern int barsToCount=34; // Ïåðèîä
extern int CountBars = 500; // Êîëè÷åñòâî áàðîâ äëÿ ïðîðèñîâêè
double buffer0[];
double buffer1[];
//--------------------------------------------------------------------
int init()
{
IndicatorShortName(StringConcatenate("TrendLR (",barsToCount,")"));
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,buffer0);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexBuffer(1,buffer1);
return(0);
}
//--------------------------------------------------------------------
int deinit()
{
return(0);
}
//--------------------------------------------------------------------
int start()
{
//
int counted_bars = IndicatorCounted();
double a, b, c, sumy, sumx, sumxy, sumx2;
double prev;
double current;
if(Bars <= CountBars + barsToCount+1) return(0);
int limit=Bars-counted_bars-1;
if (limit>CountBars) limit=CountBars;
for(int shift = limit; shift >= 0; shift--)
{
sumy=0.0; sumx=0.0; sumxy=0.0; sumx2=0.0;
for(int i=0; i<barsToCount; i++)
{
sumy+=Close[i+shift];
sumxy+=Close[i+shift]*i;
sumx+=i;
sumx2+=i*i;
}
c=sumx2*barsToCount-sumx*sumx;
if (c==0) c=0.1;
b=(sumxy*barsToCount-sumx*sumy)/c;
// a=(sumy-sumx*b)/barsToCount;
current=-1000*b;
prev=current;
if ((buffer0[shift+1] !=EMPTY_VALUE) || (buffer1[shift+1] !=EMPTY_VALUE))
if (buffer1[shift+1] !=EMPTY_VALUE) prev=buffer1[shift+1];
else prev=buffer0[shift+1];
if (current>=prev)
{
buffer0[shift]= current;
buffer1[shift]= EMPTY_VALUE;
}
else
if(current<prev)
{
buffer1[shift]= current;
buffer0[shift]= EMPTY_VALUE;
}
}
return(0);
}
Comments