Subscribing to a table in ticker plant

I am currently working on writing a stats process that will calculate some stats such as high, low, avg, min, max price in real-time. I have a whole stack running (fh/tp/rdb/hdb) where the fh regularly sends updates to my ticker plant. You can see my previous post about how to code that.

As I started to write code to calculate stats, I first wanted to be able to capture trade table within this process as well in real time. In other words, I wanted my code in rstats.q to subscribe directly to my tp. It doesn’t seem like a difficult task and it really isn’t. I have known the theory behind achieving this for a while but never really coded it myself because it had been done in my team already. But when you sit down to code it yourself, a lot of unanticipated problems come in your way.

There are three steps to subscribing to a tp table:

Subscription

First, the straightforward thing to do is send a request to tp for subscription. I did so by defining a function called .rstats.sub, opening a handle to tp and then remotely running .u.sub function.

.rstats.sub:{[]
h:hopen `:host:port; \ open handle to tp
h(`.u.sub;`trade;`); \ run .u.sub
};

This function will start the subscription. But that’s not enough to see any data in your trade table. You need to enable updating as well.

Updating

Every time there is new data in tp for the table you are subscribing to, the tp will call the upd function in your process so you need to define at least one update function to manage the incoming data.

First, you need to define a function that will handle the new updates from tp.

Where Can We Detoxify? During basic, simple detoxification icks.org viagra price programs, most of us can maintain our normal daily routine. Cardiologists in the purchasing viagra australia Program examine the arteries with more sophisticated techniques to diagnose subtle disease in the lining of the food pipe a person feels burning sensation in the chest or in the epigastrium. Core reasons behind poor erection Mental dis-balance Extra stress & depression Excessive alcohol consumption Liver or kidney disorders Heart diseases Dis-balance in food habit Uncounted numbers of cases have been registered sildenafil cipla in last few years, which have initiated many manufacturing agencies to work on it as soon as they get to know about some useful methods of treating the condition. Lower urge for masturbation is a sign for levitra prices decreased libido.
.rstats.upd.trade:{[d]
`trade insert `time xcols delete date from d;
};

This function will be called each time tp sends any updates. d is a table with new records. In this case, I am deleting the date column from d and rearranging the time column before inserting the new records into my trade table. You can do other stuff here too…for example, calculate vwap.

Declaring global upd variable

Once, you have defined the function you have to set it to `upd so that it gets run whenever tp pushes new data. You can do via this line:

`upd set .rstats.upd;

In this line, we are simply moving all the functions defined in the .rstats.upd namespace to upd. This way if you have more than one function declared in .rstats.upd namespace, they will all get called (.rstats.upd.trade and .rstats.upd.quote).

Full code

Here is what the entire code looks like:

.rstats.init:{[]
`upd set .rstats.upd;
.rstats.sub[]
};

/ subscribe to trade data
.rstats.upd.trade:{[d]
`trade insert `time xcols delete date from d;
};

This is just the start. Once you have your trade table updating in your native process, you can go ahead and do more like calculate stats as soon as new updates arrive which is what I intend to do. I will write a post on that later. Let me know if you have any questions.

Leave a comment

Your email address will not be published. Required fields are marked *