public QuoteViewModel(Quote quote)
 {
     Symbol = quote.Symbol;
     Name = quote.Name;
     CreateDate = quote.CreateDate;
     LastTradePrice = quote.LastTradePrice.Value / 100;
 }
        public PlayerViewModel(Investment investment, Quote latestQuote, Player player)
        {
            Name = player.Name;
            Description = player.Description;
            Symbol = investment.Symbol;
            CompanyName = latestQuote.Name;
            CurrentPrice = latestQuote.LastTradePrice.Value / 100;
            PurchasePrice = investment.PurchasePrice / 100;
            PurchasedOn = investment.PurchaseDate;
            ShareQuantity = investment.Quantity;

            PercentageChange = (double) ((CurrentPrice - PurchasePrice) / PurchasePrice);
        }
        public void SaveToDatabase_And_LoadFromDatabase()
        {
            // Arrange
            StockEngine engine = GetEngine();
            List<Quote> stocks = new List<Quote>();
            Quote stock1 = new Quote("SYM1")
            {
                Id = Guid.NewGuid(),
                LastTradePrice = 1.50m,
            };

            Quote stock2 = new Quote("SYM2")
            {
                Id = Guid.NewGuid(),
                LastTradePrice = 2.50m
            };
            stocks.Add(stock1);
            stocks.Add(stock2);

            // Act
            engine.InsertQuotes(stocks);
            List<Quote> stocksFromDb = engine.AllQuotes().ToList();

            // Assert
            Assert.That(stocksFromDb.Count, Is.EqualTo(2));

            Assert.That(stocksFromDb[0].Id, Is.EqualTo(stock1.Id));
            Assert.That(stocksFromDb[0].LastTradePrice, Is.EqualTo(stock1.LastTradePrice));
            Assert.That(stocksFromDb[0].Symbol, Is.EqualTo(stock1.Symbol));

            Assert.That(stocksFromDb[1].Id, Is.EqualTo(stock2.Id));
            Assert.That(stocksFromDb[1].LastTradePrice, Is.EqualTo(stock2.LastTradePrice));
            Assert.That(stocksFromDb[1].Symbol, Is.EqualTo(stock2.Symbol));
        }
        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 ShouldSellInvestment_Boundaries()
        {
            // Arrange
            Investment investment1 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };
            Investment investment2 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };
            Investment investment3 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };

            Investment investment4 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };
            Investment investment5 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };
            Investment investment6 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };

            Investment investment7 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };
            Investment investment8 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };
            Investment investment9 = new Investment() { Symbol = "", PurchasePrice = 1.0m, PurchaseDate = DateTime.Today };

            Quote highQuote1 = new Quote() { Symbol = "BLAH", LastTradePrice = 1.05m };
            Quote highQuote2 = new Quote() { Symbol = "BLAH", LastTradePrice = 1.06m };
            Quote highQuote3 = new Quote() { Symbol = "BLAH", LastTradePrice = 3.99m };

            Quote lowQuote1 = new Quote() { Symbol = "BLAH", LastTradePrice = 0.95m };
            Quote lowQuote2 = new Quote() { Symbol = "BLAH", LastTradePrice = 0.49m };
            Quote lowQuote3 = new Quote() { Symbol = "BLAH", LastTradePrice = 0 };

            Quote noChangeQuote1 = new Quote() { Symbol = "BLAH", LastTradePrice = 0.96m };
            Quote noChangeQuote2 = new Quote() { Symbol = "BLAH", LastTradePrice = 1.04m };
            Quote noChangeQuote3 = new Quote() { Symbol = "BLAH", LastTradePrice = null };

            // Act
            Chris chris = new Chris(); // 5% change
            bool shouldSellHigh1 = chris.ShouldSellInvestment(highQuote1, investment1);
            bool shouldSellHigh2 = chris.ShouldSellInvestment(highQuote2, investment2);
            bool shouldSellHigh3 = chris.ShouldSellInvestment(highQuote3, investment3);

            bool shouldSellLow1 = chris.ShouldSellInvestment(lowQuote1, investment4);
            bool shouldSellLow2 = chris.ShouldSellInvestment(lowQuote2, investment5);
            bool shouldSellLow3 = chris.ShouldSellInvestment(lowQuote3, investment6);

            bool shouldSellNoChange1 = chris.ShouldSellInvestment(noChangeQuote1, investment7);
            bool shouldSellNoChange2 = chris.ShouldSellInvestment(noChangeQuote2, investment8);
            bool shouldSellNoChange3 = chris.ShouldSellInvestment(noChangeQuote3, investment9);

            // Assert
            Assert.True(shouldSellHigh1, "High price 1 to 1.05"); Assert.That(investment1.SellReason, Is.EqualTo(SellReason.HighPrice));
            Assert.True(shouldSellHigh2, "High price 1 to 1.06"); Assert.That(investment2.SellReason, Is.EqualTo(SellReason.HighPrice));
            Assert.True(shouldSellHigh3, "High price 1 to 3.99"); Assert.That(investment3.SellReason, Is.EqualTo(SellReason.HighPrice));

            Assert.True(shouldSellLow1, "Low price 1 to 0.95"); Assert.That(investment4.SellReason, Is.EqualTo(SellReason.LowPrice));
            Assert.True(shouldSellLow2, "Low price 1 to 0.49"); Assert.That(investment5.SellReason, Is.EqualTo(SellReason.LowPrice));
            Assert.True(shouldSellLow3, "Low price 1 to 0"); Assert.That(investment6.SellReason, Is.EqualTo(SellReason.LowPrice));

            Assert.False(shouldSellNoChange1, "No change 1 to 0.96");
            Assert.False(shouldSellNoChange2, "No change 1 to 1.04");
            Assert.False(shouldSellNoChange3, "No change 1 to (null)");
        }
 public void InsertQuote(Quote quote)
 {
     using (DbConnection connection = Db.CreateConnection())
     {
         connection.Open();
         connection.Insert<Quote>(quote);
     }
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        /// <param name="quote"></param>
        /// <param name="walletSize">The size of your wallet, in £</param>
        public void InitializePlayer(Player player, Quote quote, decimal walletSize)
        {
            walletSize *= 100;
            int quantity = (int)(walletSize / quote.LastTradePrice.Value);

            Investment investment = new Investment()
            {
                Id = Guid.NewGuid(),
                PlayerName = player.GetType().Name,
                PurchaseDate = DateTime.Now,
                PurchasePrice = quote.LastTradePrice.Value,
                Quantity = quantity,
                Symbol = quote.Symbol
            };

            using (DbConnection connection = Db.CreateConnection())
            {
                connection.Open();
                connection.Insert<Investment>(investment);

                Log.Information("Initialized {0} with symbol {1}", investment.PlayerName, investment.Symbol);
            }
        }