Ejemplo n.º 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()
Ejemplo n.º 2
0
        }//ActiveGridAddNewRow()

        //
        //
        //
        //
        //
        private void ActiveGridRemoveRow(FillHub.PositionBookChangedEventArgs eventArgs)
        {
            InstrumentName instrumentName = eventArgs.Instrument;           // instrument we want to remove.

            if (m_InstrumentInfos.ContainsKey(instrumentName.FullName) &&
                activeGrid1.RowExists(instrumentName.FullName))
            {
                // Remove it from groups.
                string strKey = instrumentName.Product.Exchange;
                if (m_ExchangeGroups.ContainsKey(strKey) && m_ExchangeGroups[strKey].Contains(instrumentName.FullName))
                {
                    m_ExchangeGroups[strKey].Remove(instrumentName.FullName);
                }
                strKey = GetProductGroupKey(instrumentName);
                if (m_ProductGroups.ContainsKey(strKey) && m_ProductGroups[strKey].Contains(instrumentName.FullName))
                {
                    m_ProductGroups[strKey].Remove(instrumentName.FullName);
                }

                // Remove its info now
                if (m_InstrumentInfos.ContainsKey(instrumentName.FullName))
                {
                    m_InstrumentInfos.Remove(instrumentName.FullName);
                }

                // Remove it from the row now.
                SKACERO.ActiveRow row = activeGrid1.Items[instrumentName.FullName];
                this.activeGrid1.AllowFlashing = false;
                this.activeGrid1.BeginUpdate();
                string s = row.Name;
                row.Name = string.Format("{0}{1}", row.Name, row.GetHashCode());
                this.activeGrid1.Items.Remove(row);
                this.activeGrid1.EndUpdate();
                ActiveGridUpdateGroup(GetProductGroupKey(instrumentName));
            }
        }// ActiveGridRemoveRow()
