Monday, February 1, 2016

Real time quotes

Hello, welcome back to my blog about automated trading. Last week I have made example for the stocks screening. This week I will write about the quotes during trading session.

For the source code example, please download repository and update to branch 4-real-time-quotes, changeset f33d07def714.

Background

Realtime quotes are another essential part of the system for the automated trading. As historical quotes are used during the night for heavy computing, real time quotes are used during the trading session for knowing immediate price (and others). In my system real time quotes has the following data.
  • Symbol - company identification
  • Timestamp
  • Previous close - close price from the previous trading day
  • Open - price at the open
  • Bid
  • Ask
  • Volume
Now regarding the speed. In short, speed matters. Every symbol produces several messages per second. Now multiply it by the number of symbols you want to track (50 or 100) and it ends up as a thousands messages per hour. Trading system has to be able to consume them and process them. This is not that trvivial task on the free (or cheap) cloud. Therefore here I will introduce the speed test. Next let's look to the actual code.

Coding

On the broker side, typically there is exposed very simplistic API. Client needs to log in and subscribe or unsubscribe quotes. Then there are few administrative messages (e.g. heartbeat, error, logout). That's it. So let's look to the source code. I have implemented solution which works with MbTrading.

First there are 2 interfaces - QuoteConnector and QuoteReceiver. QuoteConnector is the actual class which allows QuoteReceiver to register for subscription. QuoteReceiver then receives the subscribed quotes. Then the actual implementation is done through MbTradingQuoteConnector with unit test MbTradingQuoteConnectorTest. Now if you have trading account with MbTrading, then you can fill in credentials into MbTradingQuoteConnectorTestInternetApp and connect it with the actual broker. And finally as I have talked about the speed, then there is example for performance testing - MbTradingQuoteUtilsPerformanceTestApp. This is application which measures how fast I am able to parse broker message received. I personally use this style for any optimization, knowing that unit test will cover the correct functionality. So play with that and have fun!

That's it for today. Next week I will introduce the generic interface for the trading algorithms. Thank you for reading!

No comments:

Post a Comment