public BuyAndHoldTransactionsViewModel BuildTransactionObjects(List<BuyAndHoldTransactionDto> transactions, NavDto nav, SecurityDto security, bool seperateReinvest)
        {
            var currentShareValue = double.Parse(nav.Nav.ToString());// GetLatestTickersNav("TWCUX");
            var splits = security.Splits;

            var viewModel = new BuyAndHoldTransactionsViewModel
            {
                Nav = currentShareValue.ToString("C"),
                SecurityTicker = security.TickerSymbol,
                SecurityName = security.CompanyName,
                Transactions = new List<BuyAndHoldTransactionViewModel>()
            };
            double totalInvested = 0;
            double totalReinvested = 0;
            double totalShares = 0;
            double currentValue = 0;
            double totalValue = 0;
            //double totalTotalValue = 0;
            double gain = 0;
            double totalGain = 0;

            decimal TOTAL = 0;
            //foreach (var transaction in transactions.Where(p => !p.ValidTo.HasValue || p.ValidTo.Value > DateTime.Now))
            foreach (var transaction in transactions)
            {
                // var transactionDate = transaction.TransactionDate.Value;
                var splitFactor = CalculateSplitFactor(splits, transaction.TransactionDate.Value);
                var currentShares = CalculateCurrentShares(transaction, splitFactor);

                var buyAndHoldTransaction = new BuyAndHoldTransactionViewModel
                {
                    Id = transaction.Id,
                    TransactionDate = transaction.TransactionDate.Value.ToShortDateString(),
                    SplitRatio = splitFactor != 1 ? Math.Round(splitFactor, 4).ToString() : string.Empty,
                    // TransactionType = transaction.TransactionType,
                    TransactionTypeId = transaction.TransactionTypeId,
                    NumberShares = CalculateShares(transaction, splitFactor, currentShares),
                    ValidTo = transaction.ValidTo.ToShortDateString(),
                    ValidFrom = transaction.ValidFrom.ToShortDateString()
                };

                if (transaction.OriginalTransaction != null)
                {
                    buyAndHoldTransaction.OriginalTransaction = BuildTransactionObject(transaction.OriginalTransaction, nav, splits, seperateReinvest);
                }

                if (IsMergerTransaction(transaction.TransactionTypeId)
                    && transaction.TransactionApplied.ToBool())
                {

                }
                else {

                    buyAndHoldTransaction.BuyPrice = CalculateBuyPrice(transaction, splitFactor);

                    var amountInvested = transaction.AmountInvested.HasValue ? double.Parse(transaction.AmountInvested.Value.ToString()) : 0;

                    if (transaction.TransactionType == "Buy" || !seperateReinvest)
                    {
                        totalInvested += amountInvested;
                    }
                    else
                    {
                        totalReinvested += amountInvested;
                    }
                    // var _shares = shares.HasValue ? decimal.Parse(shares.Value.ToString()) : 0;
                    // if (_shares > 0)
                    // {
                    //     TOTAL += _shares;
                    // }

                    if (transaction.TransactionTypeId != 15) //Split --            if (transaction.TransactionTypeId != 15) //Split                    {
                    {
                        totalShares += currentShares;
                        currentValue = currentShares * currentShareValue;
                        totalValue += currentValue;
                        gain = currentValue - amountInvested;
                        totalGain += gain;
                        //profit = currentValue - amountInvested;
                    }

                    //var sellTransaction = transaction.BuyAndHoldSellTransaction;
                    //var originalTransaction = transaction.BuyAndHoldOriginalTransaction;

                    //if (originalTransaction != null)
                    //{
                    //    var originalTransactionAmountInvested = originalTransaction.AmountInvested.HasValue ? originalTransaction.AmountInvested.Value : 0;

                    //    buyAndHoldTransaction.OriginalTransaction = new BuyAndHoldTransactionViewModel();
                    //    buyAndHoldTransaction.OriginalTransaction.Type = originalTransaction.Type + " [" + originalTransaction.OriginalSymbol.ToUpper() + "]";
                    //    buyAndHoldTransaction.OriginalTransaction.Id = originalTransaction.id;
                    //    buyAndHoldTransaction.OriginalTransaction.AmountInvested = originalTransactionAmountInvested.ToString("C");
                    //}

                    //if (sellTransaction != null)
                    //{
                    //    buyAndHoldTransaction.SellDate = sellTransaction.TransactionDate.Value.ToShortDateString();
                    //    buyAndHoldTransaction.SellPrice = sellTransaction.BuyPrice.HasValue ? sellTransaction.BuyPrice.Value.ToString("C") : "";
                    //    buyAndHoldTransaction.Proceeds = sellTransaction.AmountInvested.HasValue ? sellTransaction.AmountInvested.Value.ToString("C") : "";
                    //    buyAndHoldTransaction.Profit = ((sellTransaction.AmountInvested.HasValue ? sellTransaction.AmountInvested.Value : 0) - amountInvested).ToString("C");
                    //}

                    if (transaction.TransactionType == "Buy" || !seperateReinvest)
                    {
                        buyAndHoldTransaction.AmountInvested = amountInvested.ToString("C");
                    }
                    else if (amountInvested != 0)
                    {
                        buyAndHoldTransaction.AmountReinvested = amountInvested.ToString("C");
                    }
                    buyAndHoldTransaction.DividendAmount = transaction.DividendAmount.HasValue ? transaction.DividendAmount.Value.ToString("C") : "";

                    if (transaction.NumberShares.HasValue && transaction.TransactionType != "Split")
                    {
                        buyAndHoldTransaction.CurrentValue = currentValue.ToString("C");
                    }
                    if (transaction.TransactionType != "Split")
                    {
                        buyAndHoldTransaction.IsGainNegative = gain < 0;
                        buyAndHoldTransaction.Gain = gain.ToString("C");
                    }

                    buyAndHoldTransaction.TransactionType = transaction.TransactionType; //.ToString();

                    if ((transaction.TransactionType != null && transaction.TransactionType == "Split") || transaction.TransactionTypeId == 15)
                    {
                        buyAndHoldTransaction.TransactionType += " - " + transaction.Split;
                    }

                    buyAndHoldTransaction.TotalShares = double.Parse(totalShares.ToString("F3"));
                    buyAndHoldTransaction.TotalValue = totalValue.ToString("C"); //TOTAL.ToString("C");

                    //if (transaction.AmericanCenturySell != null)
                    //{
                    //    var sellPrice = transaction.AmericanCenturySell.SellPrice;
                    //    var proceeds = shares * sellPrice;

                    //    AmericanCenturyTransaction.SellDate = transaction.AmericanCenturySell.SellDate.Date.ToShortDateString();
                    //    AmericanCenturyTransaction.SellPrice = transaction.AmericanCenturySell.SellPrice.ToString("C");
                    //    AmericanCenturyTransaction.Proceeds = proceeds.ToString("C");
                    //    AmericanCenturyTransaction.Profit = (proceeds - amountInvested).ToString("C");
                    //    AmericanCenturyTransaction.CurrentValue = null;
                    //    AmericanCenturyTransaction.Gain = null;
                    //}
                }
                viewModel.Transactions.Add(buyAndHoldTransaction);
            }
            viewModel.TotalInvested = totalInvested.ToString("C");
            viewModel.TotalReinvested = totalReinvested.ToString("C");
            viewModel.TotalShares = totalShares.ToString("F3");
            viewModel.TotalValue = totalValue.ToString("C");
            viewModel.TotalGain = totalGain.ToString("C");
            return viewModel;
        }
        private BuyAndHoldTransactionViewModel BuildTransactionObject(BuyAndHoldTransactionDto transactionDto, NavDto nav, List<BuyAndHoldSplitDto> splits, bool seperateReinvest)
        {
            var currentShareValue = double.Parse(nav.Nav.ToString());// GetLatestTickersNav("TWCUX");

            // var transactionDate = transaction.TransactionDate.Value;
            var splitFactor = CalculateSplitFactor(splits, transactionDto.TransactionDate.Value);
            var currentShares = CalculateCurrentShares(transactionDto, splitFactor);

            var transaction = new BuyAndHoldTransactionViewModel
            {
                Id = transactionDto.Id,
                TransactionDate = transactionDto.TransactionDate.Value.ToShortDateString(),
                SplitRatio = splitFactor != 1 ? Math.Round(splitFactor, 4).ToString() : string.Empty,
               // TransactionType = transactionDto.TransactionType,
                TransactionTypeId = transactionDto.TransactionTypeId,
                NumberShares = CalculateShares(transactionDto, splitFactor, currentShares),
            };

            if (transactionDto.OriginalTransaction != null)
            {
                transaction.OriginalTransaction = BuildTransactionObject(transactionDto.OriginalTransaction, nav, splits, seperateReinvest);
            }
            //var shares = transaction.NumberShares;

            if (IsMergerTransaction(transactionDto.TransactionTypeId) && transactionDto.TransactionApplied.ToBool())
            {

            }
            else {

                transaction.BuyPrice = CalculateBuyPrice(transactionDto, splitFactor);

                var amountInvested = transactionDto.AmountInvested.HasValue ? double.Parse(transactionDto.AmountInvested.Value.ToString()) : 0;

                if (transactionDto.TransactionType == "Buy" || !seperateReinvest)
                {
                    transaction.AmountInvested = amountInvested.ToString("C");
                }
                else if (amountInvested != 0)
                {
                    transaction.AmountReinvested = amountInvested.ToString("C");
                }
                transaction.DividendAmount = transactionDto.DividendAmount.HasValue ? transactionDto.DividendAmount.Value.ToString("C") : "";

                transaction.TransactionType = transactionDto.TransactionType.ToString();

                if (transactionDto.TransactionType != null && transactionDto.TransactionType == "Split")
                {
                    transaction.TransactionType += " - " + transactionDto.Split;
                }
            }
            return transaction;
        }
        public BuyAndHoldRawTransactionsViewModel BuildRawTransactionObjects(List<BuyAndHoldRawTransactionDto> transactions, NavDto nav, bool seperateReinvest)
        {
            var currentShareValue = nav.Nav; //GetLatestTickersNav("TWCUX");

            var viewModel = new BuyAndHoldRawTransactionsViewModel
            {
                Nav = currentShareValue.ToString("C"),
                SecurityTicker = "TWCUX",
                SecurityName = "American Century Ultra",
                Transactions = new List<BuyAndHoldRawTransactionViewModel>()
            };
            decimal totalInvested = 0;
            decimal totalReinvested = 0;
            decimal totalShares = 0;
            decimal currentValue = 0;
            decimal totalValue = 0;
            decimal gain = 0;
            foreach (var transaction in transactions)
            {
                var buyAndHoldTransaction = new BuyAndHoldRawTransactionViewModel();

                var shares = transaction.NumberShares;
                var amountInvested = transaction.AmountInvested.HasValue ? transaction.AmountInvested.Value : 0;

                if (transaction.TransactionType == "Buy" || !seperateReinvest)
                {
                    totalInvested += amountInvested;
                }
                else
                {
                    totalReinvested += amountInvested;
                }

                var _shares = shares.HasValue ? decimal.Parse(shares.Value.ToString()) : 0;

                if (_shares != 0)
                {
                    totalShares += _shares;
                    currentValue = _shares * currentShareValue;
                    totalValue += currentValue;
                    gain = currentValue - amountInvested;
                    //profit = currentValue - amountInvested;
                }

                buyAndHoldTransaction.Id = transaction.Id;
                //var sellTransaction = transaction.BuyAndHoldSellTransaction;
                //var originalTransaction = transaction.BuyAndHoldOriginalTransaction;

                //if (originalTransaction != null)
                //{
                //    var originalTransactionAmountInvested = originalTransaction.AmountInvested.HasValue ? originalTransaction.AmountInvested.Value : 0;

                //    buyAndHoldTransaction.OriginalTransaction = new BuyAndHoldTransactionViewModel();
                //    buyAndHoldTransaction.OriginalTransaction.Type = originalTransaction.Type + " [" + originalTransaction.OriginalSymbol.ToUpper() + "]";
                //    buyAndHoldTransaction.OriginalTransaction.Id = originalTransaction.id;
                //    buyAndHoldTransaction.OriginalTransaction.AmountInvested = originalTransactionAmountInvested.ToString("C");
                //}

                //if (sellTransaction != null)
                //{
                //    buyAndHoldTransaction.SellDate = sellTransaction.TransactionDate.Value.ToShortDateString();
                //    buyAndHoldTransaction.SellPrice = sellTransaction.BuyPrice.HasValue ? sellTransaction.BuyPrice.Value.ToString("C") : "";
                //    buyAndHoldTransaction.Proceeds = sellTransaction.AmountInvested.HasValue ? sellTransaction.AmountInvested.Value.ToString("C") : "";
                //    buyAndHoldTransaction.Profit = ((sellTransaction.AmountInvested.HasValue ? sellTransaction.AmountInvested.Value : 0) - amountInvested).ToString("C");
                //}
                buyAndHoldTransaction.BuyDate = transaction.TransactionDate.Value.ToShortDateString();
                if (transaction.TransactionType == "Buy" || !seperateReinvest)
                {
                    buyAndHoldTransaction.AmountInvested = amountInvested.ToString("C");
                }
                else if (amountInvested != 0)
                {
                    buyAndHoldTransaction.AmountReinvested = amountInvested.ToString("C");
                }
                buyAndHoldTransaction.DividendAmount = transaction.DividendAmount.HasValue ? transaction.DividendAmount.Value.ToString("C") : "";
                buyAndHoldTransaction.BuyPrice = transaction.BuyPrice.HasValue ? transaction.BuyPrice.Value.ToString("C") : "";
                buyAndHoldTransaction.NumberShares = transaction.NumberShares.HasValue ? transaction.NumberShares.Value.ToString("F3") : "";
                if (transaction.NumberShares.HasValue)
                {
                    buyAndHoldTransaction.CurrentValue = currentValue.ToString("C");
                }
                buyAndHoldTransaction.IsGainNegative = gain < 0;
                buyAndHoldTransaction.Gain = gain.ToString("C");
                buyAndHoldTransaction.TransactionType = transaction.TransactionType;
                buyAndHoldTransaction.TotalShares = double.Parse(totalShares.ToString("F3"));
                buyAndHoldTransaction.TotalValue = totalValue.ToString("C");

                //if (transaction.AmericanCenturySell != null)
                //{
                //    var sellPrice = transaction.AmericanCenturySell.SellPrice;
                //    var proceeds = shares * sellPrice;

                //    AmericanCenturyTransaction.SellDate = transaction.AmericanCenturySell.SellDate.Date.ToShortDateString();
                //    AmericanCenturyTransaction.SellPrice = transaction.AmericanCenturySell.SellPrice.ToString("C");
                //    AmericanCenturyTransaction.Proceeds = proceeds.ToString("C");
                //    AmericanCenturyTransaction.Profit = (proceeds - amountInvested).ToString("C");
                //    AmericanCenturyTransaction.CurrentValue = null;
                //    AmericanCenturyTransaction.Gain = null;
                //}

                viewModel.Transactions.Add(buyAndHoldTransaction);
            }
            viewModel.TotalInvested = totalInvested.ToString("C");
            viewModel.TotalReinvested = totalReinvested.ToString("C");
            viewModel.TotalShares = totalShares.ToString("F3");
            viewModel.TotalValue = totalValue.ToString("C");
            return viewModel;
        }
