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 void ProcessDataTest() { int id = 1; MasterPortfolio mp = this.dbContext.MasterPortfolios.Find(id); Assert.NotNull(mp); PortfolioReturnComputeProcess target = new PortfolioReturnComputeProcess(); target.ProcessData(mp); }
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)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Date,Name,BaseAmount,Benchmark,Status")] MasterPortfolio masterPortfolio) { if (id != masterPortfolio.Id) { return(NotFound()); } if (ModelState.IsValid) { try { MasterPortfolio exist = _context.MasterPortfolios.Find(masterPortfolio.Id); exist.Name = masterPortfolio.Name; exist.Date = masterPortfolio.Date; exist.BaseAmount = masterPortfolio.BaseAmount; exist.Benchmark = masterPortfolio.Benchmark; exist.Status = masterPortfolio.Status; exist.ModifiedOn = DateTime.Now; _context.Update(exist); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MasterPortfolioExists(masterPortfolio.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(masterPortfolio)); }
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)); }