Exemplo n.º 1
0
        public IPriceCurve Solve(List <AsianSwapStrip> instruments, List <DateTime> pillars, IIrCurve discountCurve, DateTime buildDate, ICurrencyProvider currencyProvider)
        {
            _currencyProvider    = currencyProvider;
            _curveInstruments    = instruments;
            _pillars             = pillars.ToArray();
            _numberOfInstruments = _curveInstruments.Count;
            _numberOfPillars     = pillars.Count;
            _discountCurve       = discountCurve;
            _buildDate           = buildDate;

            _currentGuess = Enumerable.Repeat(instruments.Average(x => x.Swaplets.Average(s => s.Strike)), _numberOfPillars).ToArray();
            _currentCurve = new SparsePriceCurve(_buildDate, _pillars, _currentGuess, SparsePriceCurveType.Coal, currencyProvider, null);
            _currentPVs   = ComputePVs();

            ComputeJacobian();

            for (var i = 0; i < MaxItterations; i++)
            {
                ComputeNextGuess();
                _currentCurve = new SparsePriceCurve(_buildDate, _pillars, _currentGuess, SparsePriceCurveType.Coal, _currencyProvider);

                _currentPVs = ComputePVs();
                if (_currentPVs.Max(x => System.Math.Abs(x)) < Tollerance)
                {
                    UsedItterations = i + 1;
                    break;
                }
                ComputeJacobian();
            }

            return(_currentCurve);
        }
Exemplo n.º 2
0
        public static double SwapPv(IPriceCurve priceCurve, AsianSwapStrip swap, IIrCurve discountCurve)
        {
            var swapletPVs = swap.Swaplets.Select(s =>
                                                  (priceCurve.GetAveragePriceForDates(s.FixingDates.AddPeriod(RollType.F, s.FixingCalendar, s.SpotLag)) - s.Strike)
                                                  * (s.Direction == TradeDirection.Long ? 1.0 : -1.0)
                                                  * discountCurve.GetDf(priceCurve.BuildDate, s.PaymentDate));

            return(swapletPVs.Sum());
        }
Exemplo n.º 3
0
        public BasisPriceCurve(List <IAssetInstrument> instruments, List <DateTime> pillars, IIrCurve discountCurve, IPriceCurve baseCurve, DateTime buildDate, PriceCurveType curveType, INewtonRaphsonAssetBasisCurveSolver solver, List <string> pillarLabels = null)
        {
            Instruments   = instruments;
            Pillars       = pillars;
            DiscountCurve = discountCurve;
            BaseCurve     = baseCurve;
            CurveType     = curveType;

            Solver       = solver;
            BuildDate    = buildDate;
            PillarLabels = pillarLabels ?? pillars.Select(x => x.ToString("yyyy-MM-dd")).ToList();

            Curve = solver.SolveCurve(Instruments, Pillars, DiscountCurve, BaseCurve, BuildDate, CurveType);
        }
Exemplo n.º 4
0
        private void ComputeJacobian()
        {
            _jacobian = Math.Matrix.DoubleArrayFunctions.MatrixCreate(_numberOfPillars, _numberOfPillars);

            for (var i = 0; i < _numberOfPillars; i++)
            {
                _currentCurve = new SparsePriceCurve(_buildDate, _pillars, _currentGuess.Select((g, ix) => ix == i ? g + JacobianBump : g).ToArray(), SparsePriceCurveType.Coal, _currencyProvider);
                var bumpedPVs = ComputePVs();

                for (var j = 0; j < bumpedPVs.Length; j++)
                {
                    _jacobian[i][j] = (bumpedPVs[j] - _currentPVs[j]) / JacobianBump;
                }
            }
        }
