Archive for September, 2006

Matlab and kdb+

Thursday, September 21st, 2006

It’s almost trivial to query kdb+ from Matlab. You can do so through the java interface and just need to make sure that class files generated from c.java are in the classpath. This can be done by editing the classpath.txt file from within matlab, i.e.

>> edit classpath.txt

And then adding the dir of the class files (or the jar file) on a line within that file, e.g.

C:/q/java

Then save the file and restart matlab. Matlab only reads classpath.txt file on startup.

Alternatively you can change the classpath at runtime with

javaaddpath(’C:/q/java’)

Then you can simply do the following from within matlab

>> conn=c(’localhost’,5011)

conn =

c@4b3f8b

>> conn.k(’tables`.’)

ans =

java.lang.String[]:

‘trade’

>> conn.k(’exec sym from trade’)

ans =

java.lang.String[]:

‘VOD.L’
‘VOD.L’

>> r=conn.k(’select from trade’)

>> r.x

ans =

java.lang.String[]:

‘time’
’sym’
‘price’
’size’
‘exch’

>> r.y

ans =

java.lang.Object[]:
[5×1 c$Time[]]
[5×1 cell }
[5×1 double ]
[5×1 int32 ]
[5×1 cell }

>> r.y(2)

ans =

java.lang.String[]:

‘VOD.L’
‘VOD.L’

>> r.y(3)

ans =

140
140

>> r.y(4)

ans =

100
100

>> conn.close()

It might not be much fun trying to work with c.Flip and c.Dict within matlab, so you can change the types in kdb+ before the table gets sent over. Sending it over as an array of arrays probably suits matlab well, and the query could simply be wrapped with something like

toMatlab[select from t]

where toMatlab function is defined as

toMatlab:{
if[ 98h=type x;(enlist key r),(enlist (value meta x)[`type0]),toMatlab’[value r:flip 0!x] ];
if[ 14h=type x;730486+`int$x]; / matlab date is days since 0000.01.01 and q date is days since 2000.01.01
if[ 19h=type x;(`float$x)%86400000];
x};

Types can be changed from java types into matlab types using cell(x) and char(x)

Nuclear Phynance

Saturday, September 9th, 2006

A few years ago I came across this ultra cool finance forum


Nuclear Phynance

It rocks! There’s a bunch of very clever people posting there. If you have any questions relating to finance then this forum is an excellent place to post them. Please read the forum guidelines and also do a quick search to make sure your topic has not already been posted there before - there are thousands of posts here!