Beispiel #1
0
        public void TestFixedLegsZARUSD()
        {
            Date[] cfDates = { new Date(2016, 12, 23), new Date(2017, 03, 23) };

            var legZAR = new FixedLeg(TestHelpers.ZAR, cfDates, new double[] { -16000000, -16000000 }, new[] { 0.07, 0.07 },
                                      new[] { 0.25, 0.25 });
            var legUSD = new FixedLeg(TestHelpers.USD, cfDates, new double[] { 1000000, 1000000 }, new[] { 0.01, 0.01 },
                                      new[] { 0.25, 0.25 });

            // Set up the model
            var valueDate = new Date(2016, 9, 23);

            Date[]              dates         = { new Date(2016, 9, 23), new Date(2026, 9, 23) };
            double[]            rates         = { 0.0725, 0.0725 };
            double[]            basisRates    = { 0.0735, 0.0735 };
            double[]            usdRates      = { 0.01, 0.012 };
            IDiscountingSource  discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate, dates, rates);
            IDiscountingSource  zarBasis      = new DatesAndRates(TestHelpers.ZAR, valueDate, dates, basisRates);
            IDiscountingSource  usdCurve      = new DatesAndRates(TestHelpers.USD, valueDate, dates, usdRates);
            IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, TestHelpers.Jibar3M, dates, rates);
            IFXSource           fxSource      = new FXForecastCurve(TestHelpers.USDZAR, 13.66, usdCurve, zarBasis);
            var curveSim = new DeterminsiticCurves(discountCurve);

            curveSim.AddRateForecast(forecastCurve);
            curveSim.AddFXForecast(fxSource);
            var coordinator = new Coordinator(curveSim, new List <Simulator>(), 1);

            // Run the valuation
            var value    = coordinator.Value(new Product[] { legZAR, legUSD }, valueDate);
            var refValue = -477027.31; // See GeneralSwapTest.xlsx

            Assert.AreEqual(refValue, value, 0.01);
        }
        public static double LinearRateModel_FixedLegValue(string baseName, string fixedLegName)
        {
            LinearRateModel model    = ObjectMap.LinearRateModels[baseName];
            FixedLeg        fixedLeg = ObjectMap.FixedLegs[fixedLegName];

            return(model.ValueFixedLeg(fixedLeg));
        }
Beispiel #3
0
 public IFundingInstrument Clone() => new IrSwap
 {
     BasisFixed        = BasisFixed,
     BasisFloat        = BasisFloat,
     Currency          = Currency,
     Counterparty      = Counterparty,
     DiscountCurve     = DiscountCurve,
     EndDate           = EndDate,
     FixedLeg          = FixedLeg.Clone(),
     FloatLeg          = FloatLeg.Clone(),
     FlowScheduleFixed = FlowScheduleFixed.Clone(),
     FlowScheduleFloat = FlowScheduleFloat.Clone(),
     ForecastCurve     = ForecastCurve,
     NDates            = NDates,
     Notional          = Notional,
     ParRate           = ParRate,
     PillarDate        = PillarDate,
     ResetDates        = ResetDates,
     ResetFrequency    = ResetFrequency,
     SolveCurve        = SolveCurve,
     StartDate         = StartDate,
     SwapTenor         = SwapTenor,
     SwapType          = SwapType,
     TradeId           = TradeId,
     RateIndex         = RateIndex,
     PortfolioName     = PortfolioName,
     HedgingSet        = HedgingSet
 };
