Beispiel #1
0
        public void CreateQuarter2_AsExpected(int year)
        {
            var quarter  = Quarter.CreateQuarter2(year);
            var expected = new Quarter(year, 2);

            Assert.AreEqual(expected, quarter);
        }
        private static void Comparing()
        {
            #region comparing

            var qu119 = new Quarter(2019, 1);
            var qu219 = Quarter.CreateQuarter2(2019);

            // IComparable<T>.CompareTo
            Console.WriteLine(qu119.CompareTo(qu219));
            Console.WriteLine();

            // IEquatable<T>.Equals
            Console.WriteLine(qu119.Equals(qu219));
            Console.WriteLine();

            // Comparison operators
            Console.WriteLine(qu119 < qu219);
            Console.WriteLine(qu119 <= qu219);
            Console.WriteLine(qu119 == qu219);
            Console.WriteLine(qu119 != qu219);
            Console.WriteLine(qu119 > qu219);
            Console.WriteLine(qu119 >= qu219);
            Console.WriteLine();
            #endregion
        }
 private static void Expanding()
 {
     #region expanding
     var qu219 = Quarter.CreateQuarter2(2019);
     IEnumerable <Month> allMonthsInQ119 = qu219.Expand <Month>();
     Console.WriteLine("All the months in Q2-19:");
     foreach (Month month in allMonthsInQ119)
     {
         Console.WriteLine(month);
     }
     Console.WriteLine();
     #endregion expanding
 }
        public void Build_InputContractsAllEqualAndNoSeasonalAdjustments_ResultsInFlatCurve()
        {
            const double flatPrice = 84.54;
            var          curve     = new MaxSmoothnessSplineCurveBuilder <Day>()
                                     .AddContract(Month.CreateJanuary(2019), flatPrice)
                                     .AddContract(Month.CreateFebruary(2019), flatPrice)
                                     .AddContract(Month.CreateMarch(2019), flatPrice)
                                     .AddContract(Quarter.CreateQuarter2(2019), flatPrice)
                                     .AddContract(Quarter.CreateQuarter3(2019), flatPrice)
                                     .AddContract(Season.CreateWinter(2019), flatPrice)
                                     .BuildCurve();

            foreach (double price in curve.Data)
            {
                Assert.AreEqual(flatPrice, price, 1E-12);
            }
        }
        private static void ExtensionMethods()
        {
            #region extension_methods
            var quarterStart = Quarter.CreateQuarter3(2020);
            var quarterEnd   = Quarter.CreateQuarter2(2021);
            Console.WriteLine($"All the quarters from {quarterStart} to {quarterEnd}");
            foreach (Quarter quarter in quarterStart.EnumerateTo(quarterEnd))
            {
                Console.WriteLine(quarter);
            }

            Console.WriteLine();
            var dayStart = new Day(2019, 8, 30);
            var dayEnd   = new Day(2019, 9, 4);
            Console.WriteLine($"All week days from {dayStart} to {dayEnd}");
            foreach (Day weekday in dayStart.EnumerateWeekdays(dayEnd))
            {
                Console.WriteLine(weekday);
            }
            Console.WriteLine();
            #endregion extension_methods
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            // The following code shows how to use the spline to derive a smooth daily curve from
            // monthly and quarterly granularity input contract prices. Also demonstrated is the
            // optional seasonal adjustment factor, in this case used to apply day-of-week seasonality.

            var dayOfWeekAdjustment = new Dictionary <DayOfWeek, double>
            {
                [DayOfWeek.Monday]    = 0.95,
                [DayOfWeek.Tuesday]   = 0.99,
                [DayOfWeek.Wednesday] = 1.05,
                [DayOfWeek.Thursday]  = 1.01,
                [DayOfWeek.Friday]    = 0.98,
                [DayOfWeek.Saturday]  = 0.92,
                [DayOfWeek.Sunday]    = 0.91
            };

            DoubleCurve <Day> curve = new MaxSmoothnessSplineCurveBuilder <Day>()
                                      .AddContract(Month.CreateJuly(2019), 77.98)
                                      .AddContract(Month.CreateAugust(2019), 76.01)
                                      .AddContract(Month.CreateSeptember(2019), 78.74)
                                      .AddContract(Quarter.CreateQuarter4(2019), 85.58)
                                      .AddContract(Quarter.CreateQuarter1(2020), 87.01)
                                      .WithMultiplySeasonalAdjustment(day => dayOfWeekAdjustment[day.DayOfWeek])
                                      .BuildCurve();

            Console.WriteLine(curve.FormatData("F5"));

            Console.WriteLine();
            Console.WriteLine();

            //===============================================================================================
            // APPLYING AN ALTERNATIVE WEIGHTING SCHEME

            // By default, the spline calculations assume averages are weighted by the number of minutes in a contract period.
            // This assumption is fine for instruments where the commodity is delivered at a constant rate, e.g. natural gas forwards.
            // An alternative weighting scheme can be added by calling WithAverageWeighting and supplying a weighting scheme as a function.
            // The below example makes use of the Weighting helper class to provide the weighting function as the count of business days.
            // An example of when such a weighting scheme should be used is for oil swaps, based on an index which is only published on a business day
            // to create a monthly curve from quarterly granularity inputs.

            var holidays = new List <Day>()
            {
                new Day(2020, 1, 1)
            };
            Func <Month, double> busDayWeight = Weighting.BusinessDayCount <Month>(holidays);

            var contracts = new List <Contract <Month> >()
            {
                Contract <Month> .Create(Quarter.CreateQuarter4(2019), 76.58),
                Contract <Month> .Create(Quarter.CreateQuarter1(2020), 77.20),
                Contract <Month> .Create(Quarter.CreateQuarter2(2020), 76.01),
                Contract <Month> .Create(Quarter.CreateQuarter3(2020), 74.95),
                Contract <Month> .Create(Quarter.CreateQuarter4(2020), 74.92),
            };

            DoubleCurve <Month> curveBusDayWeight = new MaxSmoothnessSplineCurveBuilder <Month>()
                                                    .AddContracts(contracts)
                                                    .WithWeighting(busDayWeight)
                                                    .BuildCurve();

            Console.WriteLine("Derived smooth curve with business day weighting:");
            Console.WriteLine(curveBusDayWeight.FormatData("F5", -1));

            Console.ReadKey();
        }