Exemplo n.º 1
0
        /// <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();
        }
Exemplo n.º 2
0
        /// <summary>Inserts a Match 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 Decline(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"];
            long           rowVersion     = parameters["rowVersion"];

            ServerMarketData.MatchRow matchRow = matchTable.FindByMatchId(matchId);
            if (matchRow != null)
            {
                // Time stamps and user stamps
                int      modifiedUserId = ServerMarketData.UserId;
                DateTime modifiedTime   = DateTime.Now;

                // Call the internal method to complete the operation.
                MarkThree.Guardian.Core.Match.Update(adoTransaction, sqlTransaction, ref rowVersion, null, null, matchRow.MatchId, null, Status.Declined, null);

                // Call the internal method to decline the contra side of this match.
                foreach (ServerMarketData.MatchRow contraMatchRow in matchTable.Rows)
                {
                    if (contraMatchRow.WorkingOrderId == matchRow.ContraOrderId &&
                        contraMatchRow.ContraOrderId == matchRow.WorkingOrderId)
                    {
                        rowVersion = contraMatchRow.RowVersion;
                        MarkThree.Guardian.Core.Match.Update(adoTransaction, sqlTransaction, ref rowVersion, null, null, contraMatchRow.MatchId, null, Status.Declined, null);
                    }
                }
            }

            // Return values.
            parameters["rowVersion"] = rowVersion;
        }
Exemplo n.º 3
0
        /// <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 Decline(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"];

            int  negotiationId = int.MinValue;
            long rowVersion    = long.MinValue;

            ServerMarketData.MatchRow matchRow = matchTable.FindByMatchId(matchId);
            if (matchRow != null)
            {
                // See if there is already a pending offer.
                bool isFound = false;
                foreach (ServerMarketData.NegotiationRow innerNegotiationRow in matchRow.GetNegotiationRows())
                {
                    if (innerNegotiationRow.StatusCode == Status.Declined)
                    {
                        throw new Exception("This offer has previously been declined.");
                    }

                    if (innerNegotiationRow.StatusCode == Status.Pending)
                    {
                        // Call the internal method to complete the operation.
                        rowVersion    = innerNegotiationRow.RowVersion;
                        negotiationId = innerNegotiationRow.NegotiationId;
                        MarkThree.Guardian.Core.Negotiation.Update(adoTransaction, sqlTransaction, ref rowVersion,
                                                                   matchId, null, negotiationId, null, Status.Declined);

                        isFound = true;
                    }
                }

                // Call the internal method to complete the operation.
                if (!isFound)
                {
                    negotiationId = MarkThree.Guardian.Core.Negotiation.Insert(adoTransaction, sqlTransaction, ref rowVersion, null, matchId,
                                                                               0.0m, Status.Declined);
                }

                // If there's a counter offer, then notify the couter part that the offer has been declined.
                // 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.
                foreach (MarketData.NegotiationRow contraNegotiationRow in contraMatchRow.GetNegotiationRows())
                {
                    if (contraNegotiationRow.StatusCode == Status.Pending)
                    {
                        rowVersion = contraNegotiationRow.RowVersion;
                        MarkThree.Guardian.Core.Negotiation.Update(adoTransaction, sqlTransaction, ref rowVersion, null, null, contraNegotiationRow.NegotiationId, null, Status.Declined);
                    }
                }
            }

            // Return values.
            parameters["rowVersion"] = rowVersion;
            parameters.Return        = negotiationId;
        }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
