Esempio n. 1
0
        public override AssetValuation Calculate(IInstrumentControllerData modelData)
        {
            ModelData = modelData;
            AnalyticModelParameters = null;
            CalculationResults      = null;
            UpdateBucketingInterval(ModelData.ValuationDate, PeriodHelper.Parse(CDefaultBucketingInterval));
            // 1. First derive the analytics to be evaluated via the stream controller model
            // NOTE: These take precendence of the child model metrics
            if (AnalyticsModel == null)
            {
                AnalyticsModel = new FxLegAnalytic();
            }
            AssetValuation streamValuation;
            //Check if risk calc are required.
            bool delta0PDH = AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.LocalCurrencyDelta0PDH.ToString()) != null ||
                             AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.Delta0PDH.ToString()) != null;
            bool delta1PDH = AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.LocalCurrencyDelta1PDH.ToString()) != null ||
                             AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.Delta1PDH.ToString()) != null;
            var streamControllerMetrics = ResolveModelMetrics(AnalyticsModel.Metrics);
            var childValuations         = new List <AssetValuation>();

            foreach (var payment in Payments)
            {
                payment.PricingStructureEvolutionType = PricingStructureEvolutionType;
                payment.BucketedDates = BucketedDates;
                payment.Multiplier    = Multiplier;
            }

            if (modelData.MarketEnvironment is IFxLegEnvironment marketEnvironment)//TODO need to look at PricingStructureEvolutionType.
            {
                //Modify the second market.
                var curveName1 = marketEnvironment.GetExchangeCurrencyPaymentEnvironment1();
                var modelData1 = new InstrumentControllerData(modelData.AssetValuation, curveName1,
                                                              modelData.ValuationDate, modelData.ReportingCurrency);
                var curveName2 = marketEnvironment.GetExchangeCurrencyPaymentEnvironment2();
                var modelData2 = new InstrumentControllerData(modelData.AssetValuation, curveName2,
                                                              modelData.ValuationDate, modelData.ReportingCurrency);
                childValuations.Add(Currency1Payment.Calculate(modelData1));
                childValuations.Add(Currency2Payment.Calculate(modelData2));
            }
            else if (modelData.MarketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                var market = (MarketEnvironment)modelData.MarketEnvironment;
                if (delta0PDH)
                {
                    //Force building of the risk curves.
                    //market.SearchForPerturbedPricingStructures(FxIndexCurveName, "delta0PDH");//TODO Need to add this perturbation to fxCurve.
                }
                if (delta1PDH)
                {
                    //Force building of the risk curves.
                    market.SearchForPerturbedPricingStructures(Currency1DiscountCurveName, "delta1PDH");
                    market.SearchForPerturbedPricingStructures(Currency2DiscountCurveName, "delta1PDH");
                }
                //This defines the cross cureves from the payment currency to the reporting
                //currency for each leg of the fx trade.
                //
                if (modelData.ReportingCurrency.Value != Currency1.Value)
                {
                    ReportingCurrencyFxCurveName1 =
                        MarketEnvironmentHelper.ResolveFxCurveNames(Currency1.Value,
                                                                    modelData.ReportingCurrency.Value);
                }
                if (modelData.ReportingCurrency.Value != Currency2.Value)
                {
                    ReportingCurrencyFxCurveName2 =
                        MarketEnvironmentHelper.ResolveFxCurveNames(Currency2.Value,
                                                                    modelData.ReportingCurrency.Value);
                }
                childValuations.Add(Currency1Payment.Calculate(modelData));
                childValuations.Add(Currency2Payment.Calculate(modelData));
            }
            var paymentValuation          = AssetValuationHelper.AggregateMetrics(childValuations, new List <string>(Metrics), PaymentCurrencies);
            var childControllerValuations = new List <AssetValuation> {
                paymentValuation
            };

            // Child metrics have now been calculated so we can now evaluate the stream model metrics
            if (streamControllerMetrics.Count > 0)
            {
                var rate = ExchangeRate.rate;
                if (modelData.MarketEnvironment is MarketEnvironment market)
                {
                    var fxCurve = (IFxCurve)market.SearchForPricingStructureType(FxIndexCurveName);
                    if (HybridValuation)
                    {
                        var curve1 = (IRateCurve)market.SearchForPricingStructureType(Currency1DiscountCurveName);
                        var curve2 = (IRateCurve)market.SearchForPricingStructureType(Currency2DiscountCurveName);
                        var flag   = ExchangeRate.quotedCurrencyPair.quoteBasis == QuoteBasisEnum.Currency2PerCurrency1;
                        AnalyticsModel = new FxLegAnalytic(modelData.ValuationDate, RiskMaturityDate, fxCurve, curve1,
                                                           curve2, flag);
                    }
                    else
                    {
                        AnalyticsModel = new FxLegAnalytic(modelData.ValuationDate, RiskMaturityDate, fxCurve);
                    }
                }
                IFxLegParameters analyticModelParameters = new FxLegParameters
                {
                    Multiplier  = Multiplier,
                    MarketQuote = rate,
                };
                CalculationResults =
                    AnalyticsModel.Calculate <IFxLegInstrumentResults, FxLegInstrumentResults>(
                        analyticModelParameters, streamControllerMetrics.ToArray());
                // Now merge back into the overall stream valuation
                var streamControllerValuation = GetValue(CalculationResults, modelData.ValuationDate);
                streamValuation = AssetValuationHelper.UpdateValuation(streamControllerValuation,
                                                                       childControllerValuations,
                                                                       ConvertMetrics(streamControllerMetrics),
                                                                       new List <string>(Metrics), PaymentCurrencies);
            }
            else
            {
                streamValuation = paymentValuation;
            }
            CalculationPerfomedIndicator = true;
            streamValuation.id           = Id;
            return(streamValuation);
        }
