public void OnChangeRaiseEvent(StockPriceChangeEvent stockPriceEvt) { if (Handler != null) { Handler(this, stockPriceEvt); } }
/// <summary> /// Helper method to find out the rowIndex in the grid for a given stock object /// </summary> /// <param name="evt">StockPrice Change event object</param> /// <returns>rowindex as int</returns> private int FindCompany(StockPriceChangeEvent evt) { int rowIndex = -1, index = -1; TickerSymbols.ToList().ForEach((symbol) => { ++index; if (_cancellationToken.IsCancellationRequested) { return; } if ((string)stockTickerGrid["Company", index].Value == evt.Company) { rowIndex = stockTickerGrid["Company", index].RowIndex; } }); return(rowIndex); }
/// <summary> /// Core method which starts the ticker by generating random inputs /// </summary> public void StartServer() { Logger.Instance.Log("Starting the ticker price updater"); for (; ;) { //_token.ThrowIfCancellationRequested(); if (_token.IsCancellationRequested) { break; } var evt = new StockPriceChangeEvent( StockDataVisualizer.TickerSymbols[new Random().Next(0, StockDataVisualizer.TickerSymbols.Count)], new Random().Next(1, 700), new Random().Next(1, 750), new Random().Next(1, 600), new Random().Next(1, 650)); //:~ Deliberate attempt to slow down screen refresh interval Thread.Sleep(150); ThreadPool.QueueUserWorkItem(Parallellize, evt); } }
/// <summary> /// The Main handler for any StockPriceChangeEvent which causes the List to /// be updated with new values and also update the grid /// </summary> /// <param name="sender">Sender of the event</param> /// <param name="evt">StockPrice change object</param> public void UpdateReceived(object sender, StockPriceChangeEvent evt) { var stockObj = stocks.Find(x => x.Company.Equals(evt.Company)); var oldBidValue = stockObj.Bid; var oldAskValue = stockObj.Ask; var oldBidAskSpread = stockObj.BidAskSpread; var oldMarketPrice = stockObj.MarketPrice; stockObj.Bid = evt.BidPrice; stockObj.Ask = evt.AskPrice; stockObj.Volume = stockObj.Volume + 1; //:~ Use Func to calculate Mid Market Price Func <double, double, double> calculateMidMarketPrice = (bidPrice, askPrice) => (bidPrice + askPrice) / 2.0; stockObj.MarketPrice = calculateMidMarketPrice(evt.BidPrice, evt.AskPrice); //:~ Use Func to calculate difference between bid and askPrice difference Func <double, double, double> calculateBidAskSpread = (bidPrice, askPrice) => (askPrice - bidPrice) / 100.0; double spread = calculateBidAskSpread(evt.BidPrice, evt.AskPrice); stockObj.BidAskSpread = spread; //:~ Use Func to streamline method to determine paint color Func <double, double, Color> determineColorToPaint = (x, y) => x > y ? Color.Green : Color.Red; int rowIndex = FindCompany(evt); //:~ Hate this hack and the *IF* if (!_cancellationToken.IsCancellationRequested) { this.stockTickerGrid["Bid", rowIndex].Style.BackColor = determineColorToPaint(((double)stockObj.Bid), (double)oldBidValue); this.stockTickerGrid["Ask", rowIndex].Style.BackColor = determineColorToPaint(((double)stockObj.Ask), (double)oldAskValue); this.stockTickerGrid["BidAskSpread", rowIndex].Style.BackColor = determineColorToPaint(oldBidAskSpread, spread); this.stockTickerGrid["MarketPrice", rowIndex].Style.BackColor = determineColorToPaint(oldMarketPrice, stockObj.MarketPrice); } }