AW: joint calendar

Posted by Jens Thiel on
URL: http://quantlib.414.s1.nabble.com/joint-calendar-tp2345p2349.html

Luigi, Jack,

I just implemented it in C#, see below. I introduced

a) the concept of a main calendar, which is also used for globalization/i18n
stuff
b) a Combine enumeration ("||/&& flag")

Default combination is resulting in combined holidays (that is common
business days). More than three calendars can be combined using
CombinedCalendar like standard boolean algebra. Not tested yet...


Jens.




/// <summary>
/// Calendar combining holidays or business days from two other calendars.
/// </summary>
[ RcsId("$Id: $") ]
[ ComVisible(false) ]
public sealed class CombinedCalendar : CalendarBase
{
        /// <summary>
        /// Holidays / business days combination.
        /// </summary>
        /// <remarks>
        /// Use to combine calendars on common business days or holidays:
        /// <list type="table">
        /// <listheader>
        /// <term>Combination mode</term>
        /// <description>Description</description>
        /// </listheader>
        /// <item>
        /// <term><c>Holidays</c></term>
        /// <description>
        /// The resulting calendar has holidays from both calendars.
        /// That is, in other words, only common business days.
        /// </description>
        /// </item>
        /// <item>
        /// <term><c>BusinessDays</c></term>
        /// <description>
        /// The resulting calendar has business days from both calendars.
        /// That is, in other words, only common holidays.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        public enum Combine
        {
                /// <summary>Combine holidays of the other calendar.</summary>
                Holidays,
                /// <summary>Combine business days of the other calendar.</summary>
                BusinessDays
        };

        #region Constructors

        /// <summary>
        /// Combine a main calendar with holidays from another calendar.
        /// </summary>
        /// <param name="mainCalendar">
        /// The main calendar (also used for globalization settings).</param>
        /// <param name="otherCalendar">
        /// The calendar which adds holidays to the main calendar.
        /// </param>
        /// <remarks>
        /// The combined calendars use the <c>Holidays</c> enumeration from <see
cref="Combine"/>.
        /// The resulting name can be localized in the resource file.
        /// </remarks>
        public CombinedCalendar(IBusinessCalendar mainCalendar, IBusinessCalendar
otherCalendar)
                : this(mainCalendar, otherCalendar,
                Strings.Format(null, "CalCombHol", mainCalendar, Combine.Holidays,
otherCalendar),
                Combine.Holidays)
        {}

        /// <summary>
        /// Combine a main calendar with holidays from another calendar.
        /// </summary>
        /// <param name="mainCalendar">
        /// The main calendar (also used for globalization settings).</param>
        /// <param name="otherCalendar">
        /// The calendar which adds holidays to the main calendar.
        /// </param>
        /// <param name="name">
        /// Name of the combined calendar.
        /// </param>
        /// <remarks>
        /// The combined calendars use the <c>Holidays</c> enumeration from <see
cref="Combine"/>.
        /// </remarks>
        public CombinedCalendar(IBusinessCalendar mainCalendar, IBusinessCalendar
otherCalendar,
                string name)
                : this(mainCalendar, otherCalendar, name, Combine.Holidays)
        {}

        /// <summary>
        /// Combine a main calendar with another calendar.
        /// </summary>
        /// <param name="mainCalendar">
        /// The main calendar (also used for globalization settings).</param>
        /// <param name="otherCalendar">
        /// The calendar which adds holidays to the main calendar.
        /// </param>
        /// <param name="name">
        /// Name of the combined calendar.
        /// </param>
        /// <param name="mode">
        /// The combination mode of the two calendars.
        /// </param>
        /// <remarks>
        /// The <see cref="Combine">combination mode</see> can be used to combine
        /// calendars on common business days or holidays:
        /// <list type="table">
        /// <listheader>
        /// <term><see cref="Combine"/> enumeration</term>
        /// <description>Description</description>
        /// </listheader>
        /// <item>
        /// <term><c>Holidays</c></term>
        /// <description>
        /// The resulting calendar has holidays from both calendars.
        /// That is, in other words, only common business days.
        /// </description>
        /// </item>
        /// <item>
        /// <term><c>BusinessDays</c></term>
        /// <description>
        /// The resulting calendar has business days from both calendars.
        /// That is, in other words, only common holidays.
        /// </description>
        /// </item>
        /// </list>
        /// </remarks>
        public CombinedCalendar(IBusinessCalendar mainCalendar, IBusinessCalendar
otherCalendar,
                string name, Combine mode)
                : base(name, mainCalendar.Culture, mainCalendar.Calendar)
        {
                if( /* mainCalendar == null || */ otherCalendar == null)
                        throw new NullReferenceException(
                                Strings.Get(GetType(), "CalNullRef") );
                this.mainCalendar = mainCalendar;
                this.otherCalendar = otherCalendar;
                this.combineBusinessDays = (mode == Combine.BusinessDays);
        }

        #endregion

        private IBusinessCalendar mainCalendar;
        private IBusinessCalendar otherCalendar;
        private bool combineBusinessDays;

        /// <summary>
        /// Returns <c>true</c> iff the date is a business day for the given
market.
        /// </summary>
        /// <param name="date">The <see cref="DateTime"/> to check.</param>
        /// <returns><c>true</c> iff the date is a business day for the given
market.</returns>
        override public bool IsBusinessDay(DateTime date)
        {
                bool m = mainCalendar.IsBusinessDay(date);
                bool o = otherCalendar.IsBusinessDay(date);
                // at least two of three conditions must be true
                return (m&&o) || (m&&combineBusinessDays) || (o&&combineBusinessDays);
        }
}





> -----Ursprungliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]]Im Auftrag von Luigi
> Ballabio
> Gesendet: Montag, 27. Januar 2003 11:39
> An: [hidden email]
> Cc: [hidden email]
> Betreff: Re: [Quantlib-users] joint calendar
>
>
> At 08:35 AM 1/27/03 +0000, Chak Jack Wong wrote:
> >The behavior in Jens' e-mail is exactly what we need.  This is
> mainly used
> >for cross-ccy swap.  for USD and Yen swap on Libor , the holiday
> is typically
> >NewYork, London and Tokyo (i.e. union of holiday.), and for
> quite a lot of
> >swiss product, we use Zurich/London.   This is rather important
> (yet pretty
> >simple)
>
> Ok, so the relation is:
>
> joint.isBusinessDay(d) <= c1.isBusinessDay(d) && c2.isBusinessDay(d)
>
> Does anybody think we might need the same thing but with ||
> instead of && ?
>
> Also, I see that one might need more than two calendars. Is three the
> maximum anybody saw, or did anybody used more complex stuff?
>
>
> >Is it possible to add this simple class to the 0.3.1 release?
>
> Not to 0.3.1, sorry---it is already frozen. I can send you the
> files (once
> I have them, of course) if you have problems accessing the CVS version.
>
> Later,
>          Luigi
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
> http://www.vasoftware.com
> _______________________________________________
> Quantlib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>
>