Coin-Swap Order ExecutionI have now made changes to prevent the race scenario and I am testing it in parallel to the live system. I'll implement this version once I've done necessary testing. Also, for full transparency, here is how Coin-Swap matches trades. If I can do something better, please let me know and I'll make changes (assuming I agree).
Currently every few seconds the system checks for matches on orders that have had changes since the last cycle completed. Starting from the oldest it cycles through each of them until complete. Here is the logic behind the trade matching:
Getting orders that meet criteria:If sell order: Get all orders which has a price greater than the order's price in descending order by last update time. (This will grab all buy orders from highest price to lowest price from oldest to newest)
If buy order: Get all orders which has a price less than the order's price in ascending order by last update time. (This will grab all buy orders from lowest price to highest price from oldest to newest)
Determining PriceSell order: A query is made to find the highest bid price. If sell price is less than or equal to the highest bid price, the price is set at the highest bid price. (Change here is to check for highest bid price and match to that)
Buy order: A query is made to find the lowest offer price. If the buy price is greater than or equal to the lowest offer price, the price is set at the lowest offer price. (Change here is to check for highest bid price and match to that)
If you have any questions on how something is done or suggestions on how to do it better, please feel free to do so.

Ian