Error: calculating opposite slope direction for Linear Regression | Using vectors with MQL functions

 

Hi Happy Weekend to everyone

I am trying to calculate Slope of Linear Regression line with vectors. It is calculated but with a opposite direction signal i.e. positive slope is returned as negative slope.

I checked the code and it seems vector.CorrCoef() is causing the issue. e.g if prices are making 'Lower Highs' on recent bars, there is positive correlation between bar number and prices. But the actual slope is negative.

Can you please help me how to rectify this anomaly. Simplest one is, I can put opposite sign before slope, but that is not the correct way of doing it. As it may cause problem if I use the same method for calculating LR between two symbols where bars are not involved.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {

                vector zzHighIdx         = {107,48,34};
                vector zzHighPrice = {2528.67,2526.81,2524.82}; // Price(s) making LHs on recent bars
                vector zzHighProj        = zzHighPrice.LinearRegression();
                double r2                                = zzHighProj.RegressionMetric(zzHighPrice,REGRESSION_R2);
                // 2024.08.28 01:15:00   CLinRegression::simpleLinRegress: CorrCoff[+0.93540] lrSlope[+0.04649] r2[+0.9996]

/*
                vector zzHighIdx         = {157,133,107};
                vector zzHighPrice = {2521.25,2524.33,2528.67}; // Price(s) making HHs on recent bars
                ProjectY = zzHighPrice.LinearRegression();
                r2                       = ProjectY.RegressionMetric(zzHighPrice,REGRESSION_R2);
                // 2024.08.28 01:15:00   CLinRegression::simpleLinRegress: CorrCoff[-0.99722] lrSlope[-0.14866] r2[+0.9905]
*/
                double sdX = zzHighIdx.Std();
                double sdY = zzHighPrice.Std();
                double CorrCoef = zzHighIdx.CorrCoef(zzHighPrice);
                double lrSlope  = zzHighPrice.Std() / zzHighIdx.Std() * zzHighIdx.CorrCoef(zzHighPrice);
                PrintFormat("%s: sdX[%+.3f] sdY[%.2f] CorrCoff[%+.5f] lrSlope[%+.5f] r2[%+6.4f]",__FUNCTION__,sdX,sdY,CorrCoef,lrSlope,r2);

}
//+------------------------------------------------------------------+

Thanks in advance.

 
Anil Varma:

Hi Happy Weekend to everyone

I am trying to calculate Slope of Linear Regression line with vectors. It is calculated but with a opposite direction signal i.e. positive slope is returned as negative slope.

I checked the code and it seems vector.CorrCoef() is causing the issue. e.g if prices are making 'Lower Highs' on recent bars, there is positive correlation between bar number and prices. But the actual slope is negative.

Can you please help me how to rectify this anomaly. Simplest one is, I can put opposite sign before slope, but that is not the correct way of doing it. As it may cause problem if I use the same method for calculating LR between two symbols where bars are not involved.

Thanks in advance.

Hey Anil, there are many ways of finding linear regression slope. But before going there I want us to agree on terminology:

In the linear formula "y = mx + c" m is the slope. It's also the coefficient of x. And c is the intercept/bias. In high school, the formula above assumes you only have 1 input. However m and x may also be vectors of size n.

So when you say that you're trying to find the slope direction,  I'm assuming you're working with a simple model that only has 1 input, and you'd like to calculate the coefficient value for that input.

I've written articles showing how you can calculate Linear Regression Coefficients in one line of MQL5 code. 

I'll summarize the article for you here:

1) Create a matrix that contains the price data you are interested in.

2) Add a column of 1's to the matrix created above. (These ones represent c, the intercept)

3) Create a vector of the target future price you're trying to predict.

4) Create a vector that has the value 1 for every column in your input matrix

All of the above steps were only needed to get your data ready, to calculate the coefficient value:

5) Use the MatMul operation to multiply your target matrix with the PseudoInverse of your input matrix and store the answer in the vector created in step 4. 
 
Gamuchirai Zororo Ndawana #:

Thanks for reply Ndawana

I definitely wish to go for AI & ML in future, as I keep improving my MQL skills. I am from a non mathematical background, so these things taken longer than average time a mathematician will take to understand :)

The problem I am trying to resolve now is a simpler one. I have three High prices on ascending or descending bars. I want line of best fit (LR) on these three highs and their slope. This slope I want to use for calculating triangles. e.g. with condition if(MathAbs(slope) <= N) than I conclude it is almost flat upper slope and slope of three lows >= M than, I have ascending tringle.

I have gone through your article LDA, are you referring to the same? In not please share the link. I have read another articles by @Omega J Msigwa on linear regression. They all are good, but I was trying to experiment with vectors + MQL functions instead of using mathematical calculations.