/// <summary> /// Initialize the data used in this application. /// </summary> private void InitializeData(object parameter) { string title = string.Empty; string symbol = string.Empty; string name = string.Empty; Bitmap logo = null; decimal leavesQuantity = 0.0m; decimal minimumQuantity = 0.0m; NegotiationState negotiationState = NegotiationState.None; try { // Lock the tables. System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.MatchLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.NegotiationLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.ObjectLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.OrderTypeLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(ClientTimeout.LockWait); // Find the Match record. ClientMarketData.MatchRow matchRow = ClientMarketData.Match.FindByMatchId(this.matchId); ClientMarketData.WorkingOrderRow workingOrderRow = matchRow.WorkingOrderRow; ClientMarketData.OrderTypeRow orderTypeRow = workingOrderRow.OrderTypeRow; ClientMarketData.SecurityRow securityRow = workingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId; symbol = securityRow.Symbol; name = securityRow.ObjectRow.Name; minimumQuantity = securityRow.MinimumQuantity; if (!securityRow.IsLogoNull()) { MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(securityRow.Logo)); logo = new Bitmap(memoryStream); } title = string.Format("{0} of {1}", orderTypeRow.Description, symbol); leavesQuantity = workingOrderRow.SubmittedQuantity; foreach (ClientMarketData.DestinationOrderRow destinationOrderRow in workingOrderRow.GetDestinationOrderRows()) { foreach (ClientMarketData.ExecutionRow executionRow in destinationOrderRow.GetExecutionRows()) { leavesQuantity -= executionRow.ExecutionQuantity; } } } finally { // Release the locks. if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.NegotiationLock.IsReaderLockHeld) { ClientMarketData.NegotiationLock.ReleaseReaderLock(); } if (ClientMarketData.ObjectLock.IsReaderLockHeld) { ClientMarketData.ObjectLock.ReleaseReaderLock(); } if (ClientMarketData.OrderTypeLock.IsReaderLockHeld) { ClientMarketData.OrderTypeLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } Invoke(new SetDialogAttributesDelegate(SetDialogAttributes), new object[] { title, symbol, name, logo, leavesQuantity, negotiationState }); }
private void NegotiateTrade(object parameter) { // Extract the thread parameters decimal quantity = (decimal)parameter; bool isBatchValid = true; Batch batch = new Batch(); MethodPlan method = null; try { // Lock the tables. System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.MatchLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.OrderTypeLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(ClientTimeout.LockWait); // Find the Match record. ClientMarketData.MatchRow matchRow = ClientMarketData.Match.FindByMatchId(this.matchId); // Construct a command to rename the object. AssemblyPlan assembly = batch.Assemblies.Add("Core Service"); TypePlan type = assembly.Types.Add("MarkThree.Guardian.Core.Negotiation"); TransactionPlan transaction = batch.Transactions.Add(); method = transaction.Methods.Add(type, "Insert"); method.Parameters.Add(new InputParameter("matchId", matchId)); method.Parameters.Add(new InputParameter("quantity", quantity)); method.Parameters.Add(new InputParameter("statusCode", Status.Pending)); } catch (Exception exception) { // Write the error and stack trace out to the debug listener EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); // This indicates that the batch shouldn't be executed. isBatchValid = false; } finally { // Release the locks. if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.OrderTypeLock.IsReaderLockHeld) { ClientMarketData.OrderTypeLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } // If the command batch was built successfully, then execute it. If any part of it should fail, cancel the edit and // display the server errors. if (isBatchValid) { try { // Call the web server to rename the object on the database. Note that this method must be called when there // are no locks to prevent deadlocking. That is why it appears in it's own 'try/catch' block. ClientMarketData.Execute(batch); this.negotiationId = (int)method.Parameters.Return.Value; } catch (BatchException batchException) { // Display each error in the batch. foreach (Exception exception in batchException.Exceptions) { Invoke(new MessageDelegate(ShowMessage), exception.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
/// <summary> /// Creates a well formed working order document object model from fragments of data. /// </summary> /// <param name="userId">A list of items to be included in the DOM.</param> public MatchHistoryDocument(FragmentList fragmentList) { try { // Lock the tables System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.BlotterLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.SourceOrderLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.DestinationOrderLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.ExecutionLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.MatchLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.ObjectLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.ObjectTreeLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.OrderTypeLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.PriceTypeLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.PriceLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.StatusLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.StylesheetLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.TimeInForceLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.UserLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(CommonTimeout.LockWait); // The root of the fragment document. XmlNode fragmentNode = this.AppendChild(this.CreateElement("Fragment")); // The insert, update and delete elements are only included only when there is data in those sections. XmlNode insertNode = null; XmlNode updateNode = null; XmlNode deleteNode = null; foreach (Fragment fragment in fragmentList) { // The generic record in the fragment is cast here to a WorkingOrderRow. By design, this is the only type of // record that will be placed into the FragmentList. ClientMarketData.MatchRow matchRow = (ClientMarketData.MatchRow)fragment.Row; // Insert, Update or Delete the fragment. switch (fragment.DataAction) { case DataAction.Insert: // The 'insert' element is optional until there is something to insert. if (insertNode == null) { insertNode = fragmentNode.AppendChild(this.CreateElement("Insert")); } // Insert the record. insertNode.AppendChild(new MatchElement(this, matchRow, FieldArray.Set)); break; case DataAction.Update: // The 'update' element is optional until there is something to update. if (updateNode == null) { updateNode = fragmentNode.AppendChild(this.CreateElement("Update")); } // Update individual properties of the record. updateNode.AppendChild(new MatchElement(this, matchRow, fragment.Fields)); break; case DataAction.Delete: // The 'delete' element is optional until there is something to delete. if (deleteNode == null) { deleteNode = fragmentNode.AppendChild(this.CreateElement("Delete")); } // The original record can't be used (because it has been deleted, duh). A key is constructed from the data // stored in the fragment list. CommonElement commonElement = new CommonElement("Match", this); commonElement.AddAttribute("MatchId", (int)fragment.Key[0]); deleteNode.AppendChild(commonElement); break; } } } catch (Exception exception) { // Write the error and stack trace out to the event log. EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); } finally { // Release the table locks. if (ClientMarketData.BlotterLock.IsReaderLockHeld) { ClientMarketData.BlotterLock.ReleaseReaderLock(); } if (ClientMarketData.SourceOrderLock.IsReaderLockHeld) { ClientMarketData.SourceOrderLock.ReleaseReaderLock(); } if (ClientMarketData.DestinationOrderLock.IsReaderLockHeld) { ClientMarketData.DestinationOrderLock.ReleaseReaderLock(); } if (ClientMarketData.ExecutionLock.IsReaderLockHeld) { ClientMarketData.ExecutionLock.ReleaseReaderLock(); } if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.ObjectLock.IsReaderLockHeld) { ClientMarketData.ObjectLock.ReleaseReaderLock(); } if (ClientMarketData.ObjectTreeLock.IsReaderLockHeld) { ClientMarketData.ObjectTreeLock.ReleaseReaderLock(); } if (ClientMarketData.OrderTypeLock.IsReaderLockHeld) { ClientMarketData.OrderTypeLock.ReleaseReaderLock(); } if (ClientMarketData.PriceTypeLock.IsReaderLockHeld) { ClientMarketData.PriceTypeLock.ReleaseReaderLock(); } if (ClientMarketData.PriceLock.IsReaderLockHeld) { ClientMarketData.PriceLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.StatusLock.IsReaderLockHeld) { ClientMarketData.StatusLock.ReleaseReaderLock(); } if (ClientMarketData.StylesheetLock.IsReaderLockHeld) { ClientMarketData.StylesheetLock.ReleaseReaderLock(); } if (ClientMarketData.TimeInForceLock.IsReaderLockHeld) { ClientMarketData.TimeInForceLock.ReleaseReaderLock(); } if (ClientMarketData.UserLock.IsReaderLockHeld) { ClientMarketData.UserLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } }
public MatchElement(MatchHistoryDocument matchDocument, ClientMarketData.MatchRow matchRow, FieldArray fields) : base("Match", matchDocument) { // WorkingOrderId AddAttribute("MatchId", matchRow.MatchId); AddAttribute("WorkingOrderId", matchRow.WorkingOrderId); AddAttribute("ContraOrderId", matchRow.ContraOrderId); // Status if (fields[Field.Status]) { AddAttribute("StatusCode", matchRow.StatusCode); AddAttribute("StatusName", matchRow.StatusRow.Mnemonic); } // Blotter if (fields[Field.Blotter]) { AddAttribute("Blotter", matchRow.WorkingOrderRow.BlotterRow.BlotterId); AddAttribute("BlotterName", matchRow.WorkingOrderRow.BlotterRow.ObjectRow.Name); } // Security if (fields[Field.Security]) { AddAttribute("SecurityId", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.SecurityId); AddAttribute("SecuritySymbol", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.Symbol); AddAttribute("SecurityName", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.ObjectRow.Name); if (!matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.IsAverageDailyVolumeNull()) { AddAttribute("AverageDailyVolume", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.AverageDailyVolume / 1000); } if (!matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.IsMarketCapitalizationNull()) { AddAttribute("MarketCapitalization", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.MarketCapitalization / 1000000); } MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.Logo)); Bitmap bitmap = new Bitmap(memoryStream); Bitmap halfBitmap = new Bitmap(bitmap, new Size(bitmap.Width / 2, bitmap.Height / 2)); MemoryStream halfStream = new MemoryStream(); halfBitmap.Save(halfStream, System.Drawing.Imaging.ImageFormat.Png); AddAttribute("SecurityImage", Convert.ToBase64String(halfStream.GetBuffer())); } // Submitted Quantity if (fields[Field.SubmittedQuantity]) { AddAttribute("SubmittedQuantity", matchRow.WorkingOrderRow.SubmittedQuantity); } // Quantity Executed if (fields[Field.Execution]) { decimal executedQuantity = decimal.Zero; decimal marketValue = decimal.Zero; foreach (ClientMarketData.NegotiationRow negotiationRow in matchRow.GetNegotiationRows()) { if (negotiationRow.ExecutionRow != null) { executedQuantity = negotiationRow.ExecutionRow.ExecutionQuantity; marketValue = negotiationRow.ExecutionRow.ExecutionPrice * negotiationRow.ExecutionRow.ExecutionQuantity; } } if (executedQuantity != decimal.Zero) { AddAttribute("ExecutedQuantity", executedQuantity); AddAttribute("AveragePrice", marketValue / executedQuantity); } } // Direction (OrderType) if (fields[Field.OrderTypeCode]) { AddAttribute("OrderTypeCode", matchRow.WorkingOrderRow.OrderTypeCode); AddAttribute("OrderTypeMnemonic", matchRow.WorkingOrderRow.OrderTypeRow.Mnemonic); AddAttribute("OrderTypeDescription", matchRow.WorkingOrderRow.OrderTypeRow.Description); } // Created Time. if (fields[Field.CreatedTime]) { AddAttribute("CreatedTime", matchRow.CreatedTime.ToString("s")); } // Find the pricing record. if (!matchRow.WorkingOrderRow.IsSettlementIdNull()) { ClientMarketData.PriceRow priceRow = ClientMarketData.Price.FindBySecurityId(matchRow.WorkingOrderRow.SecurityId); if (priceRow != null) { if (fields[Field.LastPrice]) { AddAttribute("LastPrice", priceRow.LastPrice); } if (fields[Field.BidPrice]) { AddAttribute("BidPrice", priceRow.BidPrice); } if (fields[Field.AskPrice]) { AddAttribute("AskPrice", priceRow.AskPrice); } if (fields[Field.LastSize]) { AddAttribute("LastSize", priceRow.LastSize); } if (fields[Field.BidSize]) { AddAttribute("BidSize", priceRow.BidSize); } if (fields[Field.AskPrice]) { AddAttribute("AskSize", priceRow.AskSize); } if (fields[Field.Volume]) { AddAttribute("Volume", priceRow.Volume); } if (fields[Field.VolumeWeightedAveragePrice]) { AddAttribute("VolumeWeightedAveragePrice", priceRow.VolumeWeightedAveragePrice); } } } }
/// <summary> /// Method called to retrieve the securityId for the security selected in the match viewere. /// </summary> /// <param name="parameter"></param> private void InitializeData(object parameter) { int id = Int32.MinValue; try { // Lock the tables. System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.MatchLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.NegotiationLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.OrderTypeLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(ClientTimeout.LockWait); // Find the Match record. ClientMarketData.MatchRow matchRow = ClientMarketData.Match.FindByMatchId(this.matchId); ClientMarketData.WorkingOrderRow workingOrderRow = matchRow.WorkingOrderRow; ClientMarketData.OrderTypeRow orderTypeRow = workingOrderRow.OrderTypeRow; ClientMarketData.SecurityRow securityRow = workingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId; if (securityRow != null) { id = securityRow.SecurityId; } } catch (Exception e) { // Write the error and stack trace out to the debug listener EventLog.Error("{0}, {1}", e.Message, e.StackTrace); } finally { // Release the locks. if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.NegotiationLock.IsReaderLockHeld) { ClientMarketData.NegotiationLock.ReleaseReaderLock(); } if (ClientMarketData.OrderTypeLock.IsReaderLockHeld) { ClientMarketData.OrderTypeLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } if (id != Int32.MinValue) { this.Open(id); } }
public MatchElement(MatchDocument matchDocument, ClientMarketData.MatchRow matchRow, FieldArray fields) : base("Match", matchDocument) { // WorkingOrderId AddAttribute("MatchId", matchRow.MatchId); AddAttribute("WorkingOrderId", matchRow.WorkingOrderId); AddAttribute("ContraOrderId", matchRow.ContraOrderId); // Status if (fields[Field.Status]) { AddAttribute("StatusCode", matchRow.StatusCode); AddAttribute("StatusName", matchRow.StatusRow.Mnemonic); TimeSpan timeLeft = matchRow.TimerRow.StopTime.Subtract(matchRow.TimerRow.CurrentTime); AddAttribute("TimeLeft", string.Format("{0:0}:{1:00}", timeLeft.Minutes, timeLeft.Seconds)); AddAttribute("SecondsLeft", Convert.ToInt32(timeLeft.TotalSeconds)); } // Blotter if (fields[Field.Blotter]) { AddAttribute("Blotter", matchRow.WorkingOrderRow.BlotterRow.BlotterId); AddAttribute("BlotterName", matchRow.WorkingOrderRow.BlotterRow.ObjectRow.Name); } // Security if (fields[Field.Security]) { AddAttribute("SecurityId", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.SecurityId); AddAttribute("SecuritySymbol", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.Symbol); AddAttribute("SecurityName", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.ObjectRow.Name); if (!matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.IsAverageDailyVolumeNull()) { AddAttribute("AverageDailyVolume", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.AverageDailyVolume / 1000); } if (!matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.IsMarketCapitalizationNull()) { AddAttribute("MarketCapitalization", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.MarketCapitalization / 1000000); } AddAttribute("VolumeCategoryMnemonic", matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.VolumeCategoryRow.Mnemonic); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.Logo)); Bitmap bitmap = new Bitmap(memoryStream); Bitmap halfBitmap = new Bitmap(bitmap, new Size(bitmap.Width / 2, bitmap.Height / 2)); MemoryStream halfStream = new MemoryStream(); halfBitmap.Save(halfStream, System.Drawing.Imaging.ImageFormat.Png); AddAttribute("SecurityImage", Convert.ToBase64String(halfStream.GetBuffer())); } // Submitted Quantity if (fields[Field.SubmittedQuantity]) { AddAttribute("SubmittedQuantity", matchRow.WorkingOrderRow.SubmittedQuantity); } // Direction (OrderType) if (fields[Field.OrderTypeCode]) { AddAttribute("OrderTypeCode", matchRow.WorkingOrderRow.OrderTypeCode); AddAttribute("OrderTypeMnemonic", matchRow.WorkingOrderRow.OrderTypeRow.Mnemonic); } // Find the pricing record. if (!matchRow.WorkingOrderRow.IsSettlementIdNull()) { ClientMarketData.PriceRow priceRow = ClientMarketData.Price.FindBySecurityId(matchRow.WorkingOrderRow.SecurityId); if (priceRow != null) { if (fields[Field.LastPrice]) { AddAttribute("LastPrice", priceRow.LastPrice); } if (fields[Field.BidPrice]) { AddAttribute("BidPrice", priceRow.BidPrice); } if (fields[Field.AskPrice]) { AddAttribute("AskPrice", priceRow.AskPrice); } if (fields[Field.LastSize]) { AddAttribute("LastSize", priceRow.LastSize); } if (fields[Field.BidSize]) { AddAttribute("BidSize", priceRow.BidSize); } if (fields[Field.AskPrice]) { AddAttribute("AskSize", priceRow.AskSize); } if (fields[Field.Volume]) { AddAttribute("Volume", priceRow.Volume); } if (fields[Field.InterpolatedVolume]) { AddAttribute("InterpolatedVolume", VolumeHelper.CurrentVolumePercentage(DateTime.Now, matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId.AverageDailyVolume, priceRow.Volume)); } if (fields[Field.VolumeWeightedAveragePrice]) { AddAttribute("VolumeWeightedAveragePrice", priceRow.VolumeWeightedAveragePrice); } } } }
/// <summary> /// Marshals the data needed refuse a chance to negotiate a trade. /// </summary> /// <param name="parameter">The thread initialization data.</param> private void DeclineNegotiationThread(object parameter) { // Extract the thread parameters int matchId = (int)parameter; // This batch, if succesfully constructed, will be sent to the server to deline the negotiation. Batch batch = new Batch(); try { // Lock the tables. System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.MatchLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.OrderTypeLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(ClientTimeout.LockWait); // Find the Match record. ClientMarketData.MatchRow matchRow = ClientMarketData.Match.FindByMatchId(matchId); // Construct a command decline the negotiation. AssemblyPlan assembly = batch.Assemblies.Add("Core Service"); TypePlan type = assembly.Types.Add("MarkThree.Guardian.Core.Negotiation"); TransactionPlan transaction = batch.Transactions.Add(); MethodPlan method = transaction.Methods.Add(type, "Insert"); method.Parameters.Add(new InputParameter("matchId", matchId)); method.Parameters.Add(new InputParameter("quantity", 0.0m)); method.Parameters.Add(new InputParameter("statusCode", Status.Declined)); } catch (Exception exception) { // Write the error and stack trace out to the debug listener EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); // This indicates that the batch shouldn't be executed. batch = null; } finally { // Release the locks. if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.OrderTypeLock.IsReaderLockHeld) { ClientMarketData.OrderTypeLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } // If the command batch was built successfully, then execute it. if (batch != null) { try { // Call the web server to rename the object on the database. Note that this method must be called when there // are no locks to prevent deadlocking. That is why it appears in it's own 'try/catch' block. ClientMarketData.Execute(batch); } catch (BatchException batchException) { // Write any server generated error messages to the event log. foreach (Exception exception in batchException.Exceptions) { EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); } } } }
/// <summary> /// Notifies the user that a match opportunity exists. /// </summary> /// <param name="parameter">The thread initialization parameters.</param> private void NotifyUser(object parameter) { // Extract the thread parameters. int matchId = (int)parameter; // The symbol, title and the bitmap for the corporate logo will be retrieved from the data model in the code below. // They will be used to initialize the pop-up dialog after the locks on the data model have been released. string symbol = string.Empty; string title = string.Empty; Bitmap logo = null; try { // Lock the tables. System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.MatchLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.NegotiationLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.OrderTypeLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(ClientTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(ClientTimeout.LockWait); // The match record, working order, order type and security records are used to construct the title, symbol and // logo used by the notification window. ClientMarketData.MatchRow matchRow = ClientMarketData.Match.FindByMatchId(matchId); ClientMarketData.WorkingOrderRow workingOrderRow = matchRow.WorkingOrderRow; ClientMarketData.OrderTypeRow orderTypeRow = workingOrderRow.OrderTypeRow; ClientMarketData.SecurityRow securityRow = workingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId; // Get the security symbol. symbol = securityRow.Symbol; // Create a logo bitmap. if (!securityRow.IsLogoNull()) { MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(securityRow.Logo)); logo = new Bitmap(memoryStream); } // Construct the title for the notification window. title = string.Format("{0} of {1}", orderTypeRow.Description, symbol); } finally { // Release the locks. if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.NegotiationLock.IsReaderLockHeld) { ClientMarketData.NegotiationLock.ReleaseReaderLock(); } if (ClientMarketData.OrderTypeLock.IsReaderLockHeld) { ClientMarketData.OrderTypeLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } // The notification window looks and acts like the Microsoft Instant Messaging window. It will pop up in the lower // right hand corner of the screen with a title, the corporate logo and a chance to either accept or decline the // opportunity for a match. NotificationWindow notificationWindow = new NotificationWindow(); notificationWindow.MatchId = matchId; notificationWindow.Symbol = symbol; notificationWindow.Message = title; notificationWindow.CompanyLogo = logo; notificationWindow.Accept += new MatchEventHandler(AcceptNegotiation); notificationWindow.Decline += new MatchEventHandler(DeclineNegotiation); notificationWindow.ChangeOptions += new EventHandler(ChangeOptions); notificationWindow.Show(); }
/// <summary> /// Opens the document for the given object and it's argument. /// </summary> /// <param name="matchId">The primary identifier of the object to open.</param> /// <param name="argument">Options that can be used to further specify the document's properties.</param> public void OpenMatchCommand(object parameter) { // Extract the command argument. int matchId = (int)parameter; // This will force an empty placement and placement document to appear until we get some data. It's the same // as forcing a drawing of the headers only. this.matchId = matchId; try { // Lock the tables System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); ClientMarketData.EquityLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.MatchLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.ObjectLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.PriceLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.SecurityLock.AcquireReaderLock(CommonTimeout.LockWait); ClientMarketData.WorkingOrderLock.AcquireReaderLock(CommonTimeout.LockWait); // Install the event handlers. The ClientMarketData component will advise us when the data has changed. ClientMarketData.MatchRow matchRow = ClientMarketData.Match.FindByMatchId(matchId); if (matchRow == null) { return; } ClientMarketData.SecurityRow securityRow = matchRow.WorkingOrderRow.SecurityRowBySecurityWorkingOrderSecurityId; ClientMarketData.EquityRow equityRow = ClientMarketData.Equity.FindByEquityId(securityRow.SecurityId); if (equityRow == null) { return; } string securityName = securityRow.ObjectRow.Name; ClientMarketData.PriceRow priceRow = ClientMarketData.Price.FindBySecurityId(securityRow.SecurityId); decimal price = priceRow.LastPrice; Invoke(displayDelegate, new object[] { securityName, price }); } finally { if (ClientMarketData.EquityLock.IsReaderLockHeld) { ClientMarketData.EquityLock.ReleaseReaderLock(); } if (ClientMarketData.MatchLock.IsReaderLockHeld) { ClientMarketData.MatchLock.ReleaseReaderLock(); } if (ClientMarketData.ObjectLock.IsReaderLockHeld) { ClientMarketData.ObjectLock.ReleaseReaderLock(); } if (ClientMarketData.PriceLock.IsReaderLockHeld) { ClientMarketData.PriceLock.ReleaseReaderLock(); } if (ClientMarketData.SecurityLock.IsReaderLockHeld) { ClientMarketData.SecurityLock.ReleaseReaderLock(); } if (ClientMarketData.WorkingOrderLock.IsReaderLockHeld) { ClientMarketData.WorkingOrderLock.ReleaseReaderLock(); } System.Diagnostics.Debug.Assert(!ClientMarketData.IsLocked); } }