/// <summary>Archives a Negotiation record.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit</param> /// <param name="RowVersion">The version number of this row.</param> /// <param name="negotiationId">The value for the NegotiationId column.</param> /// <param name="archive">true to archive the object, false to unarchive it.</param> public static void Archive(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, long rowVersion, int negotiationId) { // Accessor for the Negotiation Table. ServerMarketData.NegotiationDataTable negotiationTable = ServerMarketData.Negotiation; // Rule #1: Make sure the record exists before updating it. ServerMarketData.NegotiationRow negotiationRow = negotiationTable.FindByNegotiationId(negotiationId); if ((negotiationRow == null)) { throw new Exception(string.Format("The Negotiation table does not have an element identified by {0}", negotiationId)); } // Rule #2: Optimistic Concurrency Check if ((negotiationRow.RowVersion != rowVersion)) { throw new System.Exception("This record is busy. Please try again later."); } // Archive the child records. // Increment the row version rowVersion = ServerMarketData.RowVersion.Increment(); // Delete the record in the ADO database. negotiationRow[negotiationTable.RowVersionColumn] = rowVersion; adoTransaction.DataRows.Add(negotiationRow); negotiationRow.Delete(); // Archive the record in the SQL database. SqlCommand sqlCommand = new SqlCommand("update \"Negotiation\" set \"IsArchived\" = 1 where \"NegotiationId\"=@negotiationId"); sqlCommand.Connection = sqlTransaction.Connection; sqlCommand.Transaction = sqlTransaction; sqlCommand.Parameters.Add(new SqlParameter("@negotiationId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, negotiationId)); sqlCommand.ExecuteNonQuery(); }
/// <summary>Archives a Match record.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit</param> /// <param name="RowVersion">The version number of this row.</param> /// <param name="matchId">The value for the MatchId column.</param> /// <param name="archive">true to archive the object, false to unarchive it.</param> public static void Archive(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, long rowVersion, int matchId) { // Accessor for the Match Table. ServerMarketData.MatchDataTable matchTable = ServerMarketData.Match; // Rule #1: Make sure the record exists before updating it. ServerMarketData.MatchRow matchRow = matchTable.FindByMatchId(matchId); if ((matchRow == null)) { throw new Exception(string.Format("The Match table does not have an element identified by {0}", matchId)); } // Rule #2: Optimistic Concurrency Check if ((matchRow.RowVersion != rowVersion)) { throw new System.Exception("This record is busy. Please try again later."); } // Archive the child records. for (int index = 0; (index < matchRow.GetNegotiationRows().Length); index = (index + 1)) { ServerMarketData.NegotiationRow childNegotiationRow = matchRow.GetNegotiationRows()[index]; Negotiation.Archive(adoTransaction, sqlTransaction, childNegotiationRow.RowVersion, childNegotiationRow.NegotiationId); } // Increment the row version rowVersion = ServerMarketData.RowVersion.Increment(); // Delete the record in the ADO database. matchRow[matchTable.RowVersionColumn] = rowVersion; adoTransaction.DataRows.Add(matchRow); matchRow.Delete(); // Archive the record in the SQL database. SqlCommand sqlCommand = new SqlCommand("update \"Match\" set \"IsArchived\" = 1 where \"MatchId\"=@matchId"); sqlCommand.Connection = sqlTransaction.Connection; sqlCommand.Transaction = sqlTransaction; sqlCommand.Parameters.Add(new SqlParameter("@matchId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, matchId)); sqlCommand.ExecuteNonQuery(); }
/// <summary>Updates a Negotiation record.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit</param> /// <param name="rowVersion">The version number of the row</param> /// <param name="executionId">The value for the ExecutionId column.</param> /// <param name="matchId">The value for the MatchId column.</param> /// <param name="negotiationId">The value for the NegotiationId column.</param> /// <param name="quantity">The value for the Quantity column.</param> /// <param name="statusCode">The value for the StatusCode column.</param> public static void Update(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, ref long rowVersion, object executionId, object matchId, int negotiationId, object quantity, object statusCode) { // Accessor for the Negotiation Table. ServerMarketData.NegotiationDataTable negotiationTable = ServerMarketData.Negotiation; // Rule #1: Make sure the record exists before updating it. ServerMarketData.NegotiationRow negotiationRow = negotiationTable.FindByNegotiationId(negotiationId); if ((negotiationRow == null)) { throw new Exception(string.Format("The Negotiation table does not have an element identified by {0}", negotiationId)); } // Rule #2: Optimistic Concurrency Check if ((negotiationRow.RowVersion != rowVersion)) { throw new System.Exception("This record is busy. Please try again later."); } // Apply Defaults if ((executionId == null)) { executionId = negotiationRow[negotiationTable.ExecutionIdColumn]; } if ((matchId == null)) { matchId = negotiationRow[negotiationTable.MatchIdColumn]; } if ((quantity == null)) { quantity = negotiationRow[negotiationTable.QuantityColumn]; } if ((statusCode == null)) { statusCode = negotiationRow[negotiationTable.StatusCodeColumn]; } // Increment the row version rowVersion = ServerMarketData.RowVersion.Increment(); // Update the record in the ADO database. negotiationRow[negotiationTable.RowVersionColumn] = rowVersion; negotiationRow[negotiationTable.ExecutionIdColumn] = executionId; negotiationRow[negotiationTable.MatchIdColumn] = matchId; negotiationRow[negotiationTable.QuantityColumn] = quantity; negotiationRow[negotiationTable.StatusCodeColumn] = statusCode; adoTransaction.DataRows.Add(negotiationRow); // Update the record in the SQL database. SqlCommand sqlCommand = new SqlCommand("update \"Negotiation\" set \"RowVersion\"=@rowVersion,\"ExecutionId\"=@executionId,\"Mat" + "chId\"=@matchId,\"Quantity\"=@quantity,\"StatusCode\"=@statusCode where \"NegotiationI" + "d\"=@negotiationId"); sqlCommand.Connection = sqlTransaction.Connection; sqlCommand.Transaction = sqlTransaction; sqlCommand.Parameters.Add(new SqlParameter("@rowVersion", SqlDbType.BigInt, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rowVersion)); sqlCommand.Parameters.Add(new SqlParameter("@executionId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, executionId)); sqlCommand.Parameters.Add(new SqlParameter("@matchId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, matchId)); sqlCommand.Parameters.Add(new SqlParameter("@negotiationId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, negotiationId)); sqlCommand.Parameters.Add(new SqlParameter("@quantity", SqlDbType.Decimal, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, quantity)); sqlCommand.Parameters.Add(new SqlParameter("@statusCode", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, statusCode)); // Update the record in the SQL database. sqlCommand.ExecuteNonQuery(); }
/// <summary> /// Authorizes a WorkingOrder to be returned to the client. /// </summary> /// <param name="userDataRow">Identifies the current user.</param> /// <param name="workingOrderDataRow">The record to be tested for authorization.</param> /// <returns>true if the record belongs in the user's hierarchy.</returns> public static bool FilterNegotiation(DataRow userDataRow, DataRow negotiationDataRow) { // This will test the record and return true if it belongs to the hierarchies this user is authorized to view. False // if the record should not be included in the user's data model. ServerMarketData.UserRow userRow = (ServerMarketData.UserRow)userDataRow; ServerMarketData.NegotiationRow negotiationRow = (ServerMarketData.NegotiationRow)negotiationDataRow; return(Hierarchy.IsDescendant(userRow.SystemFolderRow.FolderRow.ObjectRow, negotiationRow.MatchRow.WorkingOrderRow.BlotterRow.ObjectRow)); }
/// <summary>Archives a Status record.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit</param> /// <param name="RowVersion">The version number of this row.</param> /// <param name="statusCode">The value for the StatusCode column.</param> /// <param name="archive">true to archive the object, false to unarchive it.</param> public static void Archive(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, long rowVersion, int statusCode) { // Accessor for the Status Table. ServerMarketData.StatusDataTable statusTable = ServerMarketData.Status; // Rule #1: Make sure the record exists before updating it. ServerMarketData.StatusRow statusRow = statusTable.FindByStatusCode(statusCode); if ((statusRow == null)) { throw new Exception(string.Format("The Status table does not have an element identified by {0}", statusCode)); } // Rule #2: Optimistic Concurrency Check if ((statusRow.RowVersion != rowVersion)) { throw new System.Exception("This record is busy. Please try again later."); } // Archive the child records. for (int index = 0; (index < statusRow.GetDestinationOrderRows().Length); index = (index + 1)) { ServerMarketData.DestinationOrderRow childDestinationOrderRow = statusRow.GetDestinationOrderRows()[index]; DestinationOrder.Archive(adoTransaction, sqlTransaction, childDestinationOrderRow.RowVersion, childDestinationOrderRow.DestinationOrderId); } for (int index = 0; (index < statusRow.GetMatchRows().Length); index = (index + 1)) { ServerMarketData.MatchRow childMatchRow = statusRow.GetMatchRows()[index]; Match.Archive(adoTransaction, sqlTransaction, childMatchRow.RowVersion, childMatchRow.MatchId); } for (int index = 0; (index < statusRow.GetNegotiationRows().Length); index = (index + 1)) { ServerMarketData.NegotiationRow childNegotiationRow = statusRow.GetNegotiationRows()[index]; Negotiation.Archive(adoTransaction, sqlTransaction, childNegotiationRow.RowVersion, childNegotiationRow.NegotiationId); } for (int index = 0; (index < statusRow.GetSourceOrderRows().Length); index = (index + 1)) { ServerMarketData.SourceOrderRow childSourceOrderRow = statusRow.GetSourceOrderRows()[index]; SourceOrder.Archive(adoTransaction, sqlTransaction, childSourceOrderRow.RowVersion, childSourceOrderRow.SourceOrderId); } for (int index = 0; (index < statusRow.GetWorkingOrderRows().Length); index = (index + 1)) { ServerMarketData.WorkingOrderRow childWorkingOrderRow = statusRow.GetWorkingOrderRows()[index]; WorkingOrder.Archive(adoTransaction, sqlTransaction, childWorkingOrderRow.RowVersion, childWorkingOrderRow.WorkingOrderId); } // Increment the row version rowVersion = ServerMarketData.RowVersion.Increment(); // Delete the record in the ADO database. statusRow[statusTable.RowVersionColumn] = rowVersion; adoTransaction.DataRows.Add(statusRow); statusRow.Delete(); // Archive the record in the SQL database. SqlCommand sqlCommand = new SqlCommand("update \"Status\" set \"IsArchived\" = 1 where \"StatusCode\"=@statusCode"); sqlCommand.Connection = sqlTransaction.Connection; sqlCommand.Transaction = sqlTransaction; sqlCommand.Parameters.Add(new SqlParameter("@statusCode", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, statusCode)); sqlCommand.ExecuteNonQuery(); }
/// <summary>Inserts a Negotiation record.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit</param> /// <param name="executionId">The value for the ExecutionId column.</param> /// <param name="matchId">The value for the MatchId column.</param> /// <param name="quantity">The value for the Quantity column.</param> /// <param name="statusCode">The value for the StatusCode column.</param> public static int Insert(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, ref long rowVersion, object executionId, int matchId, decimal quantity, int statusCode) { // Accessor for the Negotiation Table. ServerMarketData.NegotiationDataTable negotiationTable = ServerMarketData.Negotiation; // Apply Defaults if ((executionId == null)) { executionId = System.DBNull.Value; } // Increment the row version rowVersion = ServerMarketData.RowVersion.Increment(); // Insert the record into the ADO database. ServerMarketData.NegotiationRow negotiationRow = negotiationTable.NewNegotiationRow(); negotiationRow[negotiationTable.RowVersionColumn] = rowVersion; negotiationRow[negotiationTable.ExecutionIdColumn] = executionId; negotiationRow[negotiationTable.MatchIdColumn] = matchId; negotiationRow[negotiationTable.QuantityColumn] = quantity; negotiationRow[negotiationTable.StatusCodeColumn] = statusCode; negotiationTable.AddNegotiationRow(negotiationRow); adoTransaction.DataRows.Add(negotiationRow); // Insert the record into the SQL database. SqlCommand sqlCommand = new SqlCommand("insert \"Negotiation\" (\"rowVersion\",\"ExecutionId\",\"MatchId\",\"NegotiationId\",\"Quant" + "ity\",\"StatusCode\") values (@rowVersion,@executionId,@matchId,@negotiationId,@qua" + "ntity,@statusCode)"); sqlCommand.Connection = sqlTransaction.Connection; sqlCommand.Transaction = sqlTransaction; sqlCommand.Parameters.Add(new SqlParameter("@rowVersion", SqlDbType.BigInt, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, rowVersion)); sqlCommand.Parameters.Add(new SqlParameter("@executionId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, executionId)); sqlCommand.Parameters.Add(new SqlParameter("@matchId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, matchId)); sqlCommand.Parameters.Add(new SqlParameter("@negotiationId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, negotiationRow[negotiationTable.NegotiationIdColumn])); sqlCommand.Parameters.Add(new SqlParameter("@quantity", SqlDbType.Decimal, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, quantity)); sqlCommand.Parameters.Add(new SqlParameter("@statusCode", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, statusCode)); sqlCommand.ExecuteNonQuery(); // Return Statements return(negotiationRow.NegotiationId); }
/// <summary>Inserts a Negotiation record using Metadata Parameters.</summary> /// <param name="transaction">Commits or rejects a set of commands as a unit.</param> /// <param name="remoteMethod">Contains the metadata parameters and exceptions for this command.</param> public static void Offer(ParameterList parameters) { // Accessor for the Match Table. ServerMarketData.MatchDataTable matchTable = ServerMarketData.Match; // Extract the parameters from the command batch. AdoTransaction adoTransaction = parameters["adoTransaction"]; SqlTransaction sqlTransaction = parameters["sqlTransaction"]; int matchId = parameters["matchId"]; decimal quantity = parameters["quantity"]; int negotiationId = int.MinValue; long rowVersion = long.MinValue; ServerMarketData.MatchRow matchRow = matchTable.FindByMatchId(matchId); if (matchRow != null) { // Rule #1: Insure that there are no pending offers. foreach (ServerMarketData.NegotiationRow innerNegotiationRow in matchRow.GetNegotiationRows()) { if (innerNegotiationRow.StatusCode == Status.Pending) { throw new Exception("There is already an offer pending."); } if (innerNegotiationRow.StatusCode == Status.Declined) { throw new Exception("This offer has previously been declined."); } } // Time stamps and user stamps int createdUserId = ServerMarketData.UserId; DateTime createdTime = DateTime.Now; int modifiedUserId = ServerMarketData.UserId; DateTime modifiedTime = DateTime.Now; // This will find the contra matching record. int contraMatchIndex = ServerMarketData.Match.KeyMatchWorkingOrderIdContraOrderId.Find(new object[] { matchRow.ContraOrderId, matchRow.WorkingOrderId }); if (contraMatchIndex == -1) { throw new Exception(string.Format("Corruption: the match record for {0}, {1} can't be found", matchRow.ContraOrderId, matchRow.WorkingOrderId)); } ServerMarketData.MatchRow contraMatchRow = (ServerMarketData.MatchRow)ServerMarketData.Match.KeyMatchWorkingOrderIdContraOrderId[contraMatchIndex].Row; // When both sides have agreed to the Negotiation, the Destination Orders are generated. ServerMarketData.NegotiationRow contraNegotiationRow = null; foreach (MarketData.NegotiationRow innerNegotiationRow in contraMatchRow.GetNegotiationRows()) { if (innerNegotiationRow.StatusCode == Status.Pending) { contraNegotiationRow = innerNegotiationRow; break; } } // This means that there's an offer on the other side. if (contraNegotiationRow == null) { // There is no opposite side of this transaction yet. It will be placed in the negotation table and wait there // until it times out, or the other side accepts the offer. long negotiationRowVersion = long.MinValue; negotiationId = MarkThree.Guardian.Core.Negotiation.Insert(adoTransaction, sqlTransaction, ref negotiationRowVersion, null, matchId, quantity, Status.Pending); } else { // At this point, there is an offer on both sides of the match for a follow-on order. We'll create orders and // executions for both sides of the trade for the minimum agreed upon quantity. ServerMarketData.WorkingOrderRow workingOrderRow = matchRow.WorkingOrderRow; ServerMarketData.WorkingOrderRow contraOrderRow = contraNegotiationRow.MatchRow.WorkingOrderRow; // The quantity of this negotiation will be the minimum of the two offers. decimal matchedQuantity = quantity < contraNegotiationRow.Quantity ? quantity : contraNegotiationRow.Quantity; // Create the order on this side of the trade. long rowVersionDestionation = long.MinValue; int destinationOrderId = MarkThree.Guardian.Core.DestinationOrder.Insert(adoTransaction, sqlTransaction, ref rowVersionDestionation, null, null, createdTime, createdUserId, Negotiation.destinationId, null, null, null, workingOrderRow[ServerMarketData.WorkingOrder.LimitPriceColumn], modifiedTime, modifiedUserId, workingOrderRow.OrderTypeCode, matchedQuantity, workingOrderRow.PriceTypeCode, State.Acknowledged, Status.New, workingOrderRow[ServerMarketData.WorkingOrder.StopPriceColumn], createdUserId, workingOrderRow.TimeInForceCode, workingOrderRow.WorkingOrderId); // Create the Execution for this side of the trade. long rowVersionExecution = long.MinValue; int executionId = MarkThree.Guardian.Core.Execution.Insert(adoTransaction, sqlTransaction, ref rowVersionExecution, null, null, createdTime, createdUserId, destinationOrderId, State.Acknowledged, workingOrderRow.PriceRow.LastPrice, matchedQuantity, null, null, null, modifiedTime, modifiedUserId, null, null, null, DateTime.Now, null, State.Sent, DateTime.Now, null, null, null, null); // There is no opposite side of this transaction yet. It will be placed in the negotation table and wait there // until it times out, or the other side accepts the offer. long negotiationRowVersion = long.MinValue; negotiationId = MarkThree.Guardian.Core.Negotiation.Insert(adoTransaction, sqlTransaction, ref negotiationRowVersion, executionId, matchId, quantity, Status.Accepted); // Create an order for the agreed upon quantity. int contraDestinationOrderId = MarkThree.Guardian.Core.DestinationOrder.Insert(adoTransaction, sqlTransaction, ref rowVersionDestionation, null, null, createdTime, createdUserId, Negotiation.destinationId, null, null, null, contraOrderRow[ServerMarketData.WorkingOrder.LimitPriceColumn], modifiedTime, modifiedUserId, contraOrderRow.OrderTypeCode, matchedQuantity, contraOrderRow.PriceTypeCode, State.Acknowledged, Status.New, contraOrderRow[ServerMarketData.WorkingOrder.StopPriceColumn], createdUserId, contraOrderRow.TimeInForceCode, contraOrderRow.WorkingOrderId); // Create an execution for the agreed upon quantity int contraExecutionId = MarkThree.Guardian.Core.Execution.Insert(adoTransaction, sqlTransaction, ref rowVersionExecution, null, null, createdTime, createdUserId, contraDestinationOrderId, State.Acknowledged, contraOrderRow.PriceRow.LastPrice, matchedQuantity, null, null, null, modifiedTime, modifiedUserId, null, null, null, DateTime.Now, null, State.Sent, DateTime.Now, null, null, null, null); // Update the contra offer. long contraNegotiationRowVersion = contraNegotiationRow.RowVersion; MarkThree.Guardian.Core.Negotiation.Update(adoTransaction, sqlTransaction, ref contraNegotiationRowVersion, contraExecutionId, null, contraNegotiationRow.NegotiationId, null, Status.Accepted); } } // Return values. parameters["rowVersion"] = rowVersion; parameters.Return = negotiationId; }