How to fetch the number of bars between two particular time intervals

Hi ,
I am fetching the number of bars between two particular time intervals.

dt= DateTime();
newday = dn != Ref( dn, -1);
StartTn = ValueWhen(newday, dt, 1);
EndTn =DateTime();
NumberOfBarsBetweenStartTnAndEndTn = DateTimeDiff(EndTn, StartTn)/5; // using 5 minute chart

The last line variable(NumberOfBarsBetweenStartTnAndEndTn ) is not getting the number of bar count values.
Can any one please let me know where i did the mistake in the code.

Thanks in advance.

Kindly search the forum, your recent topics have all been covered in various threads.

"Count bars in a period" search with these phrases there are plenty of posts.

1 Like

Thanks @travick.. In the forum @portfoliobuilder given the idea of get the number of bars in Daily interval using LookUp function by converting values to _DT .But in my case i want to do in intraday. So can you tell me how to convert StartTn,EndTn variables(which are in DateTime format) of my code to make use of LookUp function.
Thanks in advance.

Even in that case, this is ambiguous isn't it ?
The end time should be a specific bar so that you can retrieve a specific DateTime.

What do you intend it to be ?

Hi @travick ...

I am a beginner in AFL. My intention is to count the number of bars up to now, in a given intraday . Suppose at 11:00 AM if i run the afl, then it should display number of bars so far created from starting of the day .

Thanks in advance

nd = Day() != Ref( Day(), -1);    // New day condition
bs = LastValue( BarsSince( nd ) ) + 1; // +1 for current bar

printf( "%g", bs);

sometimes, you should explain a bit more clearly "what" your goal is apart from trying to explain your code. It could be as simple as this one.

1 Like

Hi @travick.
Please find the attachment.
I am looking for each and every time interval how many bars existed up to present(please see the expected column in the image). But the variable "bs" gives the total count. I am going to use this count in for loop . Yes of course arrays are much faster than loops. I am not using the barcount.

tn= TimeNum();
dn = DateNum();
dt= DateTime();
bi = BarIndex();
newday = dn != Ref( dn, -1);
StartBar = ValueWhen(newday, tn, 1);
EndBar = tn;

Condition1 = StartBar ==tn ;
Condition2 = EndBar ;
StartBI= ValueWhen( Condition1,bi,1); 
EndBI =  ValueWhen(Condition2,bi,1); 

barsbetween = EndBI-StartBI;

I am not able to use the "barsbetween " variable in for loop because it is a array.Can you let me know how to find count .So that i can use that in for loop.

Thanks in advance.

nd = Day() != Ref( Day(), -1);    // New day condition
bs = BarsSince( nd ) + 1; // +1 for current bar

Filter = 1;
AddColumn( bs, "Bars since nd", 1.0 );

Then just remove the LastValue(). Post your results and this time use the Exploration with enough bars in Range. You will get the desired output.

I used Printf() to display only the last value.


Hi @travick .
Now i am able to fetch as expected.Thanks for your support.But i am not able to use the "bs" in for loop because it is an array. How to fetch the value as a number .

for ( i =1;i<=bs ;i++)
      Condition =some condition;
      price =  price+ValueWhen(Condition,C,i);

Thanks in advance.

You have to use the Array subscript.

bs // This is of type Array,
bs[x] // where 0 <= x < BarCount

// in this case use lastvalue
lbs = LastValue( bs );
for ( i =1; I <= lbs; i++)

// OR
bc = BarCount - 1;
for ( i =1; I <= bs[bc]; i++)

// OR bc can be some value of your interest less than BarCount

You should state what your are really trying to do,
if you have a lot of iterations, using Array functions is detrimental to performance.
See ValueWhen() in that loop, among other things like some condition.

Hi all,

I am exploring some code snippet. My intention is to fetch today's and yesterday's close prices in exploration window.Using the below code.

dt= DateTime();
dn = DateNum();
newday = dn != Ref( dn, -1);
today = DateTime();
one_day_old=  DateTimeAdd( today , -1, inDaily );
Startdt = ValueWhen(newday, dt, 2);

Condition = one_day_old== DateTime(); 


But i am not able to fetch the yesterday's values.Please let me know what is wrong in the condition.


Thanks in advance.

You just need to reference yesterday's close, see Ref Function.

If you're running on intraday bars (which it appears you are), you might want to look at TimeFrameGetPrice():

1 Like

@fxshrat can you please guide me.

@dav perhaps you should try implementing the suggestions you've already been given instead of asking yet another forum member to write code for you. The TimeFrameGetPrice() function will get you what you want with ONE CALL.

1 Like