Exemple #1
0
        public void testFairRate()
        {
            // Testing Eonia-swap calculation of fair fixed rate...

            CommonVars vars = new CommonVars();

            Period[] lengths = new Period[] { new Period(1, TimeUnit.Years), new Period(2, TimeUnit.Years), new Period(5, TimeUnit.Years), new Period(10, TimeUnit.Years), new Period(20, TimeUnit.Years) };
            double[] spreads = { -0.001, -0.01, 0.0, 0.01, 0.001 };

            for (int i = 0; i < lengths.Length; i++)
            {
                for (int j = 0; j < spreads.Length; j++)
                {
                    OvernightIndexedSwap swap = vars.makeSwap(lengths[i], 0.0, spreads[j]);

                    swap = vars.makeSwap(lengths[i], swap.fairRate().Value, spreads[j]);

                    if (Math.Abs(swap.NPV()) > 1.0e-10)
                    {
                        QAssert.Fail("recalculating with implied rate:\n"
                                     + "    length: " + lengths[i] + " \n"
                                     + "    floating spread: "
                                     + (spreads[j]) + "\n"
                                     + "    swap value: " + swap.NPV());
                    }
                }
            }
        }
Exemple #2
0
        public void testFairSpread()
        {
            // Testing Eonia-swap calculation of fair floating spread...
            CommonVars vars = new CommonVars();

            Period[] lengths = { new Period(1,  TimeUnit.Years),
                                 new Period(2,  TimeUnit.Years),
                                 new Period(5,  TimeUnit.Years),
                                 new Period(10, TimeUnit.Years),
                                 new Period(20, TimeUnit.Years) };
            double[] rates = { 0.04, 0.05, 0.06, 0.07 };

            for (int i = 0; i < lengths.Length; i++)
            {
                for (int j = 0; j < rates.Length; j++)
                {
                    OvernightIndexedSwap swap = vars.makeSwap(lengths[i], rates[j], 0.0);
                    double?fairSpread         = swap.fairSpread();
                    swap = vars.makeSwap(lengths[i], rates[j], fairSpread.Value);

                    if (Math.Abs(swap.NPV()) > 1.0e-10)
                    {
                        QAssert.Fail("Recalculating with implied spread:" +
                                     "\n     length: " + lengths[i] +
                                     "\n fixed rate: " + rates[j] +
                                     "\nfair spread: " + fairSpread +
                                     "\n swap value: " + swap.NPV());
                    }
                }
            }
        }
        public OvernightIndexedSwap swap()
        {
            OvernightIndexedSwap ret = new OvernightIndexedSwap(NQuantLibcPINVOKE.OISRateHelper_swap(swigCPtr), true);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #4
0
        public OvernightIndexedSwap swap()
        {
            global::System.IntPtr cPtr = NQuantLibcPINVOKE.OISRateHelper_swap(swigCPtr);
            OvernightIndexedSwap  ret  = (cPtr == global::System.IntPtr.Zero) ? null : new OvernightIndexedSwap(cPtr, true);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
        public OvernightIndexedSwap underlyingSwap(Date fixingDate)
        {
            global::System.IntPtr cPtr = NQuantLibcPINVOKE.OvernightIndexedSwapIndex_underlyingSwap(swigCPtr, Date.getCPtr(fixingDate));
            OvernightIndexedSwap  ret  = (cPtr == global::System.IntPtr.Zero) ? null : new OvernightIndexedSwap(cPtr, true);

            if (NQuantLibcPINVOKE.SWIGPendingException.Pending)
            {
                throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Exemple #6
0
        public void testCachedValue()
        {
            // Testing Eonia-swap calculation against cached value...
            CommonVars vars = new CommonVars();

            Settings.setEvaluationDate(vars.today);
            vars.settlement = vars.calendar.advance(vars.today, vars.settlementDays, TimeUnit.Days);
            double flat = 0.05;

            vars.eoniaTermStructure.linkTo(Utilities.flatRate(vars.settlement, flat, new Actual360()));
            double fixedRate          = Math.Exp(flat) - 1;
            OvernightIndexedSwap swap = vars.makeSwap(new Period(1, TimeUnit.Years), fixedRate, 0.0);
            double cachedNPV          = 0.001730450147;
            double tolerance          = 1.0e-11;

            if (Math.Abs(swap.NPV() - cachedNPV) > tolerance)
            {
                QAssert.Fail("\nfailed to reproduce cached swap value:" +
                             "\ncalculated: " + swap.NPV() +
                             "\n  expected: " + cachedNPV +
                             "\n tolerance:" + tolerance);
            }
        }
Exemple #7
0
        public void testBootstrap()
        {
            // Testing Eonia-swap curve building...
            CommonVars vars = new CommonVars();

            List <RateHelper> eoniaHelpers  = new List <RateHelper>();
            List <RateHelper> swap3mHelpers = new List <RateHelper>();

            IborIndex euribor3m = new Euribor3M();
            Eonia     eonia     = new Eonia();

            for (int i = 0; i < depositData.Length; i++)
            {
                double         rate   = 0.01 * depositData[i].rate;
                SimpleQuote    simple = new SimpleQuote(rate);
                Handle <Quote> quote  = new Handle <Quote>(simple);

                Period     term   = new Period(depositData[i].n, depositData[i].unit);
                RateHelper helper = new DepositRateHelper(quote,
                                                          term,
                                                          depositData[i].settlementDays,
                                                          euribor3m.fixingCalendar(),
                                                          euribor3m.businessDayConvention(),
                                                          euribor3m.endOfMonth(),
                                                          euribor3m.dayCounter());


                if (term <= new Period(2, TimeUnit.Days))
                {
                    eoniaHelpers.Add(helper);
                }
                if (term <= new Period(3, TimeUnit.Months))
                {
                    swap3mHelpers.Add(helper);
                }
            }


            for (int i = 0; i < fraData.Length; i++)
            {
                double         rate   = 0.01 * fraData[i].rate;
                SimpleQuote    simple = new SimpleQuote(rate);
                Handle <Quote> quote  = new Handle <Quote>(simple);
                RateHelper     helper = new FraRateHelper(quote,
                                                          fraData[i].nExpiry,
                                                          fraData[i].nMaturity,
                                                          fraData[i].settlementDays,
                                                          euribor3m.fixingCalendar(),
                                                          euribor3m.businessDayConvention(),
                                                          euribor3m.endOfMonth(),
                                                          euribor3m.dayCounter());
                swap3mHelpers.Add(helper);
            }

            for (int i = 0; i < eoniaSwapData.Length; i++)
            {
                double         rate   = 0.01 * eoniaSwapData[i].rate;
                SimpleQuote    simple = new SimpleQuote(rate);
                Handle <Quote> quote  = new Handle <Quote>(simple);
                Period         term   = new Period(eoniaSwapData[i].n, eoniaSwapData[i].unit);
                RateHelper     helper = new OISRateHelper(eoniaSwapData[i].settlementDays,
                                                          term,
                                                          quote,
                                                          eonia);
                eoniaHelpers.Add(helper);
            }


            for (int i = 0; i < swapData.Length; i++)
            {
                double         rate   = 0.01 * swapData[i].rate;
                SimpleQuote    simple = new SimpleQuote(rate);
                Handle <Quote> quote  = new Handle <Quote>(simple);
                Period         tenor  = new Period(swapData[i].nIndexUnits, swapData[i].indexUnit);
                Period         term   = new Period(swapData[i].nTermUnits, swapData[i].termUnit);

                RateHelper helper = new SwapRateHelper(quote,
                                                       term,
                                                       vars.calendar,
                                                       vars.fixedSwapFrequency,
                                                       vars.fixedSwapConvention,
                                                       vars.fixedSwapDayCount,
                                                       euribor3m);
                if (tenor == new Period(3, TimeUnit.Months))
                {
                    swap3mHelpers.Add(helper);
                }
            }


            PiecewiseYieldCurve <Discount, LogLinear> eoniaTS = new PiecewiseYieldCurve <Discount, LogLinear>(vars.today,
                                                                                                              eoniaHelpers,
                                                                                                              new Actual365Fixed());

            PiecewiseYieldCurve <Discount, LogLinear> swapTS = new PiecewiseYieldCurve <Discount, LogLinear>(vars.today,
                                                                                                             swap3mHelpers,
                                                                                                             new Actual365Fixed());

            vars.eoniaTermStructure.linkTo(eoniaTS);

            // test curve consistency
            double tolerance = 1.0e-10;

            for (int i = 0; i < eoniaSwapData.Length; i++)
            {
                double expected           = eoniaSwapData[i].rate;
                Period term               = new Period(eoniaSwapData[i].n, eoniaSwapData[i].unit);
                OvernightIndexedSwap swap = vars.makeSwap(term, 0.0, 0.0);
                double?calculated         = 100.0 * swap.fairRate();

                if (Math.Abs(expected - calculated.Value) > tolerance)
                {
                    QAssert.Fail("curve inconsistency:\n"
                                 + "    swap length:     " + term + "\n"
                                 + "    quoted rate:     " + expected + "\n"
                                 + "    calculated rate: " + calculated);
                }
            }
        }
Exemple #8
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(OvernightIndexedSwap obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
        public static object eqInstIROISSwap(
            [ExcelArgument(Description = "trade id ")] string tradeid,
            [ExcelArgument(Description = "payer/receiver (1/0) ")] bool ispayer,
            [ExcelArgument(Description = "notional ")] double notional,
            [ExcelArgument(Description = "fixed rate ")] double fixedRate,
            [ExcelArgument(Description = "start date ")] DateTime startdate,
            [ExcelArgument(Description = " (String) forward start month, e.g. 7D, 3M, 7Y ")] string Tenor,
            [ExcelArgument(Description = "id of overnight index ")] string indexid,
            [ExcelArgument(Description = "floating leg spread ")] double spread,
            [ExcelArgument(Description = "id of discount curve ")] string discountId,
            [ExcelArgument(Description = "trigger ")] object trigger)
        {
            if (ExcelUtil.CallFromWizard())
            {
                return("");
            }

            string callerAddress = "";

            callerAddress = ExcelUtil.getActiveCellAddress();

            try
            {
                Xl.Range rng = ExcelUtil.getActiveCellRange();

                // by default
                // endOfMonth_(1*Months<=swapTenor && swapTenor<=2*Years ? true : false),
                bool end_of_month             = true;
                EliteQuant.DayCounter fixeddc = new EliteQuant.Actual360();

                if (!indexid.Contains('@'))
                {
                    indexid = "IDX@" + indexid;
                }
                OvernightIndex idx = OHRepository.Instance.getObject <OvernightIndex>(indexid);
                if (!discountId.Contains('@'))
                {
                    discountId = "CRV@" + discountId;
                }
                YieldTermStructure       discountcurve = OHRepository.Instance.getObject <YieldTermStructure>(discountId);
                YieldTermStructureHandle dch           = new YieldTermStructureHandle(discountcurve);

                EliteQuant.Period tenor_ = EliteQuant.EQConverter.ConvertObject <EliteQuant.Period>(Tenor);
                EliteQuant.Date   sdate  = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(startdate);
                EliteQuant.Date   fdate  = idx.fixingDate(sdate);
                EliteQuant.Date   tdate  = idx.fixingCalendar().advance(sdate, tenor_);

                // fixed leg 1 yr. Forward?
                Schedule fixedsch = new Schedule(sdate, tdate, new Period(1, TimeUnit.Years),
                                                 idx.fixingCalendar(), idx.businessDayConvention(), idx.businessDayConvention(),
                                                 DateGeneration.Rule.Forward, end_of_month);

                OvernightIndexedSwap swap = new OvernightIndexedSwap(ispayer ? _OvernightIndexedSwap.Type.Payer : _OvernightIndexedSwap.Type.Receiver,
                                                                     notional, fixedsch, fixedRate, fixeddc, idx, spread);

                DiscountingSwapEngine engine = new DiscountingSwapEngine(dch);
                swap.setPricingEngine(engine);

                Date refDate = discountcurve.referenceDate();

                // Store the futures and return its id
                string id = "SWP@" + tradeid;
                OHRepository.Instance.storeObject(id, swap, callerAddress);
                id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss");
                return(id);
            }
            catch (Exception e)
            {
                ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message);
                return("#EQ_ERR!");
            }
        }