/// <summary>
        /// Initializes a new instance of the <see cref="CommoditySpreadCurve2"/> class.
        /// </summary>
        /// <param name="referenceCurve">The reference curve.</param>
        /// <param name="spreadAssets">The spreads by asset.</param>
        /// <param name="properties">The properties of the new spread curve.</param>
        /// <param name="algorithm">The alogorithm holder. </param>
        public CommoditySpreadCurve2(NamedValueSet properties, ICommodityCurve referenceCurve,
                                     List <IPriceableCommoditySpreadAssetController> spreadAssets, PricingStructureAlgorithmsHolder algorithm)
            : base(properties, algorithm)
        {
            PricingStructureData = new PricingStructureData(CurveType.Child, AssetClass.Commodity, properties);
            //Set the identifier.
            var pricingStructureId = GetCommodityCurveId();

            if (pricingStructureId.PricingStructureType != PricingStructureTypeEnum.CommoditySpreadCurve)
            {
                return;
            }
            //Set the reference curve
            BaseCurve        = referenceCurve;
            ReferenceCurveId = BaseCurve.GetPricingStructureId();
            PriceableCommoditySpreadAssets = spreadAssets;
            //Order the assets.
            PriceableCommoditySpreadAssets = PriceableCommoditySpreadAssets.OrderBy(a => a.GetRiskMaturityDate()).ToList();
            var termCurve = SetConfigurationData();
            //Get the reference interpolated curve.
            IList <Double> xArray = new List <double>();
            IList <Double> yArray = new List <double>();

            termCurve.point = CommoditySpreadBootstrapper2.Bootstrap(PriceableCommoditySpreadAssets,
                                                                     BaseCurve,
                                                                     pricingStructureId.BaseDate,
                                                                     termCurve.extrapolationPermitted,
                                                                     Tolerance, ref xArray, ref yArray);
            CreatePricingStructure(pricingStructureId, termCurve, PriceableAssetFactory.Parse(PriceableCommoditySpreadAssets));
            SetInterpolator(BaseCurve, xArray, yArray, pricingStructureId.PricingStructureType);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CommoditySpreadCurve2"/> class.
 /// </summary>
 /// <param name="logger">The logger.</param>
 /// <param name="cache">The cache.</param>
 ///  <param name="nameSpace">The client namespace</param>
 /// <param name="referenceCurve">The reference parent curveid.</param>
 /// <param name="spreadAssets">The spreads by asset.</param>
 /// <param name="properties">The properties of the new spread curve.</param>
 /// <param name="calendar">The calendar.</param>
 public CommoditySpreadCurve2(ILogger logger, ICoreCache cache, string nameSpace,
                              ICommodityCurve referenceCurve, FxRateSet spreadAssets, NamedValueSet properties,
                              IBusinessCalendar calendar)
     : base(logger, cache, nameSpace, ProcessQuotedAssetSet(logger, cache, nameSpace, referenceCurve, spreadAssets, properties, calendar), properties, calendar, calendar)
 {
     PricingStructureData = new PricingStructureData(CurveType.Child, AssetClass.Commodity, properties);
     BaseCurve            = referenceCurve;
     ReferenceCurveId     = BaseCurve.GetPricingStructureId();
     if (PricingStructureIdentifier.PricingStructureType != PricingStructureTypeEnum.CommoditySpreadCurve)
     {
         return;
     }
     //Set the spread sets.
     PriceableCommoditySpreadAssets = PriceableAssetFactory.CreatePriceableCommoditySpreadAssets(logger, cache, nameSpace, PricingStructureIdentifier.BaseDate, spreadAssets, calendar);
     Build(logger, cache, nameSpace, calendar, calendar);
 }
        /// <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 CommoditySpreadAssetAnalytic();
            //DependencyCreator.Resolve<IModelAnalytic<ISimpleAssetParameters, SpreadMetrics>>(_modelIdentifier);
            var metrics = MetricsHelper.GetMetricsToEvaluate(Metrics, AnalyticsModel.Metrics);
            // Determine if DFAM has been requested - if so thats all we evaluate - every other metric is ignored
            var bEvalDiscountFactorAtMaturity = false;

            if (metrics.Contains(CommoditySpreadMetrics.IndexAtMaturity))
            {
                bEvalDiscountFactorAtMaturity = true;
                metrics.RemoveAll(
                    metricItem => metricItem != CommoditySpreadMetrics.IndexAtMaturity);
            }
            var analyticModelParameters = new CommodityAssetParameters();

            AnalyticResults = new CommoditySpreadAssetResults();
            var             metricsToEvaluate = metrics.ToArray();
            var             marketEnvironment = modelData.MarketEnvironment;
            ICommodityCurve curve             = null;

            //1. instantiate curve
            if (marketEnvironment.GetType() == typeof(SimpleMarketEnvironment))
            {
                curve     = (ICommodityCurve)((ISimpleMarketEnvironment)marketEnvironment).GetPricingStructure();
                CurveName = curve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SimpleCommodityMarketEnvironment))
            {
                curve     = ((ISimpleCommodityMarketEnvironment)marketEnvironment).GetCommodityCurve();
                CurveName = curve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                curve = (ICommodityCurve)modelData.MarketEnvironment.GetPricingStructure(CurveName);
            }
            var endDF = GetIndex(curve, RiskMaturityDate, modelData.ValuationDate);

            Values = new[] { endDF };
            //3. Get the end discount factor
            analyticModelParameters.CommodityForward = endDF;
            if (Spread != null)
            {
                analyticModelParameters.Spread = MarketQuoteHelper.NormaliseGeneralPriceUnits(SpreadQuotationType, Spread, "Price").value;
            }
            if (bEvalDiscountFactorAtMaturity)
            {
                //4. Set the anaytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <ICommoditySpreadAssetResults, CommoditySpreadAssetResults>(analyticModelParameters,
                                                                                                         metricsToEvaluate);
                ValueAtMaturity = AnalyticResults.IndexAtMaturity;
            }
            else
            {
                //4. Set the anaytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <ICommoditySpreadAssetResults, CommoditySpreadAssetResults>(analyticModelParameters,
                                                                                                         metricsToEvaluate);
            }
            return(GetValue(AnalyticResults));
        }
        /// <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 CommodityAssetAnalytic();
            //DependencyCreator.Resolve<IModelAnalytic<ICommodityAssetParameters, CommodityMetrics>>(_modelIdentifier);
            var metrics = MetricsHelper.GetMetricsToEvaluate(Metrics, AnalyticsModel.Metrics);
            // Determine if DFAM has been requested - if so thats all we evaluate - every other metric is ignored
            var bEvalForwardAtMaturity = false;

            if (metrics.Contains(CommodityMetrics.IndexAtMaturity))
            {
                bEvalForwardAtMaturity = true;
                metrics.RemoveAll(
                    metricItem => metricItem != CommodityMetrics.IndexAtMaturity);
            }
            ICommodityAssetParameters analyticModelParameters = new CommodityAssetParameters();

            AnalyticResults = new CommodityAssetResults();
            var metricsToEvaluate = metrics.ToArray();
            var marketEnvironment = modelData.MarketEnvironment;
            //1. instantiate curve
            ICommodityCurve commodityCurve = null;

            //1. instantiate curve
            if (marketEnvironment.GetType() == typeof(SimpleMarketEnvironment))
            {
                commodityCurve     = (ICommodityCurve)((ISimpleMarketEnvironment)marketEnvironment).GetPricingStructure();
                CommodityCurveName = commodityCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SimpleCommodityMarketEnvironment))
            {
                commodityCurve     = ((ISimpleCommodityMarketEnvironment)marketEnvironment).GetCommodityCurve();
                CommodityCurveName = commodityCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SwapLegEnvironment))
            {
                commodityCurve     = ((ISwapLegEnvironment)marketEnvironment).GetCommodityCurve();
                CommodityCurveName = commodityCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                commodityCurve = (ICommodityCurve)modelData.MarketEnvironment.GetPricingStructure(CommodityCurveName);
            }
            //3. Get the Rate
            if (CommodityValue != null)
            {
                analyticModelParameters.CommodityForward = CommodityValue.value;
            }
            if (bEvalForwardAtMaturity)
            {
                //4. Set the anaytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <ICommodityAssetResults, CommodityAssetResults>(analyticModelParameters,
                                                                                             metricsToEvaluate);
                //_forwardAtMaturity = ForwardAtMaturity;
            }
            else
            {
                analyticModelParameters.NotionalAmount = NotionalAmount;
                IDayCounter dc = Actual365.Instance;
                analyticModelParameters.YearFraction = (decimal)dc.YearFraction(modelData.ValuationDate, RiskMaturityDate);
                //3. Get the end discount factor - Need to fix this.
                analyticModelParameters.CommodityCurveForward =
                    GetIndexAtMaturity(commodityCurve, GetRiskMaturityDate(), modelData.ValuationDate);

                //4. Set the anaytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <ICommodityAssetResults, CommodityAssetResults>(analyticModelParameters,
                                                                                             metricsToEvaluate);
            }
            return(GetValue(AnalyticResults));
        }
        /// <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 CommodityFuturesAssetAnalytic();
            //DependencyCreator.Resolve<IModelAnalytic<ICommodityFuturesAssetParameters, CommodityMetrics>>(_modelIdentifier);
            var metrics = MetricsHelper.GetMetricsToEvaluate(Metrics, AnalyticsModel.Metrics);
            // Determine if DFAM has been requested - if so that is all we evaluate - every other metric is ignored
            var bEvalIndexAtMaturity = false;

            if (metrics.Contains(CommodityMetrics.IndexAtMaturity))
            {
                bEvalIndexAtMaturity = true;
                //  remove all except DiscountFactorAtMaturity metric
                //
                metrics.RemoveAll(metricItem => metricItem != CommodityMetrics.IndexAtMaturity);
            }
            var metricsToEvaluate = metrics.ToArray();
            ICommodityFuturesAssetParameters analyticModelParameters = new CommodityFuturesAssetParameters
            {
                UnitAmount   = Amount,
                TimeToExpiry = TimeToExpiry
            };
            var marketEnvironment = modelData.MarketEnvironment;
            //1. instantiate curve
            ICommodityCurve commodityCurve = null;

            //1. instantiate curve
            if (marketEnvironment.GetType() == typeof(SimpleMarketEnvironment))
            {
                commodityCurve = (ICommodityCurve)((ISimpleMarketEnvironment)marketEnvironment).GetPricingStructure();
                CurveName      = commodityCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SimpleCommodityMarketEnvironment))
            {
                commodityCurve = ((ISimpleCommodityMarketEnvironment)marketEnvironment).GetCommodityCurve();
                CurveName      = commodityCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(SwapLegEnvironment))
            {
                commodityCurve = ((ISwapLegEnvironment)marketEnvironment).GetCommodityCurve();
                CurveName      = commodityCurve.GetPricingStructureId().UniqueIdentifier;
            }
            if (marketEnvironment.GetType() == typeof(MarketEnvironment))
            {
                commodityCurve = (ICommodityCurve)modelData.MarketEnvironment.GetPricingStructure(CurveName);
            }
            //4. Get the Index
            analyticModelParameters.Index = CommodityValue.value;
            if (bEvalIndexAtMaturity)
            {
                //5. Set the analytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <ICommodityAssetResults, CommodityAssetResults>(analyticModelParameters,
                                                                                             metricsToEvaluate);
                ForwardIndex = IndexAtMaturity;
            }
            else
            {
                //4. Get the end discount factor
                ForwardIndex = GetCommodityForward(commodityCurve, GetRiskMaturityDate(), modelData.ValuationDate);
                analyticModelParameters.Index = IndexAtMaturity;
                //5. Get the TimeToExpiry
                analyticModelParameters.TimeToExpiry = GetTimeToExpiry();
                //6. Get the position
                analyticModelParameters.Position = Position;
                //7. Set the analytic input parameters and Calculate the respective metrics
                AnalyticResults =
                    AnalyticsModel.Calculate <ICommodityAssetResults, CommodityAssetResults>(analyticModelParameters,
                                                                                             metricsToEvaluate);
            }
            return(GetValue(AnalyticResults));
        }