public static bool AddOrUpdateStock(Stock stock)
 {
     stock.Symbol = stock.Symbol.Trim().ToUpper();
     if (Stocks.ContainsKey(stock.Symbol))
     {
         Stocks[stock.Symbol].Price = stock.Price;
         return false;
     }
     Stocks.TryAdd(stock.Symbol, stock);
     return true;
 }
Example #2
0
        public static bool TryUpdateStockPrice(Stock stock)
        {
            // Randomly choose whether to udpate this stock or not
            var r = UpdateOrNotRandom.NextDouble();
            if (r > 0.1)
            {
                return false;
            }

            // Update the stock price by a random factor of the range percent
            var random = new Random((int)Math.Floor(stock.Price));
            var percentChange = random.NextDouble() * RangePercent;
            var pos = random.NextDouble() > 0.51;
            var change = Math.Round(stock.Price * (decimal)percentChange, 2);
            change = pos ? change : -change;

            stock.Price += change;
            return true;
        }
 /// <summary>
 /// Do a conditional send to only clients listening for the actual stock
 /// </summary>
 /// <param name="stock"></param>
 public void Tick(Stock stock)
 {
     //Send only to client having this stock in their list
     this.InvokeTo<StockController>(p => p.MyStocks.Contains(stock.Symbol), stock, "tick");
 }