예제 #1
0
        public void testZSpreadedObs()
        {
            // Testing observability of zero-spreaded term structure
            CommonVars vars = new CommonVars();

            SimpleQuote    me = new SimpleQuote(0.01);
            Handle <Quote> mh = new Handle <Quote>(me);
            RelinkableHandle <YieldTermStructure> h = new RelinkableHandle <YieldTermStructure>(vars.dummyTermStructure);

            YieldTermStructure spreaded = new ZeroSpreadedTermStructure(h, mh);
            Flag flag = new Flag();

            spreaded.registerWith(flag.update);
            h.linkTo(vars.termStructure);
            if (!flag.isUp())
            {
                QAssert.Fail("Observer was not notified of term structure change");
            }
            flag.lower();
            me.setValue(0.005);
            if (!flag.isUp())
            {
                QAssert.Fail("Observer was not notified of spread change");
            }
        }
예제 #2
0
        public void testObservableHandle()
        {
            // Testing observability of quote handles

            SimpleQuote me1            = new SimpleQuote(0.0);
            RelinkableHandle <Quote> h = new RelinkableHandle <Quote>(me1);

            Flag f = new Flag();

            h.registerWith(f.update);

            me1.setValue(3.14);

            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of quote change");
            }

            f.lower();
            SimpleQuote me2 = new SimpleQuote(0.0);

            h.linkTo(me2);

            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of quote change");
            }
        }
예제 #3
0
        public void testFSpreadedObs()
        {
            // ("Testing observability of forward-spreaded term structure...");

            CommonVars vars = new CommonVars();

            SimpleQuote    me = new SimpleQuote(0.01);
            Handle <Quote> mh = new Handle <Quote>(me);
            RelinkableHandle <YieldTermStructure> h = new RelinkableHandle <YieldTermStructure>(); //(vars.dummyTermStructure);
            YieldTermStructure spreaded             = new ForwardSpreadedTermStructure(h, mh);
            Flag flag = new Flag();

            spreaded.registerWith(flag.update);
            h.linkTo(vars.termStructure);
            if (!flag.isUp())
            {
                Console.WriteLine("Observer was not notified of term structure change");
            }
            flag.lower();
            me.setValue(0.005);
            if (!flag.isUp())
            {
                Console.WriteLine("Observer was not notified of spread change");
            }
        }
예제 #4
0
        public void testObservable()
        {
            //.("Testing observability of instruments...");

             SimpleQuote me1 = new SimpleQuote(0.0);
             RelinkableHandle<Quote> h = new RelinkableHandle<Quote>(me1);
             Instrument s = new Stock(h);

             Flag f = new Flag();

             s.registerWith(f.update);

             s.NPV();
             me1.setValue(3.14);
             if (!f.isUp())
            Assert.Fail("Observer was not notified of instrument change");

             s.NPV();
             f.lower();
             SimpleQuote me2 = new SimpleQuote(0.0);
             h.linkTo(me2);
             if (!f.isUp())
            Assert.Fail("Observer was not notified of instrument change");

             f.lower();
             s.freeze();
             s.NPV();
             me2.setValue(2.71);
             if (f.isUp())
            Assert.Fail("Observer was notified of frozen instrument change");
             s.NPV();
             s.unfreeze();
             if (!f.isUp())
            Assert.Fail("Observer was not notified of instrument change");
        }
예제 #5
0
        public void testObservable()
        {
            //.("Testing observability of instruments...");

            SimpleQuote me1            = new SimpleQuote(0.0);
            RelinkableHandle <Quote> h = new RelinkableHandle <Quote>(me1);
            Instrument s = new Stock(h);

            Flag f = new Flag();

            s.registerWith(f.update);

            s.NPV();
            me1.setValue(3.14);
            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of instrument change");
            }

            s.NPV();
            f.lower();
            SimpleQuote me2 = new SimpleQuote(0.0);

            h.linkTo(me2);
            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of instrument change");
            }

            f.lower();
            s.freeze();
            s.NPV();
            me2.setValue(2.71);
            if (f.isUp())
            {
                QAssert.Fail("Observer was notified of frozen instrument change");
            }
            s.NPV();
            s.unfreeze();
            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of instrument change");
            }
        }
