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;
        }
Esempio n. 2
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);
 }