Example #1
0
        }//Shutdown()

        #endregion//Public Methods


        #region ActiveGrid Update Methods
        // *****************************************************************
        // ****                  Active Grid Methods                    ****
        // *****************************************************************
        //
        //
        /// <summary>
        /// This method should be called whenever the PositionBookChanges for a specific instrument.
        /// The instrument should have already triggered the PositionBookCreated event, but we double check just in case.
        /// Then, we proceed to acquire the lock for its fill book, extract data from it, and update all the cells.
        /// </summary>
        /// <param name="eventArgs"></param>
        private void ActiveGridUpdatePosition(FillHub.PositionBookChangedEventArgs eventArgs)
        {
            InstrumentName instrumentName = eventArgs.Instrument;

            if (!m_InstrumentInfos.ContainsKey(instrumentName.FullName))
            {
                return;
            }

            // Update info about the position book that changed.
            InstrumentRowData info = m_InstrumentInfos[instrumentName.FullName];
            IFillBook         positionBook;

            if (m_FillHub.TryEnterReadBook(instrumentName, out positionBook))
            {
                info.Position        = positionBook.NetPosition;
                info.StartingRealPnL = Math.Round(positionBook.RealizedStartingDollarGains, 2);
                info.RealPnL         = Math.Round(positionBook.RealizedDollarGains, 2);
                info.UnrealPnL       = Math.Round(positionBook.UnrealizedDollarGains(), 2);
                info.AverageCost     = Math.Round(positionBook.AveragePrice, info.MarketPriceDecimals);
                m_FillHub.ExitReadBook(instrumentName);
            }

            // Update the cells.
            UpdateActiveGridCell(instrumentName.FullName, "Position", info.Position, true);
            UpdateActiveGridCell(instrumentName.FullName, ColumnName_StartingRealPnL, info.StartingRealPnL, false);
            UpdateActiveGridCell(instrumentName.FullName, ColumnName_RealPnL, info.RealPnL, false);
            UpdateActiveGridCell(instrumentName.FullName, ColumnName_UnrealPnL, info.UnrealPnL, false);
            UpdateActiveGridCell(instrumentName.FullName, ColumnName_TotalPnL, info.TotalPnL, false);
            UpdateActiveGridCell(instrumentName.FullName, "AvePrice", info.AverageCost, false);

            // Update group pnl
            ActiveGridUpdateGroup(GetProductGroupKey(instrumentName));
        }//ActiveGridUpdatePosition()
Example #2
0
        /// <summary>
        /// Must be called by GUI thread.
        /// </summary>
        private void UpdateRejectList()
        {
            // Disconnect data source.
            this.dataGridView1.SuspendLayout();
            this.dataGridView1.DataSource = null;
            m_RejectedFills.Clear();

            // Create new list.
            List <InstrumentName> m_InstrumentNames = new List <InstrumentName>();

            m_FillHub.GetInstrumentNames(ref m_InstrumentNames);
            foreach (InstrumentName name in m_InstrumentNames)
            {
                IFillBook positionBook;
                if (m_FillHub.TryEnterReadBook(name, out positionBook))
                {
                    if (positionBook is BookLifo)
                    {
                        BookLifo book = (BookLifo)positionBook;
                        book.GetRejectedFills(ref m_RejectedFills);
                    }
                    m_FillHub.ExitReadBook(name);
                }
            }//next instrument name

            // Reconnect data source.
            this.dataGridView1.DataSource = m_RejectedFills;
            this.dataGridView1.ResumeLayout();
        }// UpdateRejectList()
        }// UpdateInstruments()

        //
        //
        private void UpdateInstrumentDetails()
        {
            // Get selected fill hub.
            if (comboBoxFillHubs.SelectedItem == null)
            {
                return;
            }
            string  fillHubName = comboBoxFillHubs.SelectedItem.ToString();
            FillHub fillHub     = null;

            if (!m_FillHubs.TryGetValue(fillHubName, out fillHub))
            {
                return;
            }

            // Get selected instrument
            if (comboBoxInstruments.SelectedItem == null)
            {
                return;
            }
            InstrumentName instrument = new InstrumentName();

            if (comboBoxInstruments.SelectedItem is InstrumentName)
            {
                instrument = (InstrumentName)comboBoxInstruments.SelectedItem;
            }

            // Get information about this instrument.
            IFillBook posBook = null;

            if ((!instrument.IsEmpty) && fillHub.TryEnterReadBook(instrument, out posBook))
            {
                double x = posBook.RealizedDollarGains;
                this.textBoxRealPnL.Text    = x.ToString();
                this.textBoxRealPnLNew.Text = "0";

                x = posBook.RealizedStartingDollarGains;
                this.textBoxStartRealPnL.Text    = x.ToString();
                this.textBoxStartRealPnLNew.Text = "0";

                fillHub.ExitReadBook(instrument);
            }
            else
            {   // No specific instrument is selected
                this.textBoxRealPnL.Text         = "0";
                this.textBoxRealPnLNew.Text      = "0";
                this.textBoxStartRealPnL.Text    = "0";
                this.textBoxStartRealPnLNew.Text = "0";
            }
        }// UpdateInstrumentDetails()
