Friday Challenge - maximising profit

98 views
Skip to first unread message

TorQ Guru

unread,
Nov 4, 2016, 6:51:11 AM11/4/16
to AquaQ TorQ

So I'm a day trader on the stock market; I want to buy low and sell high, making the biggest gain possible.  For this challenge you have a list of prices for the stock you're trading, and I want you to make a single buy, followed by a single sell to make the max profit.  The return of the function should be the price you bought at, followed by the sell price.  Obviously you can't buy in the future or sell in the past, so the buy price must come first

For example if the list is:
19.35 19.30 18.88 18.93 18.95 19.03 19.00 18.97 18.97 18.98
The answer is:
18.88 19.03

Try it with this list:
7.67 6.74 6.72 7.27 7.09 6.95 6.39 7.34 6.9 7.4 6.47 6.78 6.91 7.95 6.72 7.85 6.62 6.21 7.68 7.41 6.19 6.23 6.15 7.97 6.5 7.97 6.13 7.88 7.31 6.92 6.76 6.14 7.09 7.59 6.37 6.63 6.24 6.66 6.43 7.64 6.15 7.64 7.32 7.92 7.16 7.41 6.77 6.93 6.57 8 6.52 7.52 6.26 6.55 6.13 7.53 6.24 7.01 6.06 6.52 6.28 7.62 7.65 6.92 7.01 7.06 7.83 6.5 6.04 6.74 7.8 7.4 6.93 6.87 6.35 6.22 6.09 6.59 6.29 7.25 6.49 7.77 7.21 7.63 6.23 7.63 6.6 6.14 6.18 7.2 7.92 7.76 7.24 7.37 6.46 6.09 6.62 7.63 6.92 6.02

As usual, points for style, conciseness and speed

Mark Lefevre

unread,
Nov 4, 2016, 9:20:02 AM11/4/16
to TorQ Guru, AquaQ TorQ
This is far too verbose, but assuming we have our list of values in a.

{idx:where d=max d:x-y;(y[idx],x[idx])}[|\[last a;reverse a];a]

6.02 8


This assumes we have all the data a-priori and can run the time series backwards. You will need a different solution for a streaming dataset.


Mark Lefevre


--
You received this message because you are subscribed to the Google Groups "AquaQ TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/kdbtorq/9837ad81-a4f6-4b21-baa9-95c517b93b38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sridhar Eaturu

unread,
Nov 4, 2016, 11:11:33 AM11/4/16
to TorQ Guru, AquaQ TorQ
maxprofit:{
    t: {max(0,x+y)}\[0f,1_deltas x];
    ex: 1+t?max t;
    en: last where 0=ex#t;
    x (en;ex-1)};

Answer: 6.04 7.92

--

Simon Tsang

unread,
Nov 4, 2016, 11:48:25 AM11/4/16
to TorQ Guru, AquaQ TorQ

 

q) maxspread:{$[((x@1)-x@2)<(y@1)-y@2;y;x]}

q) findmax:{?[x>y;x;y]}

q) findmin:{?[x<y;x;y]}

q) reverse 1_maxspread/[3#0;{{{(z;min x; max y)}[findmax\[a@x;reverse a];findmin\[a@y-x+1;a];x]}[x;count a]}each til floor 0.5*count a]

6.09 7.97

 

Simon Tsang | Programmer Anaylst Advisory | Application Development – Fixed Income

Scotiabank | Global Wholesale and Wealth Technology
320 Bay St, 13th floor, Toronto, Ontario, Canada   M5H 4A6

T  1.416.860-1764

simon...@scotiabank.com

www.scotiabank.com

Scotiabank is a business name used by The Bank of Nova Scotia

--

You received this message because you are subscribed to the Google Groups "AquaQ TorQ" group.

To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+u...@googlegroups.com.


To unsubscribe from receiving further commercial electronic messages from The Bank of Nova Scotia in Canada, please click here: https://unsubscribe.scotiabank.com?entid=BNS&buid=SBNK

To unsubscribe from receiving further commercial electronic messages from certain other senders set out in the attached list, please click here: https://www.unsubscribe.gwm.scotiabank.com?page=gwm

This email may contain confidential information the use of which by an unintended recipient is unauthorized. This email may also contain important disclosure information for the records of the intended recipient(s). For details please click here: http://www.scotiabank.com/email_disclaimer/email_english.html

Pour vous désabonner et cesser de recevoir des messages électroniques commerciaux de La Banque de Nouvelle-Écosse, veuillez cliquer ici : https://desabonnement.banquescotia.com?entid=BNS&buid=SBNK

Pour vous désabonner et cesser de recevoir des messages électroniques commerciaux de certains autres expéditeurs figurant dans la liste ci-jointe, veuillez cliquer ici : https://www.unsubscribe.gwm.scotiabank.com?page=gwm&lang=fr

Cette transmission peut contenir de l'information confidentielle et son utilisation par toute personne autre que la personne à laquelle cette transmission est destinée est interdite. Le présent courriel peut aussi contenir des renseignements importants pour les dossiers du ou des destinataires prévus. Pour plus de détails, veuillez vous diriger vers: http://www.scotiabank.com/email_disclaimer/email_francais.html

Para dejar de recibir mensajes electrónicos comerciales de The Bank of Nova Scotia en Canadá, haga clic aquí: https://cancelarsuscripcion.scotiabank.com?entid=BNS&buid=SBNK

Para dejar de recibir mensajes electrónicos comerciales de otras entidades incluidas en la lista adjunta, haga clic aquí: https://www.unsubscribe.gwm.scotiabank.com?page=gwm&lang=es

Este correo electrónico puede contener información confidencial cuyo uso por parte de personas distintas de los destinatarios del mismo está prohibido. El mensaje puede tener también datos importantes para los archivos del(de los) destinatario(s) a quien(es) está dirigido. Para más detalles, por favor dirigirse a: http://www.scotiabank.com/email_disclaimer/email_espanol.html

Jonathon McMurray

unread,
Nov 4, 2016, 12:06:23 PM11/4/16
to AquaQ TorQ
getbest:{[l]
 d:l!max each {[l;x]n:l[x+til (count l)-x]; n where n > l[x]}[l] each til count l;
 t:(value d) - key d;
 ((key d) i),(value d) i:where t = max t
 }

q)getbest n
6.04 7.92

