public void ReadWriteCollateralCsv() { var csv = new CollateralCsv(); var csvPath = @"D:\collateral.csv"; csv.Load(csvPath); csv.UpdateCellValue(3, "PrincipalBalance", "12345"); csv.UpdateCellValue(5, "AsOfDate", "2016/12/07"); var newCsvPath = @"D:\collateral-new.csv"; csv.Save(newCsvPath); }
public static void UpdateDatasetByPredictModel(ProjectLogicModel project) { var dbAdapter = new DBAdapter(); var dealSchedule = project.DealSchedule.Instanse; var errorMsg = "Error occurred in UpdateDatasetByPredictModel"; CommUtils.Assert(dealSchedule.DeterminationDates != null && dealSchedule.DeterminationDates.Length > 0, errorMsg); CommUtils.Assert(dealSchedule.PaymentDates != null && dealSchedule.PaymentDates.Length > 0, errorMsg); CommUtils.Assert(dealSchedule.DeterminationDates.Length == dealSchedule.PaymentDates.Length, errorMsg); var asOfDateEndList = dealSchedule.DeterminationDates.ToList(); var asOfDateBeginList = asOfDateEndList.Select(x => x).ToList(); asOfDateBeginList.RemoveAt(asOfDateBeginList.Count - 1); asOfDateBeginList.Insert(0, dealSchedule.FirstCollectionPeriodStartDate); var newAsOfDateList = project.EnablePredictMode ? asOfDateBeginList : asOfDateEndList; var oldAsOfDateList = project.EnablePredictMode ? asOfDateEndList : asOfDateBeginList; var paymentDateList = dealSchedule.PaymentDates.ToList(); var durationPeriods = new List <DatasetScheduleLogicModel>(); if (project.EnablePredictMode) { //切换到预测模式 //使用过去一期的模型collateral里asset的PrincipalBalance覆盖掉未来一期的值 //从过去到未来遍历 durationPeriods.AddRange(project.DealSchedule.DurationPeriods); } else { //切换到当期模式: //使用未来一期的模型collateral里asset的PrincipalBalance覆盖掉过去一期的值 //从未来到过去遍历 durationPeriods.AddRange(project.DealSchedule.DurationPeriods); durationPeriods.Reverse(); } //Fields: asOfDate, assetId, principalBalance foreach (var period in durationPeriods) { var dataset = period.Dataset.Instance; if (dataset == null) { continue; } var idx = paymentDateList.IndexOf(period.PaymentDate); if (idx < 0 || idx >= newAsOfDateList.Count) { continue; } var ymlFolder = dbAdapter.Dataset.GetYmlFolder(project.Instance); var newAsOfDate = newAsOfDateList[idx]; var newDatasetFolder = dbAdapter.Dataset.GetDatasetFolder(project.Instance, newAsOfDate); var newCollateralCsvPath = Path.Combine(newDatasetFolder, "collateral.csv"); var oldAsOfDate = oldAsOfDateList[idx]; var oldDatasetFolder = dbAdapter.Dataset.GetDatasetFolder(project.Instance, oldAsOfDate); var oldCollateralCsvPath = Path.Combine(oldDatasetFolder, "collateral.csv"); dataset.AsOfDate = DateUtils.DateToDigitString(newAsOfDate); dbAdapter.Dataset.Update(dataset); if (File.Exists(newCollateralCsvPath)) { var assetPrincipalBalance = new Dictionary <int, string>(); if (File.Exists(oldCollateralCsvPath)) { var oldCollateralCsv = new CollateralCsv(); oldCollateralCsv.Load(oldCollateralCsvPath); foreach (var record in oldCollateralCsv) { //保存当次遍历Asset的期初本金 assetPrincipalBalance[record.AssetId] = record.Items["PrincipalBalance"]; } } else { if (oldAsOfDate > newAsOfDate && period.Next != null) { var deal = new ABSDeal(ymlFolder, newDatasetFolder); var acfDataset = deal.Result.AcfResult.Dataset.FirstOrDefault(x => x.PaymentDay == period.Previous.PaymentDate); if (acfDataset != null) { assetPrincipalBalance = acfDataset.ToDictionary(x => x.Asset.Id, x => x.Performing.ToString("n2")); } } } var collateralAsOfDate = asOfDateBeginList[idx]; var newCollateralCsv = new CollateralCsv(); newCollateralCsv.Load(newCollateralCsvPath); foreach (var record in newCollateralCsv) { var assetId = record.AssetId; //更新封包日日期 newCollateralCsv.UpdateCellValue(assetId, "AsOfDate", collateralAsOfDate.ToString("MM/dd/yyyy")); //更新Asset的期初本金 if (assetPrincipalBalance.ContainsKey(assetId)) { newCollateralCsv.UpdateCellValue(assetId, "PrincipalBalance", assetPrincipalBalance[assetId]); } } newCollateralCsv.Save(newCollateralCsvPath); } } }
public void GenerateNextDataset(int projectId) { CommUtils.Assert(m_isInitialized, "AssetModifier hasn't been initialized!"); var project = m_dbAdapter.Project.GetProjectById(projectId); var datasetFolder = m_dbAdapter.Dataset.GetDatasetFolder(project, m_asOfDay); CommUtils.Assert(Directory.Exists(datasetFolder), "Load dataset [" + project.ProjectGuid + "][" + m_asOfDay + "] failed!"); if (!m_nextAsOfDay.HasValue) { return; } //Create folder for next dataset if not exists. var nextDatasetFolder = m_dbAdapter.Dataset.GetDatasetFolder(project, m_nextAsOfDay.Value); if (!Directory.Exists(nextDatasetFolder)) { try { Directory.CreateDirectory(nextDatasetFolder); } catch (Exception e) { throw new ApplicationException("Create dataset [" + project.ProjectGuid + "][" + m_asOfDay + "] folder failed! Exception: " + e.Message); } } //Copy and modify collertal.csv var collateralPath = Path.Combine(datasetFolder, "collateral.csv"); var nextCollateralPath = Path.Combine(nextDatasetFolder, "collateral.csv"); FileUtils.Copy(collateralPath, nextCollateralPath); var amortizationPath = Path.Combine(datasetFolder, "AmortizationSchedule.csv"); var nextAmortizationPath = Path.Combine(nextDatasetFolder, "AmortizationSchedule.csv"); FileUtils.Copy(amortizationPath, nextAmortizationPath, false); var promisedCashflowPath = Path.Combine(datasetFolder, "PromisedCashflow.csv"); var nextPromisedCashflowPath = Path.Combine(nextDatasetFolder, "PromisedCashflow.csv"); FileUtils.Copy(promisedCashflowPath, nextPromisedCashflowPath, false); var reinvestmentPath = Path.Combine(datasetFolder, "Reinvestment.csv"); var nextReinvestmentPath = Path.Combine(nextDatasetFolder, "Reinvestment.csv"); FileUtils.Copy(reinvestmentPath, nextReinvestmentPath, false); var currentCombileVaiablesPath = Path.Combine(datasetFolder, "CombinedVariables.csv"); var nextCombileVaiablesPath = Path.Combine(nextDatasetFolder, "CombinedVariables.csv"); FileUtils.Copy(currentCombileVaiablesPath, nextCombileVaiablesPath, false); var currentVariablesPath = Path.Combine(datasetFolder, "CurrentVariables.csv"); var nextCurrentVariablesPath = Path.Combine(nextDatasetFolder, "CurrentVariables.csv"); FileUtils.Copy(currentVariablesPath, nextCurrentVariablesPath, false); var futureVariablesPath = Path.Combine(datasetFolder, "FutureVariables.csv"); var nextFutureVariablesPath = Path.Combine(nextDatasetFolder, "FutureVariables.csv"); FileUtils.Copy(futureVariablesPath, nextFutureVariablesPath, false); var pastVariablesPath = Path.Combine(datasetFolder, "PastVariables.csv"); var nextPastVariablesPath = Path.Combine(nextDatasetFolder, "PastVariables.csv"); FileUtils.Copy(pastVariablesPath, nextPastVariablesPath, false); BasicAnalyticsData basicAnalyticsData = NancyUtils.GetBasicAnalyticsData(projectId, null, m_asOfDay); //AssetId, 剩余本金 var dictPrincipalBalance = new Dictionary <int, double>(); foreach (var assetCashFlow in basicAnalyticsData.BasicAssetCashflow.BasicAssetCashflowItems) { if (assetCashFlow.PaymentDate == m_paymentDay) { dictPrincipalBalance[assetCashFlow.AssetId] = assetCashFlow.Performing; } } var collateralCsv = new CollateralCsv(); collateralCsv.Load(nextCollateralPath); foreach (var record in collateralCsv) { var assetId = record.AssetId; //更新封包日日期 collateralCsv.UpdateCellValue(assetId, "AsOfDate", m_nextAsOfDay.Value.ToString("MM/dd/yyyy")); //更新剩余本金 collateralCsv.UpdateCellValue(assetId, "PrincipalBalance", dictPrincipalBalance.ContainsKey(assetId) ? dictPrincipalBalance[assetId].ToString() : "0"); if (collateralCsv.ContainsColumn("AccrueFromAsOfDate")) { collateralCsv.UpdateCellValue(assetId, "AccrueFromAsOfDate", "FALSE"); } } collateralCsv.Save(nextCollateralPath); ProcessVaiables(m_dbAdapter.Dataset.GetYmlFolder(project), basicAnalyticsData); if (m_previousPaymentDay != m_paymentDay && File.Exists(nextPromisedCashflowPath)) { var p = new PromisedCashflowCsv(nextPromisedCashflowPath); p.Load(); p.Records = p.Records.Where(o => o.PaymentDate > m_previousPaymentDay).ToList(); p.Save(); } }