Last time traded at price >= signal price

Hey,

Given a table of times and prices, what’s the best way to find the last time each price traded (or a higher price) before it’s corresponding time?

I can do it with a loop and I’ve gotten pretty close with an aj however the condition on the first column of an aj is = rather than >=.

An example:

/ generate some random trades

trades: flip (timeprice)!(asc 10000?0D0;sums (10000?9)-4);

/ some trades generate a signal price (random offset in this example)

signals: 10?trades;

signals[original_price]: signals[price];

signals[`offset]: 1+10?10;

signals[price] +: signals[offset];


/ what i’m looking for

results2: signals;

j: 0;

do[count results2;

r: exec first time, first price from time xdesc trades where time<results2[j][time], price>=results2[j][`price];

results2: update real_last_time: r[time], real_last_price: r[price] from results2 where i=j;

j+:1;

];


/ closest i’ve gotten

results1: aj[pricetime;signals;update last_time: time, last_price: price from trades];


/ comparison

results1 lj timeprice xkey results2

I know there’s probably a very easy solution that I can’t see.

Thanks,

Sean

a straightforward exec .. each table - a very useful pattern
is probably the easiest to read and to extend

note that exec without aggregates conveniently means last for each column

q)a:{exec from trades where time<xtime,price\>=xprice}each signals

q)a~select time:real_last_time,price:real_last_price from results2

1b

Cheers,

   Attila

Great, thanks.