weekNumber AMIBroker

Good day. I'm looking to convert MT4 code into AFL, but finding it difficult.

Can anyone help? Code is below:

int _weekNumber (int nShift = 0)
{
datetime date = Time[nShift];
datetime date1 = Time[nShift+1];
int value1 = 0;
if (
(TimeMonth (date) == TimeMonth(date1) )
and ( (TimeDayOfWeek (date) == 1)
or (TimeDayOfWeek(date1) == 5) )
)
value1 = value1 + 1;
if (TimeMonth (date) != TimeMonth(date1) )
value1 = 1;
return value1;
}

Thanks
 
You could create a simple indicator that creates a "week" counter since the start of the data, by looking when the weekday number for today is less than yesterday (indicating a week has ended) and incrementing the counter.

Code:
function WeekCount()
{
  return Cum( DayOfWeek()<=Ref(DayOfWeek(),-1) );
}
Plot( WeekCount(), _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );

Note that this will not work on markets that have holidays/trading breaks) over 1 week long but that's pretty much unheard of the western world (Sep 11 halted trading for a week, so this formula works for that). If your market has more than 1 week you'd need to use something like DateTimeDiff for ultimate accuracy.
 
Last edited:
For what reason do you need to know the week of the month? Seems like a strange esoteric curve-fitted thing to want.
 
Back
Top