I have a large (~2.3GB) ohlcv
dataset containing columns for intraday 15m data.
date ticker open close high low volume exchange time sma50 sma25 atr14 relVol10 entry exits
-------------------------------------------------------------------------------------------------------------------------------------------------
2024.06.20 IBM 2963.25 2964 2965.7 2960.4 400564 NASDAQ 2024.06.20D13:30:00.000000000 2943.033 2940.838 8.835116 2.619959 0 0
2024.06.20 IBM 2963.95 2948.1 2964.6 2947.3 333112 NASDAQ 2024.06.20D13:45:00.000000000 2943.034 2941.708 9.43975 1.827534 0 0
2024.06.20 IBM 2948.5 2952.9 2955.35 2946.55 220532 NASDAQ 2024.06.20D14:00:00.000000000 2943.085 2943.05 9.394054 1.090741 0 0
2024.06.20 IBM 2953.15 2958.5 2960 2952.5 288016 NASDAQ 2024.06.20D14:15:00.000000000 2943.261 2944.386 9.258764 1.352764 1 0
2024.06.20 IBM 2958.55 2952 2962.9 2950.1 343157 NASDAQ 2024.06.20D14:30:00.000000000 2943.208 2945.4 9.51171 1.49124 0 0
2024.06.20 IBM 2951.7 2951.35 2952.9 2943.05 429613 NASDAQ 2024.06.20D14:45:00.000000000 2943.145 2946.594 9.535873 1.675764 0 0
2024.06.20 IBM 2951.15 2946.6 2953 2940.9 937451 NASDAQ 2024.06.20D15:00:00.000000000 2942.802 2947.832 9.719025 3.195037 0 0
2024.06.20 IBM 2947.7 2949 2950.55 2943 565435 NASDAQ 2024.06.20D15:15:00.000000000 2942.585 2949.112 9.564095 1.546279 0 0
Given the entry locations entry
which are boolean flags, I would like to find out the exits
(also boolean flag) given the condition that the exit close
>= 1.02 entry close
, if the condition is not met I’d like to exit at the end of the day.
I’ve tried two approaches based on the answers to my previous question Matching Entries and Exits in KDB . I believe they’re functionally correct but both are excruciatingly slow — to the extent that given the size of my dataset they never finish, and keep taking more and more memory till the system becomes unusable.
@cillianmurphy
‘s approach
o:select close, index:i, ticker, date from ohlcv where entry;
f:{[x;y]
index:?[x; ((>;`close;1.02 * y`close);(>;`i;y`index);(not;`exits)); (); (first;`i)];
if[null index; index:-1 + count x]; / if index is 0N, then return last index
update exits:1b from x where i=index
}
r: f/[ohlcv; o];
@Maurice Lim
‘s approach
/ @MauriceLim
f2:{
{
x[0],:$[y`entry; y`close; ()];
if[x[1]:any exits:1.02*x<y[`close] 0; x[0] _: first where exits]; x
} [(`float$();0b); x][; 1]};
r:update exits:f2 ohlcv by ticker, date from ohlcv;
The task doesn’t seem too difficult and yet kdb+
seems to be making a meal of it. There’s gotta be a far more efficient approach. Please help.