Creating a National Best Bid/Offer Engine (cont.)

GeneralDev.jpg

Well... it took me a little bit, and I had to re-arrange the code slightly, but in the end, I have the logic I need for the NBBO engine that works when you remove the one NBBO component. It's a little more complex, but it's still very clean.

The previous code was slick, and this is nearly as slick, but it's a little more complex because of the different cases I had to deal with in the change of the 'best values'. Still... it's straight-through code with only the one lock to control it's updating, and I think it's going to be plenty fast enough. But we'll have to see how long it takes in real loading conditions.

  1. if ((bidExch != '\0') && (bidPrice > 0.0) && (bidSize > 0)) {
  2. // get the exchange as an index into the arrays
  3. uint8_t idx = bidExch - 'A';
  4. if (bidPrice > myExchData->nbboBidPrice) {
  5. // with a new leading bid, we are all there is to consider
  6. myExchData->nbboBidPrice = bidPrice;
  7. myExchData->nbboBidSize = bidSize;
  8. newNBBO = true;
  9. } else if (bidPrice == myExchData->nbboBidPrice) {
  10. // if we had something in the old NBBO, remove it now
  11. if (myExchData->bidPrice[idx] == myExchData->nbboBidPrice) {
  12. myExchData->nbboBidSize -= myExchData->bidSize[idx];
  13. }
  14. // ...and we know we need to add in our contribution now
  15. myExchData->nbboBidSize += bidSize;
  16. newNBBO = ((myExchData->bidPrice[idx] != bidPrice) ||
  17. (myExchData->bidSize[idx] != bidSize));
  18. } else if (myExchData->bidPrice[idx] == myExchData->nbboBidPrice) {
  19. // we had something in the old NBBO, remove it now
  20. myExchData->nbboBidSize -= myExchData->bidSize[idx];
  21. newNBBO = (myExchData->bidSize[idx] > 0);
  22. // see if this was the only one in the NBBO
  23. if (myExchData->bboBidSize == 0) {
  24. // reset the bid part of the NBBO
  25. newBidPrice = 0.0;
  26. newBidSize = 0;
  27. // save what we have now for the scan
  28. myExchData->bidPrice[idx] = bidPrice;
  29. myExchData->bidSize[idx] = bidSize;
  30. // ...and scan all the data (skipping this guy)
  31. for (uint8_t e = 0; e < 26; ++e) {
  32. if (myExchData->bidPrice[e] > newBidPrice) {
  33. newBidPrice = myExchData->bidPrice[e];
  34. newBidSize = myExchData->bidSize[e];
  35. } else if (myExchData->bidPrice[e] == newBidPrice) {
  36. newBidSize += myExchData->bidSize[e];
  37. }
  38. }
  39. // make sure we have something
  40. if (newBidSize > 0) {
  41. myExchData->nbboBidPrice = newBidPrice;
  42. myExchData->nbboBidSize = newBidSize;
  43. // we KNOW this is a new one
  44. newNBBO = true;
  45. }
  46. }
  47. }
  48. // no matter what, save the bid for the next time
  49. myExchData->bidPrice[idx] = bidPrice;
  50. myExchData->bidSize[idx] = bidSize;
  51. }

There are a few neat things in this guy. First off, we look for the obvious winners - if we have a brand new best bid, or an addition to the size on the existing best bid. We needed to be careful when looking at adding the value in (line 11), and especially careful about sending out a new NBBO message out (line 16).

But the really new part is the scanning of the existing exchange data (line 31). This is done only when there was a single component to the best bid, and he's just taken himself out of the mix. For those cases, we have no choice but to start all over and see what we can come up with. The wrinkle was that we need to add back in our current data so it too was included in the scan. It's possible, after all, that it's still part of the best bid, just ad a different price. And maybe with some friends.

All told, I really liked this code. It's fun, and the test frame for exercising it was a blast to write. I have the ability to throw any number of sequences of ticks to the engine and test each subsequent value. It's nice. Good, fun, work.