Archive for the 'trading' Category

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

Black Box Development

In late 2008/early 2009 I made the transition from full time engineering to full time Black Box trading software and strategy development. The past several months have certainly been exciting times in the financial markets, and proven to be very good for automated strategies.

I will still be maintaining the IbAPI open source library (just saw IB posted a 9.62 beta), and if anything will be more responsive now.

I am also always interested in discussing interesting opportunities, so please continue to drop me a line at email hidden; JavaScript is required

Good Trading!

-Karl

Friday, July 20, 2007 $(403.60) @ 64 Contracts

Gross:

$ (250.00)

Fees:

$ (153.60)

Net:

$ (403.60)

Contracts:

64

 

So today was the hallmark of "overtrading". One look at my equity curve explains the morning.

I began with a big hit by forcing a trade at 7:09 am, and after catching a small break, I forced another trade at 7:52 am, again without an adequate stop (read none at all). By 8:10 am, I got back in the groove, but just did not have time to make up for my mistakes. My first problem was forcing the large trades; I need to wait for the price to withdraw from resistance/support levels before finally executing (rather than simply hitting these levels). Once executed, I need an efficient way of setting stops, a fixed tick amount above / below the entry price. I will begin working on my ATS to do this, but in the mean time I will evaluate Button Trader, as well as Bracket Trader to allow me to better manage my brackets manually.

Please note the above screenshot was actually taken a while after the market closed (forgot to press print screen earlier).

Thursday, July 19, 2007 $222.80 @ 28 Contracts

Gross:

$ 290.00

Fees:

$ (67.20)

Net:

$ 222.80

Contracts:

28

 

Today was a lot better than yesterday. I have also found it difficult to trade the first half hour of the market open; as such I will change my trading schedule to be from about 6:50 am to 8:30 am. In this time period there is still a lot of volatility with quite a few block trades, all without the unpredictability of the market open.

I am also studying the tick indicators for use as signals. Interactive Broker’s Tick Indicators are intermittent (approximately every 15 seconds) and appear to be latent (by several seconds at least). I wonder if just taking ~25 of the biggest stocks in the Russell and combining their tick values would provide a good approximate for the NasDaq/NYSE ticks. The other option would be to follow the up ticks / down ticks in IWM to approximate the up ticks/down ticks in the market. What do you think?

I discovered my P/L calculator was wrong for yesterday and will update the post shortly. I also spent quite a bit of time working through the Interactive Brokers C# Order Placement Bug, and discovered it was several subtle casting errors, I will have an update out shortly.

Wednesday, July 18, 2007 $(1,294.40) @ 56 Contracts

Gross:

$ (1,160.00)

Fees:

$ (134.40)

Net:

$ (1,294.40)

Contracts:

56

 

Wooooh! Good thing I am trading a paper account. What worked yesterday did not work today. I was trying to trade the channel between the ER2 bollinger lines, using the NYSE Tick/AD lines as indicators for tops and bottoms. This worked remarkably well yesterday, but as you can see did not work so well today.

I am going to build a tool to constrain my trading, something that really hurt today was a lack of discipline ("It will turn around, I am sure"), when in fact I really needed a very tight stop, and to cut my losses early.

I am hoping that with a lot more hands on experience trading, I will find some patterns to exploit for my ATS.

Thank you to everyone who has been submitting bug reports about the Interactive Brokers C# API. I will take a look at the order processing system tonight and try to push out another patch.

Update: I had a problem with my P/L calculator yesterday, I had the wrong contract count which affected the fees, and thus my net. The above values are correct.