Ejemplo n.º 1
0
        public void Trade()
        {
            foreach (var stockSignal in _stockSignals)
            {
                var assessment = stockSignal.Assess();

                if (assessment.Recommendation > 0)
                {
                    const int amount = 1;
                    var       price  = _stockbroker.GetPrice(assessment.Symbol) * amount;
                    if (Balance >= price)
                    {
                        Balance -= _stockbroker.Buy(assessment.Symbol, amount);
                        _portfolio.Add(assessment.Symbol, amount);
                    }
                }
                else if (assessment.Recommendation < 0)
                {
                    const int amount = 1;

                    if (_portfolio.Has(assessment.Symbol, amount))
                    {
                        Balance += _stockbroker.Sell(assessment.Symbol, amount);
                        _portfolio.Remove(assessment.Symbol, amount);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Trade_SellSignalReceived_SellsStock()
        {
            // Arrange
            _mockSignal.Assess()
            .Returns(new Assessment {
                Recommendation = -1, Symbol = "NOK"
            });
            _mockBroker.GetPrice("NOK")
            .Returns(4);
            _mockPortfolio.Has("NOK", 1)
            .Returns(true);
            _mockBroker.Sell("NOK", 1).Returns(4);

            // Act
            _trader.Register(_mockSignal);
            _trader.Trade();

            // Assert
            _mockBroker.Received().Sell("NOK", 1);
            _trader.Balance.Should().Be(4);
            _mockPortfolio.Received().Remove("NOK", 1);
        }