Exemplo n.º 5
0
        public BasisPriceCurve(TO_BasisPriceCurve transportObject, ICurrencyProvider currencyProvider, ICalendarProvider calendarProvider)
        {
            Instruments = transportObject.Instruments
                          .Select(x => (IAssetInstrument)InstrumentFactory.GetInstrument(x, currencyProvider, calendarProvider))
                          .ToList();

            Pillars       = transportObject.Pillars;
            DiscountCurve = new IrCurve(transportObject.DiscountCurve, currencyProvider);
            //need to re-link via the active model
            Curve        = PriceCurveFactory.GetPriceCurve(transportObject.Curve, currencyProvider, calendarProvider);
            BaseCurve    = PriceCurveFactory.GetPriceCurve(transportObject.BaseCurve, currencyProvider, calendarProvider);
            Name         = transportObject.Name;
            AssetId      = transportObject.AssetId;
            BuildDate    = transportObject.BuildDate;
            PillarLabels = transportObject.PillarLabels;
            Currency     = currencyProvider.GetCurrencySafe(transportObject.Currency);
            SpotCalendar = calendarProvider.GetCalendarSafe(transportObject.SpotCalendar);
            if (transportObject.SpotLag != null)
            {
                SpotLag = new Frequency(transportObject.SpotLag);
            }
            CurveType = transportObject.CurveType;
        }
Exemplo n.º 6
0
 public void AddPriceCurve(string name, IPriceCurve curve) => _assetCurves[name] = curve;
Exemplo n.º 7
0
        public static RiskyFlySurface ToRiskyFlySurfaceStepFlat(this Dictionary <DateTime, IInterpolator1D> smiles, DateTime valDate, IPriceCurve priceCurve, List <DateTime> allExpiries, ICurrencyProvider currencyProvider)
        {
            var wingDeltas     = new[] { 0.25, 0.1 };
            var expiries       = smiles.Keys.ToArray();
            var expiriesDouble = expiries.Select(e => e.ToOADate()).ToArray();
            var atmVols        = smiles.Select(x => x.Value.Interpolate(0.5)).ToArray();
            var riskies        = smiles.Select(x =>
                                               wingDeltas.Select(w => x.Value.Interpolate(1.0 - w) - x.Value.Interpolate(w))
                                               .ToArray())
                                 .ToArray();
            var flies = smiles.Select((x, ix) =>
                                      wingDeltas.Select(w => (x.Value.Interpolate(1.0 - w) + x.Value.Interpolate(w)) / 2.0 - atmVols[ix])
                                      .ToArray())
                        .ToArray();

            SmoothSmiles(wingDeltas, smiles, ref riskies, ref flies);

            var fwds = allExpiries.Select(x => priceCurve.GetPriceForDate(x)).ToArray();

            var atmInterp    = InterpolatorFactory.GetInterpolator(expiriesDouble, atmVols, Interpolator1DType.LinearInVariance);
            var riskyInterps = wingDeltas.Select((w, ixw) => InterpolatorFactory.GetInterpolator(expiriesDouble, riskies.Select(r => r[ixw]).ToArray(), Interpolator1DType.LinearFlatExtrap)).ToArray();
            var flyInterps   = wingDeltas.Select((w, ixw) => InterpolatorFactory.GetInterpolator(expiriesDouble, flies.Select(f => f[ixw]).ToArray(), Interpolator1DType.LinearFlatExtrap)).ToArray();

            var expandedRiskies = allExpiries.Select(e => wingDeltas.Select((w, ixw) => riskyInterps[ixw].Interpolate(e.ToOADate())).ToArray()).ToArray();
            var expandedFlies   = allExpiries.Select(e => wingDeltas.Select((w, ixw) => flyInterps[ixw].Interpolate(e.ToOADate())).ToArray()).ToArray();
            var expandedAtms    = allExpiries.Select(e => atmInterp.Interpolate(e.ToOADate())).ToArray();
            var expandedFwds    = allExpiries.Select(e => priceCurve.GetPriceForDate(e)).ToArray();
            var o = new RiskyFlySurface(valDate, expandedAtms, allExpiries.ToArray(), wingDeltas, expandedRiskies, expandedFlies, expandedFwds, WingQuoteType.Arithmatic,
                                        AtmVolType.ZeroDeltaStraddle, Interpolator1DType.CubicSpline, Interpolator1DType.NextValue)
            {
                Currency = currencyProvider.GetCurrency("USD")
            };

            return(o);
        }
