# Get Periodicity in AFL

Hi,
Wanted to get the periodicity in AFL, basically I need to calculate the CAR in exploration or backtesting and use different divisor according to the periodicty used.

Currently I have a code where I calculate the difference in days beetween the first bar and next bar. Is a really dirty solution and I wondered if anyone has a better one to share. TRyied to make it work at the very first sympbol, but does't work.
Any help would be appreciated.

``````fbr = Status( "FirstBarInRange" );
lbr = Status( "LastBarInRange" );
//if ( Status( "stocknum" ) == 0 )
NumOfDays=	LastValue(ValueWhen(fbr==1,Ref(DaysSince1900(),1),1)-ValueWhen(fbr==1,DaysSince1900(),1));
If(NumOfDays<5) TF=0;  If(NumOfDays>=6 AND NumOfDays <15) TF=1; If(NumOfDays>15) TF=2;
VarSet("ND",NumOfDays);
VarSet("TF",TF);
NumOfDays= VarGet("ND");
TimeFrame=StrExtract("Daily,Weekly,Monthly",VarGet("TF"));
if(TimeFrame=="Daily") CarDiv=252; if(TimeFrame=="Weekly") CarDiv=52;
if(TimeFrame=="Monthly") CarDiv=12;
``````

You can remove all your code above. Simply use Interval() for number or Interval(2) to get interval string.
https://www.amibroker.com/guide/afl/interval.html

``````TimeFrame = Interval();

if( TimeFrame < inDaily ) Error( "Set interval to minimum EOD." );

switch (TimeFrame) {
case inWeekly: CarDiv = 52; break;
case inMonthly: CarDiv = 12; break;
default: CarDiv = 252; break;// daily
}

``````

``````if( Interval() < InDaily ) Error( "Set interval to minimum EOD." );

TimeFrameStr = Interval(2);

switch (TimeFrameStr ) {
case "Weekly": CarDiv = 52; break;
case "Monthly": CarDiv = 12; break;
default: CarDiv = 252; break;// daily
}
``````
4 Likes

Thanks Fx,
it does the job !!