Hi Emily,
I can refer you to this link here
Programming idioms – Knowledge Base – kdb+ and q documentation - Kdb+ and q documentation (kx.com)which talks about sliding windows to aggregate over (primarily for monadic functions). I had a quick play around and have got a code that works based on some of this. However, I am sure you are likely to find some improvements to my code. Nevertheless, I am sure that this is enough to get you started.
// function to print out sliding window values
swin:{[w;s]{ 1_x,y }\[w#0;s]}
// function to calculate correlation between two columns from table
cf:{[w;table;syms]cor'[(w-1)_swin[w;(flip table)[syms[0]]];(w-1)_swin[w;(flip table)[syms[1]]]]}
You may wish to choose a better way to format the output, but here is some examples of how the output looks:
// Rolling correlation between sym1 and sym2 (for rows 1-3, 2-4 and 3-5)
q)cf[3;table;(`sym1`sym2)]
0.9002607 0.7809604 0.999991
// Rolling correlation for sym1 with each of sym2, sym3 and sym4 (for rows 1-3, 2-4 and 3-5)
q)cf[3;table]'[`sym1,'`sym2`sym3`sym4]
0.9002607 0.7809604 0.999991
-0.9025613 -0.08046509 -0.5109631
-0.5570187 -0.346547 0.1490308
Hope this gives you a good place to start.
All the best,
Matthew