Counting bars in a given period(s)

Hi there,
i am a complete noob when it comes to afl, and I hope you’re willing to help.
I am trying to count the number of bars between 2 different point and then print said number . to make it simple , i would like to count number of bars in a given year e.g , 2015 - 2016 ; 250 bars ; 2016-2017 ; 252 bars , 2017 -ytd ; 187 bars ;
I hope you don’t mind such basic question from newbie.
thank you.

``````barsInYear = BarsSince( Year()!= Ref(Year(),-1));
for(i=0; i < 20; i++){
printf("days %g\n",barsInYear[i]);
}
``````

output

``````days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
days -1e+010
``````

Execute below example in analysis

``````/// @link http://forum.amibroker.com/t/counting-bars-in-a-given-period-s/1064/2

fbr = Status( "firstbarinrange" );// first bar of analysis range flag
newyear = Year() != Ref(Year(), -1 );

sumdays = BarsSince( newyear OR fbr ) + 1;

if( Interval() == inDaily ) {
Filter = Ref( newyear, 1 ) OR Status( "lastbarinrange" );
} else
Error( "Set periodicity to Daily in Backtester Settings: General - Periodicity!" );
``````
6 Likes

As with fxshrat's idea, I too suggest you run Exploration in the analysis window. If you were looking for bars between any dates, and not just year-end here are a couple of alternative codes (I think the basic idea was discussed on the old forum but I don't have a record of where for proper attribution).

Also, run a couple of tests to make sure the count is correct as I just did a quick check and it looks right.

``````// Bars Between Dates v1
// Count the bars between "Start" and "End", not including the Start bar
// Go to Analysis Window, Explore
// Only need to set Exploration Range box to "1 recent bar" or "1 recent day"

bi = BarIndex();

StartDate = "2015-01-15";  // yyyy-mm-dd
EndDate = "2017-06-23";
StartBI = Lookup( bi, _DT( StartDate ));
EndBI = Lookup( bi, _DT( EndDate ));

barsbetween = EndBI-StartBI;

//SetSortColumns(-3);

Filter=1;
//AddColumn(bi, "bar index", 1.0, colorWhite, colorBlue);
``````

The above example gives this kind of result, notice it doesn't mater what dates are in the Analysis window, or that I ran the analysis today. The calculations are run on the dates in the afl and thus the dates are hard coded (and of course you can put any dates you want into the afl).

5 Likes

Second version, attempting to count the number of bars between chosen dates.

``````// Bars Between Dates v2.
// Barcount via AA window
// Getting any barcount by selecting any range setting in AA window.
// in Analyis window, Set in RANGE the "From-To dates" option
// Counts the bars between "Start" and "End", not including the Start bar

fbr  = Status( "firstbarinrange" );
lbr  = Status( "lastbarinrange" );
bi   = BarIndex();
bars = ValueWhen( lbr, bi ) - ValueWhen( fbr, bi );

Filter = lbr;
AddColumn( bars, "# Bars Between", 1.0);

``````

Output looks like this, and the dates are the dates chosen in the analysis window. So they ARE important in this example as that's where the code finds them.

4 Likes

The fastest method is single line:

``````range = LastValue( Cum( Status("barinrange" ) ) );
``````
12 Likes

first of all thank you for the replies and assistance, I apologize for not replying sooner. thank you to Fxshrat,portofoliobuilder, Tomaz.
I had not checked back until today. again sincere apologies.