Пример #1
0
        public IRZeroCurve GenerateCurve(BusinessDay currentDate)
        {
            if (_curve_dictionary.ContainsKey(currentDate.DateTime))
            {
                return(_curve_dictionary[currentDate.DateTime]);
            }

            IRZeroCurve curve = new IRZeroCurve(currentDate);

            foreach (InterestRate rate in _interest_rate_lst)
            {
                try
                {
                    double value = rate[currentDate.DateTime, TimeSeriesType.Last, DataProvider.DefaultProvider, TimeSeriesRollType.Exact] / 100.0;

                    if (double.IsNaN(value))
                    {
                        value = rate[Calendar.Close(currentDate.DateTime), TimeSeriesType.Last, DataProvider.DefaultProvider, TimeSeriesRollType.Last] / 100.0;
                    }

                    if (!double.IsNaN(value))
                    {
                        // Console.WriteLine("Generate Curve: " + this._currency + " " + currentDate.DateTime + " " + rate + " " + value);

                        if (rate.InstrumentType == InstrumentType.Deposit)
                        {
                            Deposit deposit = (rate as Deposit);
                            curve.AddCashFlow(value, deposit.MaturityType, deposit.Maturity, deposit.MaturityType, deposit.Maturity, deposit.DayCountConvention);

                            // Console.WriteLine("     Add deposit:" + rate + " " + deposit.MaturityType + " " + deposit.Maturity + " " + deposit.MaturityType + " " + deposit.Maturity + " " + deposit.DayCountConvention);
                        }
                        else if (rate.InstrumentType == InstrumentType.InterestRateSwap)
                        {
                            InterestRateSwap swap = (rate as InterestRateSwap);
                            curve.AddCashFlow(value, swap.FixedFrequencyType, swap.FixedFrequency, swap.MaturityType, swap.Maturity, swap.FixedDayCountConvention);
                            // Console.WriteLine("     Add Swap:" + rate + " " + swap.FixedFrequency + " " + swap.MaturityType + " " + swap.Maturity + " " + swap.FixedDayCountConvention);
                        }
                    }
                }
                catch { }
            }

            curve.BootStrap();
            if (_curve_dictionary.ContainsKey(currentDate.DateTime))
            {
                _curve_dictionary[currentDate.DateTime] = curve;
            }
            else
            {
                _curve_dictionary.TryAdd(currentDate.DateTime, curve);
            }

            return(curve);
        }
        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);
        }
Пример #3
0
        public InterestRateSwap FindInterestRateSwap(InterestRate instrument)
        {
            lock (objLock)
            {
                if (_swaps.ContainsKey(instrument.ID))
                {
                    return(_swaps[instrument.ID]);
                }

                if (instrument.InstrumentType != InstrumentType.InterestRateSwap)
                {
                    throw new Exception("Instrument is not an Interest Rate Swap");
                }

                string    tableName    = _interestRateSwapTableName;
                string    searchString = "ID = " + instrument.ID;
                string    targetString = null;
                DataTable table        = Database.DB["Quant"].GetDataTable(tableName, targetString, searchString);

                DataRowCollection rows = table.Rows;
                if (rows.Count == 0)
                {
                    return(null);
                }

                DataRow r = rows[0];

                int floatFrequency = GetValue <int>(r, "FloatFrequency");
                InterestRateTenorType floatFrequencyType      = (InterestRateTenorType)GetValue <int>(r, "FloatFrequencyType");
                DayCountConvention    floatDayCountConvention = (DayCountConvention)GetValue <int>(r, "FloatDayCountConvention");
                int fixedFrequency = GetValue <int>(r, "FixedFrequency");
                InterestRateTenorType fixedFrequencyType      = (InterestRateTenorType)GetValue <int>(r, "FixedFrequencyType");
                DayCountConvention    fixedDayCountConvention = (DayCountConvention)GetValue <int>(r, "FixedDayCountConvention");
                int effective = GetValue <int>(r, "Effective");

                InterestRateSwap swap = new InterestRateSwap(instrument, floatFrequency, floatFrequencyType, floatDayCountConvention, fixedFrequency, fixedFrequencyType, fixedDayCountConvention, effective);

                _swaps.Add(swap.ID, swap);
                if (_mainTables.ContainsKey(swap.ID))
                {
                    _mainTables[swap.ID] = table;
                }
                else
                {
                    _mainTables.Add(swap.ID, table);
                }

                return(swap);
            }
        }
Пример #4
0
        protected void OnVisit(InterestRateSwap instrument)
        {
            string str1, str2;

            if (instrument.IsPayFloating)
            {
                str1 = string.Format("LIBOR{0:+#0;-#0;+#0}bps", instrument.FloatingSpread * 10000);
                str2 = string.Format("{0:#0.0%}", instrument.FixedRate);
            }
            else
            {
                str1 = string.Format("{0:#0.0%}", instrument.FixedRate);
                str2 = string.Format("LIBOR{0:+#0;-#0;+0}bps", instrument.FloatingSpread * 10000);
            }
            _result = string.Format("N: {0}, Pay: {1}, Rec: {2}", instrument.Nominal, str1, str2);
        }
