/// <summary> /// public constructs, Fra Rate class acts as a /// container which holds all the necessary information /// </summary> /// <param name="logger">logger</param> /// <param name="cache">cache</param> /// <param name="nameSpace">The clients namespace</param> /// <param name="fixingCalendar">fixingCalendar</param> /// <param name="rollCalendar">rollCalendar</param> /// <param name="properties">curve properties</param> /// <param name="instruments">list of instruments</param> /// <param name="values">value of each instrument</param> /// <param name="initialFraRates">initial guesses for fra rate</param> /// <param name="shockedInsturmentIndices">array of shocked instrument indices</param> /// <param name="initialRates"></param> public FraSolver(ILogger logger, ICoreCache cache, string nameSpace, IBusinessCalendar fixingCalendar, IBusinessCalendar rollCalendar, NamedValueSet properties, List <string> instruments, IEnumerable <decimal> values, IEnumerable <decimal> initialFraRates, ICollection <int> shockedInsturmentIndices, List <decimal> initialRates) { //SetInstruments(GetInstrumentIds(instruments)); Instruments = instruments; Properties = properties.Clone(); SetRates(values); FixingCalendar = fixingCalendar; RollCalendar = rollCalendar; //initially set the rates value to the initial curve rates SetRateAdjustment(shockedInsturmentIndices.Count); SetFraGuesses(initialFraRates); Currency = Properties.GetString(CurveProp.Currency1, true); SetDayCounter(cache, nameSpace, Currency); SetShockedInstrumentIndices(shockedInsturmentIndices); SetPropertiesOfShockedCurves(properties, shockedInsturmentIndices.Count); SetInitialCurve(logger, cache, nameSpace, fixingCalendar, rollCalendar, Properties, Instruments.ToArray(), Rates.ToArray()); SetFraDates(cache, nameSpace, _initialCurve, ShockedInsturmentsIndices); SetInitalFraRates(_initialCurve, FraStartDates, FraEndDates); InitialRates = initialRates; }
/// <summary> /// Generates a perturbed curve for those items specified. /// If the instruments are not valid they are excluded. /// </summary> /// <param name="perturbationArray">The perturbation Array: instrumentId and value.</param> /// <returns></returns> public IPricingStructure PerturbCurve(List <Pair <string, decimal> > perturbationArray) { if (PriceableOptionAssets == null) { return(null); } //Set the parameters and properties. NamedValueSet properties = GetPricingStructureId().Properties.Clone(); string uniqueId = GetPricingStructureId().UniqueIdentifier; //Clone the properties. NamedValueSet curveProperties = properties.Clone(); curveProperties.Set("PerturbedCurve", true); curveProperties.Set("BaseCurve", uniqueId); curveProperties.Set(CurveProp.UniqueIdentifier, uniqueId + "." + "PerturbedCurve"); foreach (var instrument in perturbationArray) { //TODO clone the priceable assets. var asset = PriceableOptionAssets.FindAll(a => a.Id.Equals(instrument.First)); if (asset[0] == null) { continue; } var temp = asset[0]; temp.MarketQuote.value = temp.MarketQuote.value + instrument.Second / 10000; } //Create the new curve. var capVolatilityCurve = new CapVolatilityCurve(curveProperties, PriceableOptionAssets, DiscountCurve, ForecastCurve, Holder); return(capVolatilityCurve); }
//public static NamedValueSet CombinePropertySets(NamedValueSet baseProperties, NamedValueSet additionalProperties) //{ // var properties = additionalProperties.ToDictionary(); // foreach (var nvs in properties.Keys) // { // var value = properties[nvs]; // baseProperties.Set(nvs, value); // } // return baseProperties; //} public static NamedValueSet CombinePropertySetsClone(NamedValueSet baseProperties, NamedValueSet additionalProperties) { var properties = additionalProperties.ToDictionary(); var cloneProperties = baseProperties.Clone(); foreach (var nvs in properties.Keys) { var value = properties[nvs]; cloneProperties.Set(nvs, value); } return(cloneProperties); }
/// <summary> /// Create a Sorted list of bootstrap engines from the data matrix /// </summary> /// <param name="logger">The logger.</param> /// <param name="cache">The cache.</param> /// <param name="nameSpace">The client namespace</param> /// <param name="properties">The properties, including the engine handle.</param> /// <param name="forecastCurve">The forecast rate curve.</param> /// <param name="discountCurve">The discount rate curve.</param> /// <param name="instruments">An array of instrument types.</param> /// <param name="rawVolatilityGrid">The raw grid used to build the engines. Assume that all volatility and strike values are 100x true</param> /// <returns></returns> private SortedList <decimal, CapVolatilityCurve> CreateCurves(ILogger logger, ICoreCache cache, string nameSpace, NamedValueSet properties, IRateCurve discountCurve, IRateCurve forecastCurve, String[] instruments, Decimal[] rawVolatilityGrid) { var clonedProperties = properties.Clone(); // Create engine var quotedAssetSet = AssetHelper.Parse(instruments, rawVolatilityGrid, null); var engines = new SortedList <decimal, CapVolatilityCurve>(); // Create a new ATM CapletBootstrap engine. The default decimal should be 0 var engine = new CapVolatilityCurve(logger, cache, nameSpace, clonedProperties, quotedAssetSet, discountCurve, forecastCurve, null, null); // Add engine engines.Add(0, engine); return(engines); }
/// <summary> /// Create a Sortedlist of volatility curves from the data matrix /// </summary> /// <param name="logger">The logger.</param> /// <param name="cache">The cache.</param> /// <param name="nameSpace">The client namespace</param> /// <param name="properties">The properties, including the engine handle.</param> /// <param name="instruments">An array of instrument types.</param> /// <param name="rawVolatilityGrid">The raw grid used to build the engines. Assume that all volatility and strike values are 100x true</param> /// <returns></returns> private SortedList <decimal, VolatilityCurve> VolatilityCurveCreate(ILogger logger, ICoreCache cache, string nameSpace, NamedValueSet properties, String[] instruments, Decimal[] rawVolatilityGrid) { var clonedProperties = properties.Clone(); // Create engine var quotedAssetSet = AssetHelper.Parse(instruments, rawVolatilityGrid, null); var engines = new SortedList <decimal, VolatilityCurve>(); // Create a new ATM CapletBootstrap engine. The default decimal should be 0 var volatilityCurve = PricingStructureFactory.CreateCurve(logger, cache, nameSpace, null, null, clonedProperties, quotedAssetSet); // Add engine if (volatilityCurve is VolatilityCurve vCurve) { engines.Add(0, vCurve); } return(engines); }
/// <summary> /// PreCondition, check the initialCurvePorperties /// </summary> /// <param name="initialCurvePorperties"></param> /// <param name="numOfShockedInsturments"></param> private void SetPropertiesOfShockedCurves(NamedValueSet initialCurvePorperties, int numOfShockedInsturments) { ShockedCurvesProperties = new List <NamedValueSet>(); for (int i = 0; i < numOfShockedInsturments; ++i) { NamedValueSet newProperties = initialCurvePorperties.Clone(); string curveName = initialCurvePorperties.GetString(CurveProp.CurveName, false); if (!string.IsNullOrEmpty(curveName)) { newProperties.Set(CurveProp.CurveName, curveName + "ShockedCurve-" + i); } string id = initialCurvePorperties.GetString("Identifier", false); if (!string.IsNullOrEmpty(id)) { newProperties.Set(CurveProp.CurveName, id + "ShockedCurve-" + i); } ShockedCurvesProperties.Add(newProperties); } }
/// <summary> /// Creates the basic rate curve risk set, using the current curve as the base curve. /// This function takes a curves, creates a rate curve for each instrument and applying /// supplied basis point pertubation/spread to the underlying instrument in the spread curve /// </summary> /// <param name="basisPointPerturbation">The basis point perturbation.</param> /// <returns>A list of pertubed rate curves</returns> public override List <IPricingStructure> CreateCurveRiskSet(decimal basisPointPerturbation) { if (PriceableOptionAssets == null) { return(null); } //Set the parameters and properties. decimal perturbation = basisPointPerturbation / 10000.0m; NamedValueSet properties = GetPricingStructureId().Properties.Clone(); properties.Set("PerturbedAmount", basisPointPerturbation); string uniqueId = GetPricingStructureId().UniqueIdentifier; //Get the assets int index = 0; var structures = new List <IPricingStructure>(); //Get the original quotes var quotes = GetMarketQuotes(PriceableOptionAssets); //Copy the assets. var priceableRateOptionAssets = new IPriceableOptionAssetController[PriceableOptionAssets.Count]; PriceableOptionAssets.CopyTo(priceableRateOptionAssets); foreach (var instrument in priceableRateOptionAssets) { var perturbations = new decimal[quotes.Count]; quotes.CopyTo(perturbations); //Clone the properties. NamedValueSet curveProperties = properties.Clone(); perturbations[index] = quotes[index] + perturbation; curveProperties.Set("PerturbedAsset", instrument.Id); curveProperties.Set("BaseCurve", uniqueId); curveProperties.Set(CurveProp.UniqueIdentifier, uniqueId + "." + instrument.Id); curveProperties.Set(CurveProp.Tolerance, Tolerance); //Perturb the quotes PerturbedPriceableAssets(priceableRateOptionAssets.ToList(), perturbations); IPricingStructure rateCurve = new CapVolatilityCurve(curveProperties, priceableRateOptionAssets.ToList(), DiscountCurve, ForecastCurve, Holder); structures.Add(rateCurve); //Set the counter. perturbations[index] = 0; index++; } return(structures); }
/// <summary> /// Creates the basic rate curve risk set. /// This function takes a curves, creates a rate curve for each instrument and applying /// supplied basis point pertubation/spread to the underlying instrument in the spread curve /// </summary> /// <param name="basisPointPerturbation">The basis point perturbation.</param> /// <returns>A list of pertubed rate curves</returns> public override List <IPricingStructure> CreateCurveRiskSet(decimal basisPointPerturbation)//TODO make proportional perturbations! { if (PriceableEquityAssets == null) { return(null); } //Set the parameters and properties. decimal perturbation = basisPointPerturbation / 10000.0m; NamedValueSet properties = GetPricingStructureId().Properties.Clone(); properties.Set("PerturbedAmount", basisPointPerturbation); string uniqueId = GetPricingStructureId().UniqueIdentifier; //Get the assets, BUT REMOVE ALL THE BASIS SWAPS, to prevent double accounting of risks. int index = 0; var structures = new List <IPricingStructure>(); //Get the original quotes var quotes = GetMarketQuotes(PriceableEquityAssets); foreach (var instrument in PriceableEquityAssets) { var perturbations = new decimal[quotes.Count]; quotes.CopyTo(perturbations); //Clone the properties. NamedValueSet curveProperties = properties.Clone(); perturbations[index] = quotes[index] + perturbation; curveProperties.Set("PerturbedAsset", instrument.Id); curveProperties.Set("BaseCurve", uniqueId); curveProperties.Set(CurveProp.UniqueIdentifier, uniqueId + "." + instrument.Id); curveProperties.Set(CurveProp.Tolerance, Tolerance); //Create the new curve. //Perturb the quotes PerturbedPriceableAssets(PriceableEquityAssets, perturbations); IPricingStructure fxCurve = new EquityCurve(curveProperties, PriceableEquityAssets, Holder); structures.Add(fxCurve); //Set the counter. perturbations[index] = 0; index++; } return(structures); }
/// <summary> /// Create a Sortedlist of bootstrap engines from the data matrix /// </summary> /// <param name="logger">The logger.</param> /// <param name="cache">The cache.</param> /// <param name="nameSpace">The client namespace</param> /// <param name="properties">The properties, including the engine handle.</param> /// <param name="instruments">An array of instrument types.</param> /// <param name="strikes">The strike array.</param> /// <param name="rawVolatilityGrid">The raw grid used to build the engines. Assume that all volatility and strike values are 100x true</param> /// <returns></returns> private SortedList <decimal, VolatilityCurve> VolatilityCurvesCreate(ILogger logger, ICoreCache cache, string nameSpace, NamedValueSet properties, String[] instruments, Decimal[] strikes, Decimal[,] rawVolatilityGrid) { var clonedProperties = properties.Clone(); // Check there are valid strikes if (strikes != null && rawVolatilityGrid != null) { //The matrix is zero based, but the upper bound returns the last column index. var volColumns = rawVolatilityGrid.GetUpperBound(1) + 1; var columns = Math.Min(strikes.Length, volColumns); var engines = new SortedList <decimal, VolatilityCurve>(); // Loop through the strikes to create each new engine for (var i = 0; i < columns; i++) { decimal[] adjustedRates = GenerateVolatilityMatrix(instruments, i, rawVolatilityGrid); clonedProperties.Set("Strike", strikes[i]); clonedProperties.Set("StrikeQuoteUnits", StrikeQuoteUnitsEnum.DecimalRate.ToString()); // Create engine var quotedAssetSet = AssetHelper.Parse(instruments, adjustedRates, null); var engine = PricingStructureFactory.CreateCurve(logger, cache, nameSpace, null, null, clonedProperties, quotedAssetSet) as VolatilityCurve; // Add engine engines.Add(strikes[i], engine); } return(engines); } { var engines = new SortedList <decimal, VolatilityCurve>(); // Create a new ATM CapletBootstrap engine. The default decimal should be 0 var engine = PricingStructureFactory.CreateCurve(logger, cache, nameSpace, null, null, clonedProperties, instruments, null, null) as VolatilityCurve; // Add engine engines.Add(0, engine); return(engines); } }
/// <summary> /// Creates the basic rate curve risk set, using the current curve as the base curve. /// This function takes a curves, creates a rate curve for each instrument and applying /// supplied basis point perturbation/spread to the underlying instrument in the spread curve /// </summary> /// <param name="basisPointPerturbation">The basis point perturbation.</param> /// <param name="pricingStructureRiskSetType">This determine which assets to perturb. </param> /// <returns>A list of perturbed rate curves</returns> private List <IPricingStructure> CreateCurveRiskSet(decimal basisPointPerturbation, PricingStructureRiskSetType pricingStructureRiskSetType) { var structures = new List <IPricingStructure>(); if (PriceableRateSpreadAssets == null) { return(structures); } //Add the spread asset perturbed curves. // //Set the parameters and properties. decimal perturbation = basisPointPerturbation / 10000.0m; NamedValueSet properties = GetPricingStructureId().Properties.Clone(); properties.Set("PerturbedAmount", basisPointPerturbation); string uniqueId = GetPricingStructureId().UniqueIdentifier; int index = 0; //Perturb the base curve quote set if (BaseCurve.PriceableRateAssets != null && pricingStructureRiskSetType != PricingStructureRiskSetType.Child) { var basequotes = GetMarketQuotes(BaseCurve.PriceableRateAssets); foreach (var instrument in BaseCurve.PriceableRateAssets) { var perturbations = new decimal[basequotes.Count]; basequotes.CopyTo(perturbations); //Clone the properties. NamedValueSet curveProperties = properties.Clone(); perturbations[index] = basequotes[index] + perturbation; curveProperties.Set("PerturbedAsset", instrument.Id); curveProperties.Set("BaseCurve", uniqueId); curveProperties.Set(CurveProp.UniqueIdentifier, uniqueId + "." + instrument.Id); curveProperties.Set(CurveProp.Tolerance, Tolerance); //Perturb the quotes PerturbedPriceableAssets(BaseCurve.PriceableRateAssets, perturbations); //Create the new curve. var baseCurve = new RateCurve(curveProperties, BaseCurve.PriceableRateAssets, Holder); IPricingStructure rateCurve = new RateBasisCurve(curveProperties, baseCurve, PriceableRateSpreadAssets, Holder); structures.Add(rateCurve); //Set the counter. perturbations[index] = 0; index++; } } //Perturb the spread curve quotes if (pricingStructureRiskSetType != PricingStructureRiskSetType.Parent) { var spreadquotes = GetMarketQuotes(PriceableRateSpreadAssets); index = 0; foreach (var instrument in PriceableRateSpreadAssets) { var perturbations = new decimal[spreadquotes.Count]; spreadquotes.CopyTo(perturbations); //Clone the properties. NamedValueSet curveProperties = properties.Clone(); perturbations[index] = spreadquotes[index] + perturbation; curveProperties.Set("PerturbedAsset", instrument.Id); curveProperties.Set("BaseCurve", uniqueId); curveProperties.Set(CurveProp.UniqueIdentifier, uniqueId + "." + instrument.Id); curveProperties.Set(CurveProp.Tolerance, Tolerance); //Perturb the quotes PerturbedPriceableAssets(new List <IPriceableRateAssetController>(PriceableRateSpreadAssets), perturbations); IPricingStructure rateCurve = new RateBasisCurve(curveProperties, BaseCurve, PriceableRateSpreadAssets, Holder); structures.Add(rateCurve); //Set the counter. perturbations[index] = 0; index++; } } return(structures); }