Let's break your problem down a bit.
You are hinting that you need to keep track of inventory, or at least a pseudo-inventory, in order to properly track and connect orders, demands, fills, etc.
So the first thing that I would do is search these forums for inventory control threads, as there...