Not sure about correct use of expandFirst vs. expandLast

I'm learning more about how time compression and expansion work, and created this exploration to help me understand the subject better, as a sort of homework assignment.

I run the below exploration on a 1-minute DB with 24hr filtering, with periodicity set to 15 minutes.

The below code outputs correct calculations for me, but I don't understand why expandFirst works yet expandLast returns prices that are outside of the current day.

Am I using the timeframe functions in an unorthodox manner, or am I missing something else here?

-- PeterD

procedure MaskPriceArrays( Mask )
{
	// Filters out non-RTH data from AB price arrays.
	// RestorePriceArrays() must be used to un-mask them.
	// Based on 'SessionHHVs_LLVs' @link https://forum.amibroker.com/t/need-ideas-to-speed-up-plotting-of-prior-intraday-data/19519/8

	for( i = 0; i < 4; i++ )
	{
		Array = StrExtract( "O,H,L,C", i );
		VarSet( Array, IIf( Mask, VarGet( Array ), Null ) );
	}
	return;
}

AddColumn( H, "H unmasked", 1.2 );
AddColumn( L, "L unmasked", 1.2 );

tn = TimeNum();
Mask = iif( tn >= 093000 AND tn < 160000, True, False );
MaskPriceArrays( Mask );

AddColumn( H, "H masked", 1.2 );
AddColumn( L, "L masked", 1.2 );

// Create arrays with daily highs and lows from RTH.
// Intraday, this looks into future bars to find the H and L of the day before it happens.
H = RTHH = TimeFrameExpand( TimeFrameCompress( H, inDaily, compressHigh ), inDaily, expandFirst );
L = RTHL = TimeFrameExpand( TimeFrameCompress( Nz( L, 1e10 ), inDaily, compressLow ), inDaily, expandFirst );

// Calculate stats on daily basis.
Days = 30;

TimeFrameSet( inDaily );
	Range = H - L;
	AvgRange = MA( Range, Days );
	RangeSD = StDev( Range, Days, 1 );
RestorePriceArrays();

// Expand the compressed arrays.
Range = TimeFrameExpand( Range, inDaily, expandFirst );
AvgRange = TimeFrameExpand( AvgRange, inDaily, expandFirst );
RangeSD = TimeFrameExpand( RangeSD, inDaily, expandFirst );

Filter = 1;
AddColumn( RTHH, "RTHH", 1.2 );
AddColumn( RTHL, "RTHL", 1.2 );
AddColumn( Range, "Range", 1.2 );
AddColumn( AvgRange, "AvgRange " + Days + "d", 1.2 );
AddColumn( RangeSD, "RangeSD " + Days + "d", 1.2 );

image

1 Like

For an average person, it is very tough trying to understand your question.
Even if one gets around that, you should post results of using "ExpandLast" as well so one can compare. This is why one cannot understand your question.

1 Like

When you use expandLast to expand a daily array into an intraday array, your daily value from Monday will appear in the last bar for Monday and all bars except the last on Tuesday. The last intraday bar on Tuesday will hold the Tuesday daily value, as will all bars on Wednesday except the last, etc.

As for the expanded prices being outside the bounds of the current day that's what you've asked for and AmiBroker won't force anything else unless you try to use that price to enter or exit a trade.

Ah, thank you for helping me understand this better. I must have overlooked that in the documentation.

I am very grateful for your assistance!

Yours truly,
Peter

1 Like