Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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));
        }
        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");
        }
        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);
                }
            }
        }