Esempio n. 2
0
        public override AssetValuation Calculate(IInstrumentControllerData modelData)
        {
            ModelData = modelData;
            AnalyticModelParameters = null;
            CalculationResults      = null;
            UpdateBucketingInterval(ModelData.ValuationDate, PeriodHelper.Parse(CDefaultBucketingInterval));
            // 1. First derive the analytics to be evaluated via the stream controller model
            // NOTE: These take precendence of the child model metrics
            if (AnalyticsModel == null)
            {
                AnalyticsModel = new FxOptionLegAnalytic();
            }
            var            marketEnvironment = modelData.MarketEnvironment as IFxLegEnvironment;
            AssetValuation streamValuation;
            //Check if risk calc are required.
            bool delta0PDH = AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.LocalCurrencyDelta0PDH.ToString()) != null ||
                             AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.Delta0PDH.ToString()) != null;
            bool delta1PDH = AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.LocalCurrencyDelta1PDH.ToString()) != null ||
                             AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.Delta1PDH.ToString()) != null;
            var streamControllerMetrics = ResolveModelMetrics(AnalyticsModel.Metrics);
            var childValuations         = new List <AssetValuation>();

            // 2. Now evaluate only the child specific metrics (if any)
            foreach (var payment in FxLeg.Payments)
            {
                payment.Multiplier = Multiplier;
                payment.PricingStructureEvolutionType = PricingStructureEvolutionType;
                payment.BucketedDates = BucketedDates;
            }
            foreach (var premium in Premia)
            {
                premium.Multiplier = 1.0m;
                premium.PricingStructureEvolutionType = PricingStructureEvolutionType;
                premium.BucketedDates = BucketedDates;
            }
            if (marketEnvironment != null)
            {
                //Modify the second market.
                var modelData1 = new InstrumentControllerData(modelData.AssetValuation,
                                                              marketEnvironment.GetExchangeCurrencyPaymentEnvironment1(),
                                                              modelData.ValuationDate, modelData.ReportingCurrency);
                var modelData2 = new InstrumentControllerData(modelData.AssetValuation,
                                                              marketEnvironment.GetExchangeCurrencyPaymentEnvironment2(),
                                                              modelData.ValuationDate, modelData.ReportingCurrency);
                childValuations.Add(Currency1Payment.Calculate(modelData1));
                childValuations.Add(Currency2Payment.Calculate(modelData2));
                childValuations.AddRange(Premia.Select(premium => premium.Calculate(modelData2)));
            }
            else if (modelData.MarketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                var market = (MarketEnvironment)modelData.MarketEnvironment;
                if (delta0PDH)
                {
                    //    //Force building of the risk curves.
                    //    market.SearchForPerturbedPricingStructures(FxIndexCurveName, "delta0PDH");//TODO Need to add this perturbation to fxCurve.
                }
                if (delta1PDH)
                {
                    //Force building of the risk curves.
                    market.SearchForPerturbedPricingStructures(FxLeg.Currency1DiscountCurveName, "delta1PDH");
                    market.SearchForPerturbedPricingStructures(FxLeg.Currency2DiscountCurveName, "delta1PDH");
                }
                childValuations = EvaluateChildMetrics(GetChildren().ToList(), modelData, Metrics);
                //childValuations.Add(Currency1Payment.Calculate(modelData));
            }
            var paymentValuation          = AssetValuationHelper.AggregateMetrics(childValuations, new List <string>(Metrics), PaymentCurrencies);// modelData.ValuationDate);
            var childControllerValuations = new List <AssetValuation> {
                paymentValuation
            };

            TimeToExpiry = GetPaymentYearFraction(ModelData.ValuationDate, ExpiryDate);
            var volatilityCurveNodeTime = GetPaymentYearFraction(ModelData.ValuationDate, ValueDate);

            // Child metrics have now been calculated so we can now evaluate the stream model metrics
            if (streamControllerMetrics.Count > 0)
            {
                if (modelData.MarketEnvironment is MarketEnvironment market)
                {
                    var fxCurve      = (IFxCurve)market.SearchForPricingStructureType(FxLeg.FxIndexCurveName);
                    var indexSurface = (IVolatilitySurface)market.SearchForPricingStructureType(VolatilitySurfaceName);
                    if (HybridValuation)
                    {
                        var curve1 = (IRateCurve)market.SearchForPricingStructureType(FxLeg.Currency1DiscountCurveName);
                        var curve2 = (IRateCurve)market.SearchForPricingStructureType(FxLeg.Currency2DiscountCurveName);
                        var flag   = FxLeg.ExchangeRate.quotedCurrencyPair.quoteBasis == QuoteBasisEnum.Currency2PerCurrency1;
                        AnalyticsModel = new FxOptionLegAnalytic(modelData.ValuationDate, RiskMaturityDate, fxCurve, curve1,
                                                                 curve2, flag, FxStrike, TimeToExpiry, volatilityCurveNodeTime, indexSurface, FxOptionType);
                    }
                    else
                    {
                        AnalyticsModel = new FxOptionLegAnalytic(modelData.ValuationDate, RiskMaturityDate, fxCurve, FxStrike, TimeToExpiry, volatilityCurveNodeTime, indexSurface, FxOptionType);
                    }
                }
                decimal?premium = null;
                if (Premia.Count > 1)
                {
                    premium = Premia[0].PaymentAmount.amount; // We assume only a single premium in the basic case.
                }
                IFxOptionLegParameters analyticModelParameters = new FxOptionLegParameters
                {
                    Premium = premium,
                };
                CalculationResults =
                    AnalyticsModel.Calculate <IFxOptionLegInstrumentResults, FxOptionLegInstrumentResults>(
                        analyticModelParameters, streamControllerMetrics.ToArray());

                // Now merge back into the overall stream valuation
                var streamControllerValuation = GetValue(CalculationResults, modelData.ValuationDate);
                streamValuation = AssetValuationHelper.UpdateValuation(streamControllerValuation,
                                                                       childControllerValuations,
                                                                       ConvertMetrics(streamControllerMetrics),
                                                                       new List <string>(Metrics), PaymentCurrencies);// modelData.ValuationDate);
            }
            else
            {
                streamValuation = paymentValuation;
            }
            CalculationPerformedIndicator = true;
            streamValuation.id            = Id;
            return(streamValuation);
        }