-2

I am coding an Order Matching project with C# and SQL Server.

One of my database table will be storing orders, placed by users, in the following way.

ID        DATE        TIMESTAMP            SYMBOL        QUANTITY        PRICE        STATUS        SIDE
--------------------------------------------------------------------------------------------------------
1         2021-02-09  2021-02-09 11:51:00  AAPL          100             34.95        OPEN          BUY
2         2021-02-09  2021-02-09 10:52:03  MSFT           25             25.90        OPEN          BUY
3         2021-02-09  2021-02-09 11:12:33  GOGL           25             35.90        OPEN          BUY

Similar way, there will be many 100s of OPEN orders which are yet to be triggered according to live price, as live price has not reached the order price.

I will be receiving live prices for all the symbols on tick bases. So it is a large data that I am going to receive.

Now my question is,

How do I match the incoming symbol's last traded price, that I am going to receive fresh at every 1 second interval(practically tick based), with the following SQL Server order table for the symbol which matches one of the incoming symbols and change the order status to EXECUTED? This is to be done every second.

I will be receiving live traded price with JSON like following at each price change and very frequently at each tick/second.

[
    {
        SYMBOL:AAPL,
        LTP:37.00,
        LTQ:126,
        LTTIME:'2021-02-09 11:55:01'
    },
    {
        SYMBOL:MSFT,
        LTP:24.00,
        LTQ:120,
        LTTIME:'2021-02-09 11:55:02'
    },
    {
        SYMBOL:GOGL,
        LTP:37.00,
        LTQ:26,
        LTTIME:'2021-02-09 11:55:03'
    }
]

EDIT With My Code TRY

This is the demo code snippet with logic that I am planning to use. But it looks straight forward and takes more time as it is with loop. Also this will be executed very frequently. So looking for some efficient and fast way to update the records in table.

    public void UpdateOrders()
    {
        List<Order> lstOrders = //This will be filled from database with DataReader
        foreach(Order order in lstOrders)
        {
            string SYMBOL = order.SYMBOL;
            foreach(Tick tick in jsonTicks)//jsonTicks is the variable which is set each second when we receive the fresh price from provider
            {
                if(tick.SYMBOL.Equals(SYMBOL))
                {
                    if(tick.LTP<=order.PRICE)
                    {
                        string str = "UPDATE ORDER SET STATUS='EXECUTED' WHERE ID=@ID";
                        dbObj.Update(str);//dbObj is a Database Class object which has function to Update the table record
                        continue;
                    }
                }
            }
        }
    }
Hemal
  • 3,682
  • 1
  • 23
  • 54

1 Answers1

0

There are 3 ways for this I am thinking right now.

  1. As you are doing in the loop, matching and updating.
  2. Implement a "Message Queue" with persistence, as soon as you got the ticker JSON, push the message in queue for each items in JSON and implement a windows service as listener, which will listen and execute the pushed message.
  3. Create a stored procedure in SQL Server, which will accept a datatable and write the match/update logic there. And convert your JSON to datatable and pass it to stored procedure.
Dale K
  • 25,246
  • 15
  • 42
  • 71
Sulabh Agarwal
  • 291
  • 1
  • 8
  • Thank you for the inputs. Option 1 is not the best one. I would like to see some examples or snippets for option 2 and 3, if you have it or link me to those. – Hemal Feb 09 '21 at 06:56
  • For Option 3 - https://stackoverflow.com/a/29507726/1889434 – Sulabh Agarwal Feb 09 '21 at 08:07
  • Ok, let me check and test it. By the way, do not know why and who voted down my question. – Hemal Feb 09 '21 at 08:56
  • Because it is very broad and does not ask a specific technical question. Rather it is looking for suggestions about how to implement. And if you think that executing an order is as simple as setting a string column, you are in for a world of hurt. Your pricing information will need more precision. But perhaps you aren't really doing stock trading and this is just some sort of simulation / demo. – SMor Feb 09 '21 at 13:14
  • @SMor Can you suggest something, like link or something else? Answer to this will be much more helpful. – Hemal Feb 09 '21 at 16:11
  • @SMor Also would like to point to this link https://stackoverflow.com/questions/13112062/which-are-the-order-matching-algorithms-most-commonly-used-by-electronic-financi where it's broad and not technical one, still it's being asked and discussed in a very good way. My question is also similar to that one, though not specific to coding, still being downvoted, so I doubted that. – Hemal Feb 09 '21 at 16:14