mardi 7 janvier 2020

SQL for stock market order matching

I have three Stock Market App tables: BIDS, ASKS and TRADES

A new Ask order gets inserted into the ASKS table. Then if NewASK.Price is <= BIDS.Price new row(s) need be inserted into the TRADES table.

To avoid a cursor I've created a #temptable, seed it with all Bids where BIDS.Price <= NewASK.Price and then INSERT INTO Trades SELECT FROM #temptable WHERE CumulativeNumberOfContracts < NewASK.NumberOfContracts.

At present I then do another INSERT INTO Trades SELECT TOP 1 FROM #temptable WHERE CumulativeNumberOfContracts > NewAsk.NumberOfContracts (i.e. NewAsk.NumberOfContracts = 25 and #temptable contains 3 Bids of 10 Contracts each the first two of which are filled 100% and the third only 50%).

However I am running into difficulties where NewAsk.NumberContracts <= CumulativeNumberOfContracts and think the only way to handle is via IF . . . THEN blocks.

QUESTION: Am I not getting more complicated than just using a cursor (or WHILE block)???

Aucun commentaire:

Enregistrer un commentaire