开发者

Paralleling trading software

My trading software is pretty slow, I want开发者_JAVA技巧 to boost it. There are two bottle-necks.

First bottleneck:

When new bunch of data is received (new quotes, trades etc.) all strategies need to be updated asap. They need to recalculate their state/orders etc. When new bunch of data is ready to be read AllTablesUpdated method is called. This method calls then AllTablesUpdated method for each particular strategy.

     public void AllTablesUpdated()
    {
        Console.WriteLine("Start updating all tables.");

        foreach (Strategy strategy in strategies)
        {
            strategy.AllTablesUpdated();
        }
        Console.WriteLine("All tables updated in " + sw.ElapsedMilliseconds);
    }

The result defers. Sometimes it takes 0 or 1 milliseconds (that's very good), sometimes it takes 8-20 milliseconds, but sometimes it takes 800 milliseconds

There are two problems with the current implementation:

  • it uses one thread and so doesn't use multi-core processors
  • strategy.AllTablesUpdated() uses shared resources and may be blocked for a while. If some particular strategy is waiting for resources to be released, all others strategies waiting too (instead can we somehow postpone blocked strategy and start processing other strategies?)

Second bottleneck is pretty similar:

    private OrdersOrchestrator()
    {
        // A simple blocking consumer with no cancellation.
        Task.Factory.StartNew(() =>
        {
            while (!dataItems.IsCompleted)
            {

                OrdersExecutor ordersExecutor = null;
                // Blocks if number.Count == 0
                // IOE means that Take() was called on a completed collection.
                // Some other thread can call CompleteAdding after we pass the
                // IsCompleted check but before we call Take. 
                // In this example, we can simply catch the exception since the 
                // loop will break on the next iteration.
                try
                {
                    ordersExecutor = dataItems.Take();
                }
                catch (InvalidOperationException) { }

                if (ordersExecutor != null)
                {
                    ordersExecutor.IssueOrders();
                }
            }
        });
    }

ordersExecutor may wait until some resources are released. If so all other ordersExecutors are blocked.

In details: each strategy contains one ordersExecutor, they are using shared resources. strategy.AllTablesUpdated() may wait for resources to be released by it's ordersExecutor and vice versa. If this condition occurs all others stretegies/ordersExecutors are blocked too. There are more than 100 strategies.

How to modify the code to achieve?:

  • if one strategy or ordersExecutor is blocked others shouldn't be blocked?
  • use power of multi-core processors and probably multi-processors platform?


Your questions is rather broad, what you are basically asking is how to make use of parallelism in your application? You already have code which is broken up into discrete tasks, so using parallelism shouldn't be a big problem. I would recommend reading about PLinq and TPL, both provide easy-to-use APIs for this sort of thing:

http://www.codeproject.com/KB/dotnet/parallelism-in-net-4-0.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