i-Regr (linear regression) bar shift?

 
I use a linear regression indicator (i-Regr) in my EA. Specifically, I use the slope of the linear regression.
I define the "slope" like this:
slope = MathAbs(1000000*(CurrentReg - LastReg)/Period());
(where CurrentReg is the regression's value at the current bar's close price, and LastReg is the value of the previous bar's close price)

I made an expert advisor that prints a readout of slopes. The relevant statement looks like this:
Print("Slope: ",MathAbs(1000000*( iCustom(Symbol(),0,"i-Regr",1,1,34,0,0,0)-iCustom(Symbol(),0,"i-Regr",1,1,34,0,0,1))/Period()));
When run through the strategy tester on a specific time segment the following results are produced (MT4 console copy/paste):

2011.05.31 23:59 showslope EURUSD,M5: Slope: 30.2633
2011.05.31 23:55 showslope EURUSD,M5: Slope: 30.7395
2011.05.31 23:50 showslope EURUSD,M5: Slope: 30.2577
2011.05.31 23:45 showslope EURUSD,M5: Slope: 29.7591
I need to modify the indicator to let me to "bar shift" to any bar. For example, a "bar shift" of 1 should have 30.7395 as the last output instead of 30.2633.

Attempting to shift by incrementing the index shift property in the iCustom command does not do what I intend.

I tried to modify the i-Regr indicator to implement the type of "bar shift" that I require.
Here are the changes I made:
1. Commented out SetIndexShift statements
2....
SetIndexDrawBegin(0, Bars-p-1-shift);
SetIndexDrawBegin(1, Bars-p-1-shift);
SetIndexDrawBegin(2, Bars-p-1-shift); 
3. Changed every instance of: Close[n] to: Close[n+shift])
I tried calling the modified indicator like this:
Print("Slope: ",MathAbs(1000000*( iCustom(Symbol(),0,"i-Regr",1,1,34,shift,0,0)-iCustom(Symbol(),0,"i-Regr",1,1,34,shift,0,1))/Period()));
(with shift = 1)

But I got inaccurate output when I tested it on the same timeframe.

2011.05.31 23:59 showslope EURUSD,M5: Slope: 30.5434
2011.05.31 23:55 showslope EURUSD,M5: Slope: 30.5434
2011.05.31 23:50 showslope EURUSD,M5: Slope: 30.0448
2011.05.31 23:45 showslope EURUSD,M5: Slope: 29.8319

I cannot understand why this happens, but I am guessing that it is because I missed something in the i-Regr code. What I want is for the final output to read as it did a bar back: 30.7395 . Instead it repeats an incorrect number twice.

If someone would please look at my modification and help me out with this, I would really appreciate it.
Link to original indicator: https://www.mql5.com/en/code/8417

Attached is my broken mod of indicator.
Files:
 
slope = MathAbs(1000000*(CurrentReg - LastReg)/Period());
makes no sense. You want a slope per bar not per minute. The 100000 is also bogus.
double curVal = ... 1),
       preVal = ... 2),
       slope  = preVal - curVal /* /(2-1) */,
       valueAtX = slope * (X - 1) + curVal;
 
WHRoeder:
makes no sense. You want a slope per bar not per minute. The 100000 is also bogus.


Well, actually slope per minute is what I want. Please ignore whether or not my definition of slope makes sense; it works for me for a very specific purpose. Regardless of what multiplication/division occurs within my slope formula, I want to produce 30.7395 as the output of the last bar.

I tested your solution and it did not produce the results I want.

double curVal = iCustom(Symbol(),0,"i-Regr",1,1,34,0,0,shift);
double preVal = iCustom(Symbol(),0,"i-Regr",1,1,34,0,0,shift+1);


slope = MathAbs(1000000*(curVal-preVal)/Period());    
double valueAtX = slope * (shift - 1) + curVal;

Print(curVal,"    ",preVal,"    ",slope,"    ",valueAtX);

shift = 0 :

2011.05.31 23:59  showslope EURUSD,M5: 1.4435    1.4434    30.2633    1.4435
2011.05.31 23:55  showslope EURUSD,M5: 1.4436    1.4434    30.7395    1.4436
2011.05.31 23:50  showslope EURUSD,M5: 1.4434    1.4433    30.2577    1.4434
2011.05.31 23:45  showslope EURUSD,M5: 1.4433    1.4431    29.7591    1.4433

shift = 1 :

2011.05.31 23:59  showslope EURUSD,M5: 1.4435    1.4434    30.2633    1.4435
2011.05.31 23:55  showslope EURUSD,M5: 1.4436    1.4434    30.7395    1.4436
2011.05.31 23:50  showslope EURUSD,M5: 1.4434    1.4433    30.2577    1.4434
2011.05.31 23:45  showslope EURUSD,M5: 1.4433    1.4431    29.7591    1.4433

shift = 2 :

2011.05.31 23:59  showslope EURUSD,M5: 1.4434    1.4432    30.2633    31.7067
2011.05.31 23:55  showslope EURUSD,M5: 1.4434    1.4433    30.7395    32.1829
2011.05.31 23:50  showslope EURUSD,M5: 1.4433    1.4431    30.2577    31.701
2011.05.31 23:45  showslope EURUSD,M5: 1.4431    1.443     29.7591    31.2022
 
I believe that I've got the solution. I knew that the only way to do this is to modify the i-Regr indicator. My mistake was that I was placing the underlined statement inside the loop; this caused the slope buffer to be re-written at each bar.
 (inside the indicator)
 for(n=i0;n<=i0+p;n++)
  {
    sum=0;
    for(kk=1;kk<=degree;kk++)
    {
       sum+=x[kk+1]*MathPow(n,kk);
    }
    fx[n]=x[1]+sum;
    
  } 
  slope[0] = MathAbs(1000000*(fx[0]-fx[1])/Period());

EA:
double CurSlope =  iCustom(Symbol(),0,"i-RegrS",1,1,34,0,3,shift);
Print("Slope: ",CurSlope);

output:
2011.05.31 23:59  showslope EURUSD,M5: Slope: 30.2577
2011.05.31 23:55  showslope EURUSD,M5: Slope: 30.2577
2011.05.31 23:50  showslope EURUSD,M5: Slope: 29.7591
2011.05.31 23:45  showslope EURUSD,M5: Slope: 29.8319


This is the output I wanted except for 1 thing: why does the last value repeat like that?
Reason: