Search code examples
iterationkdb

Generic function to unwind portfolio


I have following sample portfolio p of positions (signed_qty) acquired daily:

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);

I want to estimate daily trades and position for different unwind strategies. Some of the different strategies that I want to back-test are as follows:

  1. Hold for 2 days and then trade out in next 3 days. (n=2, m=3)
  2. Hold for 1 day and then trade out in next 4 days. (n=1, m=4)
  3. Trade evenly in next 4 days. (n=0, m=4)
    ... so on.

Below is the verbose code to do so for n=3, m=4. Can someone suggest a more elegant and generic way to get output for various n and m.?

/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;


Solution

  • This can be achieved with xprev and functional form.

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

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

    Edit: This can actually be achieved without functional form:

    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
    

    Original Answer:

    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