Example #4
0
        }//UpdateMarketTopics.

        //
        //
        //
        //
        private void CategorizeTopic(Topic topic)
        {
            string hubName = topic.HubName;

            if (string.IsNullOrEmpty(hubName))
            {
                hubName = DefaultHubName;
            }

            // New
            Dictionary <InstrumentName, Dictionary <SubscriptionType, List <Topic> > > m_Subscriptions = null;

            if (!m_SubscriptionsByHubName.TryGetValue(hubName, out m_Subscriptions))
            {   // Create a new subscription list for this hub.
                m_Subscriptions = new Dictionary <InstrumentName, Dictionary <SubscriptionType, List <Topic> > >();
                m_SubscriptionsByHubName.Add(hubName, m_Subscriptions);
            }

            // Create subscription lists for any new Instruments found.
            if (topic.Type != SubscriptionType.Unknown && !m_Subscriptions.ContainsKey(topic.Instrument))
            {
                m_Subscriptions.Add(topic.Instrument, new Dictionary <SubscriptionType, List <Topic> >());
            }

            Dictionary <SubscriptionType, List <Topic> > subscriptionList;                      // A list of all subscriptions of the desired type!

            if (m_Subscriptions.TryGetValue(topic.Instrument, out subscriptionList))
            {
                if (!subscriptionList.ContainsKey(topic.Type))
                {
                    subscriptionList.Add(topic.Type, new List <Topic>());
                }
                List <Topic> topicList;
                if (subscriptionList.TryGetValue(topic.Type, out topicList))                // list of subscriptions for desired type and instrument.
                {
                    if (!topicList.Contains(topic))
                    {   // This is a new topic.  The user may ask for same subscription multiple times.
                        // That would be inefficient, but if he does then this list will contain multiple topics
                        // that all show the same information.
                        Log.NewEntry(LogLevel.Minor, "New subscription {0}.", topic);
                        topicList.Add(topic);
                    }
                    else
                    {   // We have already categorized this topic.  Do nothing.
                    }
                }
                else
                {
                    Log.NewEntry(LogLevel.Warning, "CategorizeTopic problem 1.");
                }
            }
            else
            {
                Log.NewEntry(LogLevel.Warning, "CategorizeTopic problem 2.");
            }
            //
            // Calculate initial value.
            //
            FillHub m_FillHub = null;

            if (m_FillHubs.TryGetValue(hubName, out m_FillHub))
            {
                switch (topic.Type)
                {
                case SubscriptionType.Position:
                    FillBookLifo book;
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.NetPosition.ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("0");
                    }
                    break;

                case SubscriptionType.AvePositionCost:
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.AveragePrice.ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("");
                    }
                    break;

                case SubscriptionType.RealPnL:
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.RealizedDollarGains.ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("");
                    }
                    break;

                case SubscriptionType.StartingRealPnL:
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.RealizedStartingDollarGains.ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("");
                    }
                    break;

                case SubscriptionType.UnRealPnL:
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.UnrealizedDollarGains().ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("");
                    }
                    break;

                case SubscriptionType.Volume:
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.Volume.ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("");
                    }
                    break;

                case SubscriptionType.StartingVolume:
                    if (m_FillHub.TryEnterReadBook(topic.Instrument, out book))
                    {
                        topic.SetValue(book.StartingVolume.ToString());
                        m_FillHub.ExitReadBook(topic.Instrument);
                    }
                    else
                    {
                        topic.SetValue("");
                    }
                    break;

                default:
                    break;
                }
            }
            else
            {
                topic.SetValue("Unknown HubName");
            }
        }//CategorizeTopic()
