/ Calling KSQL from K
\l db / running from K, just load the library
`"build stock"
stock.name: `Alcatel `Alstom `"Air France" `"France Telecom" `"Snecma" `Supra
stock.industry: `telecom `engineering `"aviation" `"telecom" `"aviation" `consulting
.d.r"stock: 'name'key stock"
.d.r"stock['Alcatel'].industry"
`"build trade"
n:1000 / number of trades
t1: ,/("trade:([]"
" stock:n rand stock.name,"
" price:50 + .25 * n rand 200,"
" amount:100 * 10 + n rand 20,"
" date:date['1998-01-01'] + n rand 449)")
.d.r t1
t2: "trade:'date' asc trade"
.d.r t2
.d.r "show'trade'"
/ now it's time to calculate 6 month hi, low, average, volume
/ for a given stock.
getstats:{[name;date]
x: " select first stock, hi: max price, low: min price, "
x,: " avgprice: avg price, vol: sum amount "
x,: " from trade where stock = '"
x,: ($name)
x,: "', date within (date['"
x,: ($date)
x,: "'], date['"
x,: ($date)
x,: "'] + 182)"
res: .d.r x
:res
}
res: getstats["Alcatel";"1998-01-01"]
.d.r "show'res'"
The first line is just a comment. K uses slashes instead of pound signs.
/ Calling KSQL from K
\l db / running from K, just load the library
`"build stock" stock.name: `Alcatel `Alstom `"Air France" `"France Telecom" `"Snecma" `Supra stock.industry: `telecom `engineering `"aviation" `"telecom" `"aviation" `consulting
.d.r"stock: 'name'key stock"
.d.r"stock['Alcatel'].industry"
`"build trade"
n:1000 / number of trades
t1: ,/("trade:([]"
" stock:n rand stock.name,"
" price:50 + .25 * n rand 200,"
" amount:100 * 10 + n rand 20,"
" date:date['1998-01-01'] + n rand 449)")
.d.r t1
t2: "trade:'date' asc trade" .d.r t2 .d.r "show'trade'"
/ now it's time to calculate 6 month hi, low, average, volume
/ for a given stock.
getstats:{[name;date]
x: " select first stock, hi: max price, low: min price, "
x,: " avgprice: avg price, vol: sum amount "
x,: " from trade where stock = '"
x,: ($name)
x,: "', date within (date['"
x,: ($date)
x,: "'], date['"
x,: ($date)
x,: "'] + 182)"
res: .d.r x
:res
}
res: getstats["Alcatel";"1998-01-01"] .d.r "show'res'"
m: 500
do[m
res: getstats["Alcatel";"1998-01-01"]
]
("These "), ($m), (" getstats requests required "),($ _t - start), (" seconds.")
.d.r "tradeseq: select price, amount, date by stock from trade"
\m i 1234
.m.g(the dots represent a kind of directory structure in the namespace; the first dot is the root).
/ Receive communication from another process
.m.g:{[pair]
x: getstatsseq[$pair[0]; $pair[1]]
:x
}
machine: ` / local machine server: 3: machine,1234 / declare a handle x: server 4: (`Alcatel; `"1998-01-01") `show $ `x
/ receive from web
.m.h:{[string]
i: string _ss "stock"
string: *(i+6) _ string
i: string ? "&"
name: string[!i]
s: #string
date: string[(s-10) + !10]
res: getstats[name;date]
out: printheader[!res]
vals: ,+res[]
out,: ,/tablehtmlize[vals]'!#vals
:out
}