Exemple #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);
        }
Exemple #2
0
        public void TransactionBookShouldAlwaysReturnAPostion()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

            book.GetOrAddOpenPosition(guid).Should().NotBeNull();
        }
Exemple #3
0
        public void TransactionBookShouldOnlyAllowSellingWhenEnoughUnitsAvailable()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

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

            act.ShouldThrow <InvalidOperationException>();
        }
Exemple #4
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);
        }
Exemple #5
0
        public void TransactionBookShouldCalulateCorrectOrderCostsForRemainingSharesAfterSplit()
        {
            var guid = Guid.NewGuid();
            var book = new TransactionBook();

            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 6500, 0.14m, 1.25m));
            book.AddEntry(TransactionEntryMock.CreateBuying(guid, 14000, 0.06m, 15.40m));
            book.AddEntry(TransactionEntryMock.CreateSplit(guid, Guid.NewGuid(), 137m, 13.209124m, DateTime.Now));

            book.GetOrAddOpenPosition(guid).PositionSize.Should().Be(137 * 13.209124m + 1.25m + 15.40m);
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
Exemple #8
0
        public void TransactionBookShouldCalulateOpenPositionsOrderCost()
        {
            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));

            var position = book.GetOrAddOpenPosition(guid);

            position.OrderCosts.Should().Be(11.65m * 4);
        }
Exemple #9
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);
        }
Exemple #10
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);
        }
Exemple #11
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);
        }
Exemple #12
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);
        }
Exemple #13
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);
        }
Exemple #14
0
        public void PerformanceServiceShouldCalulateTheProfitForADividend()
        {
            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.CreateDividend(stockId, sellTransId, 2200, 45, 0m, 0m, DateTime.Parse("2011-04-28 00:00:00.000")));

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

            result.ProfitAbsolute.Should().Be(99000);
        }
Exemple #15
0
        public void TransactionBookShouldCalulateOpenPositionWithoutSell()
        {
            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));

            var position = book.GetOrAddOpenPosition(guid);

            position.Shares.Should().Be(118);
            position.PricePerShare.Should().BeApproximately(92.553050m, 0.00001m);
            position.PositionSize.Should().Be(10921.26m);
            position.ProductId.Should().Be(guid);
        }
Exemple #16
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);
        }
Exemple #17
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);
        }
        /// <summary>
        /// Loads the binding configuration.
        /// </summary>
        /// <param name="serviceCollection">The service collection.</param>
        public void Load(IServiceCollection serviceCollection)
        {
            //Services
            serviceCollection.AddSingleton <IPriceCalculatorService, PriceCalculatorService>();
            serviceCollection.AddSingleton <IStockQuoteService, StockQuoteService>();
            serviceCollection.AddSingleton <ITransactionPerformanceService, TransactionPerformanceService>();
            serviceCollection.AddSingleton <IAccumulationPlanStatisticService, AccumulationPlanStatisticService>();
            serviceCollection.AddTransient <IInterestRateCalculatorService, InterestRateCalculatorService>();
            serviceCollection.AddTransient <ITransactionCalculationService, TransactionCalculationService>();
            serviceCollection.AddTransient <ITimeSliceCreationService, TimeSliceCreationService>();
            serviceCollection.AddTransient <IStatisticService, StatisticService>();

            var transactionBook = new TransactionBook();

            serviceCollection.AddSingleton <ITransactionBook>(s => transactionBook);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => transactionBook);

            //Model repositories
            serviceCollection.AddTransient <IModelRepositoryDeletionCoordinator, ModelRepositoryDeletionCoordinator>();

            var stockModelRepository = new StockModelRepository();

            serviceCollection.AddSingleton <IModelRepository <IStock> >(s => stockModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <IStock> >(s => stockModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <IStock> >(s => stockModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => stockModelRepository);

            var feedbackModelRepository = new FeedbackModelRepository();

            serviceCollection.AddSingleton <IModelRepository <IFeedback> >(s => feedbackModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <IFeedback> >(s => feedbackModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <IFeedback> >(s => feedbackModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => feedbackModelRepository);

            var calculationModelRepository = new CalculationModelRepository();

            serviceCollection.AddSingleton <IModelRepository <ICalculation> >(s => calculationModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <ICalculation> >(s => calculationModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <ICalculation> >(s => calculationModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => calculationModelRepository);

            var strategyModelRepository = new StrategyModelRepository();

            serviceCollection.AddSingleton <IModelRepository <IStrategy> >(s => strategyModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <IStrategy> >(s => strategyModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <IStrategy> >(s => strategyModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => strategyModelRepository);

            var transactionModelRepository = new TransactionModelRepository();

            serviceCollection.AddSingleton <IModelRepository <ITransaction> >(s => transactionModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <ITransaction> >(s => transactionModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <ITransaction> >(s => transactionModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => transactionModelRepository);

            var transactionPerformanceModelRepository = new TransactionPerformanceModelRepository();

            serviceCollection.AddSingleton <IModelRepository <ITransactionPerformance> >(s => transactionPerformanceModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <ITransactionPerformance> >(s => transactionPerformanceModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <ITransactionPerformance> >(s => transactionPerformanceModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => transactionPerformanceModelRepository);

            var accountBalanceModelRepository = new AccountBalanceModelRepository();

            serviceCollection.AddSingleton <IModelRepository <IAccountBalance> >(s => accountBalanceModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <IAccountBalance> >(s => accountBalanceModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <IAccountBalance> >(s => accountBalanceModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => accountBalanceModelRepository);

            var feedbackProportionModelRepository = new FeedbackProportionModelRepository();

            serviceCollection.AddSingleton <IModelRepository <IFeedbackProportion> >(s => feedbackProportionModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <IFeedbackProportion> >(s => feedbackProportionModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <IFeedbackProportion> >(s => feedbackProportionModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => feedbackProportionModelRepository);

            var stockStatisticsModelRepository = new StockStatisticsModelRepository();

            serviceCollection.AddSingleton <IModelRepository <IStockStatistics> >(s => stockStatisticsModelRepository);
            serviceCollection.AddSingleton <IModelReaderRepository <IStockStatistics> >(s => stockStatisticsModelRepository);
            serviceCollection.AddSingleton <IModelWriterRepository <IStockStatistics> >(s => stockStatisticsModelRepository);
            serviceCollection.AddSingleton <ISupportsDataDeletion>(s => stockStatisticsModelRepository);

            serviceCollection
            .AddSingleton <ITimeSliceModelRepository <IStatistic>, TimeSliceModelRepository <IStatistic> >();
            serviceCollection.AddSingleton <ITimeSliceModelReaderRepository <IStatistic> >(s =>
                                                                                           s.GetService <ITimeSliceModelRepository <IStatistic> >());
            serviceCollection.AddSingleton <ITimeSliceModelWriterRepository <IStatistic> >(s =>
                                                                                           s.GetService <ITimeSliceModelRepository <IStatistic> >());

            //Aggregates Repositories
            foreach (var type in TypeHelper.FindNonAbstractTypes("StockTradingAnalysis.", typeof(IAggregateRoot)))
            {
                serviceCollection.AddTransient(typeof(IAggregateRepository <>).MakeGenericType(type), typeof(AggregateRepository <>).MakeGenericType(type));
            }
        }