| 1 | <h2>Comments</h2> |
| 2 | <pre><code>foo / This is a comment |
| 3 | / This is a comment too |
| 4 | |
| 5 | / |
| 6 | Some multi-line |
| 7 | comment here |
| 8 | \ |
| 9 | |
| 10 | \ |
| 11 | This comment will |
| 12 | continue until the |
| 13 | end of code</code></pre> |
| 14 | |
| 15 | <h2>Character data and strings</h2> |
| 16 | <pre><code>"q" |
| 17 | "\"" |
| 18 | "\\" |
| 19 | "\142" |
| 20 | "foo bar baz"</code></pre> |
| 21 | |
| 22 | <h2>Symbols</h2> |
| 23 | <pre><code>` |
| 24 | `q |
| 25 | `zaphod |
| 26 | `:198.162.0.2:5042 |
| 27 | `:www.yourco.com:5042 |
| 28 | `.new</code></pre> |
| 29 | |
| 30 | <h2>Numbers</h2> |
| 31 | <pre><code>42 |
| 32 | b:-123h |
| 33 | c:1234567890j |
| 34 | pi:3.14159265 |
| 35 | float1:1f |
| 36 | r:1.4142e |
| 37 | 2.0 |
| 38 | 4.00e |
| 39 | f:1.23456789e-10 |
| 40 | r:1.2345678e-10e |
| 41 | bit:0b |
| 42 | byte:0x2a |
| 43 | a:42 |
| 44 | bit:1b |
| 45 | |
| 46 | 0w 0n 0W 0Wh 0Wj</code></pre> |
| 47 | |
| 48 | <h2>Dates</h2> |
| 49 | <pre><code>d:2006.07.04 |
| 50 | t:09:04:59.000 |
| 51 | dt:2006.07.04T09:04:59.000 |
| 52 | mon:2006.07m |
| 53 | mm:09:04 |
| 54 | sec:09:04:59 |
| 55 | d:2006.07.04 |
| 56 | |
| 57 | 0Nm 0Nd 0Nz 0Nu 0Nv 0Wd 0Wt 0Wz</code></pre> |
| 58 | |
| 59 | <h2>Verbs</h2> |
| 60 | <pre><code>99+L |
| 61 | x<42|x>98 |
| 62 | (x<42)|x>98 |
| 63 | 42~(4 2;(1 0)) |
| 64 | (4 2)~(4; 2*1)</code></pre> |
| 65 | |
| 66 | <h2>Adverbs</h2> |
| 67 | <pre><code>" ," ,/: ("Now";"is";"the";"time") |
| 68 | L1,/:\:L2 |
| 69 | 0+/10 20 30 |
| 70 | (1#) each 1001 1002 1004 1003</code></pre> |
| 71 | |
| 72 | <h2>Built-in functions and q-sql</h2> |
| 73 | <pre><code>string 42 |
| 74 | L1 cross L2 |
| 75 | type c |
| 76 | select from t where price=(max;price) fby ([]sym;ex) |
| 77 | ungroup `p xgroup sp |
| 78 | `instrument insert (`g; `$"Google"; `$"Internet")</code></pre> |
| 79 | |
| 80 | <h2>Example</h2> |
| 81 | <pre><code>/ Example from http://code.kx.com/wiki/Cookbook/CorporateActions |
| 82 | getCAs:{[caTypes] |
| 83 | / handles multiplie corporate actions on one date |
| 84 | t:0!select factor:prd factor by date-1,sym from ca where caType in caTypes; |
| 85 | t,:update date:1901.01.01,factor:1.0 from ([]sym:distinct t`sym); |
| 86 | t:`date xasc t; |
| 87 | t:update factor:reverse prds reverse 1 rotate factor by sym from t; |
| 88 | :update `g#sym from 0!t; |
| 89 | }; |
| 90 | |
| 91 | adjust:{[t;caTypes] |
| 92 | t:0!t; |
| 93 | factors:enlist 1.0^aj[`sym`date;([] date:t`date;sym:t`sym);getCAs caTypes]`factor; |
| 94 | mc:c where (lower c:cols t) like "*price"; / find columns to multiply |
| 95 | dc:c where lower[c] like "*size"; / find columns to divide |
| 96 | :![t;();0b;(mc,dc)!((*),/:mc,\:factors),((%),/:dc,\:factors)]; / multiply or divide out the columns |
| 97 | }; |
| 98 | |
| 99 | / get the adjustment factors considering all corporate actions |
| 100 | getCAs exec distinct caType from ca |
| 101 | |
| 102 | adjust[t;`dividend] / adjust trades for dividends only</code></pre> |