Previous Bar count

Hello everyone,
How can I get count of Bars of Previous Month or Year?, There are any Built in Function get that.
Thanks in advance.

Not tested, but something like this....

FirstDayOfYear = Year() != Ref(Year(), -1);
barsInYear = BarsSince(FirstDayOfYear) + 1;
PreviousYearBars = ValueWhen(FirstDayOfYear, Ref(barsInYear, -1));

@ahm.montaser, although there is no built-in function, you can still encapsulate the logic posted by @awilson in a custom one and make it flexible to use different "periods" taking an array as a parameter.

Here is an example; apply this exploration to a single/current symbol setting the periodicity to daily (or hourly, 15 minutes).

function getPrevPeriodBars( periodArray )
    local newPeriod, barsCounter, prevPeriodTradingBars;

    newPeriod = periodArray != Ref( periodArray, -1 ) OR BarIndex() == 0; // use the OR condition if you want to count from the first bar even if the period is already started, otherwise remove it
    barsCounter = BarsSince( newPeriod ) + 1;
    prevPeriodTradingBars = ValueWhen( newPeriod, Ref( barsCounter, -1 ) );
    return prevPeriodTradingBars;

// Example  of usage of the custom functions to get the previous "period" trading bars
// Apply the exploration to a single/current ticker with daily or intraday periodicty

Filter = 1;
if( Interval() < inHourly )
    AddColumn( getPrevPeriodBars( Hour() ), "Prev. Hour Bars", 1 );

if( Interval() < inDaily )
    AddColumn( getPrevPeriodBars( Day() ), "Prev. Day Bars", 1 );

AddColumn( getPrevPeriodBars( Month() ), "Prev. Month Bars", 1 );
AddColumn( getPrevPeriodBars( Year() ), "Prev. Year Bars", 1 );

if( Status( "action" ) == actionExplore )
    SetSortColumns( -2 );

1 Like

So many words...

But just this is needed

newPeriod = Nz(periodArray != Ref(periodArray, -1),1); 

Besides this whole ValueWhen expression then get previous value has been posted in AmiBroker KB already.

1 Like

Thanks you so much,
but I have other question , if I want to get previous two months? or Custom months over previous year?

@ahm.montaser in such a case you should create some custom "period" arrays:

// create custom period arrays
twoMonths = ceil(Month() / 2);
quarter = ceil(Month() / 3);
semester = ceil(Month() / 6);
Filter = 1;
AddColumn(twoMonths, "2 Months", 1);
AddColumn(quarter, "Quarter", 1);
AddColumn(semester, "Semester", 1);



To be specified I want to get number of bars of previous 12 month from the last bar , I'm tried multi times but I can't get the correct value.

@ahm.montaser, since I have the impression that we will be going round in circles without tackling the real problem, to get a more precise answer from me or other users, it would be good if you could specify exactly what the intent of your request is, why you need the bar count of a certain period, what kind of data you use (daily, intraday), if you have to operate in real-time and if the result you expect is a simple scalar number that varies with each new bar (in RT) or if you need arrays with data from previous periods (as in the posted examples) to perform explorations or backtests.

1 Like

I'm really sorry, but all statements you sent it added to my knowledge and experience
I use daily data since 2015, Which include ( open , high , low, close , Volume) and based on the data I apply some calculations to generate new data such as ( daily Return Price , Daily Turnover)
in my case I want to get count of bars over previous 12 month from my current bar.

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