Ejemplo n.º 1
0
        public void CreateQuarter3_AsExpected(int year)
        {
            var quarter  = Quarter.CreateQuarter3(year);
            var expected = new Quarter(year, 3);

            Assert.AreEqual(expected, quarter);
        }
        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);
            }
        }
Ejemplo n.º 3
0
        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
        }
Ejemplo n.º 4
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();
        }