Version 8.1 |
Supports .NET 7.0 and earlier.
Try it for free with our fully functional
30-day trial version.
|
QuickStart Samples
Simple Time Series QuickStart Sample (IronPython)
Illustrates how to perform simple operations on time series data using classes in the Extreme.Statistics.TimeSeriesAnalysis namespace in IronPython.
C# code
Visual Basic code
F# code
Back to QuickStart Samples
import numerics
import clr
clr.AddReference("System.Data")
from System.Data import *
from System.Data.OleDb import *
from System import DateTime
from Extreme.Statistics import *
from Extreme.Statistics.TimeSeriesAnalysis import *
#/ Illustrates the use of the TimeSeriesCollection class to represent
#/ and manipulate time series data.
# Time series collections can be created in a variety of ways.
# Here we use an ADO.NET data table:
def LoadTimeSeriesData():
filename = r"..\Data\MicrosoftStock.xls"
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename+";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
cnn = None
ds = DataSet()
try:
cnn = OleDbConnection(connectionString)
cnn.Open()
adapter = OleDbDataAdapter("Select * from [MicrosoftStock$]", cnn)
adapter.Fill( ds)
except OleDbException as ex:
print ex.InnerException
finally:
if cnn != None:
cnn.Close()
return ds.Tables[0]
seriesTable = LoadTimeSeriesData()
timeSeries = TimeSeriesCollection(seriesTable)
# The RowCount property returns the number of
# observations:
print "# observations:", timeSeries.Observations.Count
# The StartOfPeriodVariable property returns the
# DateTimeVariable that contains the start times
# for each period.
print "First date:", timeSeries.StartOfPeriodVariable.Minimum
# The EndOfPeriodVariable property returns the
# DateTimeVariable that contains the end times
# for each period.
print "Last date:", timeSeries.EndOfPeriodVariable.Maximum
# Data in a TimeSeriesCollection is always sorted
# in ascending time order.
#
# Accessing variables
#
# Variables are accessed by name or numeric index.
# They need to be cast to the appropriate specialized
# type (NumericalVariable, DateTimeVariable, etc.)
close = timeSeries["Close"]
print "Average close price: ${0:F2}".format(close.Mean)
# Variables can also be accessed by numeric index:
print "3rd variable:", timeSeries[2].Name
# The GetSubset method returns the data from the specified range.
y2004 = DateTime(2004, 1, 1)
y2005 = DateTime(2005, 1, 1)
series2004 = timeSeries.CreateSubset(y2004, y2005)
print "Opening price on the first trading day of 2004:", series2004["Open"].GetValue(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 Aggregator class has a number of
# pre-defined aggregator functions:
timeSeries["Open"].Aggregator = Aggregator.First
timeSeries["Close"].Aggregator = Aggregator.Last
timeSeries["High"].Aggregator = Aggregator.Maximum
timeSeries["Low"].Aggregator = Aggregator.Minimum
timeSeries["Volume"].Aggregator = Aggregator.Sum
# We can specify a subset of the series by providing
# the start and end dates.
# The TransformFrequency method returns a new series
# containing the aggregated data:
monthlySeries = timeSeries.TransformFrequency(y2004, y2005, DateTimeUnit.Month)
# We can now print the results:
print "Monthly statistics for Microsoft Corp. (MSFT)"
print "Month Open Close High Low Volume"
for row in range(monthlySeries.Observations.Count):
print " {0:MMM} {1:.2f} {2:.2f} {3:.2f} {4:.2f} {5:10}" \
.format(monthlySeries.StartOfPeriodVariable[row], monthlySeries["Open"].GetValue(row), \
monthlySeries["Close"].GetValue(row), monthlySeries["High"].GetValue(row), monthlySeries["Low"].GetValue(row), monthlySeries["Volume"].GetValue(row))
Copyright © 2003-2023, Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, Visual Studio.NET, and the Optimized for Visual Studio logo
are registered trademarks of Microsoft Corporation.