Package org.jquantlib.time.calendars

Examples of org.jquantlib.time.calendars.UnitedStates


        // Christmas, December 25th (moved to Monday if Sunday or Friday if Saturday)
        expectedHol.add(new Date(25, December, year));


        // Call the Holiday Check
        final Calendar settlement = new UnitedStates(UnitedStates.Market.SETTLEMENT);
        final CalendarUtil cbt = new CalendarUtil();
        cbt.checkHolidayList(expectedHol, settlement, year);
    }
View Full Code Here


        expectedHol.add(new Date(26, November, year));
        // Christmas, December 25th (moved to Monday if Sunday or Friday if Saturday)
        expectedHol.add(new Date(25, December, year));

        // Call the Holiday Check
        final Calendar settlement = new UnitedStates(UnitedStates.Market.SETTLEMENT);
        final CalendarUtil cbt = new CalendarUtil();
        cbt.checkHolidayList(expectedHol, settlement, year);
    }
View Full Code Here

        // Christmas, December 25th (moved to Monday if Sunday or Friday if Saturday)
        expectedHol.add(new Date(24, December, year));
        expectedHol.add(new Date(31, December, year));

        // Call the Holiday Check
        final Calendar settlement = new UnitedStates(UnitedStates.Market.SETTLEMENT);
        final CalendarUtil cbt = new CalendarUtil();
        cbt.checkHolidayList(expectedHol, settlement, year);
    }
View Full Code Here

        for (int i = 0; i < numberOfBonds; i++) {
            final Schedule schedule = new Schedule(
                    issueDates[i], maturities[i],
                    new Period(Frequency.Semiannual),
                    new UnitedStates(UnitedStates.Market.GOVERNMENTBOND),
                    BusinessDayConvention.Unadjusted,
                    BusinessDayConvention.Unadjusted,
                    DateGeneration.Rule.Backward,
                    false);
            final FixedRateBondHelper bondHelper = new FixedRateBondHelper(
                            quoteHandle.get(i),
                            settlementDays,
                            100.0,
                            schedule,
                            new double[]{ couponRates[i] },
                            new ActualActual(ActualActual.Convention.Bond),
                            BusinessDayConvention.Unadjusted,
                            redemption,
                            issueDates[i]);

            bondsHelpers.add(bondHelper);
        }

        /*********************
         ** CURVE BUILDING **
         *********************/

        // Any DayCounter would be fine.
        // ActualActual::ISDA ensures that 30 years is 30.0
        final DayCounter termStructureDayCounter = new ActualActual(ActualActual.Convention.ISDA);

        final double tolerance = 1.0e-15;

        // A depo-bond curve
        final List<RateHelper> bondInstruments = new ArrayList<RateHelper>();

        // Adding the ZC bonds to the curve for the short end
        bondInstruments.add(zc3m);
        bondInstruments.add(zc6m);
        bondInstruments.add(zc1y);

        // Adding the Fixed rate bonds to the curve for the long end
        for (int i = 0; i < numberOfBonds; i++) {
            bondInstruments.add(bondsHelpers.get(i));
        }
        final RateHelper[] instruments1 = new RateHelper[bondInstruments.size()];
        bondInstruments.toArray(instruments1);
        final Handle[] jumps1 = new Handle[0];
        final Date[] jumpDates1 = new Date[0];
        final double tolerance1 = 1.0e-15;
        final LogLinear interpolator = null;
        final IterativeBootstrap bootstrap = null;
       
        final YieldTermStructure  bondDiscountingTermStructur =
                 new PiecewiseYieldCurve<Discount,LogLinear,IterativeBootstrap>(
                     Discount.class, LogLinear.class, IterativeBootstrap.class,
                     settlementDate,
                     instruments1,
                     termStructureDayCounter,
                     jumps1,
                     jumpDates1,
                     tolerance1,
                     interpolator,
                     bootstrap){/* anonymous */};

        // Building of the Libor forecasting curve
        // deposits
        final double d1wQuote = 0.043375;
        final double d1mQuote = 0.031875;
        final double d3mQuote = 0.0320375;
        final double d6mQuote = 0.03385;
        final double d9mQuote = 0.0338125;
        final double d1yQuote = 0.0335125;
        // swaps
        final double s2yQuote = 0.0295;
        final double s3yQuote = 0.0323;
        final double s5yQuote = 0.0359;
        final double s10yQuote = 0.0412;
        final double s15yQuote = 0.0433;

        /********************
         *** QUOTES ***
         ********************/

        // SimpleQuote stores a value which can be manually changed;
        // other Quote subclasses could read the value from a database
        // or some kind of data feed.

        // deposits
        final Quote d1wRate = (new SimpleQuote(d1wQuote));
        final Quote d1mRate = (new SimpleQuote(d1mQuote));
        final Quote d3mRate = (new SimpleQuote(d3mQuote));
        final Quote d6mRate = (new SimpleQuote(d6mQuote));
        final Quote d9mRate = (new SimpleQuote(d9mQuote));
        final Quote d1yRate = (new SimpleQuote(d1yQuote));
        // swaps
        final Quote s2yRate = (new SimpleQuote(s2yQuote));
        final Quote s3yRate = (new SimpleQuote(s3yQuote));
        final Quote s5yRate = (new SimpleQuote(s5yQuote));
        final Quote s10yRate = (new SimpleQuote(s10yQuote));
        final Quote s15yRate = (new SimpleQuote(s15yQuote));

        /*********************
         *** RATE HELPERS ***
         *********************/

        // RateHelpers are built from the above quotes together with
        // other instrument dependant infos. Quotes are passed in
        // relinkable handles which could be relinked to some other
        // data source later.

        // deposits
        final DayCounter depositDayCounter = new Actual360();

        final RateHelper d1w = new DepositRateHelper(
                  new Handle<Quote>(d1wRate),
                  new Period(1, TimeUnit.Weeks),
                  fixingDays, calendar,
                  BusinessDayConvention.ModifiedFollowing,
                  true, depositDayCounter);
        final RateHelper d1m = new DepositRateHelper(
                  new Handle<Quote>(d1mRate),
                  new Period(1, TimeUnit.Months),
                  fixingDays, calendar,
                  BusinessDayConvention.ModifiedFollowing,
                  true, depositDayCounter);
        final RateHelper d3m = new DepositRateHelper(
                  new Handle<Quote>(d3mRate),
                  new Period(3, TimeUnit.Months),
                  fixingDays, calendar,
                  BusinessDayConvention.ModifiedFollowing,
                  true, depositDayCounter);
        final RateHelper d6m = new DepositRateHelper(
                  new Handle<Quote>(d6mRate),
                  new Period(6, TimeUnit.Months),
                  fixingDays, calendar,
                  BusinessDayConvention.ModifiedFollowing,
                  true, depositDayCounter);
        final RateHelper d9m = new DepositRateHelper(
                new Handle<Quote>(d9mRate),
                new Period(9, TimeUnit.Months),
                fixingDays, calendar,
                BusinessDayConvention.ModifiedFollowing,
                true, depositDayCounter);
        final RateHelper d1y = new DepositRateHelper(
                new Handle<Quote>(d1yRate),
                new Period(1, TimeUnit.Years),
                fixingDays, calendar,
                BusinessDayConvention.ModifiedFollowing,
                true, depositDayCounter);

        // setup swaps
        final Frequency swFixedLegFrequency = Frequency.Annual;
        final BusinessDayConvention swFixedLegConvention = BusinessDayConvention.Unadjusted;
        final DayCounter swFixedLegDayCounter = new Thirty360(Convention.European);
        final IborIndex  swFloatingLegIndex = new Euribor6M(new Handle<YieldTermStructure>());

        // TODO and FIXME: not sure whether the class stuff works properly
        // final IborIndex swFloatingLegIndex = Euribor.getEuribor6M(new Handle<YieldTermStructure>(YieldTermStructure.class)); //FIXME::RG::Handle
