解除投资组合的通用函数

如何解决解除投资组合的通用函数

我每天获得以下示例组合 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),'`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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?