Archive for the 'C#' Category

Page 3 of 7

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).

9.6.3.14 Release – Bug Fix Release

This release still corresponds to the Ib 9.63 beta release, but includes another bug Fix to the ReadDecimal sub routine which was plagued with some corner case floating point numbers which are now properly handled.

I also added an overload to RequestHistory which allows you to specify the duration to allow you to pass a string for duration directly to Ib.

If you have any problems, email hidden; JavaScript is required.

Download the release here, or go to the utilities page.

Next Question

As I receive common emails, I will continue to post the question / answer. The next question is regarding initially connecting to TWS via an API:

  • I’m new to programming and I just don’t get your TestApp running. How exactly do I link to TWS ? Do I need to add any references or components from the IB API software?

In TWS you will need to goto Configure -> API -> Enable ActiveX and Socket Clients.
Next select Configure -> API -> All API Settings
In the window that appears, under Trusted IP Addresses, select Create, and add “127.0.0.1″ (The local machine) to the Trusted IP Addresses. Select OK, and you are good to go.

9.6.3.13 Release Corresponds to 9.63 Ib Api

It sure has been a long time coming, and many of these fixes have been available in subversion for quite some time, but the 9.6.3.13 release is complete. It has the following changes:

  • 9.6.3.13 New Release – 7/20/09
    • Update to Ib’s 9.63 api
    • Add shortable tickType
    • Fixes to ReadDecimal
    • Fix 1 second barsize enum.
    • Fixed Bug with IBClient.ReadDecimal()
    • Fixed bug in ExecutionFilter

I also reorganized subversion to relink all of the files’ history, I had accidentally broken things when I first made the archive public.

This release has not gone through quite the normal scrutiny of my prior releases, but it was so overdue, I thought I’d get it out there, and if you have any problems, email hidden; JavaScript is required.

Download the release here, or go to the utilities page.

Common Questions

I received an email today with questions that I have been asked many times, so I thought I’d post the questions and answers here.

  • If you stop maintaining your wrapper to the latest versions and the IB API changes to newer versions, how long will your existing C# API wrapper keep working?

Ib goes to great lengths to maintain backwards compatibility with old apis. If you look at their api code, it is clearly version controlled by the method, and I wouldn’t be surprised if code from many years ago still works.

As an example, I have been running an API app I wrote in Jan / Feb of 2008 every day since without updating the api once and have had no problems.

  • If you stop maintaining your wrapper to the latest versions, would you possibly update it for a small fee? (I am not rich, just trying my hand to get rich!)  I would hate to lose all the effort into coding this ATS.

Always a possibility, but it is my intent to keep updating it for free. I’ve become somewhat less reactive to Ib version updates because each update adds relatively obscure features, and since compatibility is always maintained, it is not super urgent. That being said, I will get myself caught up here shortly, and continue to update to the latest version.

  • Has your wrapper matured over time and currently working well?  No inherent hard to debug problems?  (Sorry for my asking but my Delphi friend has many of these!)  I noticed your excellent forum for help.

Good question, it certainly has matured. Occasionally some parsing bugs have been pointed out, specifically with my use of decimals, but I address those quickly with updates to subversion, and changes get rolled into the next release.

 

Let me know if you have any other general questions and I will create an FAQ page.