using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using Framework;
using Holworth.RiskInterface;
using Contract.Domain;
using Contract.IService;
using Holworth.RiskEngine;
using Framework.IDao;
using HraModel;
namespace Holworth.Services.Risk
{
public class ForwardPriceSimulationService : BaseService.EntityService<Framework.Domain.Entity>, IForwardPriceSimulationService
{
#region Serive Area
IRskCoreFactorService _riskCoreFactorService = null;
public IRskCoreFactorService riskCoreFactorService
{
get
{
if (_riskCoreFactorService == null)
{
_riskCoreFactorService = ctx["RskCoreFactorService"] as IRskCoreFactorService;
}
return _riskCoreFactorService;
}
}
IRskFactorService _riskFactorService = null;
public IRskFactorService riskFactorService
{
get
{
if (_riskFactorService == null)
{
_riskFactorService = ctx["RskFactorService"] as IRskFactorService;
}
return _riskFactorService;
}
}
IRandomNumberService _randNumberService = null;
IRandomNumberService randNumberService
{
get
{
if (_randNumberService == null)
{
_randNumberService = ctx["RandomNumberService"] as IRandomNumberService;
}
return _randNumberService;
}
}
IMktPriceMarketDataService _indexPriceService = null;
IMktPriceMarketDataService indexPriceService
{
get
{
if (_indexPriceService == null)
{
_indexPriceService = ctx["MktPriceMarketDataService"] as IMktPriceMarketDataService;
}
return _indexPriceService;
}
}
IRskFactorVolatilityDataService _riskVolatilityService = null;
IRskFactorVolatilityDataService riskVolatilityService
{
get
{
if (_riskVolatilityService == null)
{
_riskVolatilityService = ctx["RskFactorVolatilityDataService"] as IRskFactorVolatilityDataService;
}
return _riskVolatilityService;
}
}
IRiskCorrelationDecompositionService _decomposeSerive = null;
IRiskCorrelationDecompositionService decomposeSerive
{
get
{
if (_decomposeSerive == null)
{
_decomposeSerive = ctx["RiskCorrelationDecompositionService"] as IRiskCorrelationDecompositionService;
}
return _decomposeSerive;
}
}
IGaussianDistributionService _gaussianDistService = null;
IGaussianDistributionService gaussianDistService
{
get
{
if (_gaussianDistService == null)
{
_gaussianDistService = ctx["GaussianDistributionService"] as IGaussianDistributionService;
}
return _gaussianDistService;
}
}
#endregion
public void SimulateForwardPriceForMultiPeriod(DateTime simulateDate, int simulateNumber, int horizonNumber, double timeStepLength, int windowSize, int periodTypeLookupID, int simulationMethodLookupID)
{
IList<RskCoreFactor> riskCoreFactors = riskCoreFactorService.GetValidListByIDWithSpringDao(Constants.VALID);
int riskCoreFactorNumber = riskCoreFactors.Count;
IList<string> results = new List<string>();
double[,] randNumber = new double[simulateNumber, riskCoreFactorNumber];
randNumberService.GenerateQuasiRandomNumber(simulateNumber, riskCoreFactorNumber, ref randNumber);
double[,] factorLoadingMatrix = new double[riskCoreFactorNumber, riskCoreFactorNumber];
double[] eigenValueVector = new double[riskCoreFactorNumber];
decomposeSerive.DecomposeCorrelationMatrixByRiskCoreFactor(simulateDate, riskCoreFactorNumber, riskCoreFactors, ref factorLoadingMatrix, ref eigenValueVector);
//int riskCoreFactorID = new int();
int riskFactorID = new int();
int indexPriceFactorID = new int();
int simulateID = new int();
int[] riskCoreFactorID = new int[riskCoreFactorNumber];
double[] currentPrice = new double[riskCoreFactorNumber];
double[] priceShock = new double[riskCoreFactorNumber];
double[,] simulatedPrice = new double[simulateNumber, riskCoreFactorNumber];
double[] zeroRate = new double[riskCoreFactorNumber];
double[] riskVolatility = new double[riskCoreFactorNumber];
double startForwardPrice = 0;
double sumOfRandNumber = 0;
double dayOfOneYear = Constants.DAYS_IN_ONE_YEAR;
double tempZeroRate = 0.00;
int i = 0;
foreach (RskCoreFactor riskCoreFactor in riskCoreFactors)
{
//need to get index price factor from risk factor
riskCoreFactorID[i] = int.Parse(riskCoreFactor.Id);
riskFactorID = riskCoreFactor.RiskFactorId;
indexPriceFactorID = riskFactorService.GetFactorIDByRiskFactorID(riskFactorID);
currentPrice[i] = (double)indexPriceService.GetIndexPriceValueByDateIDWithSpringDao(simulateDate, indexPriceFactorID);
zeroRate[i] = tempZeroRate; // need to get from database
riskVolatility[i] = (double)riskVolatilityService.GetValueByDateFactorIDSize(simulateDate, riskFactorID, windowSize);
i++;
}
IList<RskCoreFctSimulatedPrice> simulatedPricesToDelete = new List<RskCoreFctSimulatedPrice>();
//IList<RiskCoreFactorSimulatedPrice> outputList = new List<RiskCoreFactorSimulatedPrice>();
i = 0;
int horizonID = 0;
IList<RskCoreFctSimulatedPrice> simulatedPrices = new List<RskCoreFctSimulatedPrice>();
for (i = 0; i < simulateNumber; i++)
{
simulateID = i;
for (int j = 0; j < riskCoreFactorNumber; j++)
{
for (int l = 0; l < horizonNumber; l++)
{
horizonID = l;
if (horizonID == 0)
{
startForwardPrice = currentPrice[j];
//logForwardPrice = Math.Log(startForwardPrice);
}
else
{
startForwardPrice = simulatedPrice[i, j];
//logForwardPrice = Math.Log(startForwardPrice);
}
sumOfRandNumber = 0;
for (int k = 0; k < riskCoreFactorNumber; k++) // can be truncated by number of principal components
{
sumOfRandNumber += randNumberService.GenerateInverseRandomNumber(randNumber[i, k]) * factorLoadingMatrix[j, k];
}
priceShock[j] = (zeroRate[j] / dayOfOneYear - 0.5 * Math.Pow(riskVolatility[j], 2)) * timeStepLength + riskVolatility[j] * sumOfRandNumber;
simulatedPrice[i, j] = startForwardPrice * Math.Exp(priceShock[j]);
RskCoreFctSimulatedPrice riskFactorsimulatedPrice = new RskCoreFctSimulatedPrice();
riskFactorsimulatedPrice.RiskCoreFactorId = riskCoreFactorID[j];
riskFactorsimulatedPrice.SimulateDate = simulateDate;
riskFactorsimulatedPrice.HorizonDate = simulateDate.AddDays(horizonID);
riskFactorsimulatedPrice.HorizonId = horizonID;
riskFactorsimulatedPrice.PeriodType = periodTypeLookupID.ToString();
riskFactorsimulatedPrice.SimulationMethod = simulationMethodLookupID.ToString();
riskFactorsimulatedPrice.SimulationNumber = simulateNumber;
riskFactorsimulatedPrice.SimulationId = simulateID;
riskFactorsimulatedPrice.CurrentPrice = (decimal)startForwardPrice;
riskFactorsimulatedPrice.SimulatedPriceShock = (decimal)priceShock[j];
riskFactorsimulatedPrice.SimulatedForwardPrice = (decimal)simulatedPrice[i, j];
riskFactorsimulatedPrice.CreateUname = CurrentUser.UserName;
riskFactorsimulatedPrice.CreateUid = CurrentUser.UserId;
riskFactorsimulatedPrice.CreateDate = DateTime.Now;
riskFactorsimulatedPrice.State.MarkNew();
simulatedPrices.Add(riskFactorsimulatedPrice);
}
}
}
Framework.QueryInfo info = new Framework.QueryInfo();
info.CustomSQL = "delete from RskCoreFctSimulatedPrice";
info.AddParam("SimulateDate", simulateDate);
info.AddParam("SimulationNumber", simulateNumber);
Dao.ExecuteUpdate(info);
StringBuilder sb = new StringBuilder();
Framework.QueryInfo rInfo = new QueryInfo();
int rowCount = simulatedPrices.Count;
int pageSize = 1000;
int pages = 0;
pages = rowCount / pageSize;
if (rowCount <= pageSize)
{
sb = new StringBuilder();
for (int j = 1; j <= rowCount; j++)
{
var r = simulatedPrices[j - 1];
string s = InsertInfoRskCoreFctSimulatedPrice(r);
sb.AppendFormat(s);
}
rInfo.CustomSQL = "begin " + sb.ToString() + " end;";
Dao.ExecuteNonQuery(rInfo);
}
else
{
for (int pageIndex = 1; pageIndex <= pages; pageIndex++)
{
sb = new StringBuilder();
for (int j = (pageIndex - 1) * pageSize + 1; j <= pageIndex * pageSize; j++)
{
var r = simulatedPrices[j - 1];
string s = InsertInfoRskCoreFctSimulatedPrice(r);
sb.AppendFormat(s);
}
rInfo.CustomSQL = "begin " + sb.ToString() + " end;";
Dao.ExecuteNonQuery(rInfo);
}
if (rowCount % pageSize != 0)
{
int rows = rowCount / pageSize;
sb = new StringBuilder();
for (int j = pages * pageSize + 1; j <= pages * pageSize + rows; j++)
{
var r = simulatedPrices[j - 1];
string s = InsertInfoRskCoreFctSimulatedPrice(r);
sb.AppendFormat(s);
}
rInfo.CustomSQL = "begin " + sb.ToString() + " end;";
Dao.ExecuteNonQuery(rInfo);
}
}
// foreach (var r in simulatedPrices)
// {
// sb.AppendFormat(string.Format(@"insert into rsk_core_fct_simulated_price(CORE_SIMULATED_PRICE_ID,RISK_CORE_FACTOR_ID,SIMULATE_DATE
// ,HORIZON_DATE,HORIZON_ID ,PERIOD_TYPE,PERIOD_TYPE_NAME,SIMULATION_METHOD
// ,SIMULATION_METHOD_NAME,SIMULATION_NUMBER,SIMULATION_ID,CURRENT_PRICE,
// SIMULATED_PRICE_SHOCK ,SIMULATED_FORWARD_PRICE,
// CREATE_UID,CREATE_UNAME,CREATE_OID,CREATE_PID,CREATE_DATE,LAST_MOD_UID,
// LAST_MOD_UNAME,LAST_MOD_OID,LAST_MOD_PID,LAST_MOD_DATETIME)"));
// sb.Append("
");
// string simulationDate = DateTime.Parse(r.SimulateDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
// simulationDate = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", simulateDate);
// string horizonDate = DateTime.Parse(r.HorizonDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
// horizonDate = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", horizonDate);
// string createDate = DateTime.Parse(r.CreateDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
// createDate = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", createDate);
// string lastModDateTime = DateTime.Parse(r.LastModDatetime == null ? DateTime.Now.ToString() : r.LastModDatetime.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
// lastModDateTime = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", lastModDateTime);
// sb.AppendFormat(
// string.Format(
// @" VALUES({0},{1},{2},{3},{4},'{5}','{6}','{7}','{8}',{9},{10},{11},{12},{13},{14},'{15}','{16}','{17}',{18},'{19}','{20}','{21}','{22}',{23})",
// "HIBERNATE_SEQUENCE.NEXTVAL", r.RiskCoreFactorId, simulationDate, horizonDate,
// r.HorizonId, r.PeriodType, r.PeriodTypeName, r.SimulationMethod, r.SimulationMethodName,
// r.SimulationNumber, r.SimulationId, r.CurrentPrice, r.SimulatedPriceShock, r.SimulatedForwardPrice,
// r.CreateUid == null ? "" : r.CreateUid, r.CreateUname == null ? "" : r.CreateUname, r.CreateOid == null ? "" : r.CreateOid,
// r.CreatePid == null ? "" : r.CreatePid, createDate, r.LastModUid == null ? "" : r.LastModUid, r.LastModUname == null ? "" : r.LastModUname,
// r.LastModOid == null ? "" : r.LastModOid, r.LastModPid == null ? "" : r.LastModPid, lastModDateTime));
// sb.Append("
");
// tagCount++;
// }
//Framework.QueryInfo rInfo = new QueryInfo();
//rInfo.CustomSQL = "begin
" + sb.ToString() + "end;
";
//Dao.ExecuteNonQuery(rInfo);
// Dao.SaveOrUpdateAll<RskCoreFctSimulatedPrice>(simulatedPrices);
}
private string InsertInfoRskCoreFctSimulatedPrice(RskCoreFctSimulatedPrice r)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat(string.Format(@"insert into rsk_core_fct_simulated_price(CORE_SIMULATED_PRICE_ID,RISK_CORE_FACTOR_ID,SIMULATE_DATE ,HORIZON_DATE,HORIZON_ID ,PERIOD_TYPE,PERIOD_TYPE_NAME,SIMULATION_METHOD,SIMULATION_METHOD_NAME,SIMULATION_NUMBER,SIMULATION_ID,CURRENT_PRICE,SIMULATED_PRICE_SHOCK ,SIMULATED_FORWARD_PRICE,CREATE_UID,CREATE_UNAME,CREATE_OID,CREATE_PID,CREATE_DATE,LAST_MOD_UID,LAST_MOD_UNAME,LAST_MOD_OID,LAST_MOD_PID,LAST_MOD_DATETIME)"));
//sb.Append("
");
string simulationDate = DateTime.Parse(r.SimulateDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
simulationDate = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", simulationDate);
string horizonDate = DateTime.Parse(r.HorizonDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
horizonDate = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", horizonDate);
string createDate = DateTime.Parse(r.CreateDate.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
createDate = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", createDate);
string lastModDateTime = DateTime.Parse(r.LastModDatetime == null ? DateTime.Now.ToString() : r.LastModDatetime.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
lastModDateTime = string.Format("to_date('{0}','yyyy-mm-dd hh24:mi:ss')", lastModDateTime);
sb.AppendFormat(
string.Format(
@" VALUES({0},{1},{2},{3},{4},'{5}','{6}','{7}','{8}',{9},{10},{11},{12},{13},{14},'{15}','{16}','{17}',{18},'{19}','{20}','{21}','{22}',{23});",
"HIBERNATE_SEQUENCE.NEXTVAL", r.RiskCoreFactorId, simulationDate, horizonDate,
r.HorizonId, r.PeriodType, r.PeriodTypeName, r.SimulationMethod, r.SimulationMethodName,
r.SimulationNumber, r.SimulationId, r.CurrentPrice, r.SimulatedPriceShock, r.SimulatedForwardPrice,
r.CreateUid == null ? "" : r.CreateUid, r.CreateUname == null ? "" : r.CreateUname, r.CreateOid == null ? "" : r.CreateOid,
r.CreatePid == null ? "" : r.CreatePid, createDate, r.LastModUid == null ? "" : r.LastModUid, r.LastModUname == null ? "" : r.LastModUname,
r.LastModOid == null ? "" : r.LastModOid, r.LastModPid == null ? "" : r.LastModPid, lastModDateTime));
//sb.Append("
");
return sb.ToString();
}
DateTime _simulateDate
{
get;
set;
}
int _simulateNumber
{
get;
set;
}
int _horizonNumber
{
get;
set;
}
double _timeStepLength
{
get;
set;
}
int _windowSize
{
get;
set;
}
int _periodTypeLookupID
{
get;
set;
}
int simulationMethodLookupID
{
get;
set;
}
int _simulationMethodLookupID
{
get;
set;
}
///// <summary>
///// 采用多线程处理
///// </summary>
//void SimulateForwardPriceForMultiPeriod_NoParameter()
//{
// try
// {
// // MessageBox.Show("12345");
// Framework.QueryInfo info = new Framework.QueryInfo();
// string userId = SessionId;
// //info.CustomSQL = "delete from T_AnsycData where userId=:UserId and DataType='SimulateForwardPrice'";
// //info.Parameters.Add("UserId", userId);
// //Dao.ExecuteNonQuery(info);
// LOG.Info("In ForwardPriceSimulationService.SimulateForwardPriceForMultiPeriod()");
// IRiskCoreFactorService riskCoreFactorService =
// (IRiskCoreFactorService)ServiceLocator.Get("riskCoreFactorService");
// IList<RiskCoreFactor> riskCoreFactors =
// riskCoreFactorService.GetValidListByIDWithSpringDao(Constants.VALID);
// int riskCoreFactorNumber = riskCoreFactors.Count;
// IRandomNumberService randNumberService =
// (IRandomNumberService)ServiceLocator.Get("randomNumberService");
// IIndexPriceMarketDataService indexPriceService =
// (IIndexPriceMarketDataService)ServiceLocator.Get("indexPriceMarketDataService");
// IRiskFactorVolatilityDataService riskVolatilityService =
// (IRiskFactorVolatilityDataService)ServiceLocator.Get("riskFactorVolatilityDataService");
// IRiskCorrelationDecompositionService decomposeSerive =
// (IRiskCorrelationDecompositionService)ServiceLocator.Get("riskCorrelationDecompositionService");
// IList<string> results = new List<string>();
// IGaussianDistributionService gaussianDistService =
// (IGaussianDistributionService)ServiceLocator.Get("gaussianDistributionService");
// //int periodTypeLookupID = (int)LookupItems.PERIOD_TYPE_DAILY;
// //int simulationMethodLookupID = (int)LookupItems.SIMULATION_METHOD_DIRECT_JUMP;
// double[,] randNumber = new double[_simulateNumber, riskCoreFactorNumber];
// randNumberService.GenerateQuasiRandomNumber(_simulateNumber, riskCoreFactorNumber, ref randNumber);
// double[,] factorLoadingMatrix = new double[riskCoreFactorNumber, riskCoreFactorNumber];
// double[] eigenValueVector = new double[riskCoreFactorNumber];
// //edit daidz
// decomposeSerive.DecomposeCorrelationMatrixByRiskCoreFactor_New(WcfUserName1, _simulateDate, riskCoreFactorNumber,
// riskCoreFactors, ref factorLoadingMatrix, ref eigenValueVector);
// int indexPriceFactorID = new int();
// //int riskFactorID = new int();
// int simulateID = new int();
// int[] riskCoreFactorID = new int[riskCoreFactorNumber];
// double[] currentPrice = new double[riskCoreFactorNumber];
// double[] priceShock = new double[riskCoreFactorNumber];
// double[,] simulatedPrice = new double[_simulateNumber, riskCoreFactorNumber];
// //double[] forwardPrice = new double[riskCoreFactorNumber];
// //double[] logForwardPrice = new double[riskCoreFactorNumber];
// double[] zeroRate = new double[riskCoreFactorNumber];
// double[] riskVolatility = new double[riskCoreFactorNumber];
// //double forwardPrice = 0;
// double startForwardPrice = 0;
// //double logForwardPrice = 0;
// double sumOfRandNumber = 0;
// double dayOfOneYear = Constants.DAYS_IN_ONE_YEAR;
// double tempZeroRate = 0.00;
// int i = 0;
// var allList = indexPriceService.GetIndexPriceMarketDataByDate(_simulateDate);
// var allFatorList = riskVolatilityService.GetListByDateSize(_simulateDate, _windowSize);
// System.Collections.ArrayList sidList = new System.Collections.ArrayList();
// for (var k = 0; k < riskCoreFactors.Count; k++)
// {
// var riskCoreFactor = riskCoreFactors[k];
// indexPriceFactorID = riskCoreFactor.RiskCoreFactorID;
// riskCoreFactorID[i] = riskCoreFactor.RiskCoreFactorID;
// currentPrice[i] =
// (double)indexPriceService.GetIndexPriceValueByDateWithSpringDao(allList, indexPriceFactorID);
// zeroRate[i] = tempZeroRate; // need to get from database
// // riskVolatility[i] = (double)riskVolatilityService.GetValueByDateFactorIDSize(simulateDate, riskCoreFactorID[i], windowSize);
// riskVolatility[i] =
// (double)riskVolatilityService.GetValueByDateFactorSizeByList(riskCoreFactorID[i], allFatorList);
// i++;
// }
// IRiskCoreFactorSimulatedPriceService simulatedPriceService =
// (IRiskCoreFactorSimulatedPriceService)ServiceLocator.Get("riskCoreFactorSimulatedPriceService");
// IList<RiskCoreFactorSimulatedPrice> simulatedPricesToDelete = new List<RiskCoreFactorSimulatedPrice>();
// i = 0;
// int horizonID = 0;
// IList<RiskCoreFactorSimulatedPrice> simulatedPrices = new List<RiskCoreFactorSimulatedPrice>();
// for (i = 0; i < _simulateNumber; i++)
// {
// simulateID = i;
// // IList<RiskCoreFactorSimulatedPrice> simulatedPrices = new List<RiskCoreFactorSimulatedPrice>();
// for (int j = 0; j < riskCoreFactorNumber; j++)
// {
// for (int l = 0; l < _horizonNumber; l++)
// {
// horizonID = l;
// if (horizonID == 0)
// {
// startForwardPrice = currentPrice[j];
// //logForwardPrice = Math.Log(startForwardPrice);
// }
// else
// {
// startForwardPrice = simulatedPrice[i, j];
// //logForwardPrice = Math.Log(startForwardPrice);
// }
// sumOfRandNumber = 0;
// for (int k = 0; k < riskCoreFactorNumber; k++) // can be truncated by number of principal components
// {
// sumOfRandNumber += randNumberService.GenerateInverseRandomNumber(randNumber[i, k]) * factorLoadingMatrix[j, k];
// }
// priceShock[j] = (zeroRate[j] / dayOfOneYear - 0.5 * Math.Pow(riskVolatility[j], 2)) * _timeStepLength + riskVolatility[j] * sumOfRandNumber;
// simulatedPrice[i, j] = startForwardPrice * Math.Exp(priceShock[j]);
// RiskCoreFactorSimulatedPrice riskFactorsimulatedPrice = new RiskCoreFactorSimulatedPrice();
// riskFactorsimulatedPrice.RiskCoreFactorID = riskCoreFactorID[j];
// riskFactorsimulatedPrice.SimulateDate = _simulateDate;
// riskFactorsimulatedPrice.HorizonDate = _simulateDate.AddDays(horizonID);
// riskFactorsimulatedPrice.HorizonID = horizonID;
// riskFactorsimulatedPrice.PeriodTypeLookupID = _periodTypeLookupID;
// riskFactorsimulatedPrice.SimulationMethodLookupID = simulationMethodLookupID;
// riskFactorsimulatedPrice.SimulationNumber = _simulateNumber;
// riskFactorsimulatedPrice.SimulationID = simulateID;
// riskFactorsimulatedPrice.CurrentPrice = (decimal)startForwardPrice;
// riskFactorsimulatedPrice.SimulatedPriceShock = (decimal)priceShock[j];
// riskFactorsimulatedPrice.SimulatedForwardPrice = (decimal)simulatedPrice[i, j];
// //riskFactorsimulatedPrice.CreatedBy = WcfService.Wcf.loginUser.WcfUser.UserName;
// riskFactorsimulatedPrice.CreatedBy = WcfUserName1;
// riskFactorsimulatedPrice.CreateDate = DateTime.Now;
// simulatedPrices.Add(riskFactorsimulatedPrice);
// }
// }
// //sb.Append(string.Format("delete from RiskCoreFactorSimulatedPrices where SimulateDate='{0}'", simulateDate.ToString("yyyy-MM-dd")));
// //sb.Append(string.Format(" and SimulationID={0}", simulateID));
// //sb.Append(string.Format(" and SimulationNumber={0}
", simulateNumber));
// // simulatedPricesToDelete = simulatedPriceService.GetListByDateSimID(simulateDate, simulateID, simulateNumber);
// // simulatedPriceService.Save(simulatedPricesToDelete, new List<RiskCoreFactorSimulatedPrice>(), new List<RiskCoreFactorSimulatedPrice>());
// // simulatedPriceService.Save(new List<RiskCoreFactorSimulatedPrice>(), new List<RiskCoreFactorSimulatedPrice>(), simulatedPrices);
// }
// info = new Framework.QueryInfo();
// info.CustomSQL = string.Format("delete from RiskCoreFactorSimulatedPrices where SimulateDate='{0}' and SimulationID between 0 and {1}"
// , _simulateDate.ToString("yyyy-MM-dd"), _simulateNumber);
// info.CustomSQL += "
insert into T_AnsycData(UserId,DataType,Result)values(:UserId,'SimulateForwardPrice','1')";
// info.Parameters.Add("UserId", userId);
// Dao.ExecuteNonQuery(info);
// simulatedPriceService.Save(new List<RiskCoreFactorSimulatedPrice>(), new List<RiskCoreFactorSimulatedPrice>(), simulatedPrices);
// LOG.Info("Finishing simulatoin for all index curves for multiple horizon dates");
// }
// catch (Exception ex)
// {
// Framework.QueryInfo info = new Framework.QueryInfo();
// info.CustomSQL += "
insert into T_AnsycData(UserId,DataType,Result)values(:UserId,'SimulateForwardPrice',:Result)";
// info.Parameters.Add("UserId", SessionId);
// info.Parameters.Add("Result", ex.Message);
// Dao.ExecuteNonQuery(info);
// }
//}
/// <summary>
/// edit daidz
/// </summary>
/// <param name="simulateDate"></param>
/// <param name="simulateNumber"></param>
/// <param name="horizonNumber"></param>
/// <param name="timeStepLength"></param>
/// <param name="windowSize"></param>
/// <param name="periodTypeLookupID"></param>
/// <param name="simulationMethodLookupID"></param>
//public void SimulateForwardPriceForMultiPeriod_New(int CurrentUserID, DateTime simulateDate, int simulateNumber, int horizonNumber, double timeStepLength, int windowSize, int periodTypeLookupID, int simulationMethodLookupID)
//{
// lock (l)
// {
// _simulateDate = simulateDate;
// SessionId = WcfService.Wcf.loginUser.WcfUser.UserId;
// _simulateNumber = simulateNumber;
// _horizonNumber = horizonNumber;
// _timeStepLength = timeStepLength;
// _windowSize = windowSize;
// _periodTypeLookupID = periodTypeLookupID;
// _simulationMethodLookupID = simulationMethodLookupID;
// Framework.QueryInfo info = new Framework.QueryInfo();
// string userId = SessionId;
// info.CustomSQL = "delete from T_AnsycData where userId=:UserId and DataType='SimulateForwardPrice'";
// info.Parameters.Add("UserId", userId);
// Dao.ExecuteNonQuery(info);
// WcfUserName1 = WcfService.Wcf.loginUser.WcfUser.UserName;
// threadStart = new ThreadStart(this.SimulateForwardPriceForMultiPeriod_NoParameter);
// thread = new Thread(threadStart);
// thread.IsBackground = true;
// thread.Start();
// }
//}
//public static string WcfUserName1 = string.Empty;
//private void threadHand1_Run(string userName)
//{
// this.SimulateForwardPriceForMultiPeriod_NoParameter();
//}
//void worker_DoWork(object sender, DoWorkEventArgs e)
//{
//}
public void GetSimulatedForwardPriceForSingleHorizon(int horizonID, DateTime computeDate, int simulationNumber, int periodTypeLookupID, int simulationMethodLookupID, ref int[] simulatedRiskCoreFactorID, ref double[,] simulatedForwardPrice)
{
// IRiskCoreFactorService riskCoreFactorService = (IRiskCoreFactorService)ServiceLocator.Get("riskCoreFactorService");
//IList<RiskCoreFactor> riskCoreFactors = riskCoreFactorService.GetAll();
IList<RskCoreFactor> riskCoreFactors = riskCoreFactorService.GetValidListByIDWithSpringDao(Constants.VALID);
// IRiskCoreFactorSimulatedPriceService simulatedPriceService = (IRiskCoreFactorSimulatedPriceService)ServiceLocator.Get("riskCoreFactorSimulatedPriceService");
//IList<RiskCoreFactorSimulatedPrice> simulatedPrices = new List<RiskCoreFactorSimulatedPrice>();
//IList<RiskCoreFactorIDVO> riskCoreFactors = simulatedPriceService.GetRiskCoreFactorListByDateNumberIDWithSpringDao(computeDate, horizonID, periodTypeLookupID, simulationMethodLookupID, simulationNumber);
Framework.QueryInfo info = new Framework.QueryInfo();
info.QueryObject = "RskCoreFctSimulatedPrice";
info.AddParam("SimulateDate", computeDate);
info.AddParam("HorizonId", horizonID);
info.AddParam("PeriodType", periodTypeLookupID.ToString());
info.AddParam("SimulationMethod", simulationMethodLookupID);
info.AddParam("SimulationNumber", simulationNumber);
IList<RskCoreFctSimulatedPrice> simulatedPrices = Holworth.Utility.Utility.ListToT<RskCoreFctSimulatedPrice>(Dao.FindList(info));
// IList<RskCoreFctSimulatedPrice> simulatedPrices = simulatedPriceService.GetListByDateNumberIDWithSpringDao(computeDate, horizonID, periodTypeLookupID, simulationMethodLookupID, simulationNumber);
//int riskCoreFactorNumber = riskCoreFactors.Count;
int k = 0;
//foreach (RiskCoreFactorIDVO riskCoreFactor in riskCoreFactors)
foreach (RskCoreFactor riskCoreFactor in riskCoreFactors)
{
simulatedRiskCoreFactorID[k] = int.Parse(riskCoreFactor.Id);
k++;
}
double[] proxySimulatedPrice = new double[simulatedPrices.Count];
k = 0;
foreach (RskCoreFctSimulatedPrice simulatedPrice in simulatedPrices)
{
proxySimulatedPrice[k] = (double)simulatedPrice.SimulatedForwardPrice;
k++;
}
k = 0;
for (int i = 0; i < simulationNumber; i++)
{
for (int j = 0; j < riskCoreFactors.Count; j++)
{
simulatedForwardPrice[i, j] = proxySimulatedPrice[k];
k++;
}
}
}
public int GetRiskCoreFactorRowNumber(int riskFactorID, int simulatedRiskCoreFactorNumber, int[] simulatedRiskCoreFactorID)
{
int riskCoreFactorRowNumber = 0;
for (int i = 0; i < simulatedRiskCoreFactorNumber; i++)
{
if (simulatedRiskCoreFactorID[i] == riskFactorID)
{
riskCoreFactorRowNumber = i;
break;
}
}
return riskCoreFactorRowNumber;
}
IForwardPriceSimulationService _priceSimulationService = null;
IForwardPriceSimulationService priceSimulationService
{
get
{
if (_priceSimulationService == null)
{
_priceSimulationService = ctx["ForwardPriceSimulationService"] as IForwardPriceSimulationService;
}
return _priceSimulationService;
}
}
public void SimulateForwardPrice(DateTime computeDate, int horizonNumber, int simulationNumber)
{
double timeStepLength = (double)Constants.BASE_TIME_STEP_LENGTH;
int windowSize = (int)Constants.BASE_WINDOW_SIZE;
int periodTypeLookupID = (int)LookupItems.PERIOD_TYPE_DAILY;
int simulationMethodLookupID = (int)LookupItems.SIMULATION_METHOD_DIRECT_JUMP;
priceSimulationService.SimulateForwardPriceForMultiPeriod(computeDate, simulationNumber, horizonNumber, timeStepLength, windowSize, periodTypeLookupID, simulationMethodLookupID);
}
}
}