public async Task <IActionResult> Create([Bind("Id,MasterPortfolioId,StockId,Weight")] PortfolioHolding portfolioHolding)
        {
            MasterPortfolio mp = _context.MasterPortfolios.Find(portfolioHolding.MasterPortfolioId);
            StockInfo       si = _context.StockInfos.Find(portfolioHolding.StockId);

            if (mp == null || si == null)
            {
                return(View(portfolioHolding));
            }
            string    key   = String.Format("{0}_{1}", si.Id, mp.Date.ToString("yyyyMMdd"));
            StockPerf price = _context.StockPerfs.Find(key);

            if (price == null)
            {
                return(View(portfolioHolding));
            }
            if (ModelState.IsValid)
            {
                portfolioHolding.ModifiedOn   = portfolioHolding.CreatedOn = DateTime.Now;
                portfolioHolding.Shares       = (int)Math.Floor(portfolioHolding.Weight.Value * mp.BaseAmount.Value / price.Close.Value / 100 / 100) * 100;//整手
                portfolioHolding.ActualWeight = 100 * portfolioHolding.Shares * price.Close.Value / mp.BaseAmount;
                _context.Add(portfolioHolding);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(portfolioHolding));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,MasterPortfolioId,StockId,Weight")] PortfolioHolding portfolioHolding)
        {
            if (id != portfolioHolding.Id)
            {
                return(NotFound());
            }
            MasterPortfolio mp = _context.MasterPortfolios.Find(portfolioHolding.MasterPortfolioId);
            StockInfo       si = _context.StockInfos.Find(portfolioHolding.StockId);

            if (mp == null || si == null)
            {
                return(View(portfolioHolding));
            }
            string    key   = String.Format("{0}_{1}", si.Id, mp.Date.ToString("yyyyMMdd"));
            StockPerf price = _context.StockPerfs.Find(key);

            if (price == null)
            {
                return(View(portfolioHolding));
            }
            PortfolioHolding exist = _context.PortfolioHoldings.Find(portfolioHolding.Id);

            if (ModelState.IsValid)
            {
                try
                {
                    exist.StockId           = portfolioHolding.StockId;
                    exist.Weight            = portfolioHolding.Weight;
                    exist.MasterPortfolioId = portfolioHolding.MasterPortfolioId;
                    exist.ModifiedOn        = DateTime.Now;
                    exist.Shares            = (int)Math.Floor(portfolioHolding.Weight.Value * mp.BaseAmount.Value / price.Close.Value / 100 / 100) * 100;//整手
                    exist.ActualWeight      = 100 * portfolioHolding.Shares * price.Close.Value / mp.BaseAmount;
                    _context.Update(exist);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PortfolioHoldingExists(portfolioHolding.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(portfolioHolding));
        }
Example #3
0
        public static List <PortfolioHolding> GetTestList()
        {
            int numOfFunds = 12;

            PortfolioHolding[] testArray = new PortfolioHolding[numOfFunds];
            testArray[0] = new PortfolioHolding {
                Symbol = "FPMAX", NumberOfShares = 3766.487M
            };
            testArray[1] = new PortfolioHolding {
                Symbol = "FSEVX", NumberOfShares = 862.661M
            };
            testArray[2] = new PortfolioHolding {
                Symbol = "PRASX", NumberOfShares = 5046.534M
            };
            testArray[3] = new PortfolioHolding {
                Symbol = "PRHSX", NumberOfShares = 728.24M
            };
            testArray[4] = new PortfolioHolding {
                Symbol = "PRITX", NumberOfShares = 3928.275M
            };
            testArray[5] = new PortfolioHolding {
                Symbol = "PRNHX", NumberOfShares = 686.556M
            };
            testArray[6] = new PortfolioHolding {
                Symbol = "VEMAX", NumberOfShares = 2515.0M
            };
            testArray[7] = new PortfolioHolding {
                Symbol = "VEUSX", NumberOfShares = 237.304M
            };
            testArray[8] = new PortfolioHolding {
                Symbol = "VFSVX", NumberOfShares = 830.336M
            };
            testArray[9] = new PortfolioHolding {
                Symbol = "VPADX", NumberOfShares = 186.273M
            };
            testArray[10] = new PortfolioHolding {
                Symbol = "VTMSX", NumberOfShares = 693.775M
            };
            testArray[11] = new PortfolioHolding {
                Symbol = "VTRIX", NumberOfShares = 560.45M
            };
            return(testArray.ToList());
        }
        public async Task <IActionResult> Create([Bind("Id,Date,Benchmark,BaseAmount,Name")] MasterPortfolio masterPortfolio, string holdings)
        {
            if (masterPortfolio.Benchmark == null || !masterPortfolio.BaseAmount.HasValue)
            {
                return(View(masterPortfolio));
            }
            StockInfo si = _context.StockInfos.Find(masterPortfolio.Benchmark);

            if (si == null)
            {
                return(View(masterPortfolio));
            }
            if (masterPortfolio.BaseAmount.HasValue)
            {
                masterPortfolio.BaseAmount = 100000;
            }
            if (ModelState.IsValid)
            {
                //masterPortfolio.Name = string.Format("{0}-{1}",si.Name,masterPortfolio.Date.ToString("yyyyMMdd"));
                masterPortfolio.Status    = MasterPortfolioStatus.Pending;
                masterPortfolio.CreatedOn = masterPortfolio.ModifiedOn = DateTime.Now;
                _context.Add(masterPortfolio);
                await _context.SaveChangesAsync();

                if (!String.IsNullOrEmpty(holdings))
                {
                    string[] holdingVals = holdings.Split(new char[] { ',', ';' });
                    List <PortfolioHolding> holdingList = new List <PortfolioHolding>();
                    foreach (string val in holdingVals)
                    {
                        if (String.IsNullOrEmpty(val))
                        {
                            continue;
                        }
                        PortfolioHolding ph = new PortfolioHolding()
                        {
                            MasterPortfolioId = masterPortfolio.Id,
                            StockId           = val.Split(new char[] { '_' })[0],
                        };
                        holdingList.Add(ph);
                    }
                    foreach (PortfolioHolding ph in holdingList)
                    {
                        ph.Weight = 100.0m / holdingList.Count;
                        si        = _context.StockInfos.Find(ph.StockId);
                        if (si == null)
                        {
                            return(View(masterPortfolio));
                        }
                        string    key   = String.Format("{0}_{1}", si.Id, masterPortfolio.Date.ToString("yyyyMMdd"));
                        StockPerf price = _context.StockPerfs.Find(key);
                        if (price == null)
                        {
                            return(View(masterPortfolio));
                        }
                        ph.ModifiedOn   = ph.CreatedOn = DateTime.Now;
                        ph.Shares       = (int)Math.Floor(ph.Weight.Value * masterPortfolio.BaseAmount.Value / price.Close.Value / 100 / 100) * 100;//整手
                        ph.ActualWeight = 100 * ph.Shares * price.Close.Value / masterPortfolio.BaseAmount;
                        _context.Add(ph);
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(masterPortfolio));
        }