Example #4
0
 private BuyAndHoldTransactionsViewModel GetBuyAndHoldTransactionList(string ticker, NavDto nav, SecurityDto security)
 {
     var transactions = _buyAndHoldTransactionLibrary.GetBuyAndHoldTransactions(security.TickerSymbol, security.Id, false);
     return _buyAndHoldTransactionLibrary.BuildTransactionObjects(transactions, nav, security, true);
 }
Example #5
0
 private BuyAndHoldRawTransactionsViewModel GetBuyAndHoldRawTransactionList(string ticker, NavDto nav)
 {
     var transactions = _buyAndHoldRawTransactionLibrary.GetBuyAndHoldRawTransactions(ticker, false);
     return _buyAndHoldRawTransactionLibrary.BuildRawTransactionObjects(transactions, nav, true);
 }
Example #6
0
 //Formatted list of Transactions
 public ActionResult Transactions(string id = "")
 {
     var security = new SecurityDto();
     var nav = new NavDto();
     var viewModel = new BuyAndHoldTransactionsViewModel();
     int i = 0;
     if (int.TryParse(id, out i))
     {
         security = _securityService.GetSecurityById(i);
         nav = _navService.GetTicker(security.TickerSymbol);
         viewModel = GetBuyAndHoldTransactionList(i.ToString(), nav, security);
     }
     else {
         security = _securityService.GetSecurityBySymbol(id);
         nav = _navService.GetTicker(id);
         viewModel = GetBuyAndHoldTransactionList(id, nav, security);
     }
     viewModel.SecurityTicker = security.TickerSymbol;
     viewModel.SecurityName = security.CompanyName;
     viewModel.Nav = nav.Nav.ToString("C");
     viewModel.NavDate = nav.Date.ToShortDateString();
     return View(viewModel);
 }