Hi guys,
I am working on a project that simulates an online shares and securities trading environment. Participants can place buy/sell orders and cancel them. If an order is placed before the market closes at 1700, it should only be cancelled before 1700. It however seems that there is a loophole and participants are cancelling orders that would have already been processed.I have attached a copy of the stored procedure that I had tried but it still has that loophole. Please take a look and see how I can improve it.
Thanks
I am working on a project that simulates an online shares and securities trading environment. Participants can place buy/sell orders and cancel them. If an order is placed before the market closes at 1700, it should only be cancelled before 1700. It however seems that there is a loophole and participants are cancelling orders that would have already been processed.I have attached a copy of the stored procedure that I had tried but it still has that loophole. Please take a look and see how I can improve it.
Thanks
Code:
USE [JSESchoolsChallenge] GO SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO Alter PROCEDURE [dbo].[spInstrumentOrderCancelTrialReg] @StudentOrderTieID int, @UserID int, @retVal int OUTPUT /* Description : This stored procedure Cancels the Instrument Order with the orderID supplied Business Rules : Inputs : @InstrumentOrderID - Required Outputs : @retVal - 1 there was an error; 0 = everything went fine */ AS DECLARE @EntryID int DECLARE @InstumentID int DECLARE @Quantity int DECLARE @Price float DECLARE @InstrumentOrderID int declare @marketCloseDate datetime declare @orderPlacedDate datetime declare @dayOfTheWeek int SET NOCOUNT ON SET @marketCloseDate = DATEADD(hour, 17, DATEDIFF(DAY, 2, GETDATE())) SET @dayOfTheWeek = DATEPART(DW, GETDATE()) -- Check to see if the user is a administrator or not if not do student check IF NOT EXISTS (SELECT ID FROM Administrator WHERE ID = @UserID ) BEGIN -- Check to see that the student cancelling the order is infact the student that placed the order. IF NOT EXISTS ( SELECT TOP 1 StudentOrderTie.StudentOrderTieID FROM StudentOrderTie WHERE StudentOrderTie.StudentID = @UserID AND StudentOrderTie.StudentOrderTieID = @StudentOrderTieID) BEGIN SET @retVal = 1 RETURN END END IF EXISTS ( SELECT TOP 1 InstrumentOrder.EntryID, InstrumentOrder.instrumentID, InstrumentOrder.placedTimestamp, StudentOrderTie.quantity, StudentOrderTie.PriceAtPlacement, OrderID FROM InstrumentOrder INNER JOIN StudentOrderTie ON StudentOrderTie.OrderID = InstrumentOrder.InstrumentOrderID WHERE StudentOrderTie.StudentOrderTieID = @StudentOrderTieID AND StudentOrderTie.OrderStatusID = 4) BEGIN SELECT @EntryID = InstrumentOrder.EntryID, @InstumentID= InstrumentOrder.instrumentID, @Quantity = StudentOrderTie.quantity, @Price = StudentOrderTie.PriceAtPlacement, @InstrumentOrderID = OrderID, @orderPlacedDate= instrumentOrder.placedTimestamp FROM InstrumentOrder INNER JOIN StudentOrderTie ON StudentOrderTie.OrderID = InstrumentOrder.InstrumentOrderID WHERE StudentOrderTie.StudentOrderTieID = @StudentOrderTieID AND StudentOrderTie.OrderStatusID = 4 IF(@orderPlacedDate<@marketCloseDate) Begin SET @retVal = 1 -- StudentOrderTie order set to cancelled UPDATE StudentOrderTie SET OrderStatusID = 2 WHERE StudentOrderTie.StudentOrderTieID = @StudentOrderTieID SET @Price = @Price * @Quantity EXEC spTransactionHistoryAdd @EntryID, 5, @InstumentID, @Quantity, 0 -- 5 = 'OrderCancellation' -- Decrease the order of the main Order UPDATE InstrumentOrder SET Quantity = Quantity - @Quantity WHERE InstrumentOrderID = @InstrumentOrderID -- Main OrderCancelled if nessasary UPDATE InstrumentOrder SET InstrumentOrderStatusID = 2 WHERE Quantity = 0 Print 'I am working' SET @retval = 0 End END ELSE BEGIN SET @retVal = 1 END SET NOCOUNT OFF
Comment