New Version 8.1!

Supports .NET 6.0. Try it for free with our fully functional 60-day trial version.

Get from Nuget

QuickStart Samples

Simple Time Series QuickStart Sample (C#)

Illustrates how to perform simple operations on time series data using classes in the Extreme.Statistics.TimeSeriesAnalysis namespace in C#.

Visual Basic code F# code IronPython code Back to QuickStart Samples

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;

using Extreme.DataAnalysis;
using Extreme.Mathematics;
using Extreme.Statistics;

namespace Extreme.Numerics.QuickStart.CSharp
    /// <summary>
    /// Illustrates the use of the TimeSeriesCollection class to represent
    /// and manipulate time series data.
    /// </summary>
    class SimpleTimeSeries
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
            // Time series collections can be created in a variety of ways.
            // Here we use an ADO.NET data table:
            DataTable seriesTable = LoadTimeSeriesData();
            var timeSeries = DataFrame.FromDataTable<DateTime>(seriesTable, "Date");

            // The RowCount property returns the number of
            // observations:
            Console.WriteLine("# observations: {0}", timeSeries.RowCount);

            // Accessing variables

            // Variables are accessed by name or numeric index.
            // They need to be cast to the appropriate specialized
            // type using the As() method:
            var close = timeSeries["Close"].As<double>();
            Console.WriteLine("Average close price: ${0:F2}", close.Mean());

            // Variables can also be accessed by numeric index:
            Console.WriteLine("3rd variable: {0}", timeSeries[2].Name);

            // The GetRows method returns the data from the specified range.
            DateTime y2004 = new DateTime(2004, 1, 1);
            DateTime y2005 = new DateTime(2005, 1, 1);
            var series2004 = timeSeries.GetRows(y2004, y2005);
            Console.WriteLine("Opening price on the first trading day of 2004: {0}",

            // Transforming the Frequency

            // The first step is to define the aggregator function
            // for each variable. This function specifies how each
            // observation in the new time series is calculated
            // from the observations in the original series.

            // The Aggregators class has a number of 
            // pre-defined aggregator functions.

            // We create a dictionary that maps column names
            // to aggregators:
            var aggregators = new Dictionary<string, AggregatorGroup>()
                { "Open", Aggregators.First },
                { "Close", Aggregators.Last },
                { "High", Aggregators.Max },
                { "Low", Aggregators.Min },
                { "Volume", Aggregators.Sum}

            // We can then resample the data frame in accordance with
            // a recurrence pattern we specify, in this case monthly:
            var monthlySeries = timeSeries.Resample(Recurrence.Monthly, aggregators);

            // We can specify a subset of the series by selecting it
            // from the data frame first:
            monthlySeries = timeSeries.GetRows(y2004, y2005)
                .Resample(Recurrence.Monthly, aggregators);

            // We can now print the results:
            Console.WriteLine("Monthly statistics for Microsoft Corp. (MSFT)");

            Console.Write("Press any key to exit.");

        private static DataTable LoadTimeSeriesData()
            string filename = @"..\..\..\..\Data\MicrosoftStock.xls";
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename+";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
            OleDbConnection cnn = null;
            DataSet ds = new DataSet();
                cnn = new OleDbConnection(connectionString);
                OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [MicrosoftStock$]", cnn);
            catch (OleDbException ex)
                if (cnn != null)
            return ds.Tables[0];