Search code examples
kdbq-lang

Iterate function over list of inputs (Q/KDB)


I have a function, f[symbol;date0;date1] , as well as a range of dates, say

2017.12.04
2017.12.05
2017.12.06

I'd like to run this function for a given symbol - assume "AAPL" - once for each day. In essence:

f[AAPL;2017.12.04;2017.12.04]
f[AAPL;2017.12.05;2017.12.05]
f[AAPL;2017.12.05;2017.12.05]

This function returns a table, so I'd like each date to just be appended to the previous results. What might be the best way to do this?


Solution

  • The best approach for functions in this form is to use the kdb each-both in its generic form:

    d:2017.12.04 2017.12.05 2017.12.06
    
    f'[`AAPL;d;d]
    

    kdb recognises the atomic first argument and list second argument, and applies the function to each in order. You can use raze to join each table in order as well:

    raze f'[`AAPL;d;d]
    

    then returns a single joined table, if the schema are the same.