Get Date&Time for HHV

I want to have The exact date and Time for just example :smile:

HHV ( H , 20 )
LLV(L , 20 )

In Hour and day and week Timeframe :wink:

Few things first:

  1. Please read Multiple Time Frame support in AFL thoroughly - try out each and every examples;
  2. Get acquainted to the various Date/Time and TimeFrame functions from Categorized list of functions;
  3. Correctly differentiate Barindex() and BarCount.

Below I have written a sample code for depicting Hourly HHV( H, 20 ). You can use this as a reference or, if someone else interestingly, put forward a better technique, use that to figure out your other requirements.

_SECTION_BEGIN( "Higher TFs HHV/LLV" );
	 //Coded by Lennon for https://forum.amibroker.com/t/get-date-time-for-hhv/6494
	 SetChartOptions( 0, chartShowDates );
	 clrBk = ParamColor( "Chart Back Color", colorBlack );
	 SetChartBkColor( clrBk );
	 Plot( 100, "", clrBk, styleOwnScale | styleArea | styleNoLabel | styleNoTitle, -0.5, 100, 0, -5 );
	 FntSz = 8;
	 PlotTextSetFont( "", "Courier New", FntSz, BarCount - 1, 0, -1 );

	 per = Param( "HHV/LLV Periods", 20, 5, 252, 1 );
	 bi = BarIndex();
	 DT = DateTime();
	 frmtDT = "\n%d%b%y %H:%M:%S";
	 fvb = FirstVisibleValue( bi );
	 lvb = LastVisibleValue( bi );
	 currTF = Interval( 0 );
	 
	 if( currTF < inHourly )
	 {
		 TimeFrameSet( inHourly );
			 HrlyTFHigh = H;
			 HrlyHHV = HHV( HrlyTFHigh, per );
		 TimeFrameRestore();

		 HrlyTFHighExp = TimeFrameExpand( HrlyTFHigh, inHourly, expandLast );
		 HrlyHHVExp = TimeFrameExpand( HrlyHHV, inHourly, expandLast );
		 HrlyHHVBar = ValueWhen( HrlyTFHighExp == HrlyHHVExp, bi );
		 HrlyHHVCond = IIf( bi == HrlyHHVBar, HrlyHHVExp, Null );
		 HrlyHHVCond = IIf( HrlyHHVCond != Ref( HrlyHHVCond, -1 ), HrlyHHVCond, Null );
		 HrlyHHVCondBar = ValueWhen( HrlyHHVCond, bi );
		 Plot( HrlyHHVCond, "", colorGold, styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 0, 1 );
	 
		 for( i = fvb; i <= lvb; i++ )
		 {
			 if( NOT IsNull( HrlyHHVCond[ i ] ) )
			 {
				 PlotText( DateTimeFormat( frmtDT, DT[ i ] ) + "\n" + "Hourly HHV " + HrlyHHVExp[ i ], HrlyHHVCondBar[ i ] + 1, H[ i + 1 ], colorGold, clrBk, 55 );
			 }
		 }
	 }
	 else if( currTF == inHourly )
	 {
		 HrlyTFHigh = HHV( H, per );
		 HrlyHHVBar = ValueWhen( H == HrlyTFHigh, bi );
		 HrlyHHVcond = IIf( bi == HrlyHHVBar, H, Null );
		 Plot( HrlyHHVcond, "", colorGold, styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 0, 1 );
		 
		 for( i = fvb; i <= lvb - 1; i++ )
		 {
			 if( NOT IsNull( HrlyHHVCond[ i ] ) )
			 {
				 PlotText( DateTimeFormat( frmtDT, DT[ i ] ) + "\n" + "Hourly HHV " + HrlyTFHigh[ i ], HrlyHHVBar[ i ], H[ i + 1 ], colorGold, clrBk, 55 );
			 }
		 }
	 }
	 
	 Title = StrFormat( "{{DATE}} - Open %1.2f, High %1.2f, Low %1.2f, Close %1.2f", O, H, L, C );
	 Plot( C, "", colorDefault, styleCandle );
_SECTION_END();

Cheers!

Lennon

4 Likes

First, I am very happy to be among experts People like You .

It is just Exactly what I'm looking for.

Thank you :hibiscus: :hibiscus:

Thank you for those words.

But Deserthawk I am no expert. I am just a rookie for last 8-10 months. Questions like yours remind me of my very initial days of AmiBroker purchase about a year back.

One suggestion that I can give you would be to search extensively and try out the examples or other codes very patiently until you grasp the underlying concept.

This forum is a great place to learn AFL.

1 Like

I will follow your suggestion, and do my best :smile:

my friend IF I just need the last value of HHV and need to get this date and time By Exploration .
could you help me for that , thanks ahead

@Deserthawk this is one way to get what you are looking for:

periods = Param( "Period", 20, 5, 200, 5 );

dt = DateTime();

hh = HHV( H, periods );
nwhh = hh != Ref( hh, -1 );      // Detect new points where hhv is different from previous one
dthh = ValueWhen( nwhh, dt, 1 ); // Assign the date where the above condition was true

ll = LLV( L, periods );
nwll = ll != Ref( ll, -1 );      // Same logic as hh
dtll = ValueWhen( nwll, dt, 1 );

Filter = 1;
AddColumn( H, "High", 1.2 );
AddColumn( hh, "HHV", 1.2 );
AddColumn( IIf( nwhh, 1, Null ), "New HHV", 1 );
AddColumn( dthh, "DT last HHV", formatDateTime, -1, -1, 150 );

AddColumn( L, "Low", 1.2 );
AddColumn( ll, "LLV", 1.2 );
AddColumn( IIf( nwll, 1, Null ), "New LLV", 1 );
AddColumn( dtll, "DT last LLV", formatDateTime, -1, -1, 150 );

SetSortColumns( -2 ); // see most recent dates at top

LastHHVDate = LastValue( dthh );
LastLLVDate = LastValue( dtll );
_TRACE( "Last HHV date: " + DateTimeToStr( LastHHVdate ) );
_TRACE( "Last LLV date: " + DateTimeToStr( LastLLVdate ) );

See this useful KB article if you are not familiar with the ValueWhen() function to understand how it works.

To get the last value the I simply used the aptly named LastValue() function :smiley:

5 Likes

@beppe

Thanks a lot , really this Exactly what I need :heart_eyes:

1 Like

@Lennon
Modified your code… add "expandLast-1" & work exact same….
but problem is I don’t know what this " -1 " did….

if( currTF < inHourly )
 {
 ....
 ....
 HrlyTFHighExp = TimeFrameExpand( HrlyTFHigh, inHourly, expandLast-1 );
 HrlyHHVExp = TimeFrameExpand( HrlyHHV, inHourly, expandLast-1 );
 HrlyHHVCond = IIf( HrlyTFHighExp == HrlyHHVExp, HrlyHHVExp, Null );
 ....
 Plot( HrlyHHVCond, "", colorGold, styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 0, 1 );
 ....
 ....
 }
2 Likes

All "undocumented" values default to just expandPoint and that is what you should use, see: TimeFrameExpand expandPoint mode, was: expandLast-1 magic

2 Likes