/// <summary>
 /// Initializes a new instance of the <see cref="FxSingleLegPricer"/> class.  All the cashfloews must be signed.
 /// </summary>
 /// <param name="fxLeg">The fxLeg.</param>
 /// <param name="baseParty">The the base party.</param>
 /// <param name="productType">THe product type: This should only be FxSpot or FxForward.</param>
 public FxSingleLegPricer(FxSingleLeg fxLeg, string baseParty, ProductTypeSimpleEnum productType)
 {
     Multiplier        = 1.0m;
     OrderedPartyNames = new List <string>();
     Id              = fxLeg.id;
     AnalyticsModel  = new FxLegAnalytic();
     ProductType     = productType;
     ExchangeRate    = fxLeg.exchangeRate;
     HybridValuation = true;
     //Get the currency.
     PaymentCurrencies = new List <string>();
     Currency1         = fxLeg.exchangedCurrency1.paymentAmount.currency;
     Currency2         = fxLeg.exchangedCurrency2.paymentAmount.currency;
     if (!PaymentCurrencies.Contains(Currency1.Value))
     {
         PaymentCurrencies.Add(Currency1.Value);
     }
     if (!PaymentCurrencies.Contains(Currency2.Value))
     {
         PaymentCurrencies.Add(Currency2.Value);
     }
     //Set the default discount curve name.
     Currency1DiscountCurveName = CurveNameHelpers.GetDiscountCurveName(Currency1.Value, true);
     //Set the default discount curve name.
     Currency2DiscountCurveName = CurveNameHelpers.GetDiscountCurveName(Currency2.Value, true);
     //TODO
     //Set the appropraiet cross
     //if the quotebasis is : Currency1PerCurrency2 the currency curve is: currency1-currency2
     if (ExchangeRate.quotedCurrencyPair.quoteBasis == QuoteBasisEnum.Currency2PerCurrency1)
     {
         FxIndexCurveName = MarketEnvironmentHelper.ResolveFxCurveNames(Currency1.Value,
                                                                        Currency2.Value);
     }            //Otherwise it is Currency2-Currency1
     else
     {
         FxIndexCurveName = MarketEnvironmentHelper.ResolveFxCurveNames(Currency2.Value,
                                                                        Currency1.Value);
     }
     if (fxLeg.nonDeliverableSettlement != null)//TODO Not implemented yet.
     {
         IsNonDeliverableForward             = true;
         SettlementCurrency                  = fxLeg.nonDeliverableSettlement.settlementCurrency;
         SettlementCurrencyDiscountCurveName = CurveNameHelpers.GetDiscountCurveName(SettlementCurrency.Value, true);
     }
     //Build the coupons and principal exchanges.
     Payments = PriceableInstrumentsFactory.CreatePriceableFxLegPayment(baseParty, fxLeg);
     //TODO: add extra cashflowss.
     RiskMaturityDate = fxLeg.Items1ElementName[0] == Items1ChoiceType.valueDate ? fxLeg.Items1[0] : LastDate();
 }
        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);
        }
 public FxSingleLegPricer()
 {
     Multiplier     = 1.0m;
     AnalyticsModel = new FxLegAnalytic();
 }
Beispiel #4
0
 public PriceableFxSwapLeg()
 {
     Multiplier     = 1.0m;
     AnalyticsModel = new FxLegAnalytic();
 }