Beispiel #4
0
        public double ModelValue(IMarketCondition market, MktInstrumentCalibMethod calibMethod = MktInstrumentCalibMethod.Default)
        {
            var fixedPayRate = FixedLeg.GetPaymentRates(market);
            var fixedRate    = ((FixedCoupon)FixedLeg.Coupon).FixedRate;

            var discountCurve         = market.DiscountCurve.Value;
            var fixedLegAccumulatedDf = fixedPayRate
                                        .Where(x => x.Item1 > market.ValuationDate)
                                        .Sum(x => (x.Item2 / fixedRate * discountCurve.GetDf(x.Item1)));

            if (calibMethod == MktInstrumentCalibMethod.Default || calibMethod == MktInstrumentCalibMethod.IrsFloatingPvConst1)
            {
                // fair rate
                return((1.0 * discountCurve.GetDf(StartDate) - 1.0 * discountCurve.GetDf(fixedPayRate.Last().Item1)) / fixedLegAccumulatedDf);
            }
            else if (calibMethod == MktInstrumentCalibMethod.IrsFloatingPvReal)
            {
                var floatingPayRate = FloatingLeg.GetPaymentRates(market);
                var floatingPv      = floatingPayRate
                                      .Where(x => x.Item1 > market.ValuationDate)
                                      .Sum(x => x.Item2 * market.DiscountCurve.Value.GetDf(x.Item1));
                return(floatingPv / fixedLegAccumulatedDf);
            }

            throw new PricingLibraryException("Calibration method " + calibMethod + " is not recognized");
        }
        public static void PlainVanillaSwap_Make(string baseName, string fixedLegName, string floatLegName, int tradeSign)
        {
            FixedLeg fixedLeg = ObjectMap.FixedLegs[fixedLegName];
            FloatLeg floatLeg = ObjectMap.FloatLegs[floatLegName];
            IrSwap   swap     = new MasterThesis.IrSwap(floatLeg, fixedLeg, tradeSign);

            ObjectMap.LinearRateInstruments[baseName] = swap;
            ObjectMap.IrSwaps[baseName] = swap;
        }
        private InterestRateSwap CreateSwap(DateTimeOffset startDate, DateTimeOffset maturityDate, decimal fixedRate, FlowConventionName flowConventionName, FlowConventionName indexConventionName, string fixedLegDirection = "Pay", decimal notional = 100m)
        {
            string floatingLegDirection = fixedLegDirection == "Pay" ? "Receive" : "Pay";

            // CREATE the leg definitions
            var fixedLegDef = new LegDefinition(
                rateOrSpread: fixedRate, // fixed leg rate (swap rate)
                stubType: "Front",
                payReceive: fixedLegDirection,
                notionalExchangeType: "None",
                conventionName: flowConventionName
                );

            var floatLegDef = new LegDefinition(
                rateOrSpread: 0,
                stubType: "Front",
                payReceive: floatingLegDirection,
                notionalExchangeType: "None",
                conventionName: flowConventionName,
                indexConventionName: indexConventionName
                );

            // CREATE the fixed leg
            var fixedLeg = new FixedLeg(
                notional: notional,
                startDate: startDate,
                maturityDate: maturityDate,
                legDefinition: fixedLegDef,
                instrumentType: LusidInstrument.InstrumentTypeEnum.FixedLeg
                );

            // CREATE the floating leg
            var floatLeg = new FloatingLeg(
                notional: notional,
                startDate: startDate,
                maturityDate: maturityDate,
                legDefinition: floatLegDef,
                instrumentType: LusidInstrument.InstrumentTypeEnum.FloatingLeg
                );

            var irs = new InterestRateSwap(
                startDate: startDate,
                maturityDate: maturityDate,
                legs: new List <InstrumentLeg>
            {
                floatLeg,
                fixedLeg
            },
                instrumentType: LusidInstrument.InstrumentTypeEnum.InterestRateSwap
                );

            return(irs);
        }
Beispiel #7
0
        public void SetCalibrationDate(Date calibrationDate)
        {
            _floatLeg = SwapFactory.CreateFloatLeg(calibrationDate, _tenor, _index, _spread);
            _fixedLeg = SwapFactory.CreateFixedLeg(calibrationDate, _tenor, _index, _fixedRate);
            _floatLeg.SetValueDate(calibrationDate);
            _fixedLeg.SetValueDate(calibrationDate);
            _endDate = _floatLeg.GetCashflowDates(_index.Currency).Max();
            var otherEndDate = _fixedLeg.GetCashflowDates(_index.Currency).Max();

            if (_endDate < otherEndDate)
            {
                _endDate = otherEndDate;
            }
        }
Beispiel #8
0
        public Cashflow[] GetCashflows(IMarketCondition market, bool netted = true)
        {
            var fixedCashflows    = FixedLeg.GetCashflows(market);
            var floatingCashflows = FloatingLeg.GetCashflows(market);

            if (!netted)
            {
                return(fixedCashflows.Union(floatingCashflows).ToArray());
            }
            else
            {
                return(fixedCashflows.Union(floatingCashflows)
                       .GroupBy(cf => new { cf.AccrualStartDate, cf.AccrualEndDate, cf.PaymentDate, cf.PaymentCurrency })
                       .Select(item => new Cashflow(item.Key.AccrualStartDate, item.Key.AccrualEndDate, item.Key.PaymentDate, item.Sum(entry => entry.PaymentAmount), item.Key.PaymentCurrency, CashflowType.Coupon, false, market.DiscountCurve.Value.GetDf(item.Key.PaymentDate), null))
                       .OrderBy(cf => cf.PaymentDate)
                       .ToArray());
            }
        }
