What's wrong with this code to count high-volume bars in a range?

I would like to count the number of high-volume bars within a range and display the result.

Below is my AFL code.

function count_high_volume_bars(starting_bar, ending_bar)
{
	high_volume = IIf( Volume >= MA(Volume, 50), True_part=True, False_part=False);
	high_volume_in_range = Ref(high_volume, -(LAST_BAR - ending_bar) );
	
	if (ending_bar < starting_bar)
	{
		num_bars_in_range = 0;
	}
	else if (ending_bar >= starting_bar)
	{
		num_bars_in_range = ending_bar-starting_bar+1;
	}

	num_high_volume_bars = Sum(high_volume_in_range, num_bars_in_range );
	
	return num_high_volume_bars;
}

START_BAR = BeginValue( BarIndex() );
SELECTED_BAR = SelectedValue( BarIndex() );
number_of_high_volume_bars_in_range = count_high_volume_bars(START_BAR, SELECTED_BAR)

Something is not right as I am not able to get the right result. Can someone help spot what is wrong? I looked at the code a few times and I can't spot what went wrong.
Thank you.

bi = BarIndex();

function count_high_volume_bars(starting_bar, ending_bar)
{
	/// @link https://forum.amibroker.com/t/whats-wrong-with-this-code-to-count-high-volume-bars-in-a-range/12813/2
	high_volume = Volume >= MA(Volume, 50);
	bi_cond = bi >= starting_bar AND bi <= ending_bar AND ending_bar > starting_bar;
	result = IIf( bi_cond, Cum(IIf(bi_cond, high_volume, 0 )), Null);
	// OR
	//result = Cum(IIf(bi_cond, high_volume, 0));
	return result;
}

START_BAR = BeginValue( bi );
SELECTED_BAR = EndValue( bi );// SelectedValue(bi);//
number_of_high_volume_bars_in_range = count_high_volume_bars(START_BAR, SELECTED_BAR);

Plot( V, "Volume", colorYellow );
Plot(  MA(Volume, 50), " MA(Volume, 50)", colorRed );
Plot( number_of_high_volume_bars_in_range, "Cum(high_volume_bars)", colorOrange, styleHistogram | styleOwnScale, Null, Null, 0, -1, -60 );

So in below picture it has been four bars within range (between green and red range markers) having V >= MA(V,50) and indeed it is shown end result 4 in title.

6

2 Likes

Your text and function names are a bit miselading..
You say count bars then result sums volume...

So here in addition to counting bars is counting volume...

bi = BarIndex();
high_volume = Volume >= MA(Volume, 50);

function count_high_volume_bars(starting_bar, ending_bar)
{
	/// @link https://forum.amibroker.com/t/whats-wrong-with-this-code-to-count-high-volume-bars-in-a-range/12813/2
	bi_cond = bi >= starting_bar AND bi <= ending_bar AND ending_bar > starting_bar;
	result = IIf( bi_cond, Cum(IIf(bi_cond, high_volume, 0 )), Null);
	// OR
	//result = Cum(IIf(bi_cond, high_volume, 0));
	return result;
}

function sum_high_volume(starting_bar, ending_bar)
{
	/// @link https://forum.amibroker.com/t/whats-wrong-with-this-code-to-count-high-volume-bars-in-a-range/12813/2
	bi_cond = bi >= starting_bar AND bi <= ending_bar AND ending_bar > starting_bar;
	result = IIf( bi_cond, Cum(IIf(bi_cond AND high_volume, V, 0 )), Null);
	// OR
	//result = Cum(IIf(bi_cond AND high_volume, V, 0 ));
	return result;
}


START_BAR = BeginValue( bi );
SELECTED_BAR = EndValue( bi );// SelectedValue(bi);//
number_of_high_volume_bars_in_range = count_high_volume_bars(START_BAR, SELECTED_BAR);

high_volume_in_range = sum_high_volume(START_BAR, SELECTED_BAR);

Plot( V, "Volume", colorYellow );
Plot( MA(Volume, 50), " MA(Volume, 50)", colorRed );
Plot( number_of_high_volume_bars_in_range, "Cum(high_volume_bars)", colorOrange, styleNoDraw, Null, Null, 0, -1, -60 );
Plot( high_volume_in_range, "Cum(high_volume)", colorGreen, styleHistogram | styleLeftaxisScale, Null, Null, 0, -1, -60 );

39

5 Likes

Hi @fxshrat,

You are inspiringly very fast... Posting for the sake of pasting only as just turned to write it and returned to see your complete solution. :smiley:

//https://forum.amibroker.com/t/whats-wrong-with-this-code-to-count-high-volume-bars-in-a-range/12813
global bi;
bi = BarIndex();

function fHghVolCnt( BarA, BarB, VolAvgPer ) {
	 return Cum( IIf( BarB >= BarA && bi >= BarA && bi <= BarB && V >= MA( V, VolAvgPer ), 1, 0 ) );
}

_SECTION_BEGIN( "Counting Number of High-Volume Instances" );
	 vPer = Param( "Avg. Vol. Period", 50, 5, 252, 1 );
	 
	 StBar = BeginValue( bi );
	 SlctdBar = SelectedValue( bi );
	 
	 HghVolCnt = IIf( !StBar, 0, fHghVolCnt( StBar, SlctdBar, vPer ) );
	 
	 Plot( V, "Vol", colorBlueGrey, styleHistogram, Null, Null, 0, 0, 10 );
	 Plot( MA( V, vPer ), "Vol",  ColorRGB( 70, 160, 255 ), styleThick );
	 
	 Title = "High Vol. Bar Count: " + NumToStr( HghVolCnt, 1.0 );
_SECTION_END();
1 Like

@fxshrat, Thank you very much. You are a helpful genius. An asset to this Amibroker community!

1 Like