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;
}
}
}
}
}