Пример #1
0
        public void MultipleBuysAverageCostCorrect()
        {
            var stock = new OwnedStock("tsla", _userId);

            stock.Purchase(1, 5, DateTimeOffset.UtcNow);
            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(7.5, stock.AverageCost);
            Assert.Equal(15, stock.State.Cost);

            stock.Sell(1, 6, DateTimeOffset.UtcNow, null);

            Assert.Equal(7.5, stock.AverageCost);
            Assert.Equal(7.5, stock.State.Cost);

            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(8.75, stock.AverageCost);
            Assert.Equal(17.5, stock.State.Cost);

            stock.Sell(2, 10, DateTimeOffset.UtcNow, null);

            Assert.Equal(0, stock.State.Owned);
            Assert.Equal(0, stock.AverageCost);
            Assert.Equal(0, stock.State.Cost);
        }
Пример #2
0
        public void MultipleBuysAverageCostCorrect()
        {
            var stock = new OwnedStock("tsla", _userId);

            stock.Purchase(1, 5, DateTimeOffset.UtcNow);
            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(7.5m, stock.State.AverageCost);
            Assert.Equal(15, stock.State.Cost);

            stock.Sell(1, 6, DateTimeOffset.UtcNow, null);

            Assert.Equal(7.5m, stock.State.AverageCost);
            Assert.Equal(7.5m, stock.State.Cost);

            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(8.75m, stock.State.AverageCost);
            Assert.Equal(17.5m, stock.State.Cost);

            stock.Sell(2, 10, DateTimeOffset.UtcNow, null);

            Assert.Equal(0, stock.State.Owned);
            Assert.Equal(0, stock.State.AverageCost);
            Assert.Equal(0, stock.State.Cost);
            Assert.Single(stock.State.PositionInstances);
            Assert.Equal(0, stock.State.PositionInstances[0].DaysHeld);
            Assert.Equal(1, stock.State.PositionInstances[0].Profit);
            Assert.Equal(0.04m, stock.State.PositionInstances[0].Percentage);
        }
Пример #3
0
        public void SellCreatesPLTransaction()
        {
            var stock = new OwnedStock("tsla", _userId);

            stock.Purchase(1, 5, DateTimeOffset.UtcNow);
            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(7.5, stock.AverageCost);
            Assert.Equal(15, stock.State.Cost);

            stock.Sell(1, 6, DateTimeOffset.UtcNow, null);

            var tx = stock.State.Transactions.Last();

            Assert.True(tx.IsPL);
            Assert.Equal(1.5, tx.Debit); // average cost is 7.5, selling for 6 is 1.5 loss

            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(8.75, stock.AverageCost);

            stock.Sell(2, 10, DateTimeOffset.UtcNow, null);

            tx = stock.State.Transactions.Last();

            Assert.True(tx.IsPL);
            Assert.Equal(2.5, tx.Credit); // average cost is 8.75, selling 2 x 10 is 2.5 gain
        }
Пример #4
0
        public async Task OwnedStockStorageAsync()
        {
            var stock = new OwnedStock(GenerateTestTicker(), _userId);

            stock.Purchase(10, 2.1, DateTime.UtcNow);

            var storage = CreateStorage();

            await storage.Save(stock, _userId);

            var loadedList = await storage.GetStocks(_userId);

            Assert.NotEmpty(loadedList);

            var loaded = await storage.GetStock(stock.State.Ticker, _userId);

            Assert.Equal(loaded.State.Owned, stock.State.Owned);

            loaded.Purchase(5, 5, DateTime.UtcNow);

            await storage.Save(loaded, _userId);

            loaded = await storage.GetStock(loaded.State.Ticker, loaded.State.UserId);

            Assert.NotEqual(loaded.State.Owned, stock.State.Owned);

            await storage.Delete(_userId);

            var afterDelete = await storage.GetStocks(_userId);

            Assert.Empty(afterDelete);
        }
Пример #5
0
        public void SellingNotOwnedFails()
        {
            var stock = new OwnedStock("TEUM", _userId);

            stock.Purchase(10, 2.1, DateTime.UtcNow);

            Assert.ThrowsAny <Exception>(() => stock.Sell(20, 100, DateTime.UtcNow, "sample note"));
        }
Пример #6
0
        public void ExportStocksHeader()
        {
            var stock = new OwnedStock("tsla", Guid.NewGuid());

            stock.Purchase(1, 100, DateTime.UtcNow, "some note");

            var report = CSVExport.Generate(new[] { stock });

            Assert.Contains(CSVExport.STOCK_HEADER, report);
            Assert.Contains("TSLA", report);
        }
