How to get stocks that doubled price since 52 week low

Hi All,

I am new to the Forum. Need help with the following exploration requirement:

  1. Filter equals ticker/stock doubled closing price since 52 weeks low in the selected date range
  2. Columns include
  • default exploration columns
  • 52wkLow ClosePrice
  • 52wkLow Date
  • ClosePrice Highest high Since 52wk low
  • Date Highest high Since 52wk low
  • PercentageChange 52wkLow and highest high 52 wk low

For example: if the selected date range is 1-1-2020 to till date and the market was bearish in Mar 2020 so Ticker which has doubled price since Mar/Apr 2020 52wk low will be shown with the above columns.

Any help would be much appreciated.
Cheers
Bobby

@_Bobby welcome to AmiBroker.

You need to be more specific and to make an effort to code your solution. For example you need the 52 week low. Are you using Weekly data bars? Daily data bars? 15 minute data bars? etc

A couple of examples,

// if using Weekly bars
Low52 = LLV(L, 52);

But if using daily bars, then you use an estimate for how many trading bars in in one year.

// if using Daily bars, then 52 weeks is ~ 252 trading days
Low52 = LLV(L, 252);

You've posted a couple of questions but made no effort on your own to solve them. You will find many helpful users here on the forum but they usually only offer to help someone who has shown an interest in learning how to use AmiBroker. Read this post slowly and carefully. Make some attempts and if you need some help then post your codes.

6 Likes

Thanks for looking into my requirement and sharing your thoughts.
Please find below my attempt at doing the above. However, the result is incorrect for (52 wk high/low date and hasPriceDoubledAtLeastOnce). Please advise?

_SECTION_BEGIN( "stocks-that-doubled-at least once-since-52-week-low in a year" );

tradingDays = 252; // trading days in a year

fbr = Status( "firstbarinrange" );
lbr = Status( "lastbarinrange" );
dt = DateTime();

startDate = ValueWhen( fbr, dt );
endDate = ValueWhen( lbr, dt );

end_yr = 2020 ;
start_yr = 2019;

_SECTION_END();

if( Status( "action" ) == actionExplore )
{    
    Filter = lbr;
    AddColumn( Close, "Close", format = 1.2 );    
    AddColumn( startDate, "startDate", formatdateTime );
    AddColumn( endDate, "endDate", formatdateTime );
    
    for ( yr = start_yr; yr <= end_yr; yr++ ){	
		yMultiplier = end_yr-yr+1;
		highY = HHV( H, yMultiplier * tradingDays );
		highYDate = ValueWhen( H == highY, DateTime(), 1 );		
		
		lowY = LLV( L, yMultiplier * tradingDays );
		lowYDate = ValueWhen( L == lowY, DateTime(), 1 );
		pctChangeSinceLowY = ( ( ( C - lowY ) / lowY ) * 100 );
		highSinceLowY = HighestSince( L == lowY, H );
		highSinceLowYDate = ValueWhen( H == highSinceLowY, DateTime(), 1 );
		
		AddColumn( highY, StrFormat("%g-52WH",yr),1.2);
		AddColumn( highYDate, StrFormat("%g-52WH Date",yr), formatdateTime );
		 
		AddColumn( lowY, StrFormat("%g-52WL",yr),1.2);
		AddColumn( lowYDate, StrFormat("%g-52WL Date",yr), formatdateTime ); 
		AddColumn( pctChangeSinceLowY, StrFormat("%g -pct chg From 52WL",yr), 1.2 ); 
				
		AddColumn( highSinceLowY, StrFormat("%g-highSince 52WL",yr) );
		AddColumn( highSinceLowYDate, StrFormat("%g-highSince 52WL Date",yr), formatdateTime );
		
		AddColumn( pctChangeSinceLowY>100, StrFormat("%g-hasPriceDoubledAtLeastOnce",yr) );
		
		_TRACE("## "+ yr);
    }    
}

Cheers
Bobby

These are all incorrect to flag highest/lowest bars.

You should not use equality check but appropriate functions LLVBars,HHVBar,HighestSinceBars.

_SECTION_BEGIN( "stocks-that-doubled-at least once-since-52-week-low in a year" );
tradingDays = 252; // trading days in a year

fbr = Status( "firstbarinrange" );
lbr = Status( "lastbarinrange" );

dt = DateTime();

startDate = ValueWhen( fbr, dt );
endDate = ValueWhen( lbr, dt );

end_yr = 2020 ;
start_yr = 2019;
_SECTION_END();