Beispiel #9
0
 public static object _CreateFixedLeg(string objectName,
                                      object[,] currency,
                                      object[,] paymentDates,
                                      object[,] notionals,
                                      object[,] rates,
                                      object[,] accrualFractions)
 {
     try
     {
         Currency _currency         = XU.GetCurrency0D(currency, "currency");
         Date[]   _paymentDates     = XU.GetDate1D(paymentDates, "paymentDates");
         Double[] _notionals        = XU.GetDouble1D(notionals, "notionals");
         Double[] _rates            = XU.GetDouble1D(rates, "rates");
         Double[] _accrualFractions = XU.GetDouble1D(accrualFractions, "accrualFractions");
         FixedLeg _result           = XLRates.CreateFixedLeg(_currency, _paymentDates, _notionals, _rates, _accrualFractions);
         return(XU.AddObject(objectName, _result));
     }
     catch (Exception e)
     {
         return(XU.Error0D(e));
     }
 }
Beispiel #10
0
        public void DemonstrateCreationOfSwaption()
        {
            // CREATE an Interest Rate Swap (IRS)
            var startDate    = new DateTimeOffset(2020, 2, 7, 0, 0, 0, TimeSpan.Zero);
            var maturityDate = new DateTimeOffset(2030, 2, 7, 0, 0, 0, TimeSpan.Zero);

            // CREATE the flow conventions, index convention for swap
            var flowConventions = new FlowConventions(
                scope: null,
                code: null,
                currency: "GBP",
                paymentFrequency: "6M",
                rollConvention: FlowConventions.RollConventionEnum.MF,
                dayCountConvention: FlowConventions.DayCountConventionEnum.Act365,
                holidayCalendars: new List <string>(),
                settleDays: 2,
                resetDays: 2
                );

            var idxConvention = new IndexConvention(
                code: "GbpLibor6m",
                publicationDayLag: 0,
                currency: "GBP",
                paymentTenor: "6M",
                dayCountConvention: IndexConvention.DayCountConventionEnum.Act365,
                fixingReference: "BP00"
                );

            // CREATE the leg definitions
            var fixedLegDef = new LegDefinition(
                rateOrSpread: 0.05m, // fixed leg rate (swap rate)
                stubType: LegDefinition.StubTypeEnum.Front,
                payReceive: LegDefinition.PayReceiveEnum.Pay,
                notionalExchangeType: LegDefinition.NotionalExchangeTypeEnum.None,
                conventions: flowConventions
                );

            var floatLegDef = new LegDefinition(
                rateOrSpread: 0.002m, // float leg spread over curve rate, often zero
                stubType: LegDefinition.StubTypeEnum.Front,
                payReceive: LegDefinition.PayReceiveEnum.Pay,
                notionalExchangeType: LegDefinition.NotionalExchangeTypeEnum.None,
                conventions: flowConventions,
                indexConvention: idxConvention
                );

            // CREATE the fixed leg
            var fixedLeg = new FixedLeg(
                notional: 100m,
                startDate: startDate,
                maturityDate: maturityDate,
                legDefinition: fixedLegDef,
                instrumentType: LusidInstrument.InstrumentTypeEnum.FixedLeg
                );

            // CREATE the floating leg
            var floatLeg = new FloatingLeg(
                notional: 100m,
                startDate: startDate,
                maturityDate: maturityDate,
                legDefinition: floatLegDef,
                instrumentType: LusidInstrument.InstrumentTypeEnum.FloatingLeg
                );

            var swap = new InterestRateSwap(
                startDate: startDate,
                maturityDate: maturityDate,
                legs: new List <InstrumentLeg>
            {
                floatLeg,
                fixedLeg
            },
                instrumentType: LusidInstrument.InstrumentTypeEnum.InterestRateSwap
                );

            // CREATE swaption to upsert to LUSID
            var swaption = new InterestRateSwaption(
                startDate: new DateTimeOffset(2020, 1, 15, 0, 0, 0, TimeSpan.Zero),
                payOrReceiveFixed: InterestRateSwaption.PayOrReceiveFixedEnum.Pay,
                deliveryMethod: InterestRateSwaption.DeliveryMethodEnum.Cash,
                swap: swap,
                instrumentType: LusidInstrument.InstrumentTypeEnum.InterestRateSwaption);

            // ASSERT that it was created
            Assert.That(swaption, Is.Not.Null);
            Assert.That(swaption.Swap, Is.Not.Null);

            // CAN NOW UPSERT TO LUSID
            string uniqueId = "id-swaption-1";

            UpsertOtcToLusid(swaption, "some-name-for-this-swaption", uniqueId);

            // CAN NOW QUERY FROM LUSID
            var retrieved = QueryOtcFromLusid(uniqueId);

            Assert.That(retrieved.InstrumentType == LusidInstrument.InstrumentTypeEnum.InterestRateSwaption);
            var roundTripSwaption = retrieved as InterestRateSwaption;

            Assert.That(roundTripSwaption, Is.Not.Null);
            Assert.That(roundTripSwaption.DeliveryMethod, Is.EqualTo(swaption.DeliveryMethod));
            Assert.That(roundTripSwaption.StartDate, Is.EqualTo(swaption.StartDate));
            Assert.That(roundTripSwaption.PayOrReceiveFixed, Is.EqualTo(swaption.PayOrReceiveFixed));
            Assert.That(roundTripSwaption.Swap, Is.Not.Null);
            Assert.That(roundTripSwaption.Swap.InstrumentType, Is.EqualTo(LusidInstrument.InstrumentTypeEnum.InterestRateSwap));
        }
