Search code examples
kdb

how do I get monthly 3rd Fri and quarterly 3rd Fri


I am stuck with the logic on writing a function to accept startdate & enddate and give all the dates which is 3rd Fri and similar function to give quarterly 3rd Fri

thirdFridays[2019.01.01;2019.12.31]
//return all 3rd Friday date list 
quarterlyThirdFridays[2019.01.01;2019.12.31]
//return all quarterly 3rd friday date list

Solution

  • This function will get the Xth weekday of a month:

    q)XthWeekdayofMonth:{[d;n;m]((),o)where(),m="m"$o:(7*$[all n;n-1;til 5])+t+(d-t:"d"$m)mod 7};
    

    So with friday=6 and running it over a range of months:

    q)XthWeekdayofMonth[6;3;]each 2019.01m+til 12
    2019.01.18
    2019.02.15
    2019.03.15
    2019.04.19
    2019.05.17
    2019.06.21
    2019.07.19
    2019.08.16
    2019.09.20
    2019.10.18
    2019.11.15
    2019.12.20
    

    You could extend it to quarters pretty easily by just supplying the relevant month in each quarter