if( Status( "action" ) == actionExplore )
{    
    Filter = lbr;

    AddColumn( Close, "Close", format = 1.2 ); 
    AddColumn( startDate, "startDate", formatdateTime );
    AddColumn( endDate, "endDate", formatdateTime );

    /// @link https://forum.amibroker.com/t/how-to-get-stocks-that-doubled-price-since-52-week-low/22224/3
    /// fix at
    /// @link https://forum.amibroker.com/t/how-to-get-stocks-that-doubled-price-since-52-week-low/22224/4
    for ( yr = start_yr; yr <= end_yr; yr++ ){	
		yMultiplier = end_yr-yr+1;
		period = yMultiplier * tradingDays; 
		
		highY = HHV( H, period );
		hhbars = HHVBars(H,period);
		highYDate = Ref(dt, -hhbars);		

		lowY = LLV( L, yMultiplier * tradingDays );
		llbars = LLVBars(L,period);
		lowYDate = Ref(dt, -llbars);	
		
		pctChangeSinceLowY = (((C - lowY ) / lowY ) * 100);
		
		is_ldt = dt == lowYDate;
		highSinceLowY = HighestSince(is_ldt, H );
		highSinceLowYDate = Ref(dt, -HighestSinceBars(is_ldt, H));
		
		AddColumn( highY, StrFormat("%g-52WH",yr),1.2);
		AddColumn( highYDate, StrFormat("%g-52WH Date",yr), formatdateTime );		 

		AddColumn( lowY, StrFormat("%g-52WL",yr),1.2);
		AddColumn( lowYDate, StrFormat("%g-52WL Date",yr), formatdateTime ); 
		AddColumn( pctChangeSinceLowY, StrFormat("%g -pct chg From 52WL",yr), 1.2 ); 			

		AddColumn( highSinceLowY, StrFormat("%g-highSince 52WL",yr) );
		AddColumn( highSinceLowYDate, StrFormat("%g-highSince 52WL Date",yr), formatdateTime );		

		AddColumn( pctChangeSinceLowY>100, StrFormat("%g-hasPriceDoubledAtLeastOnce",yr) );	

		_TRACE("## "+ yr);
    }    
}
1 Like

Thanks, fxshrat for your prompt response and help.
Your code fix worked nicely. Much appreciated your time and effort.

I one follow-up question.
Is there any way I can restrict the HHVBars/LLVBars to get bars in the context/loop year?
For example, the result above has columns 2019-52WL and 2020-52WL however since 2020-52WL is the lowest between both years hence both columns have the same values. What change can I make to get a 52-week low value for the column 2019-52WLin the above column?

Cheers
Bobby

Use ValueWhen as shown in your other thread.

_SECTION_BEGIN( "stocks-that-doubled-at least once-since-52-week-low in a year" );
tradingDays = 252; // trading days in a year

fbr = Status( "firstbarinrange" );
lbr = Status( "lastbarinrange" );
yr = Year();
dt = DateTime();

startDate = ValueWhen( fbr, dt );
endDate = ValueWhen( lbr, dt );

start_yr = 2019;
end_yr = 2020;
_SECTION_END();

if( Status( "action" ) == actionExplore ) {    
	Filter = lbr;

	AddColumn( Close, "Close", format = 1.2 ); 	
	AddColumn( startDate, "startDate", formatdateTime );
	AddColumn( endDate, "endDate", formatdateTime );

	/// @link https://forum.amibroker.com/t/how-to-get-stocks-that-doubled-price-since-52-week-low/22224/3
	/// fix at
	/// @link https://forum.amibroker.com/t/how-to-get-stocks-that-doubled-price-since-52-week-low/22224/6
	for ( i = start_yr; i <= end_yr; i++ ){	
		yMultiplier = 1;//end_yr-i+1;
		period = yMultiplier * tradingDays;		
		yr_cond = yr == i;
		
		highY= ValueWhen(yr_cond, HHV(H, period));
		hhbars = HHVBars(H,period);
		highYDate = ValueWhen(yr_cond, Ref(dt, -hhbars));		

		lowY = ValueWhen(yr_cond, LLV(L, period));
		llbars = LLVBars(L,period);
		lowYDate = ValueWhen(yr_cond, Ref(dt, -llbars));				

		pctChangeSinceLowY = (C - lowY) / lowY * 100;

		is_ldt = dt == SelectedValue(lowYDate);
		highSinceLowY= HighestSince(is_ldt, H );
		highSinceLowYDate = Ref(dt, -HighestSinceBars(is_ldt, H));

		AddColumn( highY, StrFormat("%g-52WH",i),1.2, -1, colorGrey40);
		AddColumn( highYDate, StrFormat("%g-52WH Date",i), formatdateTime );		 

		AddColumn( lowY, StrFormat("%g-52WL",i),1.2);
		AddColumn( lowYDate, StrFormat("%g-52WL Date",i), formatdateTime ); 
		AddColumn( pctChangeSinceLowY, StrFormat("%g -%%chg of 52WL",i), 1.2 ); 			

		AddColumn( highSinceLowY, StrFormat("%g-highSince 52WL",i) );
		AddColumn( highSinceLowYDate, StrFormat("%g-highSince 52WL Date",i), formatdateTime );		

		AddColumn( pctChangeSinceLowY>100, StrFormat("%g-PriceDoubled>0",i) );	

		_TRACE("## "+ i);
	}    
}
1 Like

Thanks, @fxshrat, much appreciated your help.
Based on your code changes I also changed the trading days and the results were more accurate.
tradingDays = BarsSince( yr_cond);

Cheers
Bobby

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