Need ideas to speed up plotting of prior intraday data

I'm plotting recent highs and lows from prior days, but there's a twist.

Using a 1-minute database with no filtering (24H), I am looking up recent intraday highs and lows, but I am only concerned with those highs and lows that occurred during regular trading hours, although I still want to see the pre- and after-market sessions in the chart. As far as I know, this means that I can't use the standard way of looking up values from higher timeframes, which would be like this:

TimeFrameSet( inDaily );
	HH10d = TimeFrameExpand( HHV( H, 10 ), inDaily, expandFirst );
	LL10d = TimeFrameExpand( LLV( L, 10 ), inDaily, expandFirst );
	HH30d = TimeFrameExpand( HHV( H, 30 ), inDaily, expandFirst );
	LL30d = TimeFrameExpand( LLV( L, 30 ), inDaily, expandFirst );
	HH90d = TimeFrameExpand( HHV( H, 90 ), inDaily, expandFirst );
	LL90d = TimeFrameExpand( LLV( L, 90 ), inDaily, expandFirst );
	HH270d = TimeFrameExpand( HHV( H, 270 ), inDaily, expandFirst );
	LL270d = TimeFrameExpand( LLV( L, 270 ), inDaily, expandFirst );
TimeFrameRestore();

I created an experiment here, where I have two functions, HHRTH() and LLRTH() which go back and find the correct values. However, they are very slow functions. On my ancient Core i5 machine, looking up anything past the prior 10 days takes multiple seconds, or minutes, on a 1-minute DB.

Is there a better way to code this that I'm missing?

Or should I be creating composites in an overnight batch for future lookup?

I'm open to suggestions.

-PeterD

Here's the experimental code:

_SECTION_BEGIN( "Price" );

	SetChartOptions( 2, chartShowArrows | chartShowDates | chartWrapTitle );
	_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )+ "\n{{FULLNAME}}");
	Plot( C, "Close", ParamColor( "Color", colorDefault ), styleBar );

_SECTION_END();

