diff --git a/tests/test_history.py b/tests/test_history.py index 2e8473e5..a3c9ae5b 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -41,131 +41,6 @@ from zipline.sources import RandomWalkSource, DataFrameSource import zipline.utils.factory as factory from zipline.utils.test_utils import subtest -# Cases are over the July 4th holiday, to ensure use of trading calendar. - -# March 2013 -# Su Mo Tu We Th Fr Sa -# 1 2 -# 3 4 5 6 7 8 9 -# 10 11 12 13 14 15 16 -# 17 18 19 20 21 22 23 -# 24 25 26 27 28 29 30 -# 31 -# April 2013 -# Su Mo Tu We Th Fr Sa -# 1 2 3 4 5 6 -# 7 8 9 10 11 12 13 -# 14 15 16 17 18 19 20 -# 21 22 23 24 25 26 27 -# 28 29 30 -# -# May 2013 -# Su Mo Tu We Th Fr Sa -# 1 2 3 4 -# 5 6 7 8 9 10 11 -# 12 13 14 15 16 17 18 -# 19 20 21 22 23 24 25 -# 26 27 28 29 30 31 -# -# June 2013 -# Su Mo Tu We Th Fr Sa -# 1 -# 2 3 4 5 6 7 8 -# 9 10 11 12 13 14 15 -# 16 17 18 19 20 21 22 -# 23 24 25 26 27 28 29 -# 30 -# July 2013 -# Su Mo Tu We Th Fr Sa -# 1 2 3 4 5 6 -# 7 8 9 10 11 12 13 -# 14 15 16 17 18 19 20 -# 21 22 23 24 25 26 27 -# 28 29 30 31 -# -# Times to be converted via: -# pd.Timestamp('2013-07-05 9:31', tz='US/Eastern').tz_convert('UTC')}, - -INDEX_TEST_CASES_RAW = { - 'week of daily data': { - 'input': {'bar_count': 5, - 'frequency': '1d', - 'algo_dt': '2013-07-05 9:31AM'}, - 'expected': [ - '2013-06-28 4:00PM', - '2013-07-01 4:00PM', - '2013-07-02 4:00PM', - '2013-07-03 1:00PM', - '2013-07-05 9:31AM', - ] - }, - 'five minutes on july 5th open': { - 'input': {'bar_count': 5, - 'frequency': '1m', - 'algo_dt': '2013-07-05 9:31AM'}, - 'expected': [ - '2013-07-03 12:57PM', - '2013-07-03 12:58PM', - '2013-07-03 12:59PM', - '2013-07-03 1:00PM', - '2013-07-05 9:31AM', - ] - }, -} - - -def to_timestamp(dt_str): - return pd.Timestamp(dt_str, tz='US/Eastern').tz_convert('UTC') - - -def convert_cases(cases): - """ - Convert raw strings to values comparable with system data. - """ - cases = cases.copy() - for case in cases.values(): - case['input']['algo_dt'] = to_timestamp(case['input']['algo_dt']) - case['expected'] = pd.DatetimeIndex([to_timestamp(dt_str) for dt_str - in case['expected']]) - return cases - -INDEX_TEST_CASES = convert_cases(INDEX_TEST_CASES_RAW) - - -def get_index_at_dt(case_input, env): - history_spec = history.HistorySpec( - case_input['bar_count'], - case_input['frequency'], - None, - False, - env=env, - data_frequency='minute', - ) - return history.index_at_dt(history_spec, case_input['algo_dt'], env=env) - - -class TestHistoryIndex(TestCase): - - @classmethod - def setUpClass(cls): - cls.environment = TradingEnvironment() - - @classmethod - def tearDownClass(cls): - del cls.environment - - @parameterized.expand( - [(name, case['input'], case['expected']) - for name, case in INDEX_TEST_CASES.items()] - ) - def test_index_at_dt(self, name, case_input, expected): - history_index = get_index_at_dt(case_input, self.environment) - - history_series = pd.Series(index=history_index) - expected_series = pd.Series(index=expected) - - pd.util.testing.assert_series_equal(history_series, expected_series) - class TestHistoryContainer(TestCase): diff --git a/zipline/history/__init__.py b/zipline/history/__init__.py index d9a8a1cc..9efe333b 100644 --- a/zipline/history/__init__.py +++ b/zipline/history/__init__.py @@ -15,8 +15,6 @@ from . history import ( HistorySpec, - days_index_at_dt, - index_at_dt, Frequency, ) @@ -24,8 +22,6 @@ from . import history_container __all__ = [ 'HistorySpec', - 'days_index_at_dt', - 'index_at_dt', 'history_container', 'Frequency', ] diff --git a/zipline/history/history.py b/zipline/history/history.py index 8244aa01..f83f22ac 100644 --- a/zipline/history/history.py +++ b/zipline/history/history.py @@ -15,7 +15,6 @@ from __future__ import division -import numpy as np import pandas as pd import re @@ -284,59 +283,3 @@ class HistorySpec(object): def __repr__(self): return ''.join([self.__class__.__name__, "('", self.key_str, "')"]) - - -def days_index_at_dt(history_spec, algo_dt, env): - """ - Get the index of a frame to be used for a get_history call with daily - frequency. - """ - # Get the previous (bar_count - 1) days' worth of market closes. - day_delta = (history_spec.bar_count - 1) * history_spec.frequency.num - market_closes = env.open_close_window( - algo_dt, - day_delta, - offset=(-day_delta), - step=history_spec.frequency.num, - ).market_close - - if history_spec.frequency.data_frequency == 'daily': - market_closes = market_closes.apply(pd.tslib.normalize_date) - - # Append the current algo_dt as the last index value. - # Using the 'rawer' numpy array values here because of a bottleneck - # that appeared when using DatetimeIndex - return np.append(market_closes.values, algo_dt) - - -def minutes_index_at_dt(history_spec, algo_dt, env): - """ - Get the index of a frame to be used for a get_history_call with minutely - frequency. - """ - # TODO: This is almost certainly going to be too slow for production. - return env.market_minute_window( - algo_dt, - history_spec.bar_count, - step=-1, - )[::-1] - - -def index_at_dt(history_spec, algo_dt, env): - """ - Returns index of a frame returned by get_history() with the given - history_spec and algo_dt. - - The resulting index will have @history_spec.bar_count bars, increasing in - units of @history_spec.frequency, terminating at the given @algo_dt. - - Note: The last bar of the returned frame represents an as-of-yet incomplete - time window, so the delta between the last and second-to-last bars is - usually always less than `@history_spec.frequency` for frequencies greater - than 1m. - """ - frequency = history_spec.frequency - if frequency.unit_str == 'd': - return days_index_at_dt(history_spec, algo_dt, env) - elif frequency.unit_str == 'm': - return minutes_index_at_dt(history_spec, algo_dt, env)