User talk:Nicholasferguson
Programming performance/Nick Ferguson KDB+
Language: KDB+ Skill: Intermediate. Time: Approx 120 minutes. With interruptions.
Code:
1. This solution implements a real time 'continuous query' pattern. It decides to rebalance a portfolio, by running strategies, sequentially over each incoming price: first a buy strategy, then a sell strategy. 2. Thus it can be plugged into real time data streams. 3. This solution runs two strategies, while also building and updating a portfolio. (buy then sell strategy) 4. Thus, it allows 'n' number of strategies to be run sequentially: weights, deltas, gammas, betas, alphas, CVAR, buy, sell...etc Each strategy, is in a separate module(s): buy and/or sell. And of course different implementations can run different strategy sequences. 5. It lends towards ease of regression testing, since it continuously builds and updates a portfolio. 6. This solution builds in an ease to refactor and remix strategies, and can scale to handle a multitude of incoming data streams.
/////////////////////////////////////// //cashout at the last x price.... k)x:|1_*(" F";" ")0:`gspc.txt; p:([]sh:`float$();bp:`float$();sp:`float$();csh:`float$()); cc:1e4; x2:0; buy:{$[(1.03*x)<x2;$[cc<1e4;$[cx>.00009;[p,:(cx*.10%x;x;0.0f;a::cx*.90);cx::a;x2::x];];[p,:(cc*.10%x;x;0.0f;cx::cc*.90);cc::0]];];x2::x};
sell:{$[(count p.bp)>1; $[(count pd::select r:i from p where not bp=0.0f,sp=0.0f,bp<0.94*x)>0;[p,:(-1*sum p.sh[pd.r];0.0f;x;cx::cx + x*(sum p.sh[pd.r]) );@[`p.sp;pd.r;:;x] ];];];};
cashout:{last p.csh + ( sum p.sh)* x};
\t [rebal:{ buy@x;sell@x;pd::()} each x;cashout[z:last x]] show p
nicholasferguson@wingarch.com