Beispiel #1
0
        public void PerformanceServiceShouldCalulateTheSameWayWithOneSingleAndMultipleBuys()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            //2 Buys
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 370, 2.16m, 0, DateTime.Parse("2011-04-29 15:42:00.000")));
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 200, 2.30m, 0, DateTime.Parse("2011-05-04 10:13:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 570, 2.38m, 5.5m, 29.55m, DateTime.Parse("2011-06-08 19:35:00.000")));

            var result           = GetPerformance(book, stockId, sellTransId);
            var profit           = result.ProfitAbsolute;
            var profitPercentage = result.ProfitPercentage;


            var newBook = new TransactionBook();

            //Aggregated 1 buy
            newBook.AddEntry(TransactionEntryMock.CreateBuying(stockId, 570, 2.20912280701754m, 0, DateTime.Parse("2011-04-29 15:42:00.000")));
            newBook.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 570, 2.38m, 5.5m, 29.55m, DateTime.Parse("2011-06-08 19:35:00.000")));

            var newResult = GetPerformance(newBook, stockId, sellTransId);

            newResult.ProfitMade.Should().BeTrue();
            newResult.ProfitAbsolute.Should().Be(profit);
            newResult.ProfitPercentage.Should().Be(profitPercentage);
        }
Beispiel #2
0
        public void TransactionBookShouldOnlyAllowSellingWhenEnoughUnitsAvailable()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

            Action act = () => book.AddEntry(TransactionEntryMock.CreateSelling(guid, 100));

            act.ShouldThrow <InvalidOperationException>();
        }
Beispiel #3
0
        public void TransactionBookShouldCalulateRemainingShares100Buy100Sell()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 100));
            book.AddEntry(TransactionEntryMock.CreateSelling(guid, 100));

            book.GetOrAddOpenPosition(guid).Shares.Should().Be(0);
        }
Beispiel #4
0
        public void TransactionBookShouldReturnLastChanges()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();
            var buy  = TransactionEntryMock.CreateBuying(guid, 100);
            var sell = TransactionEntryMock.CreateSelling(guid, 100);

            book.AddEntry(buy);
            book.GetLastCommittedChanges(guid).Count().Should().Be(0);
            book.AddEntry(sell);
            book.GetLastCommittedChanges(guid).Count().Should().Be(2);
        }
Beispiel #5
0
        public void PerformanceServiceShouldCalulateRiskWith1Buy1Sell()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 400, 2.97m, 0, DateTime.Parse("2011-04-29 13:00:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 400, 3.32m, 5.5m, 10.71m, DateTime.Parse("2011-06-08 19:19:00.000")));

            var result = GetPerformance(book, stockId, sellTransId, 3.58m, 2.80m);

            result.R.Should().Be(1.77m);
        }
Beispiel #6
0
        public void PerformanceServiceShouldCalulateHoldingPeriod1Buy1Sell()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 80, 6.69m, 11.65m, DateTime.Parse("2014-02-03 09:02:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 80, 5.99m, 11.65m, 0, DateTime.Parse("2014-04-15 11:28:00.000")));

            var result = GetPerformance(book, stockId, sellTransId);

            result.HoldingPeriod.IsIntradayTrade.Should().BeFalse();
            result.HoldingPeriod.ToDays().Should().Be(71.10m);
        }
Beispiel #7
0
        public void PerformanceServiceShouldCalulateRiskWith2Buys1Sell()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 370, 2.16m, 0, DateTime.Parse("2011-04-29 15:42:00.000")));
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 200, 2.30m, 0, DateTime.Parse("2011-05-04 10:13:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 570, 2.38m, 5.5m, 29.55m, DateTime.Parse("2011-06-08 19:35:00.000")));

            var result = GetPerformance(book, stockId, sellTransId);

            result.R.Should().Be(0.89m);
        }