Пример #7
0
        public void DaysHeldCorrect()
        {
            var stock = new OwnedStock("tsla", _userId);

            stock.Purchase(1, 5, DateTimeOffset.UtcNow.AddDays(-5));
            stock.Purchase(1, 10, DateTimeOffset.UtcNow.AddDays(-2));

            Assert.Equal(5, stock.State.DaysHeld);
            Assert.Equal(2, stock.State.DaysSinceLastTransaction);

            stock.Sell(1, 6, DateTimeOffset.UtcNow, null);

            Assert.Equal(5, stock.State.DaysHeld);
            Assert.Equal(0, stock.State.DaysSinceLastTransaction);

            stock.Sell(1, 10, DateTimeOffset.UtcNow, null);

            Assert.Equal(0, stock.State.DaysHeld);
            Assert.Equal(0, stock.State.DaysSinceLastTransaction);
        }
Пример #8
0
        public void PurchaseWorks()
        {
            var stock = new OwnedStock("TEUM", _userId);

            stock.Purchase(10, 2.1m, DateTime.UtcNow);

            Assert.Equal("TEUM", stock.State.Ticker);
            Assert.Equal(_userId, stock.State.UserId);
            Assert.Equal(10, stock.State.Owned);
            Assert.Equal(21, stock.State.Cost);

            stock.Purchase(5, 2, DateTime.UtcNow);

            Assert.Equal(15, stock.State.Owned);
            Assert.Equal(31, stock.State.Cost, 0);

            stock.Sell(5, 20, DateTime.UtcNow, "sample note");

            Assert.Equal(10, stock.State.Owned);
        }
Пример #9
0
        public void MultipleBuysDeletingTransactions()
        {
            var stock = new OwnedStock("tsla", _userId);

            stock.Purchase(1, 5, DateTimeOffset.UtcNow);
            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(7.5m, stock.State.AverageCost);
            Assert.Equal(15, stock.State.Cost);

            stock.Sell(1, 6, DateTimeOffset.UtcNow, null);

            Assert.Equal(7.5m, stock.State.AverageCost);
            Assert.Equal(7.5m, stock.State.Cost);

            stock.Purchase(1, 10, DateTimeOffset.UtcNow);

            Assert.Equal(8.75m, stock.State.AverageCost);
            Assert.Equal(17.5m, stock.State.Cost);

            stock.Sell(2, 10, DateTimeOffset.UtcNow, null);

            Assert.Equal(0, stock.State.Owned);
            Assert.Equal(0, stock.State.AverageCost);
            Assert.Equal(0, stock.State.Cost);

            var last = stock.State.Transactions.Where(t => !t.IsPL).Last();

            stock.DeleteTransaction(last.EventId);

            Assert.Equal(2, stock.State.Owned);
            Assert.Equal(8.75m, stock.State.AverageCost);
            Assert.Equal(17.5m, stock.State.Cost);

            stock.Delete();

            Assert.Equal(0, stock.State.Owned);
            Assert.Equal(0, stock.State.AverageCost);
            Assert.Equal(0, stock.State.AverageCost);
            Assert.Empty(stock.State.Transactions);
        }
Пример #10
0
        public void EventCstrReplaysEvents()
        {
            var stock = new OwnedStock("tlsa", _userId);

            stock.Purchase(1, 10, DateTime.UtcNow);

            var events = stock.Events;

            var stock2 = new OwnedStock(events);

            Assert.Equal(stock.State.Owned, stock2.State.Owned);
        }
Пример #11
0
        public void PurchaseWithDateNotProvidedThrows()
        {
            var stock = new OwnedStock("tlsa", _userId);

            Assert.Throws <InvalidOperationException>(() => stock.Purchase(1, 20, DateTime.MinValue));
        }
Пример #12
0
        public void BuyingWithBadDateThrows()
        {
            var stock = new OwnedStock("tlsa", _userId);

            Assert.Throws <InvalidOperationException>(() => stock.Purchase(10, 0, DateTime.MinValue));
        }
Пример #13
0
        public void BuyingForZeroThrows()
        {
            var stock = new OwnedStock("tlsa", _userId);

            Assert.Throws <InvalidOperationException>(() => stock.Purchase(10, 0, DateTime.UtcNow));
        }