LRS_line_SigL_x2_mtf
Download scriptThis script is designed to analyze market trends by calculating and displaying the mathematical "slope" of price movements. Instead of just looking at where the price is currently sitting, it looks at the rate of change over specific periods to help identify the direction and strength of a trend.
Core Logic Overview
The tool operates using Linear Regression, a statistical method used to find the best-fit straight line through a series of data points (prices). By calculating this "slope," the script determines if the market is trending upward, downward, or moving sideways with increasing/decreasing momentum.
To provide a comprehensive view of the market, the script processes two different sets of data simultaneously:
1. Dual Trend Analysis The indicator calculates two distinct regression slopes based on different lengths (for example, one looking at a shorter "fast" window and another looking at a longer "slow" window). This allows users to see both immediate momentum and the broader overarching trend at the same time.
2. Signal Smoothing Raw regression data can sometimes be "noisy" or jumpy. To make the information easier to read, the script applies a Moving Average to the calculated slopes. This creates a "Signal" line for each trend, which smooths out the fluctuations and helps highlight the most significant shifts in direction.
Multi-Timeframe Capability
One of the key features of this logic is its ability to work across different timeframes. You can configure the script to show you:
- The trend of the chart you are currently viewing.
- A separate trend from a different timeframe (e.g., watching a 4-hour trend while looking at a 15-minute chart).
The script automatically syncs these different timeframes so that they can be overlaid in a single window, allowing for a "top-down" analysis where you can see if the smaller movements align with the larger market direction.
Visual Output
The logic produces four distinct data streams displayed in a separate window:
- Slope 1: The primary trend line for the first selected period.
- Signal 1: The smoothed average of the first trend.
- Slope 2: The primary trend line for the second selected period.
- Signal 2: The smoothed average of the second trend.
By observing how these lines move relative to each other and their respective signal lines, users can interpret the market's velocity and structural direction more clearly than by looking at price action alone.
//+------------------------------------------------------------------+
//| LR trend.mq4 |
//| mladen |
//+------------------------------------------------------------------+
//mod2008fxtsd LRs sigL mtf ki based on LR trend
#property copyright "mladen"
#property link "mladenfx@gmail.com"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DarkGreen
#property indicator_color2 Red
#property indicator_color3 DodgerBlue
#property indicator_color4 Orange
#property indicator_width1 3
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 1
//
//
//
//
//
extern int Length1 = 21;
extern int SigMAPeriod1 = 7;
extern int SigMAMode1 = 0;
extern bool DrawLRS1Histo =false;
extern int Length2 = 9;
extern int SigMAPeriod2 = 5;
extern int SigMAMode2 = 0;
extern int LRS_Price = PRICE_CLOSE;
extern string TimeFrame1 = "current time frame";
extern string TimeFrame2 = "current time frame";
extern string TimeFrames = "M1-MN as in MT4 periodicity toolbar or: M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-currentTF";
extern string note_Price = "0C 1O 2H 3L 4Md 5Tp 6WghC, Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
//
//
//
//
//
double LRSBuffer1[];
double SigBuffer1[];
double LRSBuffer2[];
double SigBuffer2[];
//
//
//
//
//
int timeFrame1;
int timeFrame2;
string IndicatorFileName;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
IndicatorDigits(5);
IndicatorBuffers(4);
SetIndexStyle (0,DRAW_LINE); if(DrawLRS1Histo) SetIndexStyle (0,DRAW_HISTOGRAM);
SetIndexStyle (1,DRAW_LINE);
SetIndexStyle (2,DRAW_LINE);
SetIndexStyle (3,DRAW_LINE);
SetIndexBuffer (0,LRSBuffer1); SetIndexLabel(0,"LR slope1");
SetIndexBuffer (1,SigBuffer1); SetIndexLabel(1,"LRS Sig1");
SetIndexBuffer (2,LRSBuffer2); SetIndexLabel(2,"LR slope2");
SetIndexBuffer (3,SigBuffer2); SetIndexLabel(3,"LRS Sig2");
//
//
//
//
//
timeFrame1 = stringToTimeFrame(TimeFrame1);
timeFrame2 = stringToTimeFrame(TimeFrame2);
IndicatorShortName("LRS "+Length1+" ("+SigMAPeriod1+"); "+Length2+" ("+SigMAPeriod2+") [ "+
TimeFrameToString(timeFrame1)+" | "+TimeFrameToString(timeFrame2)+" ]");
IndicatorFileName = WindowExpertName();
return(0);
}
//
//
//
//
//
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
int counted_bars=IndicatorCounted();
int i,y1,y2,limit;
if (counted_bars<0) return(-1);
if (counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//
//
//
//
//
if (timeFrame1 != Period()||timeFrame2 != Period())
{
datetime TimeArray1[],TimeArray2[];
limit = MathMax(limit,timeFrame1/Period());
limit = MathMax(limit,timeFrame2/Period());
ArrayCopySeries(TimeArray1,MODE_TIME,NULL,timeFrame1);
ArrayCopySeries(TimeArray2,MODE_TIME,NULL,timeFrame2);
//
//
//
//
//
for(i=0, y1=0,y2=0; i<limit; i++)
{
if(Time[i]<TimeArray1[y1]) y1++;
if(Time[i]<TimeArray2[y2]) y2++;
LRSBuffer1[i] = iCustom(NULL,timeFrame1,IndicatorFileName,
Length1,SigMAPeriod1,SigMAMode1,DrawLRS1Histo,
Length2,SigMAPeriod2,SigMAMode2,LRS_Price ,0,y1);
SigBuffer1[i] = iCustom(NULL,timeFrame1,IndicatorFileName,
Length1,SigMAPeriod1,SigMAMode1,DrawLRS1Histo,
Length2,SigMAPeriod2,SigMAMode2,LRS_Price ,1,y1);
LRSBuffer2[i] = iCustom(NULL,timeFrame2,IndicatorFileName,
Length1,SigMAPeriod1,SigMAMode1,DrawLRS1Histo,
Length2,SigMAPeriod2,SigMAMode2,LRS_Price ,2,y2);
SigBuffer2[i] = iCustom(NULL,timeFrame2,IndicatorFileName,
Length1,SigMAPeriod1,SigMAMode1,DrawLRS1Histo,
Length2,SigMAPeriod2,SigMAMode2,LRS_Price ,3,y2);
}
return(0);
}
//
//
//
//
//
for (i=limit;i>=0;i--)
{
LRSBuffer1[i] = LinearRegressionSlope(Length1,i);
LRSBuffer2[i] = LinearRegressionSlope(Length2,i);
}
for(i=0; i<limit; i++)
{
SigBuffer1[i]=iMAOnArray(LRSBuffer1,0,SigMAPeriod1,0,SigMAMode1,i);
SigBuffer2[i]=iMAOnArray(LRSBuffer2,0,SigMAPeriod2,0,SigMAMode2,i);
}
//
//
//
//
//
int maxDraw =MathMax(Length1,Length2);
for(i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,maxDraw);
return(0);
}
//---------------------------------------------------------------------------------|
// |
//---------------------------------------------------------------------------------|
//
//
//
//
//
int lrs.period=EMPTY_VALUE;
double lrs.sumX;
double lrs.sumXSqr;
double lrs.divisor;
//
//
//
//
//
double LinearRegressionSlope(int len,int shift)
{
double LinearRegSlope;
double SumXY = 0;
double SumY = 0;
//
//
//
//
//
if (lrs.period != len)
{
lrs.period = len;
lrs.sumX = lrs.period * (lrs.period-1) / 2;
lrs.sumXSqr = lrs.period * (lrs.period-1) * (2 * lrs.period - 1) / 6;
lrs.divisor = MathPow(lrs.sumX,2) - lrs.period * lrs.sumXSqr;
}
//
//
//
//
//
for (int i=0; i<lrs.period; i++)
{
double price = iMA(NULL,0,1,0,MODE_EMA,LRS_Price,i+shift);
SumXY += i*price;
SumY += price;
}
if( lrs.divisor != 0 )
LinearRegSlope = (lrs.period * SumXY - lrs.sumX * SumY)/lrs.divisor;
else LinearRegSlope = 0;
return (LinearRegSlope);
}
//---------------------------------------------------------------------------------|
// |
//---------------------------------------------------------------------------------|
//
//
//
//
//
double rs.len = EMPTY_VALUE;
double rs.SumX;
double rs.SumY;
double rs.SumX2;
double rs.SumY2;
double rs.SumXY;
double rs.SumR;
//
//
//
//
//
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+
//
//
//
//
//
int stringToTimeFrame(string tfs)
{
int tf=0;
tfs = StringTrimLeft(StringTrimRight(StringUpperCase(tfs)));
if (tfs=="M1" || tfs=="1") tf=PERIOD_M1;
if (tfs=="M5" || tfs=="5") tf=PERIOD_M5;
if (tfs=="M15"|| tfs=="15") tf=PERIOD_M15;
if (tfs=="M30"|| tfs=="30") tf=PERIOD_M30;
if (tfs=="H1" || tfs=="60") tf=PERIOD_H1;
if (tfs=="H4" || tfs=="240") tf=PERIOD_H4;
if (tfs=="D1" || tfs=="1440") tf=PERIOD_D1;
if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
if (tf<Period()) tf=Period();
return(tf);
}
string TimeFrameToString(int tf)
{
string tfs="";
if (tf!=Period())
switch(tf) {
case PERIOD_M1: tfs="M1" ; break;
case PERIOD_M5: tfs="M5" ; break;
case PERIOD_M15: tfs="M15" ; break;
case PERIOD_M30: tfs="M30" ; break;
case PERIOD_H1: tfs="H1" ; break;
case PERIOD_H4: tfs="H4" ; break;
case PERIOD_D1: tfs="D1" ; break;
case PERIOD_W1: tfs="W1" ; break;
case PERIOD_MN1: tfs="MN1";
}
return(tfs);
}
//
//
//
//
//
string StringUpperCase(string str)
{
string s = str;
int lenght = StringLen(str) - 1;
int char;
while(lenght >= 0)
{
char = StringGetChar(s, lenght);
//
//
//
//
//
if((char > 96 && char < 123) || (char > 223 && char < 256))
s = StringSetChar(s, lenght, char - 32);
else
if(char > -33 && char < 0)
s = StringSetChar(s, lenght, char + 224);
lenght--;
}
//
//
//
//
//
return(s);
}
Comments