예제 #6
0
        public void testObservability()
        {
            // "Testing observability of piecewise yield curve...");

            CommonVars vars = new CommonVars();

            vars.termStructure = new PiecewiseYieldCurve <Discount, LogLinear>(vars.settlementDays,
                                                                               vars.calendar, vars.instruments, new Actual360());
            Flag f = new Flag();

            vars.termStructure.registerWith(f.update);

            for (int i = 0; i < vars.deposits + vars.swaps; i++)
            {
                double testTime = new Actual360().yearFraction(vars.settlement, vars.instruments[i].latestDate());
                double discount = vars.termStructure.discount(testTime);
                f.lower();
                vars.rates[i].setValue(vars.rates[i].value() * 1.01);
                if (!f.isUp())
                {
                    QAssert.Fail("Observer was not notified of underlying rate change");
                }
                double discount_new = vars.termStructure.discount(testTime, true);
                if (discount_new == discount)
                {
                    QAssert.Fail("rate change did not trigger recalculation");
                }
                vars.rates[i].setValue(vars.rates[i].value() / 1.01);
            }

            f.lower();
            Settings.setEvaluationDate(vars.calendar.advance(vars.today, 15, TimeUnit.Days));
            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of date change");
            }
        }
예제 #7
0
        public void testObservable()
        {
            // Testing observability of quotes

            SimpleQuote me = new SimpleQuote(0.0);
            Flag        f  = new Flag();

            me.registerWith(f.update);
            me.setValue(3.14);

            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of quote change");
            }
        }
예제 #8
0
        public void testImpliedObs()
        {
            // Testing observability of implied term structure
            CommonVars vars = new CommonVars();

            Date today         = Settings.evaluationDate();
            Date newToday      = today + new Period(3, TimeUnit.Years);
            Date newSettlement = vars.calendar.advance(newToday, vars.settlementDays, TimeUnit.Days);
            RelinkableHandle <YieldTermStructure> h = new RelinkableHandle <YieldTermStructure>();
            YieldTermStructure implied = new ImpliedTermStructure(h, newSettlement);
            Flag flag = new Flag();

            implied.registerWith(flag.update);
            h.linkTo(vars.termStructure);
            if (!flag.isUp())
            {
                QAssert.Fail("Observer was not notified of term structure change");
            }
        }
예제 #9
0
파일: T_Quotes.cs 프로젝트: akasolace/qlnet
        public void testObservableHandle()
        {
            // Testing observability of quote handles

             SimpleQuote me1 = new SimpleQuote(0.0);
             RelinkableHandle<Quote> h = new RelinkableHandle<Quote>(me1);

             Flag f = new Flag();

             h.registerWith(f.update);

             me1.setValue(3.14);

             if (!f.isUp())
               Assert.Fail("Observer was not notified of quote change");

             f.lower();
             SimpleQuote me2 = new SimpleQuote(0.0);
             h.linkTo(me2);

             if (!f.isUp())
               Assert.Fail("Observer was not notified of quote change");
        }
예제 #10
0
파일: T_Quotes.cs 프로젝트: akasolace/qlnet
        public void testObservable()
        {
            // Testing observability of quotes

             SimpleQuote me = new SimpleQuote(0.0);
             Flag f = new Flag();

             me.registerWith(f.update);
             me.setValue(3.14);

             if (!f.isUp())
            Assert.Fail("Observer was not notified of quote change");
        }
예제 #11
0
        public void testLiborFixing()
        {
            // "Testing use of today's LIBOR fixings in swap curve...");

            CommonVars vars = new CommonVars();

            var       swapHelpers = new InitializedList <RateHelper>();
            IborIndex euribor6m   = new Euribor6M();

            for (int i = 0; i < vars.swaps; i++)
            {
                Handle <Quote> r = new Handle <Quote>(vars.rates[i + vars.deposits]);
                swapHelpers.Add(new SwapRateHelper(r, new Period(vars.swapData[i].n, vars.swapData[i].units),
                                                   vars.calendar,
                                                   vars.fixedLegFrequency, vars.fixedLegConvention,
                                                   vars.fixedLegDayCounter, euribor6m));
            }

            vars.termStructure = new PiecewiseYieldCurve <Discount, LogLinear>(vars.settlement, swapHelpers, new Actual360());

            Handle <YieldTermStructure> curveHandle = new Handle <YieldTermStructure>(vars.termStructure);

            IborIndex index = new Euribor6M(curveHandle);

            for (int i = 0; i < vars.swaps; i++)
            {
                Period tenor = new Period(vars.swapData[i].n, vars.swapData[i].units);

                VanillaSwap swap = new MakeVanillaSwap(tenor, index, 0.0)
                                   .withEffectiveDate(vars.settlement)
                                   .withFixedLegDayCount(vars.fixedLegDayCounter)
                                   .withFixedLegTenor(new Period(vars.fixedLegFrequency))
                                   .withFixedLegConvention(vars.fixedLegConvention)
                                   .withFixedLegTerminationDateConvention(vars.fixedLegConvention)
                                   .value();

                double expectedRate  = vars.swapData[i].rate / 100,
                       estimatedRate = swap.fairRate();
                double tolerance     = 1.0e-9;
                if (Math.Abs(expectedRate - estimatedRate) > tolerance)
                {
                    QAssert.Fail("before LIBOR fixing:\n"
                                 + vars.swapData[i].n + " year(s) swap:\n"
                                 + "    estimated rate: "
                                 + (estimatedRate) + "\n"
                                 + "    expected rate:  "
                                 + (expectedRate));
                }
            }

            Flag f = new Flag();

            vars.termStructure.registerWith(f.update);
            f.lower();

            index.addFixing(vars.today, 0.0425);

            if (!f.isUp())
            {
                QAssert.Fail("Observer was not notified of rate fixing");
            }

            for (int i = 0; i < vars.swaps; i++)
            {
                Period tenor = new Period(vars.swapData[i].n, vars.swapData[i].units);

                VanillaSwap swap = new MakeVanillaSwap(tenor, index, 0.0)
                                   .withEffectiveDate(vars.settlement)
                                   .withFixedLegDayCount(vars.fixedLegDayCounter)
                                   .withFixedLegTenor(new Period(vars.fixedLegFrequency))
                                   .withFixedLegConvention(vars.fixedLegConvention)
                                   .withFixedLegTerminationDateConvention(vars.fixedLegConvention)
                                   .value();

                double expectedRate  = vars.swapData[i].rate / 100,
                       estimatedRate = swap.fairRate();
                double tolerance     = 1.0e-9;
                if (Math.Abs(expectedRate - estimatedRate) > tolerance)
                {
                    QAssert.Fail("after LIBOR fixing:\n"
                                 + vars.swapData[i].n + " year(s) swap:\n"
                                 + "    estimated rate: "
                                 + (estimatedRate) + "\n"
                                 + "    expected rate:  "
                                 + (expectedRate));
                }
            }
        }
예제 #12
0
        public void testSpreadedCube()
        {
            // Testing spreaded swaption volatility cube
            CommonVars vars = new CommonVars();

            List <List <Handle <Quote> > > parametersGuess =
                new InitializedList <List <Handle <Quote> > >(vars.cube.tenors.options.Count * vars.cube.tenors.swaps.Count);

            for (int i = 0; i < vars.cube.tenors.options.Count * vars.cube.tenors.swaps.Count; i++)
            {
                parametersGuess[i]    = new InitializedList <Handle <Quote> >(4);
                parametersGuess[i][0] = new Handle <Quote>(new SimpleQuote(0.2));
                parametersGuess[i][1] = new Handle <Quote>(new SimpleQuote(0.5));
                parametersGuess[i][2] = new Handle <Quote>(new SimpleQuote(0.4));
                parametersGuess[i][3] = new Handle <Quote>(new SimpleQuote(0.0));
            }
            List <bool> isParameterFixed = new InitializedList <bool>(4, false);

            Handle <SwaptionVolatilityStructure> volCube = new Handle <SwaptionVolatilityStructure>(
                new SwaptionVolCube1x(vars.atmVolMatrix,
                                      vars.cube.tenors.options,
                                      vars.cube.tenors.swaps,
                                      vars.cube.strikeSpreads,
                                      vars.cube.volSpreadsHandle,
                                      vars.swapIndexBase,
                                      vars.shortSwapIndexBase,
                                      vars.vegaWeighedSmileFit,
                                      parametersGuess,
                                      isParameterFixed,
                                      true));

            SimpleQuote    spread       = new SimpleQuote(0.0001);
            Handle <Quote> spreadHandle = new Handle <Quote>(spread);
            SwaptionVolatilityStructure spreadedVolCube = new SpreadedSwaptionVolatility(volCube, spreadHandle);
            List <double> strikes = new List <double>();

            for (int k = 1; k < 100; k++)
            {
                strikes.Add(k * .01);
            }
            for (int i = 0; i < vars.cube.tenors.options.Count; i++)
            {
                for (int j = 0; j < vars.cube.tenors.swaps.Count; j++)
                {
                    SmileSection smileSectionByCube = volCube.link.smileSection(vars.cube.tenors.options[i],
                                                                                vars.cube.tenors.swaps[j]);
                    SmileSection smileSectionBySpreadedCube = spreadedVolCube.smileSection(vars.cube.tenors.options[i],
                                                                                           vars.cube.tenors.swaps[j]);
                    for (int k = 0; k < strikes.Count; k++)
                    {
                        double strike = strikes[k];
                        double diff   = spreadedVolCube.volatility(vars.cube.tenors.options[i], vars.cube.tenors.swaps[j], strike)
                                        - volCube.link.volatility(vars.cube.tenors.options[i], vars.cube.tenors.swaps[j], strike);
                        if (Math.Abs(diff - spread.value()) > 1e-16)
                        {
                            QAssert.Fail("\ndiff!=spread in volatility method:" +
                                         "\nexpiry time = " + vars.cube.tenors.options[i] +
                                         "\nswap length = " + vars.cube.tenors.swaps[j] +
                                         "\n atm strike = " + (strike) +
                                         "\ndiff = " + diff +
                                         "\nspread = " + spread.value());
                        }

                        diff = smileSectionBySpreadedCube.volatility(strike) - smileSectionByCube.volatility(strike);
                        if (Math.Abs(diff - spread.value()) > 1e-16)
                        {
                            QAssert.Fail("\ndiff!=spread in smile section method:" +
                                         "\nexpiry time = " + vars.cube.tenors.options[i] +
                                         "\nswap length = " + vars.cube.tenors.swaps[j] +
                                         "\n atm strike = " + (strike) +
                                         "\ndiff = " + diff +
                                         "\nspread = " + spread.value());
                        }
                    }
                }
            }

            //testing observability
            Flag f = new Flag();

            spreadedVolCube.registerWith(f.update);
            volCube.link.update();
            if (!f.isUp())
            {
                QAssert.Fail("SpreadedSwaptionVolatilityStructure does not propagate notifications");
            }

            f.lower();
            spread.setValue(.001);
            if (!f.isUp())
            {
                QAssert.Fail("SpreadedSwaptionVolatilityStructure does not propagate notifications");
            }
        }
예제 #13
0
        public void testFSpreadedObs()
        {
            // ("Testing observability of forward-spreaded term structure...");

             CommonVars vars = new CommonVars();

             SimpleQuote me = new SimpleQuote(0.01);
             Handle<Quote> mh = new Handle<Quote>(me);
             RelinkableHandle<YieldTermStructure> h = new RelinkableHandle<YieldTermStructure>(); //(vars.dummyTermStructure);
             YieldTermStructure spreaded = new ForwardSpreadedTermStructure(h, mh);
             Flag flag = new Flag();
             spreaded.registerWith(flag.update);
             h.linkTo(vars.termStructure);
             if (!flag.isUp())
            Console.WriteLine("Observer was not notified of term structure change");
             flag.lower();
             me.setValue(0.005);
             if (!flag.isUp())
            Console.WriteLine("Observer was not notified of spread change");
        }
예제 #14
0
        public void testImpliedObs()
        {
            // ("Testing observability of implied term structure...");

             CommonVars vars = new CommonVars();

             Date today = Settings.evaluationDate();
             Date newToday = today + new Period(3, TimeUnit.Years);
             Date newSettlement = vars.calendar.advance(newToday, vars.settlementDays, TimeUnit.Days);
             RelinkableHandle<YieldTermStructure> h = new RelinkableHandle<YieldTermStructure>();
             YieldTermStructure implied = new ImpliedTermStructure(h, newSettlement);
             Flag flag = new Flag();
             implied.registerWith(flag.update);
             h.linkTo(vars.termStructure);
             if (!flag.isUp())
            Console.WriteLine("Observer was not notified of term structure change");
        }