Ryan McCarron

unread,
Nov 4, 2016, 12:12:08 PM11/4/16
to AquaQ TorQ
{(c;x)[;b?max b:x-c:mins x]}

returns: 6.04 7.92

Simon Tsang

unread,
Nov 4, 2016, 12:15:27 PM11/4/16
to TorQ Guru, AquaQ TorQ

Sorry, forgot the handle the case so that we are not buying in the future.

 

Here’s the query:

 

q) maxspread:{$[(0f<y@1) and (0f<y@2) and ((x@1)-x@2)>(y@1)-y@2;y;x]}

q) findmax:{?[x>y;x;y]}

q) findmin:{?[x<y;x;y]}

q) 1_maxspread/[3#0;{{{buy:min x; sell:max y where y>buy;(z;buy;sell)}[findmax\[a@x;reverse a];findmin\[a@y-x+1;a];x]}[x;count a]}each til floor 0.5*count a]

6.43 7.62

 

Regards,

Simon Tsang | Programmer Anaylst Advisory | Application Development – Fixed Income

Scotiabank | Global Wholesale and Wealth Technology
320 Bay St, 13th floor, Toronto, Ontario, Canada   M5H 4A6

T  1.416.860-1764

simon...@scotiabank.com

www.scotiabank.com

Scotiabank is a business name used by The Bank of Nova Scotia

 

Ben McElderry

unread,
Nov 4, 2016, 12:41:16 PM11/4/16
to AquaQ TorQ
f:{[l] value exec from ([]l;a:reverse(maxs(reverse l))) where (a-l)=max(a-l)}
Gives an answer of 6.04 7.92

On Friday, November 4, 2016 at 10:51:11 AM UTC, TorQ Guru wrote:

Simon Tsang

unread,
Nov 4, 2016, 12:57:02 PM11/4/16
to TorQ Guru, AquaQ TorQ

Hi all,

 

I should also consider the case by finding the best buy from the sell, and the best sell from the buy:

 

q) maxspread:{$[(0f<y@1) and (0f<y@2) and ((x@1)-x@2)>(y@1)-y@2;y;x]}

q) findmax:{?[x>y;x;y]}

q) findmin:{?[x<y;x;y]}

q) 1_maxspread/[3#0;raze{{{buy:min x where x<last y;sell:max y where y>first x;((z;buy;first y);(z;first x;sell))}[findmin\[a@x;a];findmax\[a@y-x+1;a];x]}[x;count a]}each til floor 0.5*count a]

q) 6.02 7.92

Simon Tsang

unread,
Nov 4, 2016, 1:38:41 PM11/4/16
to Ben McElderry, AquaQ TorQ

Still haven’t seen a right answer.

 

Anyway, here’s another shot:

 

q) 1_{$[((-). 1_x)>(-). 1_y;y;x]}/[3#0;{{{(z;x;max y where y>x)}[a@x;|\[a@y-x+1;a];x]}[x;count a]}each til floor 0.5*count a]

6.13 8

 

Regards,

Simon Tsang | Programmer Anaylst Advisory | Application Development – Fixed Income

Scotiabank | Global Wholesale and Wealth Technology
320 Bay St, 13th floor, Toronto, Ontario, Canada   M5H 4A6

T  1.416.860-1764

simon...@scotiabank.com

www.scotiabank.com

Scotiabank is a business name used by The Bank of Nova Scotia

 

--

You received this message because you are subscribed to the Google Groups "AquaQ TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+u...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

Jack Andrews

unread,
Nov 4, 2016, 8:39:11 PM11/4/16
to TorQ Guru, AquaQ TorQ
simple approach. partitions x at all possible (til count x) points and finds the difference between extremes in partitions.  

//see x defined below
q)m d?min d:(-).'m:(min;max)@'/:(0,'til count x)_\:x
6.04 7.92
q)0N!`gain,7.92-6.04;           //check there's a gain
(`gain;1.88)
q)(x?6.04;last where x=7.92)    //check the trades aren't short
68 90


