Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PriceableInterestRateStream"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="cache">The cache.</param>
        /// <param name="nameSpace">The client namespace.</param>
        /// <param name="swapId">The swap Id.</param>
        /// <param name="payerPartyReference">The payer party reference.</param>
        /// <param name="receiverPartyReference">The receiver party reference.</param>
        /// <param name="payerIsBase">The flag for whether the payer reference is the base party.</param>
        /// : AdjustedDateHelper.ToAdjustedDate(forecastRateIndex.indexTenor.Add(AccrualStartDate), AccrualBusinessDayAdjustments);</param>
        /// <param name="paymentCalendar">The paymentCalendar.</param>
        public PriceableLease
        (
            ILogger logger
            , ICoreCache cache
            , String nameSpace
            , string swapId
            , string payerPartyReference
            , string receiverPartyReference
            , bool payerIsBase
            , IBusinessCalendar paymentCalendar)
        {
            Multiplier        = 1.0m;
            Payer             = payerPartyReference;
            Receiver          = receiverPartyReference;
            PayerIsBaseParty  = payerIsBase;
            PaymentCurrencies = new List <string>();
            AnalyticsModel    = new StructuredStreamAnalytic();
            Id = BuildId(swapId, CouponStreamType);
            //Get the currency.
            var currency = XsdClassesFieldResolver.CalculationGetNotionalSchedule(Calculation);

            Currency = currency.notionalStepSchedule.currency;
            if (!PaymentCurrencies.Contains(Currency.Value))
            {
                PaymentCurrencies.Add(Currency.Value);
            }
            //The calendars
            if (paymentCalendar == null)
            {
                paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, PaymentDates.paymentDatesAdjustments.businessCenters, nameSpace);
            }
            //Set the default discount curve name.
            DiscountCurveName = CurveNameHelpers.GetDiscountCurveName(Currency.Value, true);
            RiskMaturityDate  = LastDate();
            logger.LogInfo("Stream built");
        }
        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 StructuredStreamAnalytic();
            }
            var            streamControllerMetrics = ResolveModelMetrics(AnalyticsModel.Metrics);
            AssetValuation streamValuation;

            // 2. Now evaluate only the child specific metrics (if any)
            foreach (var coupon in Coupons)
            {
                coupon.PricingStructureEvolutionType = PricingStructureEvolutionType;
                coupon.BucketedDates = BucketedDates;
                coupon.Multiplier    = Multiplier;
            }
            var childControllers = new List <InstrumentControllerBase>(Coupons.ToArray());
            //Now the stream analytics can be completed.
            var childValuations           = EvaluateChildMetrics(childControllers, modelData, Metrics);
            var couponValuation           = AssetValuationHelper.AggregateMetrics(childValuations, new List <string>(Metrics), PaymentCurrencies);// modelData.ValuationDate);
            var childControllerValuations = new List <AssetValuation> {
                couponValuation
            };

            if (Exchanges != null && Exchanges.Count > 0)
            {
                foreach (var exchange in Exchanges)
                {
                    exchange.PricingStructureEvolutionType = PricingStructureEvolutionType;
                    exchange.BucketedDates = BucketedDates;
                    exchange.Multiplier    = Multiplier;
                }
                // Roll-Up and merge child valuations into parent Valuation
                var childPrincipalControllers = new List <InstrumentControllerBase>(Exchanges.ToArray());
                var childPrincipalValuations  = EvaluateChildMetrics(childPrincipalControllers, modelData, Metrics);
                var principalValuation        = AssetValuationHelper.AggregateMetrics(childPrincipalValuations, new List <string>(Metrics), PaymentCurrencies);// modelData.ValuationDate);
                childControllerValuations.Add(principalValuation);
            }
            // Child metrics have now been calculated so we can now evaluate the stream model metrics
            if (streamControllerMetrics.Count > 0)
            {
                var reportingCurrency = ModelData.ReportingCurrency == null ? Currency.Value : ModelData.ReportingCurrency.Value;
                var notionals         = GetCouponNotionals();
                var accrualFactors    = GetCouponAccrualFactors();
                var discountFactors   = GetPaymentDiscountFactors();
                var floatingNPV       = AggregateMetric(InstrumentMetrics.FloatingNPV, childControllerValuations);
                var accrualFactor     = AggregateMetric(InstrumentMetrics.AccrualFactor, childControllerValuations);
                //TODO need to  set the notional amount and the weighting. Also amortisation??
                IStructuredStreamParameters analyticModelParameters = new StructuredStreamParameters
                {
                    Multiplier        = Multiplier,
                    IsDiscounted      = IsDiscounted,
                    CouponNotionals   = notionals,
                    Currency          = Currency.Value,
                    ReportingCurrency = reportingCurrency,
                    AccrualFactor     = accrualFactor,
                    FloatingNPV       = floatingNPV,
                    NPV = AggregateMetric(InstrumentMetrics.NPV, childControllerValuations),
                    CouponYearFractions    = accrualFactors,
                    PaymentDiscountFactors = discountFactors,
                    TargetNPV = floatingNPV
                };
                CalculationResults = AnalyticsModel.Calculate <IStreamInstrumentResults, StreamInstrumentResults>(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);
                AnalyticModelParameters = analyticModelParameters;
            }
            else
            {
                streamValuation = AssetValuationHelper.AggregateMetrics(childControllerValuations, new List <string>(Metrics), PaymentCurrencies);// modelData.ValuationDate);
            }
            CalculationPerfomedIndicator = true;
            streamValuation.id           = Id;
            return(streamValuation);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PriceableInterestRateStream"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="cache">The cache.</param>
        /// <param name="nameSpace">The client namesspace.</param>
        /// <param name="swapId">The swap Id.</param>
        /// <param name="payerPartyReference">The payer party reference.</param>
        /// <param name="receiverPartyReference">The receiver party reference.</param>
        /// <param name="payerIsBase">The flag for whether the payerreference is the base party.</param>
        /// <param name="calculationPeriodDates">The caluclation period date information.</param>
        /// <param name="paymentDates">The payment dates of the swap leg.</param>
        /// <param name="resetDates">The reset dates of the swap leg.</param>
        /// <param name="principalExchanges">The principal Exchange type.</param>
        /// <param name="calculationPeriodAmount">The calculation period amount data.</param>
        /// <param name="stubCalculationPeriodAmount">The stub calculation information.</param>
        /// <param name="cashflows">The FpML cashflows for that stream.</param>
        /// <param name="settlementProvision">The settlement provision data.</param>
        /// <param name="forecastRateInterpolation">ForwardEndDate = forecastRateInterpolation ? AccrualEndDate
        /// : AdjustedDateHelper.ToAdjustedDate(forecastRateIndex.indexTenor.Add(AccrualStartDate), AccrualBusinessDayAdjustments);</param>
        /// <param name="fixingCalendar">The fixingCalendar.</param>
        /// <param name="paymentCalendar">The paymentCalendar.</param>
        public PriceableInterestRateStream
        (
            ILogger logger
            , ICoreCache cache
            , String nameSpace
            , string swapId
            , string payerPartyReference
            , string receiverPartyReference
            , bool payerIsBase
            , CalculationPeriodDates calculationPeriodDates
            , PaymentDates paymentDates
            , ResetDates resetDates
            , PrincipalExchanges principalExchanges
            , CalculationPeriodAmount calculationPeriodAmount
            , StubCalculationPeriodAmount stubCalculationPeriodAmount
            , Cashflows cashflows
            , SettlementProvision settlementProvision
            , bool forecastRateInterpolation
            , IBusinessCalendar fixingCalendar
            , IBusinessCalendar paymentCalendar)
        {
            Multiplier              = 1.0m;
            Payer                   = payerPartyReference;
            Receiver                = receiverPartyReference;
            PayerIsBaseParty        = payerIsBase;
            CalculationPeriodDates  = calculationPeriodDates;
            PaymentDates            = paymentDates;
            PaymentCurrencies       = new List <string>();
            ResetDates              = resetDates;
            PrincipalExchanges      = principalExchanges;
            CalculationPeriodAmount = calculationPeriodAmount;
            AnalyticsModel          = new StructuredStreamAnalytic();
            Calculation             = (Calculation)CalculationPeriodAmount.Item;
            if (Calculation.Items?[0] is Schedule strikeSchedule)
            {
                Strike = strikeSchedule.initialValue;//Only picks up the first fixed rate for the swaption calculation.
            }
            StubCalculationPeriodAmount = stubCalculationPeriodAmount;
            Cashflows        = cashflows;
            CouponStreamType = CouponTypeFromCalculation(Calculation);
            Id = BuildId(swapId, CouponStreamType);
            ForecastRateInterpolation = forecastRateInterpolation;
            var isThereDiscounting = XsdClassesFieldResolver.CalculationHasDiscounting(Calculation);

            if (isThereDiscounting)
            {
                IsDiscounted = true; //TODO need to include rate logic for the correct solved answers. What about reset cashflows??
            }
            //Get the currency.
            var currency = XsdClassesFieldResolver.CalculationGetNotionalSchedule(Calculation);

            Currency = currency.notionalStepSchedule.currency;
            if (!PaymentCurrencies.Contains(Currency.Value))
            {
                PaymentCurrencies.Add(Currency.Value);
            }
            //The calendars
            if (paymentCalendar == null)
            {
                paymentCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, PaymentDates.paymentDatesAdjustments.businessCenters, nameSpace);
            }
            SettlementProvision = settlementProvision;
            //Set the default discount curve name.
            DiscountCurveName = CurveNameHelpers.GetDiscountCurveName(Currency.Value, true);
            //Set the forecast curve name.//TODO extend this to the other types.
            if (CouponStreamType != CouponStreamType.GenericFixedRate)
            {
                if (fixingCalendar == null)
                {
                    fixingCalendar = BusinessCenterHelper.ToBusinessCalendar(cache, ResetDates.resetDatesAdjustments.businessCenters, nameSpace);
                }
                ForecastCurveName = null;
                if (Calculation.Items != null)
                {
                    var floatingRateCalculation = Calculation.Items;
                    var floatingRateIndex       = (FloatingRateCalculation)floatingRateCalculation[0];
                    ForecastCurveName = CurveNameHelpers.GetForecastCurveName(floatingRateIndex);
                }
            }
            //Build the coupons and principal exchanges.
            if (GetCashflowPaymentCalculationPeriods() != null)
            {
                Coupons = PriceableInstrumentsFactory.CreatePriceableCoupons(PayerIsBaseParty,
                                                                             GetCashflowPaymentCalculationPeriods(),
                                                                             Calculation, ForecastRateInterpolation, fixingCalendar, paymentCalendar);//TODO add the stubcalculation.
                UpdateCouponIds();
            }
            if (GetCashflowPrincipalExchanges() != null)
            {
                var exchanges = GetCashflowPrincipalExchanges();
                Exchanges = PriceableInstrumentsFactory.CreatePriceablePrincipalExchanges(PayerIsBaseParty, exchanges, Currency.Value, paymentCalendar);
                UpdateExchangeIds();
            }
            RiskMaturityDate = LastDate();
            logger.LogInfo("Stream built");
        }
 /// <summary>
 ///
 /// </summary>
 public PriceableInterestRateStream()
 {
     Multiplier     = 1.0m;
     AnalyticsModel = new StructuredStreamAnalytic();
 }