public MarketOrders AddNewOrder(Order newOrder, IContractCall contract) { Guid orderId = Guid.NewGuid(); DBMarket dBMarket = new DBMarket(opts, this.userId, this.entityId); //BEFORE WE MATCH, LETS CHECK IF WE NEED TO MOVE MONEY TO MARGIN (SHORT SELLS) MarginTransfer(newOrder, this.userId, this.entityId, contract, orderId); using (SqlConnection con = new SqlConnection(opts.ConnectionString)) { using (SqlCommand cmd = new SqlCommand("spSelectOrdersForMatch", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ENTITYID", entityId)); cmd.Parameters.Add(new SqlParameter("@TRADETYPEID", newOrder.TradeTypeId)); cmd.Parameters.Add(new SqlParameter("@PRICE", newOrder.Price)); using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { sda.Fill(dt); DataRow row = dt.NewRow(); row["OrderId"] = orderId; row["UserId"] = userId; row["EntityId"] = entityId; row["TradeTypeId"] = newOrder.TradeTypeId; row["Price"] = newOrder.Price; row["Date"] = DateTime.Now; row["Quantity"] = newOrder.Quantity; row["UnsetQuantity"] = newOrder.UnsetQuantity; row["PublicKey"] = newOrder.PublicKey; row["Status"] = (int)Status.Open; bool updated; MarketData marketData; DataTable updatedOrders = MatchOrders(contract, entityId, row, dt, out updated, out marketData); //NEW ORDERS (SOME MAY ALREADY HAVE MATCHES) sda.InsertCommand = new SqlCommandBuilder(sda).GetInsertCommand(); //ORDERS NEED TO BE UPDATED if (updated) { sda.UpdateCommand = new SqlCommandBuilder(sda).GetUpdateCommand(); } //RUNNING NEW ORDERS AND UPDATES AGAINST DB sda.Update(dt); //UPDATE THE MARKET DATA DATABASE TABLE, RETURNING CHANGE IN PRICE + VOLUME marketData = dBMarket.UpdateMarketData(marketData); //MARKET DATA AND FINAL ORDERBOOK RETURN return(new MarketOrders { MarketData = marketData, Orders = updatedOrders }); } } } } }