expryDt:{[ed] (2_string ed) except "."};
// generate a formatted ticker name for an option
symbol:{[tkr;expiryDate;typ;strike]
if[not typ in `CALL`PUT`CE`PE; show "typ is not correct. it must be either CALL or PUT or CE or PE"; exit 1];
r: $[(typ=`CALL) or (typ=`CE); string `C; string `P];
"" sv (string tkr; expryDt[expiryDate]; r; string strike)
};
// test
tkr:`IBM;
expiryDate:2025.01.09;
typ:`CALL;
strike:24000i;
show symbol[tkr;expiryDate;typ;strike]; // "IBM250109C24000" OK
我的格式化符号测试代码IBM250109C24000
运行良好。但是,当我尝试将其应用于下表时,t
它失败了,因为我的函数symbol
未矢量化。我该如何让它工作?
t:([] ticker:`IBM`IBM`IBM;
expiryDate:2025.01.09 2025.01.09 2025.01.09;
typ:`CALL`PUT`CE;
strike:24000 24000 24000;
secType:`OPT`OPT`OPT);
show t;
t:update symbol:symbol[ticker;expiryDate;typ;strike] from t where secType=`OPT; // error
运行上述代码后得到以下输出:
"IBM250109C24000"
ticker expiryDate typ strike secType
-------------------------------------
IBM 2025.01.09 CALL 24000 OPT
IBM 2025.01.09 PUT 24000 OPT
IBM 2025.01.09 CE 24000 OPT
'type
[3] tv_options.q:5: symbol:{[tkr;expiryDate;typ;strike]
if[not typ in `CALL`PUT`CE`PE; show "typ is not correct. it must be either CALL or PUT or CE or PE"; exit 1];
^
r: $[(typ=`CALL) or (typ=`CE); string `C; string `P];
最简单的方法是使用
'
https://code.kx.com/q/ref/maps/#case但原生的矢量化函数会表现得更好。例如:
如果
expryDt each expiryDate
没有太多独特的价值,expiryDate
那么您会看到改进.Q.fu[expryDt;expiryDate]