Ejemplo n.º 3
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().
Ejemplo n.º 4
0
        }//UpdateActiveGridCell()

        #endregion //ActiveGrid Update Methods


        #region ActiveGrid Creation Methods
        // *****************************************************************
        // ****             Active Grid Creation Methods                ****
        // *****************************************************************
        //
        //
        //
        private void ActiveGridAddNewRow(FillHub.PositionBookChangedEventArgs eventArgs)
        {
            InstrumentName instrumentName = eventArgs.Instrument;

            if (m_InstrumentInfos.ContainsKey(instrumentName.FullName) &&
                activeGrid1.RowExists(instrumentName.FullName))
            {   // We already have a row for this instrument!  Update its values.
                // Update things that depend on the the currency rate too... since this may have updated!  [04 Jun 2013]
                InstrumentRowData info = m_InstrumentInfos[instrumentName.FullName];
                IFillBook         positionBook;
                if (m_FillHub.TryEnterReadBook(instrumentName, out positionBook))
                {
                    info.StartingRealPnL = Math.Round(positionBook.RealizedStartingDollarGains, 2);
                    info.RealPnL         = Math.Round(positionBook.RealizedDollarGains, 2);
                    info.UnrealPnL       = Math.Round(positionBook.UnrealizedDollarGains(), 2);
                    m_FillHub.ExitReadBook(instrumentName);
                }

                SKACERO.ActiveRow row = activeGrid1.Items[instrumentName.FullName];
                row.SubItems[GetCellKey(instrumentName.FullName, ColumnName_StartingRealPnL)].Text = info.StartingRealPnL.ToString("0.00");
                row.SubItems[GetCellKey(instrumentName.FullName, ColumnName_RealPnL)].Text         = info.RealPnL.ToString("0.00");
                row.SubItems[GetCellKey(instrumentName.FullName, ColumnName_UnrealPnL)].Text       = info.UnrealPnL.ToString("0.00");
                row.SubItems[GetCellKey(instrumentName.FullName, "Expiry")].Text   = m_InstrumentInfos[instrumentName.FullName].ExpirationDate.ToString("yyyy-MM-dd");
                row.SubItems[GetCellKey(instrumentName.FullName, "Currency")].Text = info.CurrencyCode;
                row.SubItems[GetCellKey(instrumentName.FullName, "SortKey")].Text  = string.Format("{0}{1}{2}{3}", instrumentName.Product.Exchange, instrumentName.Product.Type.ToString(), instrumentName.Product.ProductName, m_InstrumentInfos[instrumentName.FullName].ExpirationDate.ToString("yyyyMMdd"));
                activeGrid1.Sort();                                                                 // sort again, now we've changed the SortKey.
                return;
            }

            //
            // Create all tables we need for each new instrument.
            //
            m_InstrumentInfos.Add(instrumentName.FullName, new InstrumentRowData(instrumentName));

            string strKey = instrumentName.Product.Exchange;

            if (!m_ExchangeGroups.ContainsKey(strKey))                                              // Exchange group
            {
                m_ExchangeGroups.Add(strKey, new List <string>());                                  // maintain an entry for each ex
            }
            if (!m_ExchangeGroups[strKey].Contains(instrumentName.FullName))
            {
                m_ExchangeGroups[strKey].Add(instrumentName.FullName);
                if (m_ExchangeGroups[strKey].Count >= m_MinMembersForExchangeGroupRow &&
                    (!activeGrid1.Items.ContainsKey(string.Format("{0}{1}", GroupRowNamePrefix, strKey))))
                {
                    CreateSummaryRows(strKey, instrumentName.Product.Exchange, string.Empty);
                }
            }
            strKey = GetProductGroupKey(instrumentName);                                            // Product group: construct the exch+prod group name.
            if (!m_ProductGroups.ContainsKey(strKey))                                               // first time this product group showed up!
            {
                m_ProductGroups.Add(strKey, new List <string>());                                   // create a place for this prod group to hold its membership list.
            }
            if (!m_ProductGroups[strKey].Contains(instrumentName.FullName))                         // If this instrument is not yet part of group, add him.
            {
                m_ProductGroups[strKey].Add(instrumentName.FullName);                               // add new member of group
                if ((m_ProductGroups[strKey].Count >= m_MinMembersForProductGroupRow) &&
                    (!activeGrid1.Items.ContainsKey(string.Format("{0}{1}", GroupRowNamePrefix, strKey))))
                {
                    CreateSummaryRows(strKey, string.Empty, instrumentName.Product.ProductName);    // need to add a new summary line
                }
            }

            //
            // Create the row.
            //
            SKACERO.ActiveRow aRow = new SKACERO.ActiveRow();
            aRow.Name = instrumentName.FullName;
            aRow.Text = instrumentName.FullName;                                                    // this will appear in the zeroth column of the row.
            for (int i = 1; i < this.activeGrid1.Columns.Count; i++)
            {
                SKACERO.ActiveRow.ActiveCell cell = new SKACERO.ActiveRow.ActiveCell(aRow, String.Empty);
                cell.Name         = GetCellKey(instrumentName.FullName, this.activeGrid1.Columns[i].Name);
                cell.DecimalValue = Decimal.Zero;
                cell.PreTextFont  = new Font("Arial", cell.Font.Size);
                cell.PostTextFont = new Font("Arial", cell.Font.Size);
                aRow.SubItems.Add(cell);
            }

            // Load constant cells
            aRow.SubItems[GetCellKey(instrumentName.FullName, "Expiry")].Text         = m_InstrumentInfos[instrumentName.FullName].ExpirationDate.ToString("yyyy-MM-dd");
            aRow.SubItems[GetCellKey(instrumentName.FullName, "SortKey")].Text        = string.Format("{0}{1}{2}{3}", instrumentName.Product.Exchange, instrumentName.Product.Type.ToString(), instrumentName.Product.ProductName, m_InstrumentInfos[instrumentName.FullName].ExpirationDate.ToString("yyyyMMdd"));
            aRow.SubItems[GetCellKey(instrumentName.FullName, ColumnName_Alias)].Text = instrumentName.SeriesName;

            // Load Exchange cell optionally
            strKey = instrumentName.Product.Exchange;
            if (IsSuppressRepetativeExchangeProductLabels && activeGrid1.Items.ContainsKey(string.Format("{0}{1}", GroupRowNamePrefix, strKey)))
            {
                aRow.SubItems[GetCellKey(instrumentName.FullName, "Exchange")].Text = string.Empty;
            }
            else
            {
                aRow.SubItems[GetCellKey(instrumentName.FullName, "Exchange")].Text = instrumentName.Product.Exchange;
            }

            // Load Product cell, optionally.
            strKey = GetProductGroupKey(instrumentName);
            if (IsSuppressRepetativeExchangeProductLabels && activeGrid1.Items.ContainsKey(string.Format("{0}{1}", GroupRowNamePrefix, strKey)))
            {
                aRow.SubItems[GetCellKey(instrumentName.FullName, "Product")].Text = string.Empty;
            }
            else
            {
                aRow.SubItems[GetCellKey(instrumentName.FullName, "Product")].Text = instrumentName.Product.ProductName;
            }

            // Set currency column as empty string.
            aRow.SubItems[GetCellKey(instrumentName.FullName, "Currency")].Text = string.Empty;

            // Add row to our collection
            this.activeGrid1.SuspendLayout();
            try
            {
                if (!this.activeGrid1.Items.ContainsKey(aRow.Name))
                {
                    this.activeGrid1.Items.Add(aRow);
                }
            }
            catch (Exception e)
            {
                if (Log != null)
                {
                    Log.NewEntry(LogLevel.Major, "ActiveGridAddNewRow: Failed to add new row with name {0}. Execption {1}", aRow.Name, e.Message);
                }
            }
            activeGrid1.Sort();
            this.activeGrid1.ResumeLayout();

            // Update the row
            ActiveGridUpdatePosition(eventArgs);
        }//ActiveGridAddNewRow()