Multithreading made easy

I love when Robotics innovations overlap with Finance. Our trading engine is a discombobulated series of threads interfacing to their respective data sources and execution platforms, with careful locking in between. Making changes to this system has become a nightmare due to all the careful locking concerns. Enter Microsoft CCR. It provides a very clean and thoroughly tested set of primitives for a large scale thread pool with inter task messaging. It even has adapters for using tasks on Windows Forms threads, as well as WPF dispatches. It was first distributed with the Microsoft Robotics Studio, as they suffer from the same problems we do in finance, talking to a host of sensors and actuators with careful locking constraints.

To illustrate a common problem with trading systems. You have a producer consumer model, where your data is coming in from the exchange on a single thread. This data is going to a large number of contracts, and it would be nice to do this in a multithreaded model. Under the ordinary paradigm, you would create a queue, have your producer thread lock on the queue, add the new market ticks, and unlock, letting your consumers take locks on the queue, and beginning to work on this. There are special concerns though, for a given contract you need to make sure all ticks are handled synchronously, and whenever the queue hits zero for a particular contract after data has changed, you want to tell your strategy to process the new data. Here you would need a second producer consumer framework, and things continue expanding from there.

With the CCR, you would break things down into tasks. There would be a task that consumes raw market data and posts it to a port. There would be a task that takes a tick from the market data and posts it to a contract. There would be a task that takes a tick in a contract and updates the contract, checking at the end if the contract’s queue is empty, if so, it would post the entire contract to the strategy saying it has been updated. There would be a task that consumes a contract and updates the strategy. All of these are small work tasks that are setup to run in a common dispatcher pool, and are all invoked every time a work product is posted to a port. No complicated setup, just arbitrated registrations.

I’d love to spend a lot more time talking about this, but here is one short example.

</code>using (var dispatcher = new Dispatcher(0, "Master Dispatcher"))
{
var dispatcherQueue = new DispatcherQueue("Master Queue");
var tickPort = new Port<int>();

Arbiter.Activate(dispatcherQueue, Arbiter.Receive(true, tickPort, tick=>
{
if(tick > 25)
{
Market.PlaceOrder(tick, 1);
}
}));
while(true)
{
tickPort.Post(new Random().Next(50));
}
}

I realize there are many simplifications made in this post – I just wanted to convey how cool the CCR framework is. If it were free, I’d post my Ib wrapper for it! Easily worth the $400, and if you are a student you can get it from the Robotics Studio for free. There is talk of merging it with the parallels framework added in C# 4.0 (another great invention… waiting for VS 2010 to fully integrate it though).

4 Responses to “Multithreading made easy”


  • You say “I just wanted to convey how cool the CCR framework is. If it were free, I’d post my Ib wrapper for it!” I’m considering the CCR framework and wondered if it could be used directly with the wrapper in its current form, or would the wrapper itself need to be changed to use CCR as well?

  • Yeah, you can use the CCR framework easily with my library in its current form. I’d suggest making a wrapper which binds to the various events and posts them to ports. The important thing is to handle the order flow and market data flow with an exclusive arbiter so that they get handled sequentially for a particular contract.

    Good luck, let me know if you run into any problems,

    -Karl

  • $400 for a threading library. Ha.. you make a Linux/FOSS (free open source software) programmer laugh heartily.

    But, “to each their own” and I respect that.. I just can’t understand it.

    Regards,
    Pete

  • Yeah – I’m sure there are open source/free alternatives but I don’t mind paying for good software, it really is well thought out.

Leave a Reply