Array version of how many OHLC fall into a range in certain pervious bars

I would like to calculate how many O, H, L, C fall into a specific range in a certain number of previous bars. I use the for loop to do it:


lookback = 50;

ATR_range_up = O + ATR(lookback)/2;
ATR_range_dn = O - ATR(lookback)/2;
condense_zone = Null;

for( i = lookback; i < BarCount; i++)
{
	condense_zone_total = 0;
	
	for( j = i - lookback; j < i; j++)
	{
		condense_zone_temp = 0;
		
		if (Open[j] > ATR_range_dn[i] AND Open[j] < ATR_range_up[i])
			condense_zone_temp = 1;
		if (High[j] > ATR_range_dn[i] AND High[j] < ATR_range_up[i])
			condense_zone_temp = 1;
		if (Low[j] > ATR_range_dn[i] AND Low[j] < ATR_range_up[i])
			condense_zone_temp = 1;
		if (Close[j] > ATR_range_dn[i] AND Close[j] < ATR_range_up[i])
			condense_zone_temp = 1;
		
		condense_zone_total = condense_zone_total + condense_zone_temp;	
			
	}

	condense_zone[i] = condense_zone_total;

}

condense_zone_happen = condense_zone > 30;//

PlotShapes(shapeStar*condense_zone_happen ,colorBlack,0,H,10)

As double for loop involved, it is very slow. Any hints that I can convert it into array? Or it is impossible? My problem right now is I am not able to determine if array version is possible or not - should I continue or just leave it as is.

Thanks!!

This is my attempt.

lookback = 50;

ATR_range_up = O + ATR(lookback)/2;
ATR_range_dn = O - ATR(lookback)/2;
condense_zone = Null;

condense_zone_temp = (O > ATR_range_dn AND O < ATR_range_up) OR
   (H > ATR_range_dn AND H < ATR_range_up) OR
   (L > ATR_range_dn AND L < ATR_range_up) OR
   (C > ATR_range_dn AND C < ATR_range_up);
   
condense_zone = Sum( condense_zone_temp ,lookback );   

condense_zone_happen = condense_zone > 30;//

PlotShapes(shapeStar*condense_zone_happen ,colorBlack,0,H,10);

I'm trying it the way you have written but if you mean something else like High AND Low bar should be within the ATR range, then the code would be different but that is not what you have posted.

Thanks for your kind reply. Your version is not the same as mine - as I am finding sum of the previous bars(50 in this case) that falling within the ATR range in the current bar. I am thinking "valuewhen", but have not yet figured it out.

But still million thanks for your reply.

This one should do it

lookback = 50;

ATR_range_up = O + ATR(lookback)/2;
ATR_range_dn = O - ATR(lookback)/2;
   
condense_zone = 0;
for( i = 1; i <= lookback; i++ ) {	
	op = Ref(O,-i);	hi = Ref(H,-i);	lo = Ref(L,-i);	cl = Ref(C,-i);
	condense_zone_temp = (op > ATR_range_dn AND op < ATR_range_up) OR
					   (hi > ATR_range_dn AND hi < ATR_range_up) OR
					   (lo > ATR_range_dn AND lo < ATR_range_up) OR
					   (cl > ATR_range_dn AND cl < ATR_range_up);
	condense_zone += condense_zone_temp;
}

condense_zone_happen = condense_zone > 30;
Plot( C, "Price", colorDefault, styleBar );
PlotShapes(shapeStar*condense_zone_happen ,colorBlack,0,H,10);
8 Likes

@fxshrat,
Your code is always so clear and concise that it not only solves the problem but also serves as a coding tutorial. Thank you.

Greg

2 Likes

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