Input List Iteration (Q / KDB)

I have a function f[symbol;date0;date1] , as well as a date range, for example

 2017.12.04 2017.12.05 2017.12.06 

I would like to run this function for a given symbol - suppose "AAPL" - once for each day. Essentially:

 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 want each date to simply be added to the previous results. What could be the best way to do this?

+4
source share
2 answers

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

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

kdb will recognize the first argument of the atom and list the second argument and apply the function to each one in order. You can use raze to join each table in order:

 raze f'[`AAPL;d;d] 

then returns one joined table if the schema is the same.

+4
source

Assuming f produces the same columns for each call, you can simply flatten the result

 q)f:{([]1#x;1#y;1#z)}; q)raze f'[`a;3#.zd;3#.zd-1] xyz ----------------------- a 2018.02.07 2018.02.06 a 2018.02.07 2018.02.06 a 2018.02.07 2018.02.06 

the result is a [larger] table with the results added to each other

+2
source

Source: https://habr.com/ru/post/1275226/


All Articles