Home » Blackbird Pages » Documentation » Use Cases » Stock Trading Grey Box

Stock Trading Application

Black box trading algorithms are interesting environments. They process massive amounts of data (sometimes tens of thousands, or even millions, of messages per second) and must provide millisecond response times in order to beat competing algorithms to the market. However, they must also interact at a much slower pace with humans, providing them with information, accepting their commands, and providing compliance controls for safety.

There are three common types of algorithms. White boxes monitor the market for opportunities and notify traders, but do no other work. Black boxes are fully automated and require little external control. Grey boxes mix the two, providing notifications to traders, but also responding to traders' commands to execute trading strategies, rather than making decisions entirely on their own.

The larger a company's trading operations, the more sophisticated its interaction cycle between algorithms, traders, and the compliance department needs to be. It takes a great deal of work for algorithms to decide where to route notifications or parse command messages and check for errors. Many companies also require compliance monitoring to be a separate application, making it harder to tamper with or bypass, and also preventing compliance monitoring from slowing down the primary application.

An ESB is an ideal method of solving this problem. Consider the diagram below:

Trading Application Structure

In this application:

  1. Trading algorithms look for opportunities in the market. When they find them, they post messages to topics in an ActiveMQ server and go back to process market data. They don't need to wait for recipients to process them, or worry about resending dropped messages, buffering, etc. - the MQ server will handle those tasks. Messages are sent in a compact, comma-delimited format to save processing time, to an appropriate topic, such as /topic/Trading/Alerts/Nasdaq/CSCO.

    Algorithms also subscribe to a similar command channel, such as /topic/Trading/Commands/Nasdasq/CSCO. They expect to receive command requests to tell them to Execute an opportunity found, and a share count to trade.

  2. Two modules are developed that run in Blackbird:
    1. Module A (on the left, above) has configuration rules that tell it how to route messages. It:
      1. Subscribes to /topic/Trading/Alerts/*. ActiveMQ supports wildcards, so it will receive any alert for any market/stock.
      2. Connects to a Jabber server and authenticates itself/marks itself online.
      3. Implements a message handler callback. In the callback, it converts the comma-delimited message bodies from the algorithms into XML/text Jabber messages, and routes them to Jabber recipients (could be individuals or groups) per its routing rules.
      4. Listens to incoming messages from Jabber senders, and verifies that they are formatted properly. If so, it converts the commands to the appropriate format for the trading algorithms and transmits them to /topic/Trading/Commands/Nasdaq/CSCO.

    2. Module B (on the right, above) subscribes to /topic/Trading/*, so it will receive both alerts and commands. It stores them in a database to provide an audit trail for later review.

  3. A basic Web application provides an interface for the audit department to review the alert/command entries in the database.

 

The real power of an ESB is in the speed with which the above may be developed (each module is just a few lines of code), and the number of ways this simple example may be easily extended, such as:

 

The actual integration work required to perform the above tasks is minimal, allowing developers to focus on the important code, such as parsing a news feed.



ITema - "Making IT Simple."