Get date when HHV(High, 252) occurred

Couple of questions

  1. How do i get accurate date when high of 252 days occurs. I have tried few different approaches using value when and Ref(DateTime()) along with bar index. Either shows date of last bar or garbage values or any date in 1990
  2. What is the best way to compare dates, one date is less than or greater than another
// Test backup

// Best stocks to buy for consolidation breakout

dt = DateTime();

BARH  = HHV( Close, 252 );
BARL = LLV( Close, 252 );

//dtH = ValueWhen( BARH, dt, 1 );
//dtL = ValueWhen( BARL, dt, 1 );
HighDate = ValueWhen( HHV( Close, 252 ), DateTime(), 1 );
LowDate = ValueWhen( LLV( Close, 252 ), DateTime(), 1 );



Filter = StrToNum( GetFnData( "Alias" ) ) < 500;

AddColumn( Close, "Close" );
AddColumn( MA(Close, 5), "5DMA" );
AddColumn( MA(Close, 20), "20DMA" );
AddColumn( MA(Close, 50), "50DMA" );
AddColumn( MA(Close, 100), "100DMA" );
AddColumn( MA(Close, 200), "200DMA" );
AddColumn( BARH, "52 Week High" );
AddColumn( HighDate, "DT last HHV", formatDateTime, -1, -1, 150 );
//AddColumn( HighDate, "DT last HHV");
AddColumn( BARL, "52 Week Low" );
AddColumn( LowDate, "DT last LLV", formatDateTime, -1, -1, 150 );
//AddColumn( LowDate, "DT last LLV");

Plenty examples of this already exist on the forum.

dt = DateTime();
highDate_dt = ValueWhen(High == HHV(High, 252), dt);

See Date Time Diff.

2 Likes

That is not proper code to get the date of when HHV occurred.

Rather you should use existing function HHVbars and Ref().

dt = Datetime();
HighDate = Ref(dt, -HHVBars(H,252));
4 Likes

@erukumk,

Depending on what result you want returned please note the differences in methods.

When the data quote makes an equal price to the ‘HHV’:

  • 'Value When' - the latest occurrence date is returned
  • 'Ref with HHVBars' - the first occurrence date is returned

image

1 Like

@TrendSurfer

Your conclusion is not correct.

With HHVbars you simply get the date of the highest high value within the defined bar range.

With the Valuewhen line you may not get that all the time.
You may get HHV of long long time ago exceeding the defined bar range.
So it is wrong conclusion that you get latest high of defined range every time.

E.g. in a downward trend you don't.
See the sample chart below.
Yellow circle shows the highest high of chart range (using ValueWhen)
But that's not what is looked for. It is out of the defined look back range of e.g. 252 bars.

On the other hand the example of red circle shows the actual highest high of 252 bars range.

Just look yourself and apply the code

highDate_dt = ValueWhen(High == HHV(High, 252), H);
Plot( highDate_dt, "highDate_dt", colorDefault, styleLine );

HighDate = Ref(H, -HHVBars(H,252));
Plot( HighDate, "HighDate", colorRed, styleLine );

2 Likes

Ok - thanks for pointing that out.

Why is the HHV date 2012? Thanks

BARH  = HHV( Close, 252 );
BARL = LLV( Close, 252 );

//dtH = ValueWhen( BARH, dt, 1 );
//dtL = ValueWhen( BARL, dt, 1 );
//HighDate = ValueWhen( HHV( Close, 252 ), DateTime(), 1 );
//LowDate = ValueWhen( LLV( Close, 252 ), DateTime(), 1 );

//highDate_dt = ValueWhen(High == HHV(High, 252), H);
//Plot( highDate_dt, "highDate_dt", colorDefault, styleLine );

HighDate = Ref(H, -HHVBars(H,252));
LowDate = Ref(H, -LLVBars(L,252));

//Plot( HighDate, "HighDate", colorRed, styleLine );

//Filter = StrToNum( GetFnData( "Alias" ) ) < 500;
Filter = 1 ; 

AddColumn( Close, "Close" );
/*
AddColumn( MA(Close, 5), "5DMA" );
AddColumn( MA(Close, 20), "20DMA" );
AddColumn( MA(Close, 50), "50DMA" );
AddColumn( MA(Close, 100), "100DMA" );
AddColumn( MA(Close, 200), "200DMA" );
*/
AddColumn( BARH, "52 Week High" );
AddColumn( HighDate, "DT last HHV", formatDateTime, -1, -1, 150 );
//AddColumn( HighDate, "DT last HHV");
AddColumn( BARL, "52 Week Low" );
AddColumn( LowDate, "DT last LLV", formatDateTime, -1, -1, 150 );
//AddColumn( LowDate, "DT last LLV");

Because you have to insert datetime to first argument of Ref instead of H !!

I was using High array only for the purpose of pointing out the Highest values in the graphs. That's all.

1 Like

How do I display something like this with the correct date

highDate = ValueWhen(High == HHV(High, 252), H);
lowDate = ValueWhen(Low == LLV(Low, 252), L);


HighDate = Ref(H, -HHVBars(H,252));
LowDate = Ref(H, -LLVBars(L,252));

I tried these 2 Afl, but still wrong. Thank's

Have you read my previous reply carefully.

Insert Datetime array to first argument of Ref function.

dt= DateTime();
HighDate = Ref(dt, -HHVBars(H,252));
LowDate = Ref(dt, -LLVBars(L,252));
1 Like

Thank Mr Fxshrat for your help and you are the best at AFL Coding and Scripts :+1:

You need to be very clear on the condition you are trying to detect. Most people would agree that a new 252-day high means a high price that is higher than the previous 251 days. A new N-Day high can be identified on the day that it occurs, and there is no way to tell how long it will be until another N-Day high occurs.

The solution provided by @TrendSurfer will find the date of the most recent new 252-day high, even if that event occurred a very long time before the reference bar. The solution provided by @fxshrat will find the date of the highest high in the prior 252 days, but that highest high may or may not be a 252-day high. Look carefully at his chart showing a symbol in a long downtrend. He identifies the bar that is the highest high, but as he explained in his post that high is not a new 252-day high.

7 Likes

That is the exact reason why I always say to people: describe the goal, don't focus on tiny details : How to ask a good question

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.