Пример #5
0
        public void SetProperty(InterestRateSwap swap, string name, object value)
        {
            lock (objLock)
            {
                DataTable table = _mainTables[swap.ID];

                if (table != null)
                {
                    DataRowCollection rows = table.Rows;
                    if (rows.Count == 0)
                        return;

                    DataRow row = rows[0];
                    row[name] = value;
                    Database.DB["Kernel"].UpdateDataTable(table);
                }
            }
        }
Пример #6
0
        public void Remove(InterestRateSwap swap)
        {
            lock (objLock)
            {
                _swaps.Remove(swap.ID);

                DataTable table = _mainTables[swap.ID];

                if (table != null)
                {
                    DataRowCollection rows = table.Rows;
                    if (rows.Count == 0)
                        return;

                    DataRow row = rows[0];
                    row.Delete();
                    Database.DB["Kernel"].UpdateDataTable(table);
                }
            }
        }
Пример #7
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));
        }
Пример #8
0
        protected ICalibrationSupportedInstrument CreateIrsInstrument(RateMktData rateMktData,
                                                                      out MktInstrumentCalibMethod calibMethod)
        {
            MktIrsJson irsJson = null;
            ICalibrationSupportedInstrument irs = null;

            if (rateMktData.TradeInfo != null)
            {
                var irsInfo = (InterestRateSwapInfo)rateMktData.TradeInfo;
                var vf      = new InterestRateSwapVf(irsInfo);
                irs     = vf.GenerateInstrument();
                irsJson = MktInstrumentIrsRule.MktIrsRule[irsInfo.Index.ToIndexType()];
            }
            else
            {
                irsJson = MktInstrumentIrsRule.MktIrsRule[rateMktData.IndexType.ToIndexType()];
                var irsInfo  = irsJson.InterestRateSwapInfo;
                var calendar = irsInfo.Calendar.ToCalendarImpl();

                var startDate    = calendar.NextBizDay(Market.ReferenceDate);
                var isTernor     = rateMktData.IsTerm();
                var tenor        = isTernor ? rateMktData.Tenor : null;
                var maturityDate = isTernor ? new Term(tenor).Next(startDate) : new Date(DateTime.Parse(rateMktData.Tenor));

                var fixedLeg = new SwapLeg(startDate,
                                           maturityDate,
                                           -1.0,
                                           false,
                                           irsInfo.Currency.ToCurrencyCode(),
                                           new FixedCoupon(rateMktData.Rate),
                                           calendar,
                                           irsInfo.FixedLegFreq.ToFrequency(),
                                           irsInfo.FixedLegStub.ToStub(),
                                           irsInfo.FixedLegDC.ToDayCountImpl(),
                                           irsInfo.FixedLegBD.ToBda()
                                           );

                var floatingLegFrequency    = irsInfo.FloatingLegFreq.ToFrequency();
                var floatingCouponResetTerm = new Term(irsInfo.ResetTerm);
                if (floatingCouponResetTerm.Equals(floatingLegFrequency.GetTerm()))
                {
                    floatingCouponResetTerm = null;
                }
                var floatingCoupon =
                    new FloatingCoupon(
                        new Index(rateMktData.IndexType.ToIndexType(), 1, irsInfo.ResetCompound.ToCouponCompound()),
                        calendar,
                        irsInfo.FloatingLegDC.ToDayCountImpl(),
                        0.0,
                        floatingCouponResetTerm,
                        irsInfo.ResetStub.ToStub(),
                        irsInfo.ResetBD.ToBda(),
                        new DayGap(irsInfo.ResetToFixingGap));
                var floatingLeg = new SwapLeg(startDate,
                                              maturityDate,
                                              1.0,
                                              false,
                                              irsInfo.Currency.ToCurrencyCode(),
                                              floatingCoupon,
                                              calendar,
                                              irsInfo.FloatingLegFreq.ToFrequency(),
                                              irsInfo.FloatingLegStub.ToStub(),
                                              irsInfo.FloatingLegDC.ToDayCountImpl(),
                                              irsInfo.FloatingLegBD.ToBda()
                                              );
                irs = new InterestRateSwap(fixedLeg, floatingLeg, SwapDirection.Payer, tenor);
            }

            calibMethod = irsJson.CalibrationMethod.ToCalibMethod();
            return(irs);
        }
Пример #9
0
 protected void OnVisit(InterestRateSwap instrument)
 {
     _result = string.Format("Interest Rate Swap - {0}", instrument.IsPayFloating ? "Pay Floating" : "Pay Fixed");
 }
        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));
        }