How can I code 5 days high and low with TimeNum

Hi all, I want to code 5 days high and low , and use in 5 min trading. My coding as follows:

TimeFrameSet(inDaily);

FiveDayH = HHV(High, 4);
FiveDayL = LLV(Low, 4);

TimeFrameRestore();

FiveDayH = TimeFrameExpand(FiveDayH, inDaily);
FiveDayL = TimeFrameExpand(FiveDayL, inDaily);

Plot(FiveDayH,"Five Days High",colorOrange,styleLine|styleThick);
Plot(FiveDayL,"Five Days Low",colorAqua,styleLine|styleThick);


However, i found that the problem is i use the code at Heng Seng Futures, the opening time is from 9:15am, to 3:00am next day, so if just count the "day", it makes mistakes. So i think i should add timenum into the code, however, i tried but since not right, can anyone help me? here is my coding:

//Tradingtimeset
tn1 = TimeNum();
startTime = 091500;
endTime = 270000;
Tradingtime = tn1 >= startTime AND tn1 <= endTime;

//5DaysHigh & Low
TimeFrameSet(inDaily);

DayTwo = Ref(Tradingtime, -1);
DayThree = Ref(Tradingtime,-2);
DayFour = Ref(Tradingtime,-3);
DayFive = Ref(Tradingtime,-4);
DaySix = Ref(Tradingtime,-5);

FiveDays = DayTwo AND DayThree AND DayFour AND DayFive;

FiveDayH = Ref(High, FiveDays);
FiveDayL = Ref(Low, FiveDays);

TimeFrameRestore();

FiveDayH = TimeFrameExpand(FiveDayH, inDaily);
FiveDayL = TimeFrameExpand(FiveDayL, inDaily);

Plot(FiveDayH,"Five Days High",colorOrange,styleLine|styleThick);
Plot(FiveDayL,"Five Days Low",colorAqua,styleLine|styleThick);

TimeFrameExpand() function supports three expansion modes default being 0 - expandLast. As explained in the guide, 2 - expandPoint is irrelevant for this case. You might want to test which one of the other two (0 - expandLast or 1 - expandFirst) suits your need best.

After applying below example to your Chart, from Parameters toggle TF Expansion Type:

_SECTION_BEGIN( "n-days High and Low" );
	 if( Interval() < inDaily ) {
		 SetChartOptions( 1, chartShowDates );
		 nD = Param( "Lookback Period (N-days)", 5, 1, 21, 1 );
		 ExpTyp = ParamToggle( "TF Expansion Type", "expandLast|expandFirst", 0 );
		 
		 TimeFrameSet( inDaily );
			 nHHVComp = HHV( H, nD );
			 nLLVComp = LLV( L, nD );
		 TimeFrameRestore();
		 nHHV = TimeFrameExpand( nHHVComp, inDaily, ExpTyp );
		 nLLV = TimeFrameExpand( nLLVComp, inDaily, ExpTyp );
		 
		 Plot( C, "Price", colorDefault, styleCandle );		 
		 Plot( nHHV, "nHHV", colorOrange, styleThick );
		 Plot( nLLV, "nLLV", colorAqua, styleThick );
		 
		 Title =
		 StrFormat( "{{DATE}}\nO %1.2f, H %1.2f, L %1.2f, C %1.2f\nnHHV %1.2f nLLV %1.2f", O, H, L, C, nHHV, nLLV );		 
	 }
	 else Error( "Please set the interval of Chart to less than Daily." );
_SECTION_END();

Not required!

Please refer to How does the intraday-to-daily compression work?

Cougar, thx so much, really appreciate, just seems that the intraday setting in Database only 24 hour, i couldn't 2700, and if i type 0300, it still switch to next day at 0:00.

As per hkex.com, the mentioned Trading Hours shows no further activity beyond those sessions, so you can go ahead with below settings:

Not sure what you mean!

Please consider the below image taken from daily time-compression uses Exchange time or Local time section of How does the intraday-to-daily compression work?

In the above image, the only difference is that Filtering is set to Show 24 hours trading (no filtering). You would've chosen that option if you considered capturing all available quotes for charting or analysis (provided your Exchange (or datafeed vendor) facilitated any extended Market activity beyond the stipulated sessions).

So, in your case as far as the charts are concerned, the quotes from 00:00 till 03:00 of next day shall form intraday candles (or bars) visually on next day but the Daily Compression shall happen as per the Trading hours mentioned by you in Intraday Setting i.e. from 09:15 till 03:00 next day.

