Creating a function in Q

Hi all,

I am trying to create a function in Q. For this purpose, I created a function in a text editor and then I tried to paste the code in the Q prompt.

I am not sure what mistake I am doing but Q returns with some error.

Even when I try to type the function on Q prompt, I am getting the error as soon as I press enter after first line of code.

Please guide.

Here is my function script

filltrade: {[tname;s;p;n] sc:n#s; dc:2007.01.01+n?31; tc:n?24:00:00.000; qc:10*n?1000; pc:.01*floor (.9*p)+n?.2*p*:100; tname insert (sc;dc;tc;qc;pc)};

while creating function, I am getting error as below after first line

q)filltrade: {[tname;s;p;n]'{

You should place it in a file and load the file.
Alternatively on console define it on a single line.

You are also missing a space in front of the closing curly bracket.

Being a multi lines function, it can’t be created on q prompt so, you need to make it’s definition in single line and execute it…

q) filltrade: {[tname;s;p;n]  sc:n#s;  dc:2007.01.01+n?31;  tc:n?24:00:00.000;  qc:10*n?1000;  pc:.01*floor (.9*p)+n?.2*p*:100; tname insert (sc;dc;tc;qc;pc)};

Otherwise, use qpad for multi line function or put this function in q file and execute the q file.

\l file.q

Thanks a lot WP and Hemant. Your suggestions worked perfectly. I created the function in single line. Thanks again.

Regarding qpad, I tried to create the connection using qpad with kdb running on my local machine.

I tried server name as blank (nothing) with ports as 5000 (and 5001 second time) but none of them worked. I am getting symbol as `::5001::

Can you please suggest?

While I execute the function to generate dummy data into the trade table, I am getting  data type error. Below is my script.

trade:([] sym:instruments$(); date:date$(); time:time$(); qty:int$(); pr:int$())trade insert(ibm; 2007.01.01; 10:10:10.10; 1000; 110)filltrade: {[tname;s;p;n] sc:n#s; dc:2007.01.01+n?31; tc:n?24:00:00.000; qc:10n?1000; pc:.01floor (.9p)+n?.2p*:100; tname insert (sc;dc;tc;qc;pc) };filltrade[trade;ibm;115;1000]`

While I execute function, I am getting below error. Any suggestion?

{[tname;s;p;n] sc:n#s; dc:2007.01.01+n?31; tc:n?24:00:00.000; qc:10*n?1000; pc:.01*floor (.9*p)+n?.2*p*:100; tname insert (sc;dc;tc;qc;pc) }'typeinserttrade(ibmibmibmibmibmibmibmibmibmibmibmibmibmibmibmibmibmibmibm..

trade:( instruments:symbol$()</font>; date:date$(); time:time$(); qty:long$(); pr:`float$())

trade insert(ibm; 2007.01.01; 10:10:10.10; 1000; 110f)


filltrade: {[tname;s;p;n] sc:n#s; dc:2007.01.01+n?31; tc:n?24:00:00.000; qc:10n?1000; pc:.01floor (.9p)+n?.2p*:100; tname insert (sc;dc;tc;qc;pc) };

filltrade[trade;ibm;115;1000]

Oh wow Yan Yan. Can you please explain what was the issue with my code?

for this example, I was referring q for mortals book.

I updated the table definition as

trade:([] sym:instruments$(); date:date$(); time:time$(); qty:long$(); pr:float$())`

Hi Naresh,

trade:flip symdatetimeqtypr!"sdtif"$\:();&nbsp; // Table creation trade:([] sym:$(); date:date$(); time:time$(); qty:int$(); **pr:float$()**);  // Table creation another way
filltrade: {[tname;s;p;n] sc:n#s; dc:2007.01.01+n?31; tc:n?24:00:00.000; qc:10*n?1000; pc:.01*floor (.9*p)+n?.2*p*:100; tname insert/: flip (sc;dc;tc;qc;pc)};
filltrade[trade;ibm;115;1000]
count trade //1000

Please see the insert statement in function.

Regards,

Hemant

Thank you Hemant again.

I understand the table creation but confused of function code. I will try to understand each char in the function. If you provide some hint, it would be better.

I understand what the function does but don’t know how it does.

The  instrument table  is a reference keyed table

http://code.kx.com/wiki/JB:QforMortals2/queries\_q\_sql#The\_Table\_Schemas

There are 2 gray boxes in that section of the book. The instrument table in box1 is a prerequisite of the trade table in box2.