示例#1
0
        private void PortfolioData_DataChanged(object sender, DataEventArgs <RowChangeBase[]> e)
        {
            PortfolioData portfolioData = (PortfolioData)sender;
            decimal       portfolioPnl;

            lock (this._summary)
            {
                decimal[] pnl        = this._pnls[portfolioData.PortfolioId];
                DataRow   summaryRow = this._summary.Rows.Find(portfolioData.PortfolioId);
                portfolioPnl = summaryRow.Field <decimal>("Pnl");
                int portfolioDataPnlOrdinal = PositionData.SchemaTable.Columns.IndexOf("Pnl");
                foreach (RowChangeBase rowChange in e.Data)
                {
                    string ticker = (string)rowChange.RowKey;
                    int    index  = Array.BinarySearch(this._tickers, ticker);
                    if (rowChange.ChangeType == RowChangeType.Added)
                    {
                        decimal rowPnl = (decimal)((RowAdded)rowChange).Data[portfolioDataPnlOrdinal];
                        portfolioPnl += (pnl[index] = rowPnl);
                    }
                    else
                    {
                        decimal currentRowPnl = pnl[index];
                        switch (rowChange.ChangeType)
                        {
                        case RowChangeType.Removed:
                            portfolioPnl -= currentRowPnl;
                            pnl[index]    = 0m;
                            break;

                        case RowChangeType.ColumnsChanged:
                            ColumnChange columnChange = ((RowColumnsChanged)rowChange).ColumnChanges.FirstOrDefault(change => change.ColumnOrdinal == portfolioDataPnlOrdinal);
                            if (columnChange == null)
                            {
                                continue;
                            }
                            decimal newRowPnl = (decimal)columnChange.Value;
                            pnl[index]    = newRowPnl;
                            portfolioPnl += newRowPnl - currentRowPnl;
                            break;
                        }
                    }
                }
                summaryRow.SetField("Pnl", portfolioPnl);
                summaryRow.AcceptChanges();
            }
            lock (this._rowChanges)
            {
                int index = this._rowChanges.BinarySearchByValue(portfolioData.PortfolioId, change => (string)change.RowKey);
                if (index < 0)
                {
                    this._rowChanges.Insert(~index, new RowColumnsChanged
                    {
                        RowKey        = portfolioData.PortfolioId,
                        ColumnChanges =
                        {
                            new ColumnChange
                            {
                                ColumnOrdinal = this._summary.Columns.IndexOf("Pnl"),
                                Value         = portfolioPnl
                            }
                        }
                    });
                }
                else
                {
                    RowColumnsChanged rowChange = (RowColumnsChanged)this._rowChanges[index];
                    rowChange.ColumnChanges[0].Value = portfolioPnl;
                }
            }
        }