From Forumla Editor, Apply the below code on to a Blank Chart. The code will help you visualize, from which bar a new day starts and where it ends in Daily timeframe on the Base Intraday Interval (such as 5 min).

_SECTION_BEGIN( "Two Timeframe Candles" );
	 BaseIntrvl = Interval( 0 );
	 HghrIntrvl = Param( "Set Higher Timeframe (in hours)", 24, 1, 24, 1 ) * 3600; // Default is 24 * 3600 = 86400 seconds = inDaily (refer to https://www.amibroker.com/guide/afl/interval.html)
	 if( HghrIntrvl >= BaseIntrvl ) {
		 SetChartBkColor( colorBlack );
		 Plot( C, "Price", colorDefault, styleCandle );

		 bi = BarIndex();
		 HghrIntrvlBiStrt = TimeFrameExpand( TimeFrameCompress( bi, HghrIntrvl, compressOpen ), HghrIntrvl, expandFirst ); // Day's first bar in Base interval
		 HghrIntrvlBiEnd = TimeFrameExpand( TimeFrameCompress( bi, HghrIntrvl, compressLast ), HghrIntrvl, expandFirst ); // Day's last bar in Base interval
		 HghrIntrvlH = HighestSince( bi == HghrIntrvlBiStrt, H );
		 HghrIntrvlL = LowestSince( bi == HghrIntrvlBiStrt, L );
		 
		 GfxSetZOrder( -1 );
		 GfxSetCoordsMode( 1 );
		 for( i = 0; i < BarCount; i++ ) {
			 if( i == HghrIntrvlBiStrt[ i ] ) {
				 VarSet( "x1", i );
				 VarSet( "y1", O[ i ] );
			 }

			 if( i == HghrIntrvlBiEnd[ i ] ) {
				 GreenCandle = C[ i ] > VarGet( "y1" );
				 RedCandle = C[ i ] < VarGet( "y1" );

				 GfxSelectSolidBrush( 
					 IIf( GreenCandle, ColorRGB( 0, 28, 0 ),
                     IIf( RedCandle, ColorRGB( 28, 0, 0 ), colorGrey50 ) ) );
				 GfxRectangle( VarGet( "x1" ), VarGet( "y1" ), i, C[ i ] );

				 GfxSelectSolidBrush( colorDarkGrey );
				 GfxRectangle( ( VarGet( "x1" ) + i ) / 2 - 1, HghrIntrvlH[ i ], ( VarGet( "x1" ) + i ) / 2 + 1, IIf( GreenCandle, C[ i ], IIf( RedCandle, VarGet( "y1" ), C[ i ] ) ) );
				 GfxRectangle( ( VarGet( "x1" ) + i ) / 2 - 1, HghrIntrvlL[ i ], ( VarGet( "x1" ) + i ) / 2 + 1, IIf( GreenCandle, VarGet( "y1" ), IIf( RedCandle, C[ i ], O[ i ] ) ) );
			 }
		 }
	 }
	 else Error( "Cannot compress bars when Higher Timeframe is less than the Base interval." ); // Refer to https://www.amibroker.com/guide/h_timeframe.html
_SECTION_END();

/*_______________________________________________________________________________________________________________________
|This code is for AmiBroker Formula Language (AFL) learning (non-commercial) purposes only. Please do not copy this code|
|(or any other version of it) and paste it over on other forums or anywhere else on the Internet or in any other form	|
|without the AmiBroker Forum owner's consent (https://forum.amibroker.com/).                                            |
_______________________________________________________________________________________________________________________*/

// https://forum.amibroker.com/t/how-can-i-code-5-days-high-and-low-with-timenum/15643/5?u=cougar

Once the Intraday database settings are settled, you'll be able to correctly decipher N-day HHV-LLV as discussed earlier.

thx Cougar, i had read it, but it seems just work below 24 hours market, while i set 3:00 as the end of trading time, it still will switch to next trading day at 0:00

Setting
Chart

I tried "show 24 hours trading (9no filtering) ,but doesn't help. I only looking for any single trading day should start at 9:15, and end at 3:00 (next day), thx so much

@ken20111,

Are you using a Local Database?

Ideally your Data vendor's plugin should have taken care of the Exchange time! Anyways, try these settings and it must work as you want.

image

Thank so much Congar, it is exactly what i looking for, thanks for your patience to a rookie like me.