/// <summary>
        /// Calculates the specified model data.
        /// </summary>
        /// <param name="curve">The curve.</param>
        /// <param name="valuationDate">The valuation date.</param>
        /// <returns></returns>
        public decimal CalculateAssetSwap(IRateCurve curve, DateTime valuationDate)
        {
            AnalyticsModel = new BondAssetAnalytic();
            IBondAssetParameters analyticModelParameters = new BondAssetParameters();

            CalculationResults = new BondAssetResults();
            //2. Set the rate and Multiplier
            analyticModelParameters.Multiplier             = Multiplier;
            analyticModelParameters.Quote                  = QuoteValue; // MarketQuoteHelper.NormalisePriceUnits(Quote, "DecimalRate").value;
            analyticModelParameters.CouponRate             = GetCouponRate();
            analyticModelParameters.NotionalAmount         = Notional;
            analyticModelParameters.Frequency              = Frequency;
            analyticModelParameters.IsYTMQuote             = IsYTMQuote;
            analyticModelParameters.AccruedFactor          = GetAccruedFactor();
            analyticModelParameters.RemainingAccruedFactor = GetRemainingAccruedFactor();
            //2. Get the discount factors
            analyticModelParameters.PaymentDiscountFactors =
                GetDiscountFactors(curve, AdjustedPeriodDates, valuationDate);
            //3. Get the respective year fractions
            analyticModelParameters.AccrualYearFractions = GetYearFractions();
            //4. Get the Weightings
            Weightings =
                CreateWeightings(CDefaultWeightingValue, analyticModelParameters.PaymentDiscountFactors.Length);
            analyticModelParameters.Weightings = Weightings;
            //5. Set the analytic input parameters and Calculate the respective metrics
            AnalyticModelParameters = analyticModelParameters;
            CalculationResults      =
                AnalyticsModel.Calculate <IBondAssetResults, BondAssetResults>(analyticModelParameters,
                                                                               new[] { BondMetrics.AssetSwapSpread });
            return(CalculationResults.AssetSwapSpread);
        }
        /// <summary>
        /// Calculates the specified model data.
        /// </summary>
        /// <param name="modelData">The model data.</param>
        /// <returns></returns>
        public override BasicAssetValuation Calculate(IAssetControllerData modelData)
        {
            ModelData      = modelData;
            AnalyticsModel = new BondAssetAnalytic();
            var metrics = MetricsHelper.GetMetricsToEvaluate(Metrics, AnalyticsModel.Metrics);
            // Determine if DFAM has been requested - if so that's all we evaluate - every other metric is ignored
            var metricsToEvaluate       = metrics.ToArray();
            var analyticModelParameters = new BondAssetParameters();

            CalculationResults = new BondAssetResults();
            var marketEnvironment = modelData.MarketEnvironment;
            //IRateCurve rate forecast curve = null;
            IRateCurve rateDiscountCurve = null;

            //0. Set the valuation date and recalculate the settlement date. This could mean regenerating all the coupon dates as well
            //Alternatively the bond can be recreated with a different base date = valuation date.
            //TODO Check that the dates are correct and that the last coupon date is used.
            //Set the purchase price.
            analyticModelParameters.PurchasePrice = PurchasePrice;
            //1. instantiate curve
            if (marketEnvironment.GetType() == typeof(SimpleMarketEnvironment))
            {
                rateDiscountCurve     = (IRateCurve)((ISimpleMarketEnvironment)marketEnvironment).GetPricingStructure();
                SwapDiscountCurveName = rateDiscountCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SimpleRateMarketEnvironment))
            {
                rateDiscountCurve     = ((ISimpleRateMarketEnvironment)marketEnvironment).GetRateCurve();
                SwapDiscountCurveName = rateDiscountCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SwapLegEnvironment))
            {
                rateDiscountCurve     = ((ISwapLegEnvironment)marketEnvironment).GetDiscountRateCurve();
                SwapDiscountCurveName = rateDiscountCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                var bondCurve = (IBondCurve)modelData.MarketEnvironment.GetPricingStructure(BondCurveName);
                if (bondCurve != null)
                {
                    var marketDataType =
                        bondCurve.GetPricingStructureId().Properties.GetValue <string>(AssetMeasureEnum.MarketQuote.ToString(), false);
                    if (marketDataType != null && marketDataType == BondPriceEnum.YieldToMaturity.ToString())
                    {
                        IsYTMQuote = true;
                    }
                    //TODO handle the other cases like: AssetSwapSpread; DirtyPrice and ZSpread.
                    var mq = (decimal)bondCurve.GetYieldToMaturity(modelData.ValuationDate, SettlementDate);
                    Quote = BasicQuotationHelper.Create(mq, AssetMeasureEnum.MarketQuote.ToString(),
                                                        PriceQuoteUnitsEnum.DecimalRate.ToString());
                }
                //The forecast rate curve will need to be set if it is a floating rate note.
                rateDiscountCurve = (IRateCurve)modelData.MarketEnvironment.GetPricingStructure(SwapDiscountCurveName);
            }
            //2. Set the rate and the Multiplier
            analyticModelParameters.Multiplier             = Multiplier;
            analyticModelParameters.Quote                  = QuoteValue;
            analyticModelParameters.CouponRate             = GetCouponRate();
            analyticModelParameters.NotionalAmount         = Notional;
            analyticModelParameters.Frequency              = Frequency;
            analyticModelParameters.IsYTMQuote             = IsYTMQuote;
            analyticModelParameters.AccruedFactor          = GetAccruedFactor();
            analyticModelParameters.RemainingAccruedFactor = GetRemainingAccruedFactor();
            //3. Get the discount factors
            analyticModelParameters.PaymentDiscountFactors =
                GetDiscountFactors(rateDiscountCurve, AdjustedPeriodDates, modelData.ValuationDate);
            //4. Get the respective year fractions
            analyticModelParameters.AccrualYearFractions = GetYearFractions();
            //5. Get the Weightings
            Weightings =
                CreateWeightings(CDefaultWeightingValue, analyticModelParameters.PaymentDiscountFactors.Length);
            analyticModelParameters.Weightings = Weightings;
            //6. Set the analytic input parameters and Calculate the respective metrics
            AnalyticModelParameters = analyticModelParameters;
            CalculationResults      =
                AnalyticsModel.Calculate <IBondAssetResults, BondAssetResults>(analyticModelParameters,
                                                                               metricsToEvaluate);
            return(GetValue(CalculationResults));
        }