Exemplo n.º 8
0
 public BasisPriceCurve ReCalibrate(IPriceCurve NewBaseCurve)
 => new BasisPriceCurve(Instruments.Select(x => (IAssetInstrument)x.Clone()).ToList(), Pillars, DiscountCurve, NewBaseCurve, BuildDate, CurveType, Solver)
 {
     Currency = Currency, AssetId = AssetId, Name = Name
 };
Exemplo n.º 9
0
 public void RemovePriceCurve(IPriceCurve curve)
 {
     _assetCurves.Remove(curve.Name);
 }
Exemplo n.º 10
0
 public FactorPriceCurve(IPriceCurve baseCurve, double scalingFactor)
 {
     BaseCurve     = baseCurve;
     ScalingFactor = scalingFactor;
 }
Exemplo n.º 11
0
 public Product(IPriceCurve curve, ITax tax, IPriceDeduction deduction)
 {
     Curve     = curve;
     Tax       = tax;
     Deduction = deduction;
 }
Exemplo n.º 12
0
 public Product(IPriceCurve curve, ITax tax)
 {
     Curve = curve;
     Tax   = tax;
 }
Exemplo n.º 13
0
        public static TO_PriceCurve GetTransportObject(this IPriceCurve curve)
        {
            switch (curve)
            {
            case ConstantPriceCurve cons:
                return(new TO_PriceCurve
                {
                    ConstantPriceCurve = new TO_ConstantPriceCurve
                    {
                        AssetId = cons.AssetId,
                        BuildDate = cons.BuildDate,
                        Currency = cons.Currency,
                        Name = cons.Name,
                        Price = cons.Price,
                    }
                });

            case BasicPriceCurve basic:
                return(new TO_PriceCurve
                {
                    BasicPriceCurve = new TO_BasicPriceCurve
                    {
                        AssetId = basic.AssetId,
                        BuildDate = basic.BuildDate,
                        CollateralSpec = basic.CollateralSpec,
                        Currency = basic.Currency,
                        CurveType = basic.CurveType,
                        Name = basic.Name,
                        PillarDates = basic.PillarDates,
                        PillarLabels = basic.PillarLabels,
                        Prices = basic.Prices,
                        SpotCalendar = basic.SpotCalendar?.Name,
                        SpotLag = basic.SpotLag.ToString()
                    }
                });

            case BasisPriceCurve basis:
                return(new TO_PriceCurve
                {
                    BasisPriceCurve = new TO_BasisPriceCurve
                    {
                        AssetId = basis.AssetId,
                        Currency = basis.Currency.Ccy,
                        Name = basis.Name,
                        SpotCalendar = basis.SpotCalendar?.Name,
                        SpotLag = basis.SpotLag.ToString(),
                        BaseCurve = basis.BaseCurve.GetTransportObject(),
                        BuildDate = basis.BuildDate,
                        Curve = basis.Curve.GetTransportObject(),
                        CurveType = basis.CurveType,
                        DiscountCurve = ((IrCurve)basis.DiscountCurve).GetTransportObject(),
                        Instruments = basis.Instruments.Select(x => x.GetTransportObject()).ToList(),
                        PillarLabels = basis.PillarLabels,
                        Pillars = basis.Pillars
                    }
                });

            case ContangoPriceCurve c:
                return(new TO_PriceCurve
                {
                    ContangoPriceCurve = new TO_ContangoPriceCurve
                    {
                        AssetId = c.AssetId,
                        Currency = c.Currency,
                        Name = c.Name,
                        SpotCalendar = c.SpotCalendar?.Name,
                        SpotLag = c.SpotLag.ToString(),
                        BuildDate = c.BuildDate,
                        PillarLabels = c.PillarLabels,
                        PillarDates = c.PillarDates,
                        Basis = c.Basis,
                        Contangos = c.Contangos,
                        Spot = c.Spot,
                        SpotDate = c.SpotDate
                    }
                });

            default:
                throw new Exception($"Unable to serialize price curve of type {curve.GetType()}");
            }
        }