Porting TA-lib based Linear Regression With Custom "Lag"


I'm trying to port my first strategy to AmiBroker, but I'm having some problems with it's LinReg indicator.

In my old code I'm using TA-lib and applying some "lag" to some instances of linear regression, but it looks like AmiBroker doesn't expose all configurable parameters and only comes with "data" and "period".

Here is the problematic part of code I'm trying to port:

function LINEARREG(data, period, lag){
    lag = lag ? lag : 0; //default lag is 0
    period = data.length < period ?  data.length : period; //period needs to be lower than data.length
    return talib.LINEARREG (data, 0, data.length - lag - 1, period); //TA-lib arguments: (inReal, startIdx, endIdx, optInTimePeriod);

Does anyone know, is there a way around this limit?

Either via Ref() or via SparseCompress(). Second one uses start and end idx like TA-lib.

function TL_LinearReg1(data, period, lag){
    // by fxshrat at gmail.com
    lag = Max(0, lag); 
    period = Min(period, BarCount-1-lag); 
    return Ref(LinearReg(data, period), -lag);

function TL_LinearReg2(data, startidx, endidx, period){
	// by fxshrat at gmail.com
	// TA-lib arguments: (inReal, startIdx, endIdx, optInTimePeriod);
	local bi, length, dateiwndow, spcomp, spexp;
	bi = BarIndex();
    length = BarCount-1;
    startidx = Max(-1e-9, Min(startidx, length));
    endidx = Max(startidx, Min(endidx, length));
    datewindow = bi >= startidx AND bi <= endidx;
    spcomp = SparseCompress(datewindow, LinearReg(data, period));   
    return spcomp; 

period = 10;
lag = 10;

linreg = TL_LinearReg1(C, period, lag);
Plot( linreg, "TL_LinearReg1", colorOrange, styleLine );

linreg = TL_LinearReg2(C, startidx = 0, endidx = BarCount-1-lag, period);
Plot( linreg, "TL_LinearReg2", colorRed, styleLine );

linreg = LinearReg(C, period);
Plot( linreg, "LinearReg default", colorYellow, styleLine );

Plot( C, "Price", colorDefault, styleCandle );



Thanks fxshrat, you are awsome.

I was thinking to use Ref() but wasn't sure if I can just shift the data or it may impact the result. However, ran a test today with old code and proved that it doesn't impact the results (adding lag or shifting the whole result produces the same output). Thanks again for pointing that out.