Beispiel #8
0
        public void TransactionBookShouldCalulateAveragePrizeUsingFifo()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 100, 1.2m, 0m));
            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 50, 1.6m, 0m));
            book.AddEntry(TransactionEntryMock.CreateSelling(guid, 80, 1.2m, 0m));

            book.GetOrAddOpenPosition(guid).PricePerShare.Should().BeApproximately(1.48571m, 0.00001m);

            book.AddEntry(TransactionEntryMock.CreateSelling(guid, 60, 1.46666m, 0m));

            book.GetOrAddOpenPosition(guid).PricePerShare.Should().BeApproximately(1.60m, 0.00001m);
        }
Beispiel #9
0
        public void PerformanceServiceShouldCalulateProfitWith1Buy1Sell()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 80, 6.689m, 11.65m, DateTime.Parse("2014-02-03 09:02:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 80, 5.99m, 11.65m, 0, DateTime.Parse("2014-04-15 11:28:00.000")));

            var result = GetPerformance(book, stockId, sellTransId);

            result.ProfitMade.Should().BeFalse();
            result.ProfitAbsolute.Should().Be(-79.22m);
            result.ProfitPercentage.Should().Be(-14.49m);
        }
Beispiel #10
0
        public void PerformanceServiceShouldCalulateHoldingPeriodWith2Buys1Sell()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 370, 2.16m, 0, DateTime.Parse("2011-04-29 15:42:00.000")));
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 200, 2.30m, 0, DateTime.Parse("2011-05-04 10:13:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 570, 2.38m, 5.5m, 0, DateTime.Parse("2011-06-08 19:35:00.000")));

            var result = GetPerformance(book, stockId, sellTransId);

            result.HoldingPeriod.IsIntradayTrade.Should().BeFalse();
            result.HoldingPeriod.ToDays().Should().Be(40.16m);
        }
Beispiel #11
0
        public void PerformanceServiceShouldCalulateTheProfitUsingTheFifoPrinciples()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 1000, 25, 0, DateTime.Parse("2008-04-15 00:00:00.000")));
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 1000, 40, 0, DateTime.Parse("2009-01-03 00:00:00.000")));
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 500, 26, 0, DateTime.Parse("2010-03-07 00:00:00.000")));
            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 500, 30, 0, DateTime.Parse("2011-04-07 00:00:00.000")));
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 2200, 45, 0m, 0m, DateTime.Parse("2011-04-28 00:00:00.000")));

            var result = GetPerformance(book, stockId, sellTransId);

            result.ProfitAbsolute.Should().Be(28800);
        }
Beispiel #12
0
        public void TransactionBookShouldCalulateOpenPositionWithOnePartialSale()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 20, 101.96m, 11.65m));
            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 30, 98m, 11.65m));
            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 33, 90.62m, 11.65m));
            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 35, 83m, 11.65m));
            book.AddEntry(TransactionEntryMock.CreateSelling(guid, 60, 100m, 11.65m));

            var position = book.GetOrAddOpenPosition(guid);

            position.Shares.Should().Be(58);
            position.PricePerShare.Should().BeApproximately(86.36258098m, 0.00001m);
            position.PositionSize.Should().BeApproximately(5009.02969m, 0.00001m);
            position.ProductId.Should().Be(guid);
        }
Beispiel #13
0
        public void PerformanceServiceShouldCalulateProfitOfPartlyFilledOrder()
        {
            var stockId     = Guid.NewGuid();
            var sellTransId = Guid.NewGuid();
            var book        = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(stockId, 550, 1.92m, 9.90m, DateTime.Parse("2012-02-09 20:00:00.000")));
            //First sell
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 422, 1.75m, 11.15m, 0, DateTime.Parse("2012-03-01 09:02:00.000")));

            var result = GetPerformance(book, stockId, sellTransId);

            result.ProfitMade.Should().BeFalse();
            result.ProfitAbsolute.Should().Be(-90.49m);
            result.ProfitPercentage.Should().Be(-11.06m);

            //Second sell
            book.AddEntry(TransactionEntryMock.CreateSelling(stockId, sellTransId, 128, 1.74m, 0.56m, 0, DateTime.Parse("2012-03-01 09:02:00.000")));
            result = GetPerformance(book, stockId, sellTransId);
            result.ProfitMade.Should().BeFalse();
            result.ProfitAbsolute.Should().Be(-25.90m);
            result.ProfitPercentage.Should().Be(-10.44m);
        }