_SECTION_BEGIN( "Intraday RTH value lookup experiment" );

	// Plot recent highs and lows from prior day regular trading hours, using a database
	// that has no filtering (24H).
	// In Preferences, time stamp of compressed intraday bars shows START time of interval.

	Intrvl = Interval(0);
	tn = TimeNum();
	DayOpenTimeNum = 093000;
	DayCloseTimeNum = 160000;
	RTHStart = tn == DayOpenTimeNum;
	RTHEnd = tn == DayCloseTimeNum;
	RTH = iif( tn >= DayOpenTimeNum AND tn < DayCloseTimeNum, True, False );
	BI = BarIndex();

	function TimeNumAdd( ReferenceTime, IntervalSeconds, NumIntervals )
	{
		// Returns the time based on a reference time, plus or minus time.
		// Based on https://forum.amibroker.com/t/timenum-addition-subtraction/3324
		dn = LastValue( DateNum() );					// any 'DateNum' will work
		dt = DateTimeConvert( 2, dn, ReferenceTime );   // create 'DateTime'
		dt2 = DateTimeAdd( dt, IntervalSeconds, NumIntervals );			// do arithmetic
		nt = DateTimeConvert( 1, dt2 );					// grab time portion of 'dt2'
		return nt;
	}

	function HHRTH( Days )
	{
		// Returns the HHV over the past number of days, but only in RTH.
		// The last bar of RTH must align to a bar in the DB, or it will not work.
		// This function gets slower the further the number of days to look back.

		// Find the BarIndex of the starting point.
		// Create a BarMask to filter out non-RTH.
		StartBI = ValueWhen( RTHEnd, BI, Days );
		BarMask = RTH AND BI >= StartBI;
		Masked = IIf( BarMask, H, 0 );
		Output = HHV( Masked, BI - StartBI );

		return Output;
	}

	function LLRTH( Days )
	{
		// Returns the LLV over the past number of days, but only in RTH.
		// The last bar of RTH must align to a bar in the DB, or it will not work.
		// This function gets slower the further the number of days to look back.

		// Find the BarIndex of the starting point.
		// Create a BarMask to filter out non-RTH.
		StartBI = ValueWhen( RTHEnd, BI, Days );
		BarMask = RTH AND BI >= StartBI;
		Masked = IIf( BarMask, L, 1e+12 ); 
		Output = LLV( Masked, BI - StartBI ); 
		
		return Output;
	}

	LastRTHBarTN = TimeNumAdd( DayCloseTimeNum, -Intrvl, 1 );
	
	//Parameters
	PCLColor = ParamColor( "PCL", colorBlack );
	PriorHxColor = ParamColor( "Hx", colorGreen );
	PriorLxColor = ParamColor( "Lx", colorRed );
	DayOColor = ParamColor( "O", colorGrey40 );
	DayHColor = ParamColor( "H", colorGreen );
	DayLColor = ParamColor( "L", colorRed );

	if( Intrvl < inDaily )
	{
		PCL = IIf( tn == LastRTHBarTN, 
			ValueWhen( tn == LastRTHBarTN, C, 2 ),	// On the last bar of RTH, use the prior day close and not the current day close to avoid future leak.
			ValueWhen( tn == LastRTHBarTN, C, 1 ) );

		DyO = ValueWhen( RTHStart, O, 1 );
		DyH = HighestSince( RTHStart, H, 1 );
		DyL = LowestSince( RTHStart, L, 1 );
		H1d = ValueWhen( RTHEnd, DyH, 1 );
		L1d = ValueWhen( RTHEnd, DyL, 1 );
		H2d = ValueWhen( RTHEnd, DyH, 2 );
		L2d = ValueWhen( RTHEnd, DyL, 2 );

		// This is the slow part. The 90 and 270 day calculations are set to 0 because
		// they take too long.
		HH10d = HHRTH( 10 );
		LL10d = LLRTH( 10 );
		HH30d = HHRTH( 30 );
		LL30d = LLRTH( 30 );
		HH90d = 0; // HHRTH( 90 );
		LL90d = 0; // LLRTH( 90 );
		HH270d = 0; // HHRTH( 270 );
		LL270d = 0; // LLRTH( 270 );

		/* This is the only fast way that I know, but it doesn't doesn't pull from RTH only.
		TimeFrameSet( inDaily );
			HH10d = TimeFrameExpand( HHV( H, 10 ), inDaily, expandFirst );
			LL10d = TimeFrameExpand( LLV( L, 10 ), inDaily, expandFirst );
			HH30d = TimeFrameExpand( HHV( H, 30 ), inDaily, expandFirst );
			LL30d = TimeFrameExpand( LLV( L, 30 ), inDaily, expandFirst );
			HH90d = TimeFrameExpand( HHV( H, 90 ), inDaily, expandFirst );
			LL90d = TimeFrameExpand( LLV( L, 90 ), inDaily, expandFirst );
			HH270d = TimeFrameExpand( HHV( H, 270 ), inDaily, expandFirst );
			LL270d = TimeFrameExpand( LLV( L, 270 ), inDaily, expandFirst );
		TimeFrameRestore();
		*/
	
	}
	else
	{
		PCL = Ref( C, -1 );
		H1d = Ref( H, -1 );
		L1d = Ref( L, -1 );
		H2d = Ref( H, -2 );
		L2d = Ref( L, -2 );
		HH10d = HHV( H, 10 );
		LL10d = LLV( L, 10 );
		HH30d = HHV( H, 30 );
		LL30d = LLV( L, 30 );
		HH90d = HHV( H, 90 );
		LL90d = LLV( L, 90 );
		HH270d = HHV( H, 270 );
		LL270d = LLV( L, 270 );
	}

	printf( "PCL " + " " + PCL + "\n" );
	printf( "H1d " + " " + H1d + "\n" );
	printf( "L1d " + " " + L1d + "\n" );
	printf( "H2d " + " " + H2d + "\n" );
	printf( "L2d " + " " + L2d + "\n" );
	printf( "HH10d " + " " + HH10d + "\n" );
	printf( "LL10d " + " " + LL10d + "\n" );
	printf( "HH30d " + " " + HH30d + "\n" );
	printf( "LL30d " + " " + LL30d + "\n" );
	printf( "HH90d " + " " + HH90d + "\n" );
	printf( "LL90d " + " " + LL90d + "\n" );
	printf( "HH270d " + " " + HH270d + "\n" );
	printf( "LL270d " + " " + LL270d + "\n" );

	// Only on intraday chart, plot current and prior day high, and low, as well as prior day close, and yellow zone.
	if( Intrvl < inDaily )
	{
		SetBarFillColor( colorDefault );
		PlotOHLC( DyO, DyO, DyO, DyO, "Open", DayOColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		Plot( DyH, "DayH", DayHColor, styleLine | styleNoRescale | styleNoTitle );
		Plot( DyL, "DayL", DayLColor, styleLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( PCLColor );
		PlotOHLC( PCL, PCL, PCL, PCL, "PCL", PCLColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( H1d, H1d, H1d, H1d, "H1d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L1d, L1d, L1d, L1d, "L1d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( H2d, H2d, H2d, H2d, "H2d" , PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L2d, L2d, L2d, L2d, "L2d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH10d, HH10d, HH10d, HH10d, "HH10d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL10d, LL10d, LL10d, LL10d, "LL10d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH30d, HH30d, HH30d, HH30d, "HH30d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL30d, LL30d, LL30d, LL30d, "LL30d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH90d, HH90d, HH90d, HH90d, "HH90d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL90d, LL90d, LL90d, LL90d, "LL90d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH270d, HH270d, HH270d, HH270d, "HH270d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL270d, LL270d, LL270d, LL270d, "LL270d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

	}

	// Label the lines on the chart.
	FontSize = 8;
	LVVBI = LastVisibleValue( BarIndex() );

	if( Intrvl < inDaily )
	{
		DayOOffset = LastVisibleValue( BarsSince( DateNum() != Ref( DateNum(), -1 ) ) );
		DayHOffset = LastVisibleValue( BarsSince( H == DyH ) );
		DayLOffset = LastVisibleValue( BarsSince( L == DyL ) );
		FirstVisibleBarOfDay = LVVBI - DayOOffset;

		PlotTextSetFont( "PCL", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( PCL ), colorWhite, PCLColor, -FontSize/2 );
		PlotTextSetFont( "O", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( DyO ), colorWhite, DayOColor, -FontSize/2 );
		PlotTextSetFont( "H", "Arial Black", FontSize, LVVBI - DayHOffset, LastVisibleValue( DyH ), DayHColor, colorDefault, (FontSize-6) );
		PlotTextSetFont( "L", "Arial Black", FontSize, LVVBI - DayLOffset, LastVisibleValue( DyL ), DayLColor, colorDefault, -(FontSize+2) );
	}
		
	if( Intrvl == inDaily )
	{
		H1d = IIf( ( LVVBI - BI ) <= 1, LastVisibleValue( H1d ), Null );
		L1d = IIf( ( LVVBI - BI ) <= 1, LastVisibleValue( L1d ), Null );
		H2d = IIf( ( LVVBI - BI ) <= 2, LastVisibleValue( H2d ), Null );
		L2d = IIf( ( LVVBI - BI ) <= 2, LastVisibleValue( L2d ), Null );
		HH10d = IIf( ( LVVBI - BI ) <= 10, LastVisibleValue( HH10d ), 0 );
		LL10d = IIf( ( LVVBI - BI ) <= 10, LastVisibleValue( LL10d ), 0 );
		HH30d = IIf( ( LVVBI - BI ) <= 30, LastVisibleValue( HH30d ), 0 );
		LL30d = IIf( ( LVVBI - BI ) <= 30, LastVisibleValue( LL30d ), 0 );
		HH90d = IIf( ( LVVBI - BI ) <= 90, LastVisibleValue( HH90d ), 0 );
		LL90d = IIf( ( LVVBI - BI ) <= 90, LastVisibleValue( LL90d ), 0 );
		HH270d = IIf( ( LVVBI - BI ) <= 270, LastVisibleValue( HH270d ), 0 );
		LL270d = IIf( ( LVVBI - BI ) <= 270, LastVisibleValue( LL270d ), 0 );
		
		SetBarFillColor( colorDefault );
		PlotOHLC( H1d, H1d, H1d, H1d, "H1d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L1d, L1d, L1d, L1d, "L1d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( H2d, H2d, H2d, H2d, "H2d" , PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L2d, L2d, L2d, L2d, "L2d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH10d, HH10d, HH10d, HH10d, "HH10d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL10d, LL10d, LL10d, LL10d, "LL10d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH30d, HH30d, HH30d, HH30d, "HH30d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL30d, LL30d, LL30d, LL30d, "LL30d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH90d, HH90d, HH90d, HH90d, "HH90d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL90d, LL90d, LL90d, LL90d, "LL90d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH270d, HH270d, HH270d, HH270d, "HH270d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL270d, LL270d, LL270d, LL270d, "LL270d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
	}
	
	PlotTextSetFont( "H1d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( H1d ), colorWhite, PriorHxColor, -FontSize/2 );
	PlotTextSetFont( "L1d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( L1d ), colorWhite, PriorLxColor, -FontSize/2 );
	PlotTextSetFont( "H2d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( H2d ), colorWhite, PriorHxColor, -FontSize/2 );
	PlotTextSetFont( "L2d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( L2d ), colorWhite, PriorLxColor, -FontSize/2 );
	PlotTextSetFont( "HH270d", "Arial Black", FontSize, LVVBI+3, LastVisibleValue( HH270d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL270d", "Arial Black", FontSize, LVVBI+3, LastVisibleValue( LL270d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH90d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH90d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL90d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL90d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH30d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH30d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL30d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL30d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH10d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH10d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL10d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL10d ), colorWhite, PriorLxColor, -(FontSize+2) );

_SECTION_END();

Before ideas, you need to urgently read carefully the documentation
https://www.amibroker.com/guide/h_timeframe.html

Thanks, you're right. Although I read that page several times before, I didn't fully comprehend how to use TimeFrameCompress properly which was the missing link.

I appreciate your help! It's running super fast now that I am doing things according to the design of the system.

I still have a lot of redundant code in here that I need to clean up, but here's the updated code.

Yours truly,
Peter

_SECTION_BEGIN( "Price" );

	SetChartOptions( 2, chartShowArrows | chartShowDates | chartWrapTitle );
	_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )+ "\n{{FULLNAME}}");
	Plot( C, "Close", ParamColor( "Color", colorDefault ), styleBar | styleNoLabel );

_SECTION_END();

_SECTION_BEGIN( "Intraday RTH value lookup experiment" );

	// Plot recent highs and lows from prior day regular trading hours, using a database
	// that has no filtering (24H).
	// In Preferences, time stamp of compressed intraday bars shows START time of interval.

	Intrvl = Interval(0);
	tn = TimeNum();
	DayOpenTimeNum = 093000;
	DayCloseTimeNum = 160000;
	RTHStart = tn == DayOpenTimeNum;
	RTHEnd = tn == DayCloseTimeNum;
	RTH = iif( tn >= DayOpenTimeNum AND tn < DayCloseTimeNum, True, False );
	BI = BarIndex();

	function TimeNumAdd( ReferenceTime, IntervalSeconds, NumIntervals )
	{
		// Returns the time based on a reference time, plus or minus time.
		// Based on https://forum.amibroker.com/t/timenum-addition-subtraction/3324
		dn = LastValue( DateNum() );					// any 'DateNum' will work
		dt = DateTimeConvert( 2, dn, ReferenceTime );   // create 'DateTime'
		dt2 = DateTimeAdd( dt, IntervalSeconds, NumIntervals );			// do arithmetic
		nt = DateTimeConvert( 1, dt2 );					// grab time portion of 'dt2'
		return nt;
	}

	LastRTHBarTN = TimeNumAdd( DayCloseTimeNum, -Intrvl, 1 );
	
	//Parameters
	PCLColor = ParamColor( "PCL", colorBlack );
	PriorHxColor = ParamColor( "Hx", colorGreen );
	PriorLxColor = ParamColor( "Lx", colorRed );
	DayOColor = ParamColor( "O", colorGrey40 );
	DayHColor = ParamColor( "H", colorGreen );
	DayLColor = ParamColor( "L", colorRed );

	if( Intrvl < inDaily )
	{
		PCL = IIf( tn == LastRTHBarTN, 
			ValueWhen( tn == LastRTHBarTN, C, 2 ),	// On the last bar of RTH, use the prior day close and not the current day close to avoid future leak.
			ValueWhen( tn == LastRTHBarTN, C, 1 ) );

		DyO = ValueWhen( RTHStart, O, 1 );
		DyH = HighestSince( RTHStart, H, 1 ); // this includes after-market... causing trouble with HH/LL
		DyH = Min( DyH, ValueWhen( tn == LastRTHBarTN, DyH, 1 ) );
		DyL = LowestSince( RTHStart, L, 1 ); // this includes after-market... causing trouble with HH/LL
		DyL = Max( DyL, ValueWhen( tn == LastRTHBarTN, DyL, 1 ) );
		
		H1d = ValueWhen( RTHEnd, DyH, 1 );
		L1d = ValueWhen( RTHEnd, DyL, 1 );
		H2d = ValueWhen( RTHEnd, DyH, 2 );
		L2d = ValueWhen( RTHEnd, DyL, 2 );
 
		RTHH = TimeFrameCompress( DyH, inDaily, compressLast );
		RTHL = TimeFrameCompress( DyL, inDaily, compressLast );
		printf( "RTHH " + " " + RTHH + "\n" );
		printf( "RTHL " + " " + RTHL + "\n" );

		HH10d = TimeFrameExpand( HHV( RTHH, 10 ), inDaily, expandLast );
		LL10d = TimeFrameExpand( LLV( RTHL, 10 ), inDaily, expandLast );
		HH30d = TimeFrameExpand( HHV( RTHH, 30 ), inDaily, expandLast );
		LL30d = TimeFrameExpand( LLV( RTHL, 30 ), inDaily, expandLast );
		HH90d = TimeFrameExpand( HHV( RTHH, 90 ), inDaily, expandLast );
		LL90d = TimeFrameExpand( LLV( RTHL, 90 ), inDaily, expandLast );
		HH270d = TimeFrameExpand( HHV( RTHH, 270 ), inDaily, expandLast );
		LL270d = TimeFrameExpand( LLV( RTHL, 270 ), inDaily, expandLast );
		
	}
	else
	{
		PCL = Ref( C, -1 );
		H1d = Ref( H, -1 );
		L1d = Ref( L, -1 );
		H2d = Ref( H, -2 );
		L2d = Ref( L, -2 );
		HH10d = Ref( HHV( H, 10 ), -1 );
		LL10d = Ref( LLV( L, 10 ), -1 );
		HH30d = Ref( HHV( H, 30 ), -1 );
		LL30d = Ref( LLV( L, 30 ), -1 );
		HH90d = Ref( HHV( H, 90 ), -1 );
		LL90d = Ref( LLV( L, 90 ), -1 );
		HH270d = Ref( HHV( H, 270 ), -1 );
		LL270d = Ref( LLV( L, 270 ), -1 );
	}

	printf( "PCL " + " " + PCL + "\n" );
	printf( "H1d " + " " + H1d + "\n" );
	printf( "L1d " + " " + L1d + "\n" );
	printf( "H2d " + " " + H2d + "\n" );
	printf( "L2d " + " " + L2d + "\n" );
	printf( "HH10d " + " " + HH10d + "\n" );
	printf( "LL10d " + " " + LL10d + "\n" );
	printf( "HH30d " + " " + HH30d + "\n" );
	printf( "LL30d " + " " + LL30d + "\n" );
	printf( "HH90d " + " " + HH90d + "\n" );
	printf( "LL90d " + " " + LL90d + "\n" );
	printf( "HH270d " + " " + HH270d + "\n" );
	printf( "LL270d " + " " + LL270d + "\n" );

	// Only on intraday chart, plot current and prior day high, and low, as well as prior day close, and yellow zone.
	if( Intrvl < inDaily )
	{
		SetBarFillColor( colorDefault );
		PlotOHLC( DyO, DyO, DyO, DyO, "Open", DayOColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		Plot( DyH, "DayH", DayHColor, styleLine | styleNoRescale | styleNoTitle );
		Plot( DyL, "DayL", DayLColor, styleLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( PCLColor );
		PlotOHLC( PCL, PCL, PCL, PCL, "PCL", PCLColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( H1d, H1d, H1d, H1d, "H1d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L1d, L1d, L1d, L1d, "L1d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( H2d, H2d, H2d, H2d, "H2d" , PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L2d, L2d, L2d, L2d, "L2d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH10d, HH10d, HH10d, HH10d, "HH10d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL10d, LL10d, LL10d, LL10d, "LL10d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH30d, HH30d, HH30d, HH30d, "HH30d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL30d, LL30d, LL30d, LL30d, "LL30d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH90d, HH90d, HH90d, HH90d, "HH90d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL90d, LL90d, LL90d, LL90d, "LL90d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH270d, HH270d, HH270d, HH270d, "HH270d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL270d, LL270d, LL270d, LL270d, "LL270d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

	}

	// Label the lines on the chart.
	FontSize = 8;
	LVVBI = LastVisibleValue( BarIndex() );

	if( Intrvl < inDaily )
	{
		DayOOffset = LastVisibleValue( BarsSince( DateNum() != Ref( DateNum(), -1 ) ) );
		DayHOffset = LastVisibleValue( BarsSince( H == DyH ) );
		DayLOffset = LastVisibleValue( BarsSince( L == DyL ) );
		FirstVisibleBarOfDay = LVVBI - DayOOffset;

		PlotTextSetFont( "PCL", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( PCL ), colorWhite, PCLColor, -FontSize/2 );
		PlotTextSetFont( "O", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( DyO ), colorWhite, DayOColor, -FontSize/2 );
		PlotTextSetFont( "H", "Arial Black", FontSize, LVVBI - DayHOffset, LastVisibleValue( DyH ), DayHColor, colorDefault, (FontSize-6) );
		PlotTextSetFont( "L", "Arial Black", FontSize, LVVBI - DayLOffset, LastVisibleValue( DyL ), DayLColor, colorDefault, -(FontSize+2) );
	}
		
	if( Intrvl == inDaily )
	{
		H1d = IIf( ( LVVBI - BI ) <= 1, LastVisibleValue( H1d ), Null );
		L1d = IIf( ( LVVBI - BI ) <= 1, LastVisibleValue( L1d ), Null );
		H2d = IIf( ( LVVBI - BI ) <= 2, LastVisibleValue( H2d ), Null );
		L2d = IIf( ( LVVBI - BI ) <= 2, LastVisibleValue( L2d ), Null );
		HH10d = IIf( ( LVVBI - BI ) <= 10, LastVisibleValue( HH10d ), 0 );
		LL10d = IIf( ( LVVBI - BI ) <= 10, LastVisibleValue( LL10d ), 0 );
		HH30d = IIf( ( LVVBI - BI ) <= 30, LastVisibleValue( HH30d ), 0 );
		LL30d = IIf( ( LVVBI - BI ) <= 30, LastVisibleValue( LL30d ), 0 );
		HH90d = IIf( ( LVVBI - BI ) <= 90, LastVisibleValue( HH90d ), 0 );
		LL90d = IIf( ( LVVBI - BI ) <= 90, LastVisibleValue( LL90d ), 0 );
		HH270d = IIf( ( LVVBI - BI ) <= 270, LastVisibleValue( HH270d ), 0 );
		LL270d = IIf( ( LVVBI - BI ) <= 270, LastVisibleValue( LL270d ), 0 );
		
		SetBarFillColor( colorDefault );
		PlotOHLC( H1d, H1d, H1d, H1d, "H1d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L1d, L1d, L1d, L1d, "L1d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( H2d, H2d, H2d, H2d, "H2d" , PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( L2d, L2d, L2d, L2d, "L2d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH10d, HH10d, HH10d, HH10d, "HH10d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL10d, LL10d, LL10d, LL10d, "LL10d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH30d, HH30d, HH30d, HH30d, "HH30d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL30d, LL30d, LL30d, LL30d, "LL30d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH90d, HH90d, HH90d, HH90d, "HH90d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL90d, LL90d, LL90d, LL90d, "LL90d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( HH270d, HH270d, HH270d, HH270d, "HH270d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );

		SetBarFillColor( colorDefault );
		PlotOHLC( LL270d, LL270d, LL270d, LL270d, "LL270d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
	}
	
	PlotTextSetFont( "H1d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( H1d ), colorWhite, PriorHxColor, -FontSize/2 );
	PlotTextSetFont( "L1d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( L1d ), colorWhite, PriorLxColor, -FontSize/2 );
	PlotTextSetFont( "H2d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( H2d ), colorWhite, PriorHxColor, -FontSize/2 );
	PlotTextSetFont( "L2d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( L2d ), colorWhite, PriorLxColor, -FontSize/2 );
	PlotTextSetFont( "HH270d", "Arial Black", FontSize, LVVBI+3, LastVisibleValue( HH270d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL270d", "Arial Black", FontSize, LVVBI+3, LastVisibleValue( LL270d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH90d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH90d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL90d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL90d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH30d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH30d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL30d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL30d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH10d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH10d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL10d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL10d ), colorWhite, PriorLxColor, -(FontSize+2) );

_SECTION_END();

I corrected part of your code, so you get the idea . But there is plenty of room for improvement

	SetChartOptions( 2, chartShowArrows | chartShowDates | chartWrapTitle );
	_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )+ "\n{{FULLNAME}}");
	Plot( C, "Close", ParamColor( "Color", colorDefault ), styleBar );

	// Plot recent highs and lows from prior day regular trading hours, using a database
	// that has no filtering (24H).
	// In Preferences, time stamp of compressed intraday bars shows START time of interval.

	Intrvl = Interval(0);
	tn = TimeNum();
	DayOpenTimeNum = 093000;
	DayCloseTimeNum = 160000;
	RTHStart = tn == DayOpenTimeNum;
	RTHEnd = tn == DayCloseTimeNum;
	RTH = tn >= DayOpenTimeNum AND tn < DayCloseTimeNum;
	FirstBarOfRTH = RTH AND Ref(RTH, -1) == False;
	LastBarOfRTH = RTH AND Ref(RTH, 1) == False;

	BI = BarIndex();
/*
	function TimeNumAdd( ReferenceTime, IntervalSeconds, NumIntervals )
	{
		// Returns the time based on a reference time, plus or minus time.
		// Based on https://forum.amibroker.com/t/timenum-addition-subtraction/3324
		dn = LastValue( DateNum() );					// any 'DateNum' will work
		dt = DateTimeConvert( 2, dn, ReferenceTime );   // create 'DateTime'
		dt2 = DateTimeAdd( dt, IntervalSeconds, NumIntervals );			// do arithmetic
		nt = DateTimeConvert( 1, dt2 );					// grab time portion of 'dt2'
		return nt;
	}
*/
	function HHRTH( Days )
	{
		// Returns the HHV over the past number of days, but only in RTH.
		// The last bar of RTH must align to a bar in the DB, or it will not work.
		// This function gets slower the further the number of days to look back.

		// Find the BarIndex of the starting point.
		// Create a BarMask to filter out non-RTH.
		/*
		StartBI = ValueWhen( RTHEnd, BI, Days );
		BarMask = RTH AND BI >= StartBI;
		Masked = IIf( BarMask, H, 0 );
		Output = HHV( Masked, BI - StartBI );
		*/
		Output = HighestSince(FirstBarOfRTH, H, Days);
		Output = ValueWhen(LastBarOfRTH, Output);
		
		return Output;
	}

	function LLRTH( Days )
	{
		// Returns the LLV over the past number of days, but only in RTH.
		// The last bar of RTH must align to a bar in the DB, or it will not work.
		// This function gets slower the further the number of days to look back.

		// Find the BarIndex of the starting point.
		// Create a BarMask to filter out non-RTH.
		/*
		StartBI = ValueWhen( RTHEnd, BI, Days );
		BarMask = RTH AND BI >= StartBI;
		Masked = IIf( BarMask, L, 1e+12 ); 
		Output = LLV( Masked, BI - StartBI ); 
		*/
		Output = LowestSince(FirstBarOfRTH, L, Days);
		Output = ValueWhen(LastBarOfRTH, Output);
		
		return Output;
	}

	//LastRTHBarTN = TimeNumAdd( DayCloseTimeNum, -Intrvl, 1 );
	
	//Parameters
	PCLColor = ParamColor( "PCL", colorBlack );
	PriorHxColor = ParamColor( "Hx", colorGreen );
	PriorLxColor = ParamColor( "Lx", colorRed );
	DayOColor = ParamColor( "O", colorGrey40 );
	DayHColor = ParamColor( "H", colorGreen );
	DayLColor = ParamColor( "L", colorRed );

	if( Intrvl < inDaily )
	{
		DayClose = ValueWhen( LastBarOfRTH, C);
		// On the last bar of RTH, use the prior day close and not the current day close to avoid future leak.
		PCL = IIf( tn == LastBarOfRTH, Ref(DayClose, -1), DayClose); 

		DyO = ValueWhen( RTHStart, O, 1 );
		DyH = HighestSince( RTHStart, H, 1 );
		DyL = LowestSince( RTHStart, L, 1 );
		H1d = ValueWhen( RTHEnd, DyH, 1 );
		L1d = ValueWhen( RTHEnd, DyL, 1 );
		H2d = ValueWhen( RTHEnd, DyH, 2 );
		L2d = ValueWhen( RTHEnd, DyL, 2 );

		// This is the slow part. The 90 and 270 day calculations are set to 0 because
		// they take too long.
		HH10d = HHRTH( 10 );
		LL10d = LLRTH( 10 );
		HH30d = HHRTH( 30 );
		LL30d = LLRTH( 30 );
		HH90d = 0; // HHRTH( 90 );
		LL90d = 0; // LLRTH( 90 );
		HH270d = 0; // HHRTH( 270 );
		LL270d = 0; // LLRTH( 270 );
	}
	else
	{
		PCL = Ref( C, -1 );
		H1d = Ref( H, -1 );
		L1d = Ref( L, -1 );
		H2d = Ref( H, -2 );
		L2d = Ref( L, -2 );
		HH10d = HHV( H, 10 );
		LL10d = LLV( L, 10 );
		HH30d = HHV( H, 30 );
		LL30d = LLV( L, 30 );
		HH90d = HHV( H, 90 );
		LL90d = LLV( L, 90 );
		HH270d = HHV( H, 270 );
		LL270d = LLV( L, 270 );
	}

	printf( "PCL " + " " + PCL + "\n" );
	printf( "H1d " + " " + H1d + "\n" );
	printf( "L1d " + " " + L1d + "\n" );
	printf( "H2d " + " " + H2d + "\n" );
	printf( "L2d " + " " + L2d + "\n" );
	printf( "HH10d " + " " + HH10d + "\n" );
	printf( "LL10d " + " " + LL10d + "\n" );
	printf( "HH30d " + " " + HH30d + "\n" );
	printf( "LL30d " + " " + LL30d + "\n" );
	printf( "HH90d " + " " + HH90d + "\n" );
	printf( "LL90d " + " " + LL90d + "\n" );
	printf( "HH270d " + " " + HH270d + "\n" );
	printf( "LL270d " + " " + LL270d + "\n" );

	// Only on intraday chart, plot current and prior day high, and low, as well as prior day close, and yellow zone.
	if( Intrvl < inDaily )
	{
		Plot( DyO, "Open", DayOColor,  styleNoRescale | styleNoTitle);
		Plot( DyH, "DayH", DayHColor, styleNoRescale | styleNoTitle );
		Plot( DyL, "DayL", DayLColor, styleNoRescale | styleNoTitle );
		Plot( PCL, "PCL", PCLColor, styleNoRescale | styleNoTitle );
		Plot( H1d, "H1d", PriorHxColor, styleNoRescale | styleNoTitle );
		Plot( L1d, "L1d", PriorLxColor, styleNoRescale | styleNoTitle );
		Plot( H2d, "H2d" , PriorHxColor, styleNoRescale | styleNoTitle );
		Plot( L2d, "L2d", PriorLxColor, styleNoRescale | styleNoTitle );
		Plot( HH10d, "HH10d", PriorHxColor, styleNoTitle );
		Plot( LL10d, "LL10d", PriorLxColor, styleNoRescale | styleNoTitle );
		Plot( HH30d, "HH30d", PriorHxColor, styleNoRescale | styleNoTitle );
		Plot( LL30d, "LL30d", PriorLxColor, styleNoRescale | styleNoTitle );
		Plot( HH90d, "HH90d", PriorHxColor, styleNoRescale | styleNoTitle );
		Plot( LL90d, "LL90d", PriorLxColor, styleNoRescale | styleNoTitle );
		Plot( HH270d, "HH270d", PriorHxColor, styleNoRescale | styleNoTitle );
		Plot( LL270d, "LL270d", PriorLxColor, styleNoRescale | styleNoTitle );

	}

	// Label the lines on the chart.
	FontSize = 8;
	LVVBI = LastVisibleValue( BarIndex() );

	if( Intrvl < inDaily )
	{
		DayOOffset = LastVisibleValue( BarsSince( DateNum() != Ref( DateNum(), -1 ) ) );
		DayHOffset = LastVisibleValue( BarsSince( H == DyH ) );
		DayLOffset = LastVisibleValue( BarsSince( L == DyL ) );
		FirstVisibleBarOfDay = LVVBI - DayOOffset;

		PlotTextSetFont( "PCL", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( PCL ), colorWhite, PCLColor, -FontSize/2 );
		PlotTextSetFont( "O", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( DyO ), colorWhite, DayOColor, -FontSize/2 );
		PlotTextSetFont( "H", "Arial Black", FontSize, LVVBI - DayHOffset, LastVisibleValue( DyH ), DayHColor, colorDefault, (FontSize-6) );
		PlotTextSetFont( "L", "Arial Black", FontSize, LVVBI - DayLOffset, LastVisibleValue( DyL ), DayLColor, colorDefault, -(FontSize+2) );
	}
		
	if( Intrvl == inDaily )
	{
		H1d = IIf( ( LVVBI - BI ) <= 1, LastVisibleValue( H1d ), Null );
		L1d = IIf( ( LVVBI - BI ) <= 1, LastVisibleValue( L1d ), Null );
		H2d = IIf( ( LVVBI - BI ) <= 2, LastVisibleValue( H2d ), Null );
		L2d = IIf( ( LVVBI - BI ) <= 2, LastVisibleValue( L2d ), Null );
		HH10d = IIf( ( LVVBI - BI ) <= 10, LastVisibleValue( HH10d ), 0 );
		LL10d = IIf( ( LVVBI - BI ) <= 10, LastVisibleValue( LL10d ), 0 );
		HH30d = IIf( ( LVVBI - BI ) <= 30, LastVisibleValue( HH30d ), 0 );
		LL30d = IIf( ( LVVBI - BI ) <= 30, LastVisibleValue( LL30d ), 0 );
		HH90d = IIf( ( LVVBI - BI ) <= 90, LastVisibleValue( HH90d ), 0 );
		LL90d = IIf( ( LVVBI - BI ) <= 90, LastVisibleValue( LL90d ), 0 );
		HH270d = IIf( ( LVVBI - BI ) <= 270, LastVisibleValue( HH270d ), 0 );
		LL270d = IIf( ( LVVBI - BI ) <= 270, LastVisibleValue( LL270d ), 0 );
		
		PlotOHLC( H1d, H1d, H1d, H1d, "H1d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( L1d, L1d, L1d, L1d, "L1d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( H2d, H2d, H2d, H2d, "H2d" , PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( L2d, L2d, L2d, L2d, "L2d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( HH10d, HH10d, HH10d, HH10d, "HH10d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( LL10d, LL10d, LL10d, LL10d, "LL10d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( HH30d, HH30d, HH30d, HH30d, "HH30d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( LL30d, LL30d, LL30d, LL30d, "LL30d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( HH90d, HH90d, HH90d, HH90d, "HH90d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( LL90d, LL90d, LL90d, LL90d, "LL90d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( HH270d, HH270d, HH270d, HH270d, "HH270d", PriorHxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
		PlotOHLC( LL270d, LL270d, LL270d, LL270d, "LL270d", PriorLxColor, styleCandle | styleNoLine | styleNoRescale | styleNoTitle );
	}
	
	PlotTextSetFont( "H1d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( H1d ), colorWhite, PriorHxColor, -FontSize/2 );
	PlotTextSetFont( "L1d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( L1d ), colorWhite, PriorLxColor, -FontSize/2 );
	PlotTextSetFont( "H2d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( H2d ), colorWhite, PriorHxColor, -FontSize/2 );
	PlotTextSetFont( "L2d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( L2d ), colorWhite, PriorLxColor, -FontSize/2 );
	PlotTextSetFont( "HH270d", "Arial Black", FontSize, LVVBI+3, LastVisibleValue( HH270d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL270d", "Arial Black", FontSize, LVVBI+3, LastVisibleValue( LL270d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH90d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH90d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL90d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL90d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH30d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH30d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL30d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL30d ), colorWhite, PriorLxColor, -(FontSize+2) );
	PlotTextSetFont( "HH10d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( HH10d ), colorWhite, PriorHxColor, (FontSize-6) );
	PlotTextSetFont( "LL10d", "Arial Black", FontSize, LVVBI+2, LastVisibleValue( LL10d ), colorWhite, PriorLxColor, -(FontSize+2) );

1 Like

This is incorrect use of TimeFrame functions.
Apply TimeFrameExpand after restoring (yes, you have corrected it. I'm just specifying mistake for others).


Below is my code and it is much faster (90% faster) and shorter then the one of post #1.
NOTE: if you are on 1-min TF and look back 270 days then you will load at maximum 1440 x 270 = 388,800 bars (one day has maximum 1440 minutes).

(Additional note: I have exclude last day from highest/lowest determination as that's what you seem to try to achieve in addition).

/// @link https://forum.amibroker.com/t/need-ideas-to-speed-up-plotting-of-prior-intraday-data/19519/5
/// AFL by fxshrat@gmail.com feat. PeterD
/// Commercial use prohibited
procedure PrevSessionHighLow(session, days, L_name, H_name, shift) {
	/// @link https://tinyurl.com/y7hxahwd
	local i, n, field, shh, sll, size, tmfrm;
	for ( i = 0; i < 4; i++ ) {	
		field = StrExtract("O,H,L,C", i);
		VarSet(field, IIf(session, VarGet(field), Null));
	}
	//
	tmfrm = inDaily;
	shh = TimeFrameCompress(Ref(H,-shift), tmfrm, compressHigh);
	sll = TimeFrameCompress(Ref(Nz(L,1e10),-shift), tmfrm, compressLow);
	//	
	size = MxGetSize(days, 0);
	for ( i = 0; i < size; i++ ) {	
		n = days[i][0];
		VarSet(H_name+n, TimeFrameExpand(HHV(shh, n), tmfrm, expandLast)); 
		VarSet(L_name+n, TimeFrameExpand(LLV(sll, n), tmfrm, expandLast)); 
	}
}

Version(6.10);

days = MxFromString("[1;2;10;30;90;270]");
dd = MxGetSize(days, 0);

tn = TimeNum();
intra_day = Interval() < inDaily;
if ( intra_day ) {	
	shift = 0;
	session = tn >= 93000 AND tn < 160000;
	session_start = session AND NOT Ref(session,-1);	
} else {
	shift = 1;
	session = 1;
	session_start = Day() != Ref(Day(), -1);	
}

// PLOT ALL PRICE (comment it if not needed)
all_bar = ParamList("All price mode", "styleBar|styleNoDraw");
Plot( C, "Price", colorDefault, VarGet(all_bar) );
//Plot(session_start, "", colorRed, styleHistogram | styleOwnScale);
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

PrevSessionHighLow(session, days, "sll", "shh", shift);

// PLOT SESSION PRICE ONLY
if ( intra_day )
	Plot( C, "Price", colorRed, styleBar, Null, Null, 0, 1 );

RestorePriceArrays();// Restore Price arrays #############################################################

fnt_size = 9;
offset = -fnt_size/2;
fnt = "Arial Black";
bi = BarIndex();
lvb = SelectedValue(bi);
daily_bars = inDaily/Max(1,Interval());
style = styleStaircase | styleDashed /*| styleNoRescale*/;
for ( i = 0; i < dd; i++ ) {
	n = days[i][0];
	sll = VarGet("sll"+n);
	shh = VarGet("shh"+n);
	plot_cond = bi >= lvb-n*daily_bars AND bi <= lvb;		
	Plot(IIf(plot_cond, sll[lvb], Null), "\nLL"+n, colorRed, style);
	Plot(IIf(plot_cond, shh[lvb], Null), "HH"+n, colorGreen, style);
	PlotTextSetFont("H"+n+"d", fnt, fnt_size, lvb+2, shh[lvb], colorWhite, colorGreen, offset);
	PlotTextSetFont("L"+n+"d", fnt, fnt_size, lvb+2, sll[lvb], colorWhite, colorRed, offset);
}

daysback = days[dd-1][0];
SetBarsRequired(daysback*daily_bars);

If you want to avoid rescaling of plot then modfiy style variable to
style = styleStaircase | styleDashed | styleNoRescale;

13

2 Likes

Thanks so much for your help! I'm digesting your code and learning some new techniques.

Yours turly,
Peter

Wow, that's quite a refactoring job! There are some great techniques in here for me to learn, and I appreciate your time, as it seems that you put a lot of time into this work on my behalf.

Coding truly is art and I love how you reduced the code length by about 60% while simultaneously improving performance. I'll be using these "secrets" for a long time to come, once I understand them better of course. I'm still digesting how this all works so this is an incredible learning experience for me.

Thank you for helping me learn the better ways of coding!

Yours truly,
Peter

2 Likes

Your're welcome.

Here is an update.
Added printf for output to Interpretation window and made some modifications in regards to shifting & few other minor changes.

/// @link https://forum.amibroker.com/t/need-ideas-to-speed-up-plotting-of-prior-intraday-data/19519/8
/// AFL by fxshrat@gmail.com feat. PeterD, version 1.1
/// Commercial use prohibited
procedure SessionHHVs_LLVs(session, period_mat, L_name, H_name, shift, expand_mode) {
	/// @link https://tinyurl.com/y7hxahwd
	local i, n, field, shh, sll, size, tmfrm;
	for ( i = 0; i < 4; i++ ) {	
		field = StrExtract("O,H,L,C", i);
		VarSet(field, IIf(session, VarGet(field), Null));
	}
	//
	tmfrm = inDaily;
	shh = TimeFrameCompress(H, tmfrm, compressHigh);
	sll = TimeFrameCompress(Nz(L,1e10), tmfrm, compressLow);
	//	
	size = MxGetSize(period_mat, 0);
	for ( i = 0; i < size; i++ ) {	
		n = period_mat[i][0];
		VarSet(H_name+n, TimeFrameExpand(Ref(HHV(shh, n),shift), tmfrm, expand_mode)); 
		VarSet(L_name+n, TimeFrameExpand(Ref(LLV(sll, n),shift), tmfrm, expand_mode)); 
	}
}

Version(6.10);
EnableTextOutput(0);

days = MxFromString("[1;2;10;30;90;270]");
dd = MxGetSize(days, 0);

tn = TimeNum();
intra_day = Interval() < inDaily;
shift = 0;// set NEG. number to shift backwards
if ( intra_day ) {	
	session = tn >= 93000 AND tn < 160000;
	session_start = session AND NOT Ref(session,-1);	
} else {
	session = True;
	session_start = Null;	
}
printf("Shift: %g\nInterval: %s\n", shift, Interval(2));

// PLOT ALL PRICE (comment it if not needed)
all_bar = ParamList("All price mode", "styleBar|styleNoDraw");
Plot( C, "", colorDefault, VarGet(all_bar), Null, Null, 0, -1);
//Plot(session_start, "", colorBlueGrey, styleHistogram | styleDashed | styleOwnScale, 0, 1, 0, -5);
SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle | chartDisableTooltips );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} O: %g, H: %g, L: %g, C: %g (%.1f%%), V: %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
// ##############################################################
SessionHHVs_LLVs(session, days, "sll", "shh", shift, expandLast);
// ##############################################################
// PLOT SESSION PRICE ONLY
if ( intra_day )
	Plot( C, "Price", colorBlueGrey, styleBar, Null, Null, 0, 0 );

RestorePriceArrays();// Restore Price arrays #############################################################

fnt_size = 8;
offset = -fnt_size/2;
fnt = "Arial Black";
bi = BarIndex();
lvb = SelectedValue(bi);
daily_bars = inDaily/Max(1,Interval());
formatstr = "%s%s: <b>%g</b>\t%s%s: <b>%g</b>%s\n";
style1 = styleStaircase | styleDashed | styleNoTitle | styleNoRescale;
style2 = styleHistogram | styleDashed | styleOwnScale;
for ( i = 0; i < dd; i++ ) {
	n = days[i][0];
	lstr ="L"+n+"d";	hstr ="H"+n+"d";		
	sll = VarGet("sll"+n);	shh = VarGet("shh"+n);	
	plot_cond = bi >= lvb-(n-shift)*daily_bars AND bi <= lvb;
	Plot(IIf(plot_cond, sll[lvb], Null), "\n"+lstr, colorRed, style1);
	Plot(IIf(plot_cond, shh[lvb], Null), hstr, colorGreen, style1);	
	PlotTextSetFont(hstr, fnt, fnt_size, lvb+2, shh[lvb], colorWhite, colorGreen, offset);
	PlotTextSetFont(lstr, fnt, fnt_size, lvb+2, sll[lvb], colorWhite, colorRed, offset);
	printf(formatstr, EncodeColor(colorRed), lstr, sll, EncodeColor(colorGreen), hstr, shh, EncodeColor(-1));	
}
if ( shift != 0 ) // plot vertical line if shifting 	
	Plot(bi == lvb+daily_bars*shift, "", colorRed, style2, 0, 1, 0, -5);

daysback = days[dd-1][0];
SetBarsRequired(daysback*daily_bars);
//

13

6 Likes

Thank you once again for your help! I'll review this newer version and incorporate your techniques into my coding.

Yours truly,
Peter