q)x:7.67 6.74 6.72 7.27 7.09 6.95 6.39 7.34 6.9 7.4 6.47 6.78 6.91 7.95 6.72 7.85 6.62 6.21 7.68 7.41 6.19 6.23 6.15 7.97 6.5 7.97 6.13 7.88 7.31 6.92 6.76 6.14 7.09 7.59 6.37 6.63 6.24 6.66 6.43 7.64 6.15 7.64 7.32 7.92 7.16 7.41 6.77 6.93 6.57 8 6.52 7.52 6.26 6.55 6.13 7.53 6.24 7.01 6.06 6.52 6.28 7.62 7.65 6.92 7.01 7.06 7.83 6.5 6.04 6.74 7.8 7.4 6.93 6.87 6.35 6.22 6.09 6.59 6.29 7.25 6.49 7.77 7.21 7.63 6.23 7.63 6.6 6.14 6.18 7.2 7.92 7.76 7.24 7.37 6.46 6.09 6.62 7.63 6.92 6.02


-

--
You received this message because you are subscribed to the Google Groups "AquaQ TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/kdbtorq/9837ad81-a4f6-4b21-baa9-95c517b93b38%40googlegroups.com.

Jack Andrews

unread,
Nov 4, 2016, 8:44:05 PM11/4/16
to Ryan McCarron, AquaQ TorQ
nice

--
You received this message because you are subscribed to the Google Groups "AquaQ TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.

Ravi Raninga

unread,
Nov 7, 2016, 4:03:59 AM11/7/16
to kdb...@googlegroups.com
l:7.67 6.74 6.72 7.27 7.09 6.95 6.39 7.34 6.9 7.4 6.47 6.78 6.91 7.95 6.72 7.85 6.62 6.21 7.68 7.41 6.19 6.23 6.15 7.97 6.5 7.97 6.13 7.88 7.31 6.92 6.76 6.14 7.09 7.59 6.37 6.63 6.24 6.66 6.43 7.64 6.15 7.64 7.32 7.92 7.16 7.41 6.77 6.93 6.57 8 6.52 7.52 6.26 6.55 6.13 7.53 6.24 7.01 6.06 6.52 6.28 7.62 7.65 6.92 7.01 7.06 7.83 6.5 6.04 6.74 7.8 7.4 6.93 6.87 6.35 6.22 6.09 6.59 6.29 7.25 6.49 7.77 7.21 7.63 6.23 7.63 6.6 6.14 6.18 7.2 7.92 7.76 7.24 7.37 6.46 6.09 6.62 7.63 6.92 6.02;

d:()!();
{ls:l[til x];rs:l[x+til neg[x]+count l];d[max[rs]-min[ls]]:(max[rs];min[ls])} each til count l;
d max key d;

On Mon, Nov 7, 2016 at 2:20 PM, Ravi Raninga <ravi.r...@gmail.com> wrote:
l:7.67 6.74 6.72 7.27 7.09 6.95 6.39 7.34 6.9 7.4 6.47 6.78 6.91 7.95 6.72 7.85 6.62 6.21 7.68 7.41 6.19 6.23 6.15 7.97 6.5 7.97 6.13 7.88 7.31 6.92 6.76 6.14 7.09 7.59 6.37 6.63 6.24 6.66 6.43 7.64 6.15 7.64 7.32 7.92 7.16 7.41 6.77 6.93 6.57 8 6.52 7.52 6.26 6.55 6.13 7.53 6.24 7.01 6.06 6.52 6.28 7.62 7.65 6.92 7.01 7.06 7.83 6.5 6.04 6.74 7.8 7.4 6.93 6.87 6.35 6.22 6.09 6.59 6.29 7.25 6.49 7.77 7.21 7.63 6.23 7.63 6.6 6.14 6.18 7.2 7.92 7.76 7.24 7.37 6.46 6.09 6.62 7.63 6.92 6.02;

d:()!();
{ls:l[til x];rs:l[x+til neg[x]+count l];d[max[rs]-min[ls]]:(max[rs];min[ls])} each til count l;
d max key d;



--
You received this message because you are subscribed to the Google Groups "AquaQ TorQ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kdbtorq+unsubscribe@googlegroups.com.

AquaQMatt

unread,
Nov 7, 2016, 5:19:03 AM11/7/16
to AquaQ kdb+/TorQ
testlist:list
do[(count testlist)-1;diflist,:((max testlist) - testlist[0]);testlist:1_testlist]
sellp:max diflist + buyp:list[diflist?(max diflist)]

Probably not the fastest implementation. 
Ans: 6.04, 7.92
Reply all
Reply to author
Forward
0 new messages