Coding for Count days from New Highs

Hi everybody,
I've been using Amibroker for exploring stocks, and this software helps me so much,
I'm looking for a code that helps me know how many days since stocks reach New High,
For example, I want to know these New High were either 3 months, 6 months or more (display automatically in exploring screen without look back the charts or count by eyes)
Count%20days

I'm looking forward to your help,
Thanks so much :slight_smile:

Plot and explore.

a = Cross(C,Ref(HHV(H,100),-1));  // change 100 to whatever you want.
aa = BarsSince(a);
Filter = V*C>1000000;
AddColumn(C,"Close");
AddColumn(aa,"bars since last cross of 100 day High"); 
PlotShapes ( IIf ( a , shapesmallcircle , shapeNone ), colorWhite , 0 , c +Ref(ATR(1),-1)*1 , 0 );

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();
2 Likes

@quangtrung1789 I interpreted your question a little differently than @C_M did and I think that possible answers can be found in this previous thread,

1 Like

@quangtrung1789 I think that @Tomasz code was returning the value of the High and not the number of bars since there was a higher high (just looking at the Plot and the values returned). So I tried a little modification of his code and it seems to return the number of bars since there was a higher high.

// modification of Tomasz code to get Number of Bars Since Higher High
///@link https://forum.amibroker.com/t/bars-since-higher-or-equal-value/4829/16

function BarsSinceHigherValueSelectedVal( SrcArray )
{
	bi = BarIndex();
	sbi = SelectedValue( bi );
	
	hs = Reverse( HighestSinceBars( Reverse( bi == sbi ), Reverse( SrcArray ) ) ) ;
	
	hs = Ref( bi - ValueWhen( hs == 0, bi ), -1 ) + 1;
		
	shs = SelectedValue( hs );
	
	return IIf( bi >= sbi - shs AND bi <= sbi, hs[ sbi ], Null );
}
// original was not returning the bars since, 
// but the actual value of the High (or chosen array)??


// an exploration for debugging, 
// suggest trying different "end dates" in the analysis window to illustrate what is being calculated

//Filter = Status( "lastbarinrange" );
Filter=1;
AddColumn(High, "High");
AddColumn(BarsSinceHigherValueSelectedVal( High ), "bars since higher", 1.0);

image

5 Likes