public void ProcessTrades(IEnumerable <Trade> trades) { List <RowChangeBase> list = new List <RowChangeBase>(); PositionChangeProcessor changeProcessor = new PositionChangeProcessor(this._dataTable); lock (this) { foreach (Trade trade in trades) { int index = this._positions.BinarySearchByValue(trade.Security.Ticker, pos => pos.Ticker); if (index < 0) { Position position = new Position { Ticker = trade.Security.Ticker, Description = trade.Security.Description }; PortfolioData.ProcessTrade(position, trade.Quantity, trade.Price); this._positions.Insert(~index, position); DataRow dataRow = this.CreateRow(position); this._dataTable.Rows.InsertAt(dataRow, ~index); list.Add(new RowAdded { RowKey = position.Ticker, Data = dataRow.ItemArray }); } else { Position position = this._positions[index]; position.PropertyChanged += changeProcessor.PropertyChangedEventHandler; PortfolioData.ProcessTrade(position, trade.Quantity, trade.Price); position.PropertyChanged -= changeProcessor.PropertyChangedEventHandler; } } } this.OnDataChanged(list.Count == 0 ? changeProcessor.RowChanges : list.Concat(changeProcessor.RowChanges)); }
public void ProcessPriceChanges(IEnumerable <PriceChange> priceChanges) { PositionChangeProcessor changeProcessor = new PositionChangeProcessor(this._dataTable); lock (this) { foreach (PriceChange priceChange in priceChanges) { int index = this._positions.BinarySearchByValue(priceChange.Ticker, pos => pos.Ticker); if (index < 0) { continue; } Position position = this._positions[index]; position.PropertyChanged += changeProcessor.PropertyChangedEventHandler; PortfolioData.CalculatePosition(position, priceChange.Price); position.PropertyChanged -= changeProcessor.PropertyChangedEventHandler; } if (changeProcessor.RowChanges.Count == 0) { return; } this._dataTable.AcceptChanges(); } this.OnDataChanged(changeProcessor.RowChanges); }