Example #5
0
        }//ProcessRequest()

        //
        //
        // *********************************************************
        // ****             Process FillHub Event               ****
        // *********************************************************
        /// <summary>
        /// Processes events from a fill hub.  Events include PositionBook changed or created events.
        /// The fillhub ptr and InstrumentName (corresponding to the book) is passed to us.  We lookup any subscription
        /// topics for these.
        /// </summary>
        /// <param name="eventArg"></param>
        private void ProcessFillHubEvent(FillHub.PositionBookChangedEventArgs eventArg)
        {
            FillHub callingHub = eventArg.Sender;                               // pointer to the particular FillHub from which event was created.
            string  name       = callingHub.Name;

            if (string.IsNullOrEmpty(name))
            {
                name = DefaultHubName;
            }
            InstrumentName instrument = eventArg.Instrument;

            // Search for the FillHub in our list of subscriptions.
            Dictionary <InstrumentName, Dictionary <SubscriptionType, List <Topic> > > m_Subscriptions = null;

            if (!m_SubscriptionsByHubName.TryGetValue(name, out m_Subscriptions))
            {
                return;
            }

            Dictionary <SubscriptionType, List <Topic> > subscriptionList;

            if (m_Subscriptions.TryGetValue(instrument, out subscriptionList) && subscriptionList.Count > 0)
            {   // We have some active subscriptions for this instrument.
                string       newValue;
                FillBookLifo book;
                if (callingHub.TryEnterReadBook(instrument, out book))
                {   // We found the book that changed its position.  Update all things that may have changed.
                    //
                    // Update everything about this book.
                    //
                    List <Topic> topicList;
                    if (subscriptionList.TryGetValue(SubscriptionType.Position, out topicList))
                    {
                        newValue = book.NetPosition.ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.AvePositionCost, out topicList))
                    {
                        newValue = book.AveragePrice.ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.RealPnL, out topicList))
                    {
                        newValue = book.RealizedDollarGains.ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.StartingRealPnL, out topicList))
                    {
                        newValue = book.RealizedStartingDollarGains.ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.UnRealPnL, out topicList))
                    {
                        newValue = book.UnrealizedDollarGains().ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.Volume, out topicList))
                    {
                        newValue = book.Volume.ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.StartingVolume, out topicList))
                    {
                        newValue = book.StartingVolume.ToString();
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }


                    callingHub.ExitReadBook(instrument);
                }
                else
                {   // We have a subscription request for this instrument, we obtained an event from it,
                    // yet, when we inquire about this instrument, it is unknown!?!?
                    // This CAN happen whenever an specific fill book is deleted by the user.
                    List <Topic> topicList;
                    newValue = "0";
                    if (subscriptionList.TryGetValue(SubscriptionType.Position, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.AvePositionCost, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.RealPnL, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.StartingRealPnL, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.UnRealPnL, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.Volume, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                    if (subscriptionList.TryGetValue(SubscriptionType.StartingVolume, out topicList))
                    {
                        foreach (Topic topic in topicList)
                        {
                            if (!topic.PeekAtValue().Equals(newValue))
                            {
                                topic.SetValue(newValue);
                            }
                        }
                    }
                }
            }
            //else
            //    Log.NewEntry(LogLevel.Minor, "ProcessFillHubEvent: No subscriptions for {0}.",instrument);   // user hasn't subscribed to this instrument.
        }//ProcessFillHubEvent().