public ActionResult QuoteHistoryForPlayers()
        {
            StockEngine engine = new YahooStockEngine();

            DateTime lastWeek = DateTime.Today.AddDays(-7);
            IEnumerable<QuoteViewModel> model = engine.AllQuotesForPlayersAfter(lastWeek)
                .OrderByDescending(x => x.CreateDate)
                .OrderBy(x => x.Symbol)
                .Select(x => new QuoteViewModel(x));
            return View(model);
        }
        public ActionResult PlayerHistory(string playerName)
        {
            ViewData["PlayerName"] = playerName;

            List<PlayerViewModel> players = new List<PlayerViewModel>();
            StockEngine engine = new YahooStockEngine();
            IEnumerable<Investment> investments = engine.GetInvestmentHistoryForPlayer(playerName);
            IEnumerable<InvestmentViewModel> model = investments.Select(x => new InvestmentViewModel(x));

            return View(model);
        }
        private static void Initialize()
        {
            StockEngine engine = new YahooStockEngine();
            engine.ReInitializeAllPlayers(400);
            IEnumerable<Quote> stocks = engine.LookupQuotesForPlayers(engine.GetAllPlayers().Select(x => x.Name).ToArray());
            engine.InsertQuotes(stocks);

            Log.Information("Saved {0} new stock prices to the database.", stocks.Count());
            Log.Information("Initialized all players");

            Console.WriteLine("Saved {0} new stock prices to the database.", stocks.Count());
            Console.WriteLine("Initialized all players");
        }
        public ActionResult Index()
        {
            List<PlayerViewModel> players = new List<PlayerViewModel>();
            StockEngine engine = new YahooStockEngine();

            // Chris
            Player player = new Chris();
            Investment investment = engine.GetCurrentInvestmentForPlayer(player.Name);
            Quote quote = engine.GetCurrentQuoteForPlayer(player.Name);
            PlayerViewModel model = new PlayerViewModel(investment, quote, player);
            players.Add(model);

            // Fiona
            player = new Fiona();
            investment = engine.GetCurrentInvestmentForPlayer(player.Name);
            quote = engine.GetCurrentQuoteForPlayer(player.Name);
            model = new PlayerViewModel(investment, quote, player);
            players.Add(model);

            // Wilson
            player = new Wilson();
            investment = engine.GetCurrentInvestmentForPlayer(player.Name);
            quote = engine.GetCurrentQuoteForPlayer(player.Name);
            model = new PlayerViewModel(investment, quote, player);
            players.Add(model);

            // Katherine
            player = new Katherine();
            investment = engine.GetCurrentInvestmentForPlayer(player.Name);
            quote = engine.GetCurrentQuoteForPlayer(player.Name);
            model = new PlayerViewModel(investment, quote, player);
            players.Add(model);

            // Jon
            player = new Jon();
            investment = engine.GetCurrentInvestmentForPlayer(player.Name);
            quote = engine.GetCurrentQuoteForPlayer(player.Name);
            model = new PlayerViewModel(investment, quote, player);
            players.Add(model);

            return View(players);
        }
        private static void Scan()
        {
            StockEngine engine = new YahooStockEngine();

            IEnumerable<string> symbols = engine.LoadSymbolsFromTextFile("aim.txt");
            //IEnumerable<Quote> stocks = engine.LookupPrices(symbols); // Get everything
            string[] playerNames = engine.GetAllPlayers().Select(x => x.Name).ToArray();

            IEnumerable<Quote> stocks = engine.LookupQuotesForPlayers(playerNames);
            if (stocks.Count() < 1 || stocks.Any(s => s == null))
            {
                // Try Google
                Log.Information("No stocks found - switching to the Google API");
                engine = new GoogleStockEngine();
                symbols = engine.LoadSymbolsFromTextFile("aim.txt");
                stocks = engine.LookupQuotesForPlayers(playerNames);
            }

            engine.InsertQuotes(stocks);
            Log.Information("Saved {0} new stock prices to the database.", stocks.Count());

            //
            // Calculate if investments should be sold for each players
            //
            foreach (Player player in engine.GetAllPlayers())
            {
                Quote quote = engine.GetCurrentQuoteForPlayer(player.Name);
                Investment investment = engine.GetCurrentInvestmentForPlayer(player.Name);
                if (player.ShouldSellInvestment(quote, investment))
                {
                    // Sell sell sell
                    Log.Information("Selling {0}'s investment in {1} - ({2}) current price is {3}", player.Name, investment.Symbol, investment.SellReason, quote.LastTradePrice.Value);
                    engine.SellInvestment(investment.Id, quote.LastTradePrice.Value, investment.SellReason.Value, DateTime.Now);

                    // Buy buy buy
                    string nextSymbol = engine.PickRandomSymbol(symbols);
                    Quote nextQuote = engine.LookupPrice(nextSymbol);
                    engine.InitializePlayer(player, nextQuote, 400);
                    Log.Information("New investment set up for {0}. Bought {1} shares in {2} at price of {3}", player.Name, investment.Quantity, quote.Symbol, quote.LastTradePrice.Value);
                }
            }
        }
        public void GetCurrentQuoteForPlayer()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();
            IEnumerable<string> symbols = engine.LoadSymbolsFromTextFile("aim.txt");
            string randomSymbol = engine.PickRandomSymbol(symbols);
            Quote quote = engine.LookupPrice(randomSymbol);
            decimal walletSize = 400; // £400

            Player player = new Chris();

            // Act
            engine.InsertQuote(quote);
            engine.InitializePlayer(player, quote, walletSize);
            Quote currentQuote = engine.GetCurrentQuoteForPlayer(player.Name);

            // Assert
            Assert.That(currentQuote.Id, Is.EqualTo(quote.Id));
            Assert.That(currentQuote.Symbol, Is.EqualTo(quote.Symbol));
            Assert.That(currentQuote.LastTradePrice, Is.EqualTo(quote.LastTradePrice));
        }
        public void GetPlayerHistory()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();
            IEnumerable<string> symbols = engine.LoadSymbolsFromTextFile("aim.txt");
            string randomSymbol = engine.PickRandomSymbol(symbols);
            decimal walletSize = 400;

            Player player = new Chris();
            Quote quote1 = engine.LookupPrice(randomSymbol);
            engine.InitializePlayer(player, quote1, walletSize);

            Quote quote2 = engine.LookupPrice(randomSymbol);
            engine.InitializePlayer(player, quote2, walletSize);

            // Act
            IEnumerable<Investment> investments = engine.GetInvestmentHistoryForPlayer(player.Name);

            // Assert
            Assert.That(investments.Count(), Is.EqualTo(2));
        }
        public void InitializePlayer()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();
            IEnumerable<string> symbols = engine.LoadSymbolsFromTextFile("aim.txt");
            string randomSymbol = engine.PickRandomSymbol(symbols);
            Quote quote = engine.LookupPrice(randomSymbol);
            decimal walletSize = 400; // £400

            Player player = new Chris();

            // Act
            engine.InsertQuote(quote);
            engine.InitializePlayer(player, quote, walletSize);

            // Assert
            Investment investment = engine.GetCurrentInvestmentForPlayer(player.Name);
            Assert.That(investment.Symbol, Is.EqualTo(randomSymbol));
            Assert.That(investment.PlayerName, Is.EqualTo(player.GetType().Name));
            Assert.That(investment.PurchaseDate, Is.GreaterThan(DateTime.Today));
            Assert.That(investment.PurchasePrice, Is.EqualTo(quote.LastTradePrice.Value));
        }
        public void LookupQuotesForPlayers()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();
            decimal walletSize = 400;
            engine.ReInitializeAllPlayers(walletSize);

            // Act
            IEnumerable<Quote> quotes = engine.LookupQuotesForPlayers(engine.GetAllPlayers().Select(x => x.Name).ToArray());

            // Assert
            Assert.That(quotes.Count(), Is.EqualTo(5));
        }
        public void ShouldSellInvestment_No_Change()
        {
            // Arrange
            Investment investment = new Investment()
            {
                Symbol = "BLAH",
                PurchasePrice = 1.0m,
                PurchaseDate = DateTime.Today
            };
            Quote quote = new Quote() { Symbol = "BLAH", LastTradePrice = 1.0m };

            // Act
            StockEngine engine = new YahooStockEngine();

            foreach (Player player in engine.GetAllPlayers())
            {
                bool shouldSell = player.ShouldSellInvestment(quote, investment);

                // Assert
                Assert.False(shouldSell, "Failed ShouldSellInvestment being false for " + player.Name);
                Assert.IsNull(investment.SellReason, "Failed SellReason being null for " + player.Name);
            }
        }
        public void ShouldSellInvestment_Low_Price()
        {
            // Arrange
            Investment investment = new Investment()
            {
                Symbol = "BLAH",
                PurchasePrice = 1.0m,
                PurchaseDate = DateTime.Today
            };
            Quote quote = new Quote() { Symbol = "BLAH", LastTradePrice = 0.7m };

            // Act
            StockEngine engine = new YahooStockEngine();

            foreach (Player player in engine.GetAllPlayers())
            {
                bool shouldSell = player.ShouldSellInvestment(quote, investment);

                // Assert
                Assert.True(shouldSell, "Failed ShouldSellInvestment being true for " + player.Name);
                Assert.That(investment.SellReason, Is.EqualTo(SellReason.LowPrice), "Failed SellReason being Low for " + player.Name);
            }
        }
        public void SellInvestment()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();
            IEnumerable<Quote> stocks = engine.AllQuotes();

            Investment investment = new Investment() { Id = Guid.NewGuid() };
            decimal newPrice = 1.1m;

            // Act
            engine.SellInvestment(investment.Id, newPrice, SellReason.HighPrice, DateTime.Today);

            // Assert
        }
        public void ReInitializeAllPlayers()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();
            decimal walletSize = 400;

            // Act
            engine.ReInitializeAllPlayers(walletSize);

            // Assert
            List<Investment> investments = engine.GetInvestments().ToList();
            Assert.That(investments.Count, Is.EqualTo(5));
            Assert.NotNull(investments.FirstOrDefault(x => x.PlayerName == "Chris"));
            Assert.NotNull(investments.FirstOrDefault(x => x.PlayerName == "Fiona"));
            Assert.NotNull(investments.FirstOrDefault(x => x.PlayerName == "Wilson"));
            Assert.NotNull(investments.FirstOrDefault(x => x.PlayerName == "Katherine"));
            Assert.NotNull(investments.FirstOrDefault(x => x.PlayerName == "Jon"));
        }
        public void PickRandomSymbol()
        {
            // Arrange
            StockEngine engine = new YahooStockEngine();

            // Act
            IEnumerable<string> symbols = engine.LoadSymbolsFromTextFile("aim.txt");
            string randomSymbol = engine.PickRandomSymbol(symbols);

            // Assert
            Console.WriteLine("Picked: {0}", randomSymbol);
            Assert.IsNotNullOrEmpty(randomSymbol);
        }