コード例 #1
0
ファイル: StocksController.cs プロジェクト: AlexKarlsen/ITONK
        public async System.Threading.Tasks.Task PostAsync([FromUri] string value)
        {
            // Decoding input
            var firstDelimiter = value.IndexOf(';');
            var lastDelimiter  = value.LastIndexOf(';');

            var username  = value.Substring(0, firstDelimiter);
            var stockname = value.Substring(firstDelimiter + 1, value.Length - (firstDelimiter + 1));

            stockname = stockname.Substring(0, stockname.IndexOf(';'));

            var amount = value.Substring(lastDelimiter + 1, value.Length - (lastDelimiter + 1));

            int amountInt;

            if (int.TryParse(amount, out amountInt))
            {
                var stock = new Common.Stock()
                {
                    StockName = stockname, StockType = Common.Stock.SaleOrPurchase.Purchase, Username = username, Amount = amountInt
                };

                await Buyer.PlaceRequestAsync(stock);
            }
        }
コード例 #2
0
        public void AddStock(string symbol, decimal? price = null)
        {
            var stock = new Stock
            {
                Symbol = symbol,
                Price = price.GetValueOrDefault(_priceRandomizer.Next(10, 700))
            };

            AddStock(stock);
        }
コード例 #3
0
        public void Handle(IStockChangeEvent message)
        {
            var symbol = message.Symbol;
            var stock = _stocks.GetStockBySymbol(symbol);

            if (stock == null)
            {
                stock = new Stock { Symbol = symbol, Price = message.Price };
                _stocks.AddStock(stock);

                GetClients().addStock(message);
            }
            else
            {
                stock.Price = message.Price;
                GetClients().updateStockPrice(message);
            }
        }
コード例 #4
0
        public static IStockChangeEvent Create(Stock stock)
        {
            StockPriceChange change;

            if (stock.Change > 0)
                change = new StockPriceIncrease();
            else if (stock.Change < 0)
                change = new StockPriceDecrease();
            else
            {
                return null;
            }

            change.Change = stock.Change;
            change.DayHigh = stock.DayHigh;
            change.DayLow = stock.DayLow;
            change.DayOpen = stock.DayOpen;
            change.LastChange = stock.LastChange;
            change.PercentChange = stock.PercentChange;
            change.Price = stock.Price;
            change.Symbol = stock.Symbol;

            return change;
        }
コード例 #5
0
 public void AddStock(Stock stock)
 {
     _stocks.Add(stock.Symbol, stock);
 }
コード例 #6
0
        public void AddStock(Stock stock)
        {
            _stocks.AddStock(stock);
            if (stock == null)
                return;

            stock.Price += GetRandomPriceChange(stock);

            if (StockChanged != null)
                StockChanged(this, new EventArgs<Stock>(stock));
        }
コード例 #7
0
        private decimal GetRandomPriceChange(Stock stock)
        {
            // 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() > .51;
            var change = Math.Round(stock.Price * (decimal)percentChange, 2);
            change = pos ? change : -change;

            return change;
        }