Interesting Bug in Dealing with Negative Prices

MarketData.jpg

One of the support crew in London pointed out to me that a Money Market ticker in one of the applications fed from my ticker plant was not updating properly. That the quote from my source had both the bid and ask less than zero was interesting. What happened was the conversion I did from working in "all positive" price to "mixed sign" prices on the calculation of 'Best Price' in the client code had a bug. Basically, there is a test for a 'good' quote that the bid and ask are in the right relation to one another - with a little 'fudge' factor, and also that they are not horribly disparate either.

The problem code looked like:

    if (((fabs(mAsk) <= 0.5) ||
         (1.1*fabs(mAsk) >= fabs(mBid))) &&
        (fabs(mBid)*1000.0 > fabs(mAsk))) {
      retval = (mBid + mAsk)/2.0;
    }

where the firs check is to make sure that if the ask is small enough we don't mess with the check - like 0.05/0.10 quotes. The second check is the problem... without the fabs() calls, it works for positive numbers: of the ask is bigger than the bid, and the ask isn't too much bigger than the bid, then we take the mid point. Easy.

But if we put the fabs() calls in there and try to work with negative numbers, we're messed up. Negative bids need to be numerically less than the asks but with the fabs() calls, they get flipped to being numerically greater than the asks. This is the problem.

If we change the code to look like:

    if (((fabs(mAsk) <= 0.5) ||
         ((mBid - mAsk)/fabs(mAsk) <= 0.1)) &&
        (fabs(mBid)*1000.0 > fabs(mAsk))) {
      retval = (mBid + mAsk)/2.0;
    }

then we're looking at the percentage change and that works for both signs - positive and negative.

I put this into the clients and things started ticking nicely - if you can call a negative price quote 'nice'. It was just interesting to me that I thought I had it right the first time, but you have to be very tricky about throwing around fabs() in your code.