Getting log return

Greetings,

I’m trying to calculate the log return of price.

For example,

datetime  |  price  |  return

9:30            70

9:45            80        log(80/70)

10:00          85        log(85/80)

10:15          78        log(78/85)

Is there a quick way to calculate based on fields in different rows?

Many thanks!

Best,

Kelly

update return:log price%prev price

  Konstantin

select (log 0n,1_%':[price]) from table

or

select log price%prev price from table

Thanks Konstantin! Your answer really helps.

Thanks Ajay! Both of your answers work fine for me!

each previous can take two args, left arg controlling the initial result

q)(;)':[til 3]

0

1 0

2 1

q)(;)':[::;til 3]

0 ::

1 0

2 1

can help make your code cleaner and a bit quicker too

q)table:(price:1000000?1000.)

q)\ts:10 a:select (log 0n,1_%':[price]) from table

317 16778224

q)\ts:10 b:select log price%prev price from table

277 16777936

q)\ts:10 c:select log %':[0n;price] from table

244 16777872

HTH,

Sean

There’s also the ratios keyword: 

select log ratios[0n;price] from table

Nice…