0
        /// <summary>Inserts a Match 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 Accept(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"];
            long           rowVersion     = parameters["rowVersion"];

            ServerMarketData.MatchRow matchRow = matchTable.FindByMatchId(matchId);
            if (matchRow != null)
            {
                // Time stamps and user stamps
                int      createdUserId  = ServerMarketData.UserId;
                DateTime createdTime    = DateTime.Now;
                int      modifiedUserId = ServerMarketData.UserId;
                DateTime modifiedTime   = DateTime.Now;

                // Call the internal method to complete the operation.
                MarkThree.Guardian.Core.Match.Update(adoTransaction, sqlTransaction, ref rowVersion, null, null,
                                                     matchRow.MatchId, null, Status.Accepted, null);

                // This is the working order associated with the match.
                ServerMarketData.WorkingOrderRow workingOrderRow = matchRow.WorkingOrderRow;

                // This will find the contra order.
                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 match, the Destination Orders are generated.
                if (contraMatchRow.StatusCode != Status.Accepted)
                {
                    return;
                }

                ServerMarketData.WorkingOrderRow contraOrderRow = contraMatchRow.WorkingOrderRow;

                decimal quantity = workingOrderRow.SubmittedQuantity < contraOrderRow.SubmittedQuantity ? workingOrderRow.SubmittedQuantity : contraOrderRow.SubmittedQuantity;

                long rowVersionDestionation = long.MinValue;
                int  destinationOrderId     = MarkThree.Guardian.Core.DestinationOrder.Insert(adoTransaction, sqlTransaction,
                                                                                              ref rowVersionDestionation, null, null, createdTime, createdUserId, Match.destinationId, null, null,
                                                                                              null, workingOrderRow[ServerMarketData.WorkingOrder.LimitPriceColumn], modifiedTime, modifiedUserId,
                                                                                              workingOrderRow.OrderTypeCode, quantity, workingOrderRow.PriceTypeCode, State.Acknowledged, Status.New,
                                                                                              workingOrderRow[ServerMarketData.WorkingOrder.StopPriceColumn], createdUserId, workingOrderRow.TimeInForceCode,
                                                                                              workingOrderRow.WorkingOrderId);

                long rowVersionExecution = long.MinValue;
                MarkThree.Guardian.Core.Execution.Insert(adoTransaction, sqlTransaction, ref rowVersionExecution, null, null, null,
                                                         null, createdTime, createdUserId, destinationOrderId, State.Acknowledged, workingOrderRow.PriceRow.LastPrice,
                                                         quantity, null, null, null, modifiedTime, modifiedUserId, null, null, null, DateTime.Now, null, State.Sent,
                                                         DateTime.Now, null, null, null, null);

                rowVersion = contraMatchRow.RowVersion;
                MarkThree.Guardian.Core.Match.Update(adoTransaction, sqlTransaction, ref rowVersion, null, null,
                                                     contraMatchRow.MatchId, null, Status.Accepted, null);

                int contraDestinationOrderId = MarkThree.Guardian.Core.DestinationOrder.Insert(adoTransaction, sqlTransaction,
                                                                                               ref rowVersionDestionation, null, null, createdTime, createdUserId, Match.destinationId, null, null,
                                                                                               null, contraOrderRow[ServerMarketData.WorkingOrder.LimitPriceColumn], modifiedTime, modifiedUserId,
                                                                                               contraOrderRow.OrderTypeCode, quantity, contraOrderRow.PriceTypeCode, State.Acknowledged, Status.New,
                                                                                               contraOrderRow[ServerMarketData.WorkingOrder.StopPriceColumn], createdUserId, contraOrderRow.TimeInForceCode,
                                                                                               contraOrderRow.WorkingOrderId);

                MarkThree.Guardian.Core.Execution.Insert(adoTransaction, sqlTransaction, ref rowVersionExecution, null, null, null,
                                                         null, createdTime, createdUserId, contraDestinationOrderId, State.Acknowledged,
                                                         contraOrderRow.PriceRow.LastPrice, quantity, null, null, null, modifiedTime, modifiedUserId, null, null, null,
                                                         DateTime.Now, null, State.Sent, DateTime.Now, null, null, null, null);
            }

            // Return values.
            parameters["rowVersion"] = rowVersion;
        }
Exemplo n.º 6
0
        /// <summary>Updates 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 the row</param>
        /// <param name="contraOrderId">The value for the ContraOrderId column.</param>
        /// <param name="createdTime">The value for the CreatedTime column.</param>
        /// <param name="matchId">The value for the MatchId column.</param>
        /// <param name="timerId">The value for the TimerId column.</param>
        /// <param name="statusCode">The value for the StatusCode column.</param>
        /// <param name="workingOrderId">The value for the WorkingOrderId column.</param>
        public static void Update(AdoTransaction adoTransaction, SqlTransaction sqlTransaction, ref long rowVersion, object contraOrderId, object createdTime, int matchId, object timerId, object statusCode, object workingOrderId)
        {
            // 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.");
            }
            // Apply Defaults
            if ((contraOrderId == null))
            {
                contraOrderId = matchRow[matchTable.ContraOrderIdColumn];
            }
            if ((createdTime == null))
            {
                createdTime = matchRow[matchTable.CreatedTimeColumn];
            }
            if ((timerId == null))
            {
                timerId = matchRow[matchTable.TimerIdColumn];
            }
            if ((statusCode == null))
            {
                statusCode = matchRow[matchTable.StatusCodeColumn];
            }
            if ((workingOrderId == null))
            {
                workingOrderId = matchRow[matchTable.WorkingOrderIdColumn];
            }
            // Increment the row version
            rowVersion = ServerMarketData.RowVersion.Increment();
            // Update the record in the ADO database.
            matchRow[matchTable.RowVersionColumn]     = rowVersion;
            matchRow[matchTable.ContraOrderIdColumn]  = contraOrderId;
            matchRow[matchTable.CreatedTimeColumn]    = createdTime;
            matchRow[matchTable.TimerIdColumn]        = timerId;
            matchRow[matchTable.StatusCodeColumn]     = statusCode;
            matchRow[matchTable.WorkingOrderIdColumn] = workingOrderId;
            adoTransaction.DataRows.Add(matchRow);
            // Update the record in the SQL database.
            SqlCommand sqlCommand = new SqlCommand("update \"Match\" set \"RowVersion\"=@rowVersion,\"ContraOrderId\"=@contraOrderId,\"Creat" +
                                                   "edTime\"=@createdTime,\"TimerId\"=@timerId,\"StatusCode\"=@statusCode,\"WorkingOrderId" +
                                                   "\"=@workingOrderId where \"MatchId\"=@matchId");

            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("@contraOrderId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, contraOrderId));
            sqlCommand.Parameters.Add(new SqlParameter("@createdTime", SqlDbType.DateTime, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, createdTime));
            sqlCommand.Parameters.Add(new SqlParameter("@matchId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, matchId));
            sqlCommand.Parameters.Add(new SqlParameter("@timerId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, timerId));
            sqlCommand.Parameters.Add(new SqlParameter("@statusCode", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, statusCode));
            sqlCommand.Parameters.Add(new SqlParameter("@workingOrderId", SqlDbType.Int, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, workingOrderId));
            // Update the record in the SQL database.
            sqlCommand.ExecuteNonQuery();
        }