1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (C) 2008-2019 Vicent Mas. All rights reserved 5# 6# This program is free software: you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation, either version 3 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18# 19# Author: Vicent Mas - vmas@vitables.org 20 21"""Storing time series created with scikits.timeseries module in PyTables. 22Example 2. 23 24Notes: 25 -The dates from the yahoo quotes module get returned as integers, which happen 26 to correspond to the integer representation of 'DAILY' frequency dates in the 27 scikits.timeseries module. 28 -`fill_missing_dates` will insert masked values for any missing data points. 29 Note that you could plot the series without doing this, but it would cause 30 missing values to be linearly interpolated rather than left empty in the plot 31""" 32 33import os 34import datetime 35 36from matplotlib.finance import quotes_historical_yahoo 37import scikits.timeseries as ts 38import scikits.timeseries.lib.tstables as tstab 39 40startdate = datetime.date(2002, 1, 5) 41enddate = datetime.date(2003, 12, 1) 42 43# retrieve data from yahoo. 44# Data format is [(d, open, close, high, low, volume), ...] where d is 45# a floating point representation of the number of days since 01-01-01 UTC 46quotes = quotes_historical_yahoo('INTC', startdate, enddate) 47 48# Create a DateArray of daily dates and convert it to business day frequency 49dates = ts.date_array([q[0] for q in quotes], freq='DAILY').asfreq('BUSINESS') 50 51opens = [q[1] for q in quotes] 52 53# opens: the data portion of the timeserie 54# dates: the date portion of the timeserie 55raw_series = ts.time_series(opens, dates) 56test_series = raw_series 57#test_series = ts.fill_missing_dates(raw_series, fill_value=-1) 58 59# Write to a PyTables file 60output_dir = '../timeseries' 61try: 62 os.mkdir(output_dir) 63except OSError: 64 pass 65 66hdf5_name = 'scikits_test2.hdf5' 67filepath_hdf5 = os.path.join(output_dir, hdf5_name) 68h5file = tstab.open_file(filepath_hdf5, mode="w", 69title='Example table with csikits time series') 70group_doc = h5file.create_group("/", 'examples', 'Test Data') 71table = h5file.createTimeSeriesTable(group_doc, 'Example_2', test_series) 72h5file.close() 73 74