Beispiel #11
0
 public double GetAccruedInterest(Date calcDate, IMarketCondition market, bool isEod = true)
 {
     return(FixedLeg.GetAccruedInterest(calcDate, market, isEod) + FloatingLeg.GetAccruedInterest(calcDate, market, isEod));
 }
Beispiel #12
0
 public ICalibrationSupportedInstrument Stretch(Date startDate, Date maturityDate)
 {
     return(new InterestRateSwap(FixedLeg.Stretch(startDate, maturityDate), FloatingLeg.Stretch(startDate, maturityDate), SwapDirection));
 }
Beispiel #13
0
 public ICalibrationSupportedInstrument Bump(int bp)
 {
     return(new InterestRateSwap(FixedLeg.Bump(bp), FloatingLeg, SwapDirection));
 }
        public void DemonstrateCreationOfSwap()
        {
            // CREATE an Interest Rate Swap (IRS) (that can then be upserted into LUSID)
            var startDate    = new DateTimeOffset(2020, 2, 7, 0, 0, 0, TimeSpan.Zero);
            var maturityDate = new DateTimeOffset(2030, 2, 7, 0, 0, 0, TimeSpan.Zero);

            // CREATE the flow conventions, index convention
            var flowConventions = new FlowConventions(
                scope: null,
                code: null,
                currency: "GBP",
                paymentFrequency: "6M",
                rollConvention: "MF",
                dayCountConvention: "Act365",
                paymentCalendars: new List <string>(),
                resetCalendars: new List <string>(),
                settleDays: 2,
                resetDays: 2
                );

            var idxConvention = new IndexConvention(
                code: "GbpLibor6m",
                publicationDayLag: 0,
                currency: "GBP",
                paymentTenor: "6M",
                dayCountConvention: "Act365",
                fixingReference: "BP00"
                );

            // CREATE the leg definitions
            var fixedLegDef = new LegDefinition(
                rateOrSpread: 0.05m, // fixed leg rate (swap rate)
                stubType: "Front",
                payReceive: "Pay",
                notionalExchangeType: "None",
                conventions: flowConventions
                );

            var floatLegDef = new LegDefinition(
                rateOrSpread: 0.002m, // float leg spread over curve rate, often zero
                stubType: "Front",
                payReceive: "Pay",
                notionalExchangeType: "None",
                conventions: flowConventions,
                indexConvention: idxConvention
                );

            // CREATE the fixed leg
            var fixedLeg = new FixedLeg(
                notional: 100m,
                startDate: startDate,
                maturityDate: maturityDate,
                legDefinition: fixedLegDef,
                instrumentType: LusidInstrument.InstrumentTypeEnum.FixedLeg
                );

            // CREATE the floating leg
            var floatLeg = new FloatingLeg(
                notional: 100m,
                startDate: startDate,
                maturityDate: maturityDate,
                legDefinition: floatLegDef,
                instrumentType: LusidInstrument.InstrumentTypeEnum.FloatingLeg
                );

            var irs = new InterestRateSwap(
                startDate: startDate,
                maturityDate: maturityDate,
                legs: new List <InstrumentLeg>
            {
                floatLeg,
                fixedLeg
            },
                instrumentType: LusidInstrument.InstrumentTypeEnum.InterestRateSwap
                );

            // ASSERT that it was created
            Assert.That(irs, Is.Not.Null);

            // CAN NOW UPSERT TO LUSID
            string uniqueId = "id-swap-1";

            UpsertOtcToLusid(irs, "some-name-for-this-swap", uniqueId);

            // CAN NOW QUERY FROM LUSID
            var retrieved = QueryOtcFromLusid(uniqueId);

            Assert.That(retrieved.InstrumentType == LusidInstrument.InstrumentTypeEnum.InterestRateSwap);
            var retrSwap = retrieved as InterestRateSwap;

            Assert.That(retrSwap, Is.Not.Null);
            Assert.That(retrSwap.MaturityDate, Is.EqualTo(irs.MaturityDate));
            Assert.That(retrSwap.StartDate, Is.EqualTo(irs.StartDate));
            Assert.That(retrSwap.Legs.Count, Is.EqualTo(irs.Legs.Count));
        }