Indicator with lag or referencing past bars, was: Selecting subarray

I've found the documentation on how to access one array element with index e.g. Close[0] but how do I select a sub-array of any range from certain start index to certain end index? Close[start,end] and Close[start:end] are not working. Thank you!

The start:end syntax is not supported but if you care to describe the GOAL you are after then somebody may find substitute either using array functions or matrix functions (MxGetBlock/MxSetBlock).
For example if you wanted to "copy" range from one array to another without modifying elements outside range you can do this using code as follows (assumes that you already have arrayB defined earlier)

bi = BarIndex();
arrayB = IIF( bi >= start AND bi <= end, Close, arrayB /*no change*/ );
1 Like

Thanks a lot, Tomasz. I literally want to get Close[0:n] where n is something like BarCount - 14. I'm using it to compute indicators with a lag. So for example, I want to do linear regression for Close with a lag i.e. only using data up to the current candle minus 14. I don't want to make another array just for it if I don't have to. Thanks again! :smiley:

If you want to have indicator with lag just use Ref() function http://www.amibroker.com/f?ref

period = 50;
laggyLR = Ref( LinearReg( Close, period ), -14 );
Plot( LaggyLR, "LaggyLR", colorRed );

Oh! I see! Thank you very much! Just to confirm, that lag1 and lag2 compute the same thing right?

lag1 = Ref(LinearReg(Close, P), -14);
lag2 = LinearReg(Ref(Close, -14), P);

@ehsueh, this is the kind of thing you can easily test/debug yourself using plots or exploration.

Periods = 20;
lag1 = Ref( LinearReg( Close, Periods ), -14 );
lag2 = LinearReg( Ref( Close, -14 ), Periods );

// Inspect visually
Plot( lag1, "Lag 1", colorYellow, styleLine , Null, Null, 0, 0 );
Plot( lag2, "Lag 2", colorRed, styleDashed, Null, Null, 0, 1 );	// overlap lines using Z-order

// Inspect using an exploration
Filter = 1;
AddColumn( Lag1, "Lag 1", 1.5 );
AddColumn( Lag2, "Lag 2", 1.5 );
equality = AlmostEqual( Lag1, Lag2 ); // A more reliable comparisons and less headache caused by IEEE floating point acurracy issues*
AddColumn( equality, "==", 1, colorWhite, IIf( equality, colorGreen, colorRed ) );

if( Status( "action" ) == actionExplore )
    SetSortColumns( -2 );

* Comparing floating point results can be tricky!

1 Like

@ehsueh - Yes these are the same thing because Ref() is just referencing (shifts) data back and forth, so it does not make difference if you shift data before calculating LR or shift result after calculating LR.

@beppe
Thank you very much for the codes! Yeah, I did plotted them out and noticed they overlapped. However, I wasn't sure how those functions were implemented so I wasn't confident in using my results as proof. :smile:

@Tomasz
That makes sense. Thanks a lot!

1 Like

Thanks for your AFL. If we combine with another regression heading with "
Porting TA-lib based Linear Regression With Custom “Lag”" in this forum , we can get tremendous entries. Thanks for the both contributors.

@VARADAN,

See