解除投资组合的通用功能

Generic function to unwind portfolio

我有以下示例投资组合 p 每天获取的头寸 (signed_qty):

p:([]date:(2013.07.01+1000#til 200);bb_code:((200#`TSLA),(200#`MSFT),(200#`GOOG),(200#`GS),(200#`AAPL));fill_px:1000?10e; signed_qty:(1000?(-1, 1)) * 1000?10000);

我想估计不同平仓策略的每日交易量和头寸。我想回测的一些不同策略如下:

  1. 持有 2 天,然后在接下来的 3 天内交易出去。 (n=2, m=3)
  2. 持有 1 天,然后在接下来的 4 天内交易出去。 (n=1, m=4)
  3. 未来 4 天交易均匀。 (n=0, m=4)
    ...等等。

下面是针对 n=3m=4 执行此操作的详细代码。有人可以建议一种更优雅和通用的方法来获取各种 nm.?

的输出
/Adding prev_bb_code column to facilitate computation
p:update prev_bb_code: prev bb_code from p;

/No trading t+1.
p:update qty:prev signed_qty from p;
p:update signed_qty_p1:qty from p where bb_code = prev_bb_code;
p:update traded_qty_p1:(signed_qty_p1 - qty) from p;

/No trading t+2.
p:update qty:prev signed_qty_p1 from p;
p:update signed_qty_p2:qty from p where bb_code = prev_bb_code;
p:update traded_qty_p2:(signed_qty_p2 - qty) from p;

/1st trade (=1/4th position acquired on t) generated on t+3 with carry forward position = 3/4.
p:update qty:prev signed_qty_p2 from p;
p:update signed_qty_p3:"f"${$[x<0; ceiling((3%4.0)*x); floor((3%4.0)*x)]} each qty from p where bb_code = prev_bb_code;
p:update traded_qty_p3:(signed_qty_p3 - qty) from p;

/2nd trade (=1/4th position acquired on t) generated on t+4 with carry forward position = 2/4.
p:update qty:prev qty, prev_qty:prev signed_qty_p3 from p;
p:update signed_qty_p4:"f"${$[y=0n;0n;$[x<0; max((ceiling((2%4.0)*x)),y); min((floor((2%4.0)*x)),y)]]}'[qty; prev_qty] from p where bb_code=prev_bb_code;
p:update traded_qty_p4:(signed_qty_p4 - prev_qty) from p;

/3rd trade (=1/4th position acquired on t) generated on t+5 with carry forward position = 1/4.
p:update qty:prev qty, prev_qty:prev signed_qty_p4 from p;
p:update signed_qty_p5:"f"${$[y=0n;0n;$[x<0; max((ceiling((1%4.0)*x)),y); min((floor((1%4.0)*x)),y)]]}'[qty; prev_qty] from p where bb_code=prev_bb_code;
p:update traded_qty_p5:(signed_qty_p5 - prev_qty) from p;

/4th trade (=1/4th position acquired on t) generated on t+6 with carry forward position = 0. This abso
p:update qty:prev qty, prev_qty:prev signed_qty_p5 from p;
p:update signed_qty_p6:"f"${$[y=0n;0n;$[x<0; max((ceiling((0%4.0)*x)),y); min((floor((0%4.0)*x)),y)]]}'[qty; prev_qty] from p where bb_code=prev_bb_code;
p:update traded_qty_p6:(signed_qty_p6 - prev_qty) from p;

/Aggregate trades and positions.
p:update unwind_qty:((0^traded_qty_p1) + (0^traded_qty_p2) + (0^traded_qty_p3) + (0^traded_qty_p4) + (0^traded_qty_p5) + (0^traded_qty_p6)) from p;
p:update net_position:((0^signed_qty) + (0^signed_qty_p1) + (0^signed_qty_p2) + (0^signed_qty_p3) + (0^signed_qty_p4) + (0^signed_qty_p5) + (0^signed_qty_p6)) from p;

/ Finally only retain the columns of interest.
p: select date, bb_code, fill_px, signed_qty, unwind_qty, net_position from p;

这可以通过 xprev 和函数形式来实现。

https://code.kx.com/q/ref/next/#xprev

https://code.kx.com/q/basics/funsql/

编辑:其实不用函数形式也可以实现:

g:{[n;m;x] sums[x]+sums sum each neg (flip xprev[;x] each n + til m)%m}

update net_position:g[3;4;signed_qty] by bb_code from t

原答案:

f:{[t;n;m]
    //[table;where;by;cols]
    ![t;();(enlist `bb_code)!(enlist `bb_code);

        (enlist `net_position)!enlist 

            // cumulative position change
            (+;(sums;`signed_qty);

                // cumulative unwind position change
                // neg to invert the sign to unwind in opposite direction to original position
                (sums;(each;sum;(neg;

                    // this part dynamically builds the number of xprev's required
                    // n being the start / hold period
                    // m for the number of unwind periods
                    (%;(flip;(enlist),(xprev),/:(n + til m),'`signed_qty);m)))))]
    
    }
// No Comments
f:{[t;n;m]

    ![t;();(enlist `bb_code)!(enlist `bb_code);(enlist `net_position)!enlist (+;(sums;`signed_qty);(sums;(each;sum;(neg;(%;(flip;(enlist),(xprev),/:(n + til m),'`signed_qty);m)))))]
    
    };

q)f[p;3;4]
date       bb_code fill_px    signed_qty net_position
-----------------------------------------------------
2013.07.01 TSLA    4.695818   8159       8159
2013.07.02 TSLA    0.747672   2203       10362
2013.07.03 TSLA    8.014479   -566       9796
2013.07.04 TSLA    3.805866   -831       8965
2013.07.05 TSLA    2.884907   -7792      -866.75
2013.07.06 TSLA    1.303814   9188       5730.75
2013.07.07 TSLA    8.517136   2267       5548.75
2013.07.08 TSLA    5.645172   5352       8659.5
2013.07.09 TSLA    0.04426234 7867       18273