//        final YieldTermStructure nullYieldTermStructure = new AbstractYieldTermStructure() {
//            @Override
//            protected double discountImpl(final double t) {
//                throw new UnsupportedOperationException();
//            }
//            @Override
//            public Date maxDate() {
//                throw new UnsupportedOperationException();
//            }
//        };
//        final IborIndex swFloatingLegIndex = new Euribor6M(new Handle<YieldTermStructure>(nullYieldTermStructure));


        final Period forwardStart = new Period(1, TimeUnit.Days);
       
        final RateHelper s2y = new SwapRateHelper(
              new Handle<Quote>(s2yRate),
              new Period(2, TimeUnit.Years),
              calendar,
              swFixedLegFrequency,
              swFixedLegConvention,
              swFixedLegDayCounter,
              swFloatingLegIndex,
              new Handle<Quote>(),
              forwardStart);
         final RateHelper s3y = new SwapRateHelper(
                 new Handle<Quote>(s3yRate),
                 new Period(3, TimeUnit.Years),
                 calendar,
                 swFixedLegFrequency,
                 swFixedLegConvention,
                 swFixedLegDayCounter,             
                 swFloatingLegIndex,
                 new Handle<Quote>(),
                 forwardStart);
        final RateHelper  s5y = new SwapRateHelper(
                new Handle<Quote>(s5yRate),
                new Period(5, TimeUnit.Years),
                calendar,
                swFixedLegFrequency,
                swFixedLegConvention,
                swFixedLegDayCounter,
                swFloatingLegIndex,
                new Handle<Quote>(),
                forwardStart);
        final RateHelper s10y = new SwapRateHelper(
                new Handle<Quote>(s10yRate),
                new Period(10, TimeUnit.Years),
                calendar,
                swFixedLegFrequency,
                swFixedLegConvention,
                swFixedLegDayCounter,
                swFloatingLegIndex,
                new Handle<Quote>(),
                forwardStart);
        final RateHelper  s15y = new SwapRateHelper(
                new Handle<Quote>(s15yRate),
                new Period(15, TimeUnit.Years),
                calendar,
                swFixedLegFrequency,
                swFixedLegConvention,
                swFixedLegDayCounter,
                swFloatingLegIndex,
                new Handle<Quote>(),
                forwardStart);

         /*********************
         ** CURVE BUILDING **
         *********************/
       
         // Any DayCounter would be fine.
         // ActualActual::ISDA ensures that 30 years is 30.0
       
         // A depo-swap curve
         final List<RateHelper> depoSwapInstruments = new ArrayList<RateHelper>();
         depoSwapInstruments.add(d1w);
         depoSwapInstruments.add(d1m);
         depoSwapInstruments.add(d3m);
         depoSwapInstruments.add(d6m);
         depoSwapInstruments.add(d9m);
         depoSwapInstruments.add(d1y);
         depoSwapInstruments.add(s2y);
         depoSwapInstruments.add(s3y);
         depoSwapInstruments.add(s5y);
         depoSwapInstruments.add(s10y);
         depoSwapInstruments.add(s15y);
        
         final RateHelper[] instruments = new RateHelper[depoSwapInstruments.size()];
         depoSwapInstruments.toArray(instruments);
         final Handle[] jumps= new Handle[0];//]<Quote>[]) new ArrayList<Handle<Quote>>().toArray();
         final Date[] jumpDates = new Date[0];// new ArrayList<Date>().toArray();
        
         final YieldTermStructure  depoSwapTermStructure =
             new PiecewiseYieldCurve<Discount,LogLinear,IterativeBootstrap>(
                 Discount.class, LogLinear.class, IterativeBootstrap.class,
                 settlementDate,
                 instruments,
                 termStructureDayCounter,
                 jumps,
                 jumpDates,
                 tolerance,
            interpolator,/*Hack*/
             bootstrap /*Hack*/){/* anonymous */};

         // Term structures that will be used for pricing:
         // the one used for discounting cash flows
         final RelinkableHandle<YieldTermStructure> discountingTermStructure = new RelinkableHandle<YieldTermStructure>();
         // the one used for forward rate forecasting
         final RelinkableHandle<YieldTermStructure> forecastingTermStructure = new RelinkableHandle<YieldTermStructure>();
       
         /*********************
         * BONDS TO BE PRICED *
         **********************/
       
         // Common data
         final double faceAmount = 100;
       
         // Pricing engine
        final PricingEngine  bondEngine = new DiscountingBondEngine(discountingTermStructure);
       
         // Zero coupon bond
         final ZeroCouponBond zeroCouponBond = new ZeroCouponBond(
                           settlementDays,
                           new UnitedStates(UnitedStates.Market.GOVERNMENTBOND),
                           faceAmount,
                           new Date(15,Month.August,2013),
                           BusinessDayConvention.Following,
                           116.92,
                           new Date(15,Month.August,2003));
       
         zeroCouponBond.setPricingEngine(bondEngine);
       
         // Fixed 4.5% US Treasury Note
         final Schedule fixedBondSchedule = new Schedule(
                     new Date(15, Month.May, 2007),
                     new Date(15,Month.May,2017),
                     new Period(Frequency.Semiannual),
                     new UnitedStates(UnitedStates.Market.GOVERNMENTBOND),
                     BusinessDayConvention.Unadjusted,
                     BusinessDayConvention.Unadjusted,
                     DateGeneration.Rule.Backward, false);
       
         final FixedRateBond fixedRateBond = new FixedRateBond(
                     settlementDays,
                     faceAmount,
                     fixedBondSchedule,
                     new double[]{0.045},
                     new ActualActual(ActualActual.Convention.Bond),
                     BusinessDayConvention.ModifiedFollowing,
                     100.0,
                     new Date(15, Month.May, 2007));
       
         fixedRateBond.setPricingEngine(bondEngine);
       
         // Floating rate bond (3M USD Libor + 0.1%)
         // Should and will be priced on another curve later...
       
         final RelinkableHandle<YieldTermStructure> liborTermStructure = new RelinkableHandle<YieldTermStructure>();
         final IborIndex libor3m =  new USDLibor(new Period(3, TimeUnit.Months), liborTermStructure);
         libor3m.addFixing(new Date(17, Month.July, 2008), 0.0278625);
       
         final Schedule floatingBondSchedule = new Schedule(
                     new Date(21, Month.October, 2005),
                     new Date(21, Month.October, 2010), new Period(Frequency.Quarterly),
                     new UnitedStates(UnitedStates.Market.NYSE),
                     BusinessDayConvention.Unadjusted,
                     BusinessDayConvention.Unadjusted,
                     DateGeneration.Rule.Backward, true);
       
         final FloatingRateBond floatingRateBond = new FloatingRateBond(
View Full Code Here

        final StopClock clock = new StopClock();
        clock.startClock();

        // <=========================Basic Calendar functions==============================>
        //Let's take UnitedStates New-York-Stock-Exchange calendar
        final Calendar unitedStatesCalendar = new UnitedStates(Market.NYSE);
        System.out.println("The name of this calendar is = "+ unitedStatesCalendar.name());
        // Let's get the list of holidays from todays date till the date obtained by advancing today's date by 90 days
        final Date dateToday = Date.todaysDate();
        final Date dateAdvanced = dateToday.add(90);
        final List<Date> holidayList = UnitedStates.holidayList(unitedStatesCalendar,dateToday, dateAdvanced, true);
        System. out.println("The holidays between dateToday = " + dateToday+ " till the date dateAdvanced = " + dateAdvanced+ " are as shown below");
        final StringBuffer bufferToHoldHolidays = new StringBuffer();
        for (final Date date : holidayList) {
            bufferToHoldHolidays.append(date +"::");
        }
        System. out.println("The holidays separated by :: are = "+ bufferToHoldHolidays);

        // Let's get the business days between dateToday and dateAdvanced as obtained above by advancing
        // today's date by 90 days as shown below and checking if the incremented date isBusinessDate
        final List<Date> businessDaysInBetweenUsingIsBusDay = new ArrayList<Date>();
        Date dateTodayTemp = dateToday.clone();
        for (; !dateTodayTemp.eq(dateAdvanced); dateTodayTemp.inc()) {
            if (unitedStatesCalendar.isBusinessDay(dateTodayTemp)) {
                businessDaysInBetweenUsingIsBusDay.add(dateTodayTemp);
            }
        }

        final Long sizeOfBusinessDays = Long.parseLong(Integer.toString((businessDaysInBetweenUsingIsBusDay.size())));
        // Let's get the business days between dateToday and dateAdvanced as obtained above by advancing
        // today's date by 90 days using calendar api businessDaysBetween
        final long sizeOfBusinessDaysUsingCalApi = unitedStatesCalendar.businessDaysBetween(dateToday, dateAdvanced, true, true);
        // Check if the sizes obtained are same as they both represent same set of business days
        if (sizeOfBusinessDays == sizeOfBusinessDaysUsingCalApi) {
            System.out.println("The sizes are same");
        }

        // Let's get the same business days using calendars isHoliday API
        final List<Date> businessDaysInBetweenUsingIsHolDay = new ArrayList<Date>();
        dateTodayTemp = dateToday.clone();
        for (; !dateTodayTemp.eq(dateAdvanced); dateTodayTemp.inc()) {
            if (!unitedStatesCalendar.isHoliday(dateTodayTemp)) {
                businessDaysInBetweenUsingIsHolDay.add(dateTodayTemp);
            }
        }

        //Essentially the lists businessDaysInBetweenUsingIsBusDay and businessDaysInBetweenUsingIsHolDay are same
        if (businessDaysInBetweenUsingIsBusDay.equals(businessDaysInBetweenUsingIsHolDay)) {
            System.out.println("The lists businessDaysInBetweenUsingIsBusDay and businessDaysInBetweenUsingIsHolDay are same");
        }

        // <========================Let's use some Calendar airthmetics==============================>
        // Get the first holiday from today
        final Date firstHolidayDate = dateToday.clone();
        while (!unitedStatesCalendar.isHoliday(firstHolidayDate)) {
            firstHolidayDate.inc();
        }

        // Check to see if firstHoliday is holiday
        if (unitedStatesCalendar.isHoliday(firstHolidayDate)) {
            System.out.println("FirstHolidayDate = " + firstHolidayDate+ " is a holiday date");
        }

        // Advance the first holiday date using calendars's advance(date) API and get the nexBusinessDay
        final Date nextBusinessDay = unitedStatesCalendar.advance(firstHolidayDate, new Period(1, TimeUnit.Days));
        if (unitedStatesCalendar.isBusinessDay(nextBusinessDay)) {
            System.out.println("NextBusinessDayFromFirstHolidayFromToday = "+ nextBusinessDay + " is a business date");
        }

        // Adjust todaysDate using different businessDayConventions

        // Using FOLLOWING as a business day convention
        final Date nextFollowingBusinessDay = unitedStatesCalendar.adjust(dateToday,BusinessDayConvention.Following);
        if (unitedStatesCalendar.isBusinessDay(nextFollowingBusinessDay)) {
            System. out.println("NextFollowingBusinessDate = "+ nextFollowingBusinessDay+ " from today is a business date");
        }

        // Using MODIFIED_FOLLOWING as a business day convention
        final Date nextModified_FollowingBusinessDay = unitedStatesCalendar.adjust(dateToday, BusinessDayConvention.ModifiedFollowing);
        if (unitedStatesCalendar.isBusinessDay(nextModified_FollowingBusinessDay)) {
            System.out.println("NextModified_FollowingBusinessDate = "+ nextModified_FollowingBusinessDay+ " from today is a business date");
        }

        // Using PRECEDING as a business day convention
        final Date nextPrecidingBusinessDay = unitedStatesCalendar.adjust(dateToday,BusinessDayConvention.Preceding);
        if (unitedStatesCalendar.isBusinessDay(nextPrecidingBusinessDay)) {
            System.out.println("NextPrecidingBusinessDay = "+ nextPrecidingBusinessDay+ " from today is a business date");
        }

        // Using MODIFIED_PRECEDING as a business day convention
        final Date nextModified_PrecidingBusinessDay = unitedStatesCalendar.adjust(dateToday, BusinessDayConvention.ModifiedPreceding);
        if (unitedStatesCalendar.isBusinessDay(nextModified_PrecidingBusinessDay)) {
            System.out.println("NextModified_PrecidingBusinessDay = "+ nextModified_PrecidingBusinessDay+ " from today is a business date");
        }

        // Using UNADJUSTED as a business day convention
        final Date nextUnadjustedBusinessDay = unitedStatesCalendar.adjust(dateToday,BusinessDayConvention.Unadjusted);
        if (unitedStatesCalendar.isBusinessDay(nextModified_PrecidingBusinessDay)) {
            System.out.println("NextUnadjustedBusinessDay = "+ nextUnadjustedBusinessDay+ " from today is a business date and is same as today");
        }

        // Advance the current date using calendars's advance(date,n,unit) where n = 10 and unit=DAYS
        Date advancedDate = unitedStatesCalendar.advance(dateToday, 10,TimeUnit.Days);
        System.out.println("Next business date when today's date is advanced by 10 days = "+ advancedDate);

        // Advance the current date using calendars's advance(date,n,unit) where n = 10 and unit=WEEKS
        advancedDate = unitedStatesCalendar.advance(dateToday, 10, TimeUnit.Weeks);
        System.out.println("Next business date when today's date is advanced by 10 weeks = "+ advancedDate);

        // Advance the current date using calendars's advance(date,n,unit) where n = 10 and unit=MONTHS
        advancedDate = unitedStatesCalendar.advance(dateToday, 10, TimeUnit.Months);
        System.out.println("Next business date when today's date is advanced by 10 months = "+ advancedDate);

        // Advance the current date using calendars's advance(date,n,unit) where n = 10 and unit=YEARS
        advancedDate = unitedStatesCalendar.advance(dateToday, 10, TimeUnit.Years);
        System.out.println("Next business date when today's date is advanced by 10 years = "+ advancedDate);

        // Advance the current date using calendars's advance(date,period-->lenth=1,Unit=Days,BusinessDayConvention=FOLLOWING)
        advancedDate = unitedStatesCalendar.advance(dateToday,new Period(1, TimeUnit.Days), BusinessDayConvention.Following);
        System.out.println("Next business date when today's date is advanced 1 day = "+ advancedDate);

        // Advance the current date using calendars's advance(date,period-->lenth=1,Unit=WEEKS,BusinessDayConvention=MODIFIED_FOLLOWING)
        advancedDate = unitedStatesCalendar.advance(dateToday,new Period(1, TimeUnit.Weeks),BusinessDayConvention.ModifiedFollowing);
        System.out.println("Next business date when today's date is advanced 1 week = "+ advancedDate);

        // Advance the current date using calendars's advance(date,period-->lenth=1,Unit=MONTHS,BusinessDayConvention=MODIFIED_PRECEDING)
        advancedDate = unitedStatesCalendar.advance(dateToday,new Period(1, TimeUnit.Months),BusinessDayConvention.ModifiedPreceding);
        System.out.println("Next business date when today's date is advanced 1 month = "+ advancedDate);

        // Advance the current date using calendars's advance(date,period-->lenth=1,Unit=YEARS,BusinessDayConvention=PRECEDING)
        advancedDate = unitedStatesCalendar.advance(dateToday,new Period(1, TimeUnit.Years), BusinessDayConvention.Preceding);
        System.out.println("Next business date when today's date is advanced 1 year = "+ advancedDate);

        //<==================Joining Calendars===============================>
        final Calendar unitedStatesCalendarGvtBondCalendar = new UnitedStates(Market.GOVERNMENTBOND);
        final Calendar jointCalendar = new JointCalendar(unitedStatesCalendar,unitedStatesCalendarGvtBondCalendar,JointCalendarRule.JoinBusinessDays);

        //The above joint calendar has been obtained by joining businessdays of newyork stock exchange and government bond calendar
        //which means a day will be a business day for the joint calendar if it is a business day for both of the calendars used
        //(NYSE,GovtBond) and will be a holiday if the day is a holiday in atleast one of the calendars.
View Full Code Here

   
    public BMAIndex() {
      this(new Handle<YieldTermStructure>());
    }
    public BMAIndex(final Handle<YieldTermStructure> h) {
        super("BMA", new Period(1,TimeUnit.Weeks), 1, new USDCurrency(), new UnitedStates(UnitedStates.Market.NYSE), new ActualActual(ActualActual.Convention.ISDA));

        this.termStructure = h;
        if (termStructure != null) {
          termStructure.addObserver(this);
        }
View Full Code Here

        ((BlackVarianceCurve)varianceCurve).setInterpolation();

        //Dividend termstructure
        final SimpleQuote dividendQuote = new SimpleQuote(0.3);
        final RelinkableHandle<Quote>  handleToInterestRateQuote = new RelinkableHandle<Quote>(dividendQuote);
        final YieldTermStructure dividendTermStructure = new FlatForward(2,new UnitedStates(Market.NYSE),handleToInterestRateQuote, new Actual365Fixed(), Compounding.Continuous,Frequency.Daily);

        //Risk free term structure
        final SimpleQuote riskFreeRateQuote = new SimpleQuote(0.3);
        final RelinkableHandle<Quote>  handleToRiskFreeRateQuote = new RelinkableHandle<Quote>(riskFreeRateQuote);
        final YieldTermStructure riskFreeTermStructure = new FlatForward(2,new UnitedStates(Market.NYSE),handleToRiskFreeRateQuote, new Actual365Fixed(), Compounding.Continuous,Frequency.Daily);

        //Creating the process
        final StochasticProcess1D process = new GeneralizedBlackScholesProcess(handleToStockQuote,new RelinkableHandle<YieldTermStructure>(dividendTermStructure),new RelinkableHandle<YieldTermStructure>(riskFreeTermStructure),new RelinkableHandle<BlackVolTermStructure>(varianceCurve),new EulerDiscretization());

        //Calculating the drift of the stochastic process after time = 18th day from today with value of the stock as specified from the quote
View Full Code Here

  public USDLibor(final Period tenor,
      final Handle<YieldTermStructure> h) {
    super("USDLibor", tenor, 2,
        new USDCurrency(),
        new UnitedStates(UnitedStates.Market.SETTLEMENT),
        new Actual360(), h);
  }
View Full Code Here

  public DailyTenorUSDLibor(final int settlementDays,
      final Handle<YieldTermStructure> h) {
    super("USDLibor", settlementDays,
        new USDCurrency(),
        new UnitedStates(UnitedStates.Market.SETTLEMENT),
        new Actual360(), h);
  }
View Full Code Here

        //Let's explore BlackConstantVolatility by calculating blackVolatility,blackForwardVolatility,blackVariance and blackForwardVariance
        //Following example explains that when volatility is assumed to be constant BlackConstantVol termstructure can be used to represent
        //such a volatility termstructure.
        final SimpleQuote volatilityQuote = new SimpleQuote(0.3);
        final RelinkableHandle<Quote>  handleToVolatilityQuote = new RelinkableHandle<Quote>(volatilityQuote);
        BlackVolatilityTermStructure constantVolatility = new BlackConstantVol(2,new UnitedStates(Market.NYSE),handleToVolatilityQuote, new Actual365Fixed());

        //Calculating blackVolatility using maturity as 10 days after today and strike as 20
        Double volatility1 = constantVolatility.blackVol(date10.clone(), 20);
        System.out.println("BlackVolatility = "+volatility1);

        //Calculating blackVolatility using maturity as 20 days after today and strike as 30
        Double volatility2 = constantVolatility.blackVol(date20.clone(), 30);
        System.out.println("BlackVolatility = "+volatility2);

        //Calculating blackVolatility using maturity as 30 days after today and strike as 40
        Double volatility3 = constantVolatility.blackVol(date30.clone(), 40);
        System.out.println("BlackVolatility = "+volatility3);

        //The volatilities calculated above are same as it's constant volatility termstructure
        if(volatility1.equals(volatility2) && volatility2.equals(volatility3)){
            System.out.println("All the volatilities calculated above are same and = "+volatility1);
        }

        //Calculating blackForwardVolatility between 10 days after today and 15 days after today with strike as 20
        Double forwardVolatility1 = constantVolatility.blackForwardVol(date10.clone(), date15.clone(), 20, true);
        System.out.println("BlackForwardVolatility = "+forwardVolatility1);

        //Calculating blackForwardVolatility between 20 days after today and 25 days after today with strike as 40
        Double forwardVolatility2 = constantVolatility.blackForwardVol(date10.clone(), date15.clone(), 20, true);
        System.out.println("BlackForwardVolatility = "+forwardVolatility2);

        //Calculating blackForwardVolatility between 27 days after today and 35 days after today with strike as 60
        Double forwardVolatility3 = constantVolatility.blackForwardVol(date27.clone(), date35.clone(), 60, true);
        System.out.println("BlackForwardVolatility = "+forwardVolatility3);

        //The volatilities calculated above are same as it's constant volatility termstructure
        if(forwardVolatility1.equals(forwardVolatility2) && forwardVolatility2.equals(forwardVolatility3)){
            System.out.println("All the forward volatilities calculated above are same and = "+forwardVolatility1);
        }else{
            System.out.println("The forward volatilities may not be constant");
        }

        //Calculating blackVariance
        System.out.println("BlackVariance = "+constantVolatility.blackVariance(date10.clone(), 20));

        //Calculating blackForwardVariance
        System.out.println("BlackForwardVariance = "+constantVolatility.blackForwardVariance(date10.clone(), date15.clone(), 20, true));

        //As BlackConstantVol termstructure has been initialized using relinkable handle so lets change the observable SimpleQuote of this handle
        //and see the change getting reflected to all the calculations done above.
        volatilityQuote.setValue(0.04) ;
        constantVolatility = new BlackConstantVol(2,new UnitedStates(Market.NYSE),handleToVolatilityQuote, new Actual365Fixed());

        //Calculating blackVolatility using maturity as 10 days after today and strike as 20
        volatility1 = constantVolatility.blackVol(date10.clone(), 20);
        System.out.println("BlackVolatility = "+volatility1);

        //Calculating blackVolatility using maturity as 20 days after today and strike as 30
        volatility2 = constantVolatility.blackVol(date20.clone(), 30);
        System.out.println("BlackVolatility = "+volatility2);

        volatility3 = constantVolatility.blackVol(date30.clone(), 40);
        System.out.println("BlackVolatility = "+volatility3);

        //The volatilities calculated above are same as it's constant volatility termstructure
        if(volatility1.equals(volatility2) && volatility2.equals(volatility3)){
            System.out.println("All the volatilities calculated above are same and = "+volatility1);
        }

        //Calculating blackForwardVolatility between 10 days after today and 15 days after today with strike as 20
        forwardVolatility1 = constantVolatility.blackForwardVol(date10.clone(), date15.clone(), 20, true);
        System.out.println("BlackForwardVolatility = "+forwardVolatility1);

        //Calculating blackForwardVolatility between 20 days after today and 25 days after today with strike as 40
        forwardVolatility2 = constantVolatility.blackForwardVol(date10.clone(), date15.clone(), 20, true);
        System.out.println("BlackForwardVolatility = "+forwardVolatility2);

        //Calculating blackForwardVolatility between 27 days after today and 35 days after today with strike as 60
        forwardVolatility3 = constantVolatility.blackForwardVol(date27.clone(), date35.clone(), 60, true);
        System.out.println("BlackForwardVolatility = "+forwardVolatility3);

        //The volatilities calculated above are same as it's constant volatility termstructure
        if(forwardVolatility1.equals(forwardVolatility2) && forwardVolatility2.equals(forwardVolatility3)){
            System.out.println("All the volatilities calculated above are same and = "+forwardVolatility1);
        }else{
            System.out.println("The forward volatilities may not be constant");
        }

        //Calculating blackVariance
        System.out.println("BlackVariance = "+constantVolatility.blackVariance(date10.clone(), 20));

        //Calculating blackForwardVariance
        System.out.println("BlackForwardVariance = "+constantVolatility.blackForwardVariance(date10.clone(), date15.clone(), 20, true));

        System.out.println("//===============================BlackVarianceCurve================================");

        //Let's create black variance curve and calculate volatilities/variances by interpolating on
        //the created curve for a given strike.

        //-- Date today = DateFactory.getFactory().getTodaysDate();

        //Following is the time axis
        final Date[] dates = {date10.clone(), date15.clone(), date20.clone(), date25.clone(), date30.clone(), date40.clone() };

        //Following is the volatility axis
        final double[] volatilities = {0.1,0.2,0.3,0.4,0.5,0.6};

        //Following is the curve
        final BlackVarianceTermStructure varianceCurve = new BlackVarianceCurve(today,dates,volatilities, new Actual365Fixed(), false);
        ((BlackVarianceCurve)varianceCurve).setInterpolation();

        //Calculating blackVolatility using maturity as 12 days after today and strike as 20
        volatility1 = varianceCurve.blackVol(date12.clone(), 20);
        System.out.println("Interpolated BlackVolatility on BlackVarianceCurve = "+volatility1);

        //Calculating blackVolatility using maturity as 22 days after today and strike as 30
        volatility2 = varianceCurve.blackVol(date22.clone(), 30);
        System.out.println("Interpolated BlackVolatility on BlackVarianceCurve = "+volatility2);

        //Calculating blackVolatility using maturity as 32 days after today and strike as 40
        volatility3 = varianceCurve.blackVol(date32.clone(), 40);
        System.out.println("Interpolated BlackVolatility on BlackVarianceCurve = "+volatility3);


        //Calculating blackForwardVolatility between 12 days after today and 16 days after today with strike as 20
        forwardVolatility1 = varianceCurve.blackForwardVol(date12.clone(), date16.clone(), 20, true);
        System.out.println("Interpolated BlackForwardVolatility on BlackVarianceCurve = "+forwardVolatility1);

        //Calculating blackForwardVolatility between 22 days after today and 26 days after today with strike as 40
        forwardVolatility2 = varianceCurve.blackForwardVol(date22.clone(), date26.clone(), 40, true);
        System.out.println("Interpolated BlackForwardVolatility on BlackVarianceCurve = "+forwardVolatility2);

        //Calculating blackForwardVolatility between 27 days after today and 35 days after today with strike as 60
        forwardVolatility3 = varianceCurve.blackForwardVol(date27.clone(), date35.clone(), 60, true);
        System.out.println("Interpolated BlackForwardVolatility on BlackVarianceCurve = "+forwardVolatility3);


        //Calculating blackVariance using maturity as 12 days after today and strike as 20
        System.out.println("Interpolated BlackVariance on BlackVarianceCurve = "+varianceCurve.blackVariance(date12.clone(), 20));

        //Calculating blackForwardVariance between 12 days after today and 16 days after today with strike as 20
        System.out.println("Interpolated BlackForwardVariance on BlackVarianceCurve = "+varianceCurve.blackForwardVariance(date12.clone(), date16.clone(), 20, true));

        System.out.println("//===============================BlackVarianceSurface================================");

        //Let's create black variance surface and calculate volatilities/variances by interpolating on
        //the created curve for a given strike.

        //-- today = DateFactory.getFactory().getTodaysDate();

        //Following is the time axis
        final Date[] datesAxis = {date10.clone(), date15.clone(), date20.clone(), date25.clone(), date30.clone(), date40.clone() };

        final Array strikeAxis = new Array(new double[] {10,20,35,40,56,60});

        //Following is the volatility surface on which interpolations will be done
        final Matrix volatilityMatrix = new Matrix(new double[][] {
                {0.01,0.02,0.03,0.04,0.05,0.06},
                {0.02,0.03,0.04,0.05,0.06,0.07},
                {0.03,0.04,0.05,0.06,0.07,0.08},
                {0.3,0.5,0.6,0.7,0.8,0.9},
                {0.1,0.4,0.6,0.7,0.8,0.9},
                {0.2,0.5,0.6,0.7,0.8,0.9}
        });


        //Following is the variance surface where variance = f(strike,maturity) and f = function
        final BlackVarianceTermStructure varianceSurface = new BlackVarianceSurface(
                today, datesAxis,
                strikeAxis, volatilityMatrix, new Actual365Fixed(),
                Extrapolation.InterpolatorDefaultExtrapolation,
                Extrapolation.InterpolatorDefaultExtrapolation);
        ((BlackVarianceSurface)varianceSurface).setInterpolation(null);

        //As the surface has been set up to do interpolations so let's start calculating the volatilities for strikes
        //and maturities lying between the points as mentioned by strikesAxis and dateAxis.
        //Calculating blackVolatility using maturity as 12 days after today and strike as 18
        volatility1 = varianceSurface.blackVol(date12.clone(), 18);
        System.out.println("Interpolated BlackVolatility on BlackVarianceSurface = "+volatility1);

        //Calculating blackVolatility using maturity as 22 days after today and strike as 33
        volatility2 = varianceSurface.blackVol(date22.clone(), 33);
        System.out.println("Interpolated BlackVolatility on BlackVarianceSurface = "+volatility2);

        //Calculating blackVolatility using maturity as 32 days after today and strike as 45
        volatility3 = varianceSurface.blackVol(date32.clone(), 45);
        System.out.println("Interpolated BlackVolatility on BlackVarianceSurface = "+volatility3);


        //Calculating blackForwardVolatility between 12 days after today and 16 days after today with strike as 20
        forwardVolatility1 = varianceSurface.blackForwardVol(date12.clone(), date16.clone(), 20, true);
        System.out.println("Interpolated BlackForwardVolatility on BlackVarianceSurface = "+forwardVolatility1);

        //Calculating blackForwardVolatility between 22 days after today and 26 days after today with strike as 40
        forwardVolatility2 = varianceSurface.blackForwardVol(date22.clone(), date26.clone(), 40, true);
        System.out.println("Interpolated BlackForwardVolatility on BlackVarianceSurface = "+forwardVolatility2);

        //Calculating blackForwardVolatility between 27 days after today and 35 days after today with strike as 50
        forwardVolatility3 = varianceSurface.blackForwardVol(date27.clone(), date35.clone(), 50, true);
        System.out.println("Interpolated BlackForwardVolatility on BlackVarianceSurface = "+forwardVolatility3);


        //Calculating blackVariance using maturity as 12 days after today and strike as 20
        System.out.println("Interpolated BlackVariance on BlackVarianceSurface = "+varianceSurface.blackVariance(date12.clone(), 20));

        //Calculating blackForwardVariance between 12 days after today and 16 days after today with strike as 20
        System.out.println("Interpolated BlackForwardVariance on BlackVarianceSurface = "+varianceSurface.blackForwardVariance(date12.clone(), date16.clone(), 20, true));

        System.out.println("//================================ImpliedVolTermStructure=============================");

        //As mentioned in the java docs the implied volatility termstructure remains linked to
        //the underlying termstructure and changes to same are linked to ImpliedVolTermStructure
        //as well.

        //Lets use underlying as varianceCurve defined above by creating a relinkable handle as shown below
        final RelinkableHandle<BlackVolTermStructure> varianceCurveHandle = new RelinkableHandle<BlackVolTermStructure>(varianceCurve);
        final BlackVarianceTermStructure impliedVolTermStructure = new ImpliedVolTermStructure(varianceCurveHandle, today);

        //Calculating blackVolatility using maturity as 12 days after today and strike as 20
        volatility1 = varianceCurve.blackVol(date12.clone(), 20);
        final double impliedVolatility1 = impliedVolTermStructure.blackVol(date12.clone(), 20);

        if(volatility1 == impliedVolatility1){
            System.out.println("Interpolated BlackVolatility on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+volatility1);
        }

        //Calculating blackVolatility using maturity as 22 days after today and strike as 30
        volatility2 = varianceCurve.blackVol(date22.clone(), 30);
        final double impliedVolatility2 = impliedVolTermStructure.blackVol(date22.clone(), 30);
        if(volatility2 == impliedVolatility2){
            System.out.println("Interpolated BlackVolatility on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+volatility2);
        }

        //Calculating blackVolatility using maturity as 32 days after today and strike as 40
        volatility3 = varianceCurve.blackVol(date32.clone(), 40);
        final double impliedVolatility3 = impliedVolTermStructure.blackVol(date32.clone(), 40);
        if(volatility3 == impliedVolatility3){
            System.out.println("Interpolated BlackVolatility on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+volatility3);
        }


        //Calculating blackForwardVolatility between 12 days after today and 16 days after today with strike as 20
        forwardVolatility1 = varianceCurve.blackForwardVol(date12.clone(), date16.clone(), 20, true);
        final double impliedForwardVolatility1 = impliedVolTermStructure.blackForwardVol(date12.clone(), date16.clone(), 20, true);
        if(forwardVolatility1 == impliedForwardVolatility1){
            System.out.println("Interpolated BlackForwardVolatility on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+forwardVolatility1);
        }

        //Calculating blackForwardVolatility between 22 days after today and 26 days after today with strike as 40
        forwardVolatility2 = varianceCurve.blackForwardVol(date22.clone(), date26.clone(), 40, true);
        final double impliedForwardVolatility2 = impliedVolTermStructure.blackForwardVol(date22.clone(), date26.clone(), 40, true);
        if(forwardVolatility2 == impliedForwardVolatility2){
            System.out.println("Interpolated BlackForwardVolatility on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+forwardVolatility2);
        }

        //Calculating blackForwardVolatility between 27 days after today and 35 days after today with strike as 60
        forwardVolatility3 = varianceCurve.blackForwardVol(date27.clone(), date35.clone(), 60, true);
        final double impliedForwardVolatility3 = impliedVolTermStructure.blackForwardVol(date27.clone(), date35.clone(), 60, true);
        if(forwardVolatility3 == impliedForwardVolatility3){
            System.out.println("Interpolated BlackForwardVolatility on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+forwardVolatility3);
        }


        //Calculating blackVariance using maturity as 12 days after today and strike as 20
        final double variance = varianceCurve.blackVariance(date12.clone(), 20);
        final double impliedVariance = impliedVolTermStructure.blackVariance(date12.clone(), 20);
        if(variance == impliedVariance){
            System.out.println("Interpolated BlackVariance on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+varianceCurve.blackVariance(date12.clone(), 20));
        }

        //Calculating blackForwardVariance between 12 days after today and 16 days after today with strike as 20
        final double forwardVariance = varianceCurve.blackForwardVariance(date12.clone(), date16.clone(), 20, true);
        final double impliedForwardVariance = impliedVolTermStructure.blackForwardVariance(date12.clone(), date16.clone(), 20, true);
        if(forwardVariance == impliedForwardVariance){
            System.out.println("Interpolated BlackForwardVariance on BlackVarianceCurve is same for varianceCurve and ImpliedVolTermStructure derived on it and = "+varianceCurve.blackForwardVariance(date12.clone(), date16.clone(), 20, true));
        }

        System.out.println("//================================LocalConstantVol=======================================");

        //LocalConstantVolatility is essentially same as BlackConstantVol and is a local volatility version of BlackConstantVol

        //Let's set the quoteValue = 0.05 and use the constantVolatility a BlackConstantVol
        volatilityQuote.setValue(0.05);
        final LocalVolTermStructure localConstantVolatility = new LocalConstantVol(2,new UnitedStates(Market.NYSE),handleToVolatilityQuote, new Actual365Fixed());

        //Calculating blackVolatility using maturity as 10 days after today and strike as 20
        if(constantVolatility.blackVol(date10.clone(), 20) == localConstantVolatility.localVol(date10.clone(), 20,true)){
            System.out.println("BlackVolatility and LocalVolatility are same and are = "+localConstantVolatility.localVol(date10.clone(), 20,true));
        }
View Full Code Here

TOP

Related Classes of org.jquantlib.time.calendars.UnitedStates

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.