Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply kdb tick architecture #8

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40,433 changes: 40,355 additions & 78 deletions data/USA500IDXUSD.csv

Large diffs are not rendered by default.

406,604 changes: 203,302 additions & 203,302 deletions data/USATECHIDXUSD.csv

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions tick/feed.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// / Read data
tab1: update sym:`SP500 from 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv;
tab2: update sym:`NASDAQ100 from 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USATECHIDXUSD.csv;
tradesSP: 0!1_(update delta:0f^deltas dateTime from
distinct select "n"$dateTime,sym, log bid, log ask from
update dateTime:"P"$@[;19;:;"."] each dateTime from
`dateTime xasc tab1);

tradesNYS: 0!1_(update delta:0f^deltas dateTime from
distinct select "n"$dateTime,sym, log bid, log ask from
update dateTime:"P"$@[;19;:;"."] each dateTime from
`dateTime xasc tab2);

.tick.i:-1
timer:{t:.z.p;while[.z.p<t+x&abs x-16*1e6]} / 16 <- timer variable
h:neg hopen `::5010
// / send tables

.z.ts:{
i+:1;
dataSP:value tradesSP i;
dataNYS:value tradesNYS i;
timer[last dataSP];
h(".u.upd";`trade;-1_dataSP,'dataNYS)}

\t 16
18 changes: 18 additions & 0 deletions tick/r.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if[not "w"=first string .z.o;system "sleep 1"];
upd:insert;

.u.x:.z.x,(count .z.x)_(":5010";":5012");

.u.end:{
t:tables`.;
t@:where `g=attr each t@\:`sym;
.Q.hdpf[`$":",.u.x 1;`:.;x;`sym];
@[;`sym;`g#] each t;};

.u.rep:{
(.[;();:;].)each x;
if[null first y;:()];
-11!y;
system "cd ",1_-10_string first reverse y };

.u.rep .(hopen `$":",.u.x 0)"(.u.sub[`;`];`.u `i`L)";
40 changes: 40 additions & 0 deletions tick/rts.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/q tick/r.q [host]:port[:usr:pwd] [host]:port[:usr:pwd]
/2008.09.09 .k ->.q
\l ../linear_regression.q

if[not "w"=first string .z.o;system "sleep 1"];

// It should use the gateway + hdb
historial_tab1: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv;
historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv;

// Calculate alpha and beta from historical values
beta_lr: betaF[px:-100#log historial_tab1`close;py:-100#log historial_tab2`close];
alpha_lr: alphaF[px;py];
// We calculate an historical standard deviation
std_lr: dev[(1000#exec bid from priceY) - (1000#exec bid from priceX)];

updPairs: {[t;d]
// calculate spreads
s: d[1][2] - ((d[0][2] * beta_lr)+alpha_lr);
// Update table
d: update spread: s, up: 1.96*std_lr, low: -1.96*std_lr from d;
// insert on trade table
`trade insert d;
}

upd:insert;

/ get the ticker plant and history ports, defaults are 5010,5012
.u.x:.z.x,(count .z.x)_(":5010";":5012");

/ end of day: save, clear, hdb reload
.u.end:{t:tables`.;t@:where `g=attr each t@\:`sym;.Q.hdpf[`$":",.u.x 1;`:.;x;`sym];@[;`sym;`g#] each t;};

/ init schema and sync up from log file;cd to hdb(so client save can run)
.u.rep:{(.[;();:;].)each x;if[null first y;:()];-11!y;system "cd ",1_-10_string first reverse y};
/ HARDCODE \cd if other than logdir/db

/ connect to ticker plant for (schema;(logcount;log))
.u.rep .(hopen `$":",.u.x 0)"(.u.sub[`;`];`.u `i`L)";

2 changes: 2 additions & 0 deletions tick/sym.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// trade table schema
trade:([]time:`timespan$();sym:`g#`symbol$();bid:`float$();ask:`float$());
39 changes: 39 additions & 0 deletions tick/tick.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
system"l tick/",(src:first .z.x,enlist"sym"),".q"

if[not system"p";system"p 5010"]
\l tick/u.q
\d .u
ld:{
if[not type key L::`$(-10_string L),string x;.[L;();:;()]];i::j::-11!(-2;L);if[0<=type i;-2 (string L)," is a corrupt log. Truncate to length ",(string last i)," and restart"; exit 1];hopen L};

tick:{
init[];
if[not min(`time`sym~2#key flip value@)each t;'`timesym];
@[;`sym;`g#]each t;
d::.z.D;
if[l::count y;L::`$":",y,"/",x,10#".";l::ld d]};

endofday:{
end d;
d+:1;
if[l;hclose l;l::0(`.u.ld;d)]};

ts:{
if[d<x;if[d<x-1;system"t 0";'"more than one day?"];endofday[]]};

if[system"t";
.z.ts:{pub'[t;value each t];@[`.;t;@[;`sym;`g#]0#];i::j;ts .z.D};
upd:{[t;x]
if[not -16=type first first x;if[d<"d"$a:.z.P;.z.ts[]];a:"n"$a;x:$[0>type first x;a,x;(enlist(count first x)#a),x]];
t insert x;if[l;l enlist (`upd;t;x);j+:1];}];

if[not system"t";system"t 1000";
.z.ts:{ts .z.D};
upd:{[t;x]ts"d"$a:.z.P;
if[not -16=type first first x;a:"n"$a;x:$[0>type first x;a,x;(enlist(count first x)#a),x]];
f:key flip value t;
pub[t;$[0>type first x;enlist f!x;flip f!x]];if[l;l enlist (`upd;t;x);i+:1];}];

\d .

.u.tick[src;.z.x 1];
18 changes: 18 additions & 0 deletions tick/u.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/2019.06.17 ensure sym has g attr for schema returned to new subscriber
/2008.09.09 .k -> .q
/2006.05.08 add

\d .u
init:{w::t!(count t::tables`.)#()}

del:{w[x]_:w[x;;0]?y};.z.pc:{del[;x]each t};

sel:{$[`~y;x;select from x where sym in y]}

pub:{[t;x]{[t;x;w]if[count x:sel[x]w 1;(neg first w)(`upd;t;x)]}[t;x]each w t}

add:{$[(count w x)>i:w[x;;0]?.z.w;.[`.u.w;(x;i;1);union;y];w[x],:enlist(.z.w;y)];(x;$[99=type v:value x;sel[v]y;@[0#v;`sym;`g#]])}

sub:{if[x~`;:sub[;y]each t];if[not x in t;'x];del[x].z.w;add[x;y]}

end:{(neg union/[w[;;0]])@\:(`.u.end;x)}