title: Paralleling trading software
body: <p>My trading software is pretty slow, I want to boost it. There are two bottle-necks.</p>
  
  <p>First bottleneck:</p>
  
  <p>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 <code>AllTablesUpdated</code> method is called. This method calls then <code>AllTablesUpdated</code> method for each particular strategy.</p>
  
  <pre><code>     public void AllTablesUpdated()
      {
          Console.WriteLine("Start updating all tables.");
  
          foreach (Strategy strategy in strategies)
          {
              strategy.AllTablesUpdated();
          }
          Console.WriteLine("All tables updated in " + sw.ElapsedMilliseconds);
      }
  </code></pre>
  
  <p>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</p>
  
  <p>There are two problems with the current implementation:</p>
  
  <ul>
  <li>it uses one thread and so doesn't use multi-core processors</li>
  <li>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?)</li>
  </ul>
  
  <p>Second bottleneck is pretty similar:</p>
  
  <pre><code>    private OrdersOrchestrator()
      {
          // A simple blocking consumer with no cancellation.
          Task.Factory.StartNew(() =&gt;
          {
              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();
                  }
              }
          });
      }
  </code></pre>
  
  <p><code>ordersExecutor</code> may wait until some resources are released. If so all other <code>ordersExecutors</code> are blocked.</p>
  
  <p>In details: each <code>strategy</code> contains one <code>ordersExecutor</code>, they are using shared resources. <code>strategy.AllTablesUpdated()</code> may wait for resources to be released by it's <code>ordersExecutor</code> and vice versa. If this condition occurs all others <code>stretegies/ordersExecutors</code> are blocked too. There are more than 100 strategies.</p>
  
  <p>How to modify the code to achieve?:</p>
  
  <ul>
  <li>if one <code>strategy</code> or <code>ordersExecutor</code> is blocked others shouldn't be blocked?</li>
  <li>use power of multi-core processors and probably multi-processors platform?</li>
  </ul>