Search code examples
kdb

Convert string tenor to year


I have the following tenors in string (converted to symbols below) coming from a swap curve:

tenors_str : `1d`6m`1y`1y6m`2y`2y6m`3y`3y6m`4y`4y6m`5y`10y`20y`30y6m

I would like to create a function to parse those tenors and get the corresponding time in years.

For instance:

  • 1d should be 1/365.25
  • 6m should be 0.5
  • 1y should be 1.0
  • 1y6m should be 1.5
  • 10y should be 10.0

I have started by creating a dictionary like this:

tenor_to_year:`d`m`y!(1%365.25;1%12;1f);

But then I can't handle the case when there are more than 1 digit preceding the char d, m or y


Solution

  • I think this approach covers your use-cases:

    f:{sum prd each 2 cut d[`$c]^"F"$c:cut[0,where b|prev b:x in raze string key d:tenor_to_year;x]};
    
    q)f each string tenors_str
    0.002737851 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 10 20 30.5