weekNumber AMIBroker

Discussion in 'App Development' started by davidp13, Jan 30, 2018.

  1. davidp13

    davidp13

    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
     
  2. jharmon

    jharmon

    code is flawed since it assumes a Monday or a Friday. Ever heard of holidays?
     
  3. davidp13

    davidp13

    OK thanks, but do you know how to code AFL to find the week of the month? Thanks
     
  4. jharmon

    jharmon

    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: Jan 30, 2018
    KeLo likes this.
  5. jharmon

    jharmon

    For what reason do you need to know the week of the month? Seems like a strange esoteric curve-fitted thing to want.