Showing results for 
Search instead for 
Did you mean: 

table casting not working within upd

Hey Qbies, 
I'm trying to cast the value of a table query to a table name outside of the upd function.
Somehow I ended up using .store along the way -- no idea where I found that tbh. 

Unsure we're I'm going wrong.


minBars: ([]time:`minute$(); sym:`$(); price:`float$());
upd : {[t;x];
`cleanTrades insert(select time, sym, date, price, quantity, marketcondition, block, sweep from x);
.store.minBars:select last price, last time by 1 xbar time.minute, sym from cleanTrades;
.store.temp: select last price by sym, last minute from .store.minBars;
.store.t: flip select time:minute, sym, price from .store.temp;
.store.t: flip .store.t;
minBars: select time, sym, price from .store.t;[minBars];


New Contributor II
I'm not quite sure what you're trying to do here, or what you mean by "table casting". 

Are you trying to get the incoming data to be stored in the in-memory minBars table in that instance? Or are you trying to publish the minBars downstream to a different instance/subscriber?

New Contributor
I think the intent of your code is to ultimately publish minute bars generated from cleanTrades. If that is the case, then you will need to have your barring logic outside of the upd function and in a separate function that gets run on a timer. Unless you have trade data coming in its entirety every minute, you'll be computing and publishing minute bars multiple times for the same minute, which you don't want to do. Below is a snippet to achieve publishing of one minute bars (also takes in 2 args, first being table name):

minBars: ([]time:`minute$(); sym:`$(); price:`float$());
`cleanTrades insert `time`sym`date`price`quantity`marketcondition`block`sweep#x; }
:exec max time.minute from minBars;
data:select time, sym, price from cleanTrades where time.minute>lastTime;
if[not flush; data: select from data where 
time<`minute$max time;];
    data:0!select last price by `minute$time, sym from data;[`minBars;data];
minBars insert data;

.z.ts: { genMinBars 0b; }

\t 100

The genMinBars function gets the last barring execution time from the minBars time column, and gets trades from cleanTrades that occur after this time. By default, the flush boolean will be set to false so the last minute's worth of data in cleanTrades will be skipped because there may still be incoming data for that minute. I advise calling genMinBars[1b] in your end function or scheduling it whenever you expect the trades to stop flowing to include the last minute. It then computes the minute bars for those trades, publishes to subscribers, and inserts into minBars. This function is run on a timer so it keeps doing this.