From affe87319b12c1c0ac7a11d64370cd66ea3fa0be Mon Sep 17 00:00:00 2001 From: MBounouar Date: Thu, 24 Nov 2022 15:54:44 +0100 Subject: [PATCH 01/15] replace deprecated abstractproperty --- src/zipline/data/bar_reader.py | 14 ++++--- src/zipline/data/history_loader.py | 39 ++++++------------- src/zipline/data/session_bars.py | 5 ++- .../pipeline/loaders/earnings_estimates.py | 5 ++- src/zipline/pipeline/term.py | 14 ++++--- src/zipline/testing/core.py | 5 ++- 6 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/zipline/data/bar_reader.py b/src/zipline/data/bar_reader.py index 01b2db9983..9a2752e5a7 100644 --- a/src/zipline/data/bar_reader.py +++ b/src/zipline/data/bar_reader.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod, abstractproperty +from abc import ABCMeta, abstractmethod class NoDataOnDate(Exception): @@ -42,7 +42,8 @@ class NoDataForSid(Exception): class BarReader(object, metaclass=ABCMeta): - @abstractproperty + @property + @abstractmethod def data_frequency(self): pass @@ -69,7 +70,8 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): """ pass - @abstractproperty + @property + @abstractmethod def last_available_dt(self): """ Returns @@ -79,7 +81,8 @@ def last_available_dt(self): """ pass - @abstractproperty + @property + @abstractmethod def trading_calendar(self): """ Returns the zipline.utils.calendar.trading_calendar used to read @@ -87,7 +90,8 @@ def trading_calendar(self): """ pass - @abstractproperty + @property + @abstractmethod def first_trading_day(self): """ Returns diff --git a/src/zipline/data/history_loader.py b/src/zipline/data/history_loader.py index 54f199a634..0fe6e875c0 100644 --- a/src/zipline/data/history_loader.py +++ b/src/zipline/data/history_loader.py @@ -15,7 +15,6 @@ from abc import ( ABCMeta, abstractmethod, - abstractproperty, ) from numpy import concatenate @@ -93,9 +92,7 @@ def _get_adjustments_in_range(self, asset, dts, field): end = normalize_date(dts[-1]) adjs = {} if field != "volume": - mergers = self._adjustments_reader.get_adjustments_for_sid( - "mergers", sid - ) + mergers = self._adjustments_reader.get_adjustments_for_sid("mergers", sid) for m in mergers: dt = m[0] if start < dt <= end: @@ -106,9 +103,7 @@ def _get_adjustments_in_range(self, asset, dts, field): adjs[adj_loc].append(mult) except KeyError: adjs[adj_loc] = [mult] - divs = self._adjustments_reader.get_adjustments_for_sid( - "dividends", sid - ) + divs = self._adjustments_reader.get_adjustments_for_sid("dividends", sid) for d in divs: dt = d[0] if start < dt <= end: @@ -173,9 +168,7 @@ def load_pricing_adjustments(self, columns, dts, assets): out[i] = adjs return out - def _make_adjustment( - self, adjustment_type, front_close, back_close, end_loc - ): + def _make_adjustment(self, adjustment_type, front_close, back_close, end_loc): adj_base = back_close - front_close if adjustment_type == "mul": adj_value = 1.0 + adj_base / front_close @@ -217,17 +210,11 @@ def _get_adjustments_in_range(self, cf, dts, field): ) if isnull(last_front_dt) or isnull(last_back_dt): continue - front_close = self._bar_reader.get_value( - front_sid, last_front_dt, "close" - ) - back_close = self._bar_reader.get_value( - back_sid, last_back_dt, "close" - ) + front_close = self._bar_reader.get_value(front_sid, last_front_dt, "close") + back_close = self._bar_reader.get_value(back_sid, last_back_dt, "close") adj_loc = dts.searchsorted(roll_dt) end_loc = adj_loc - 1 - adj = self._make_adjustment( - cf.adjustment, front_close, back_close, end_loc - ) + adj = self._make_adjustment(cf.adjustment, front_close, back_close, end_loc) try: adjs[adj_loc].append(adj) except KeyError: @@ -306,9 +293,7 @@ def __init__( if equity_adjustment_reader is not None: self._adjustment_readers[ Equity - ] = HistoryCompatibleUSEquityAdjustmentReader( - equity_adjustment_reader - ) + ] = HistoryCompatibleUSEquityAdjustmentReader(equity_adjustment_reader) if roll_finders: self._adjustment_readers[ ContinuousFuture @@ -324,11 +309,13 @@ def __init__( } self._prefetch_length = prefetch_length - @abstractproperty + @property + @abstractmethod def _frequency(self): pass - @abstractproperty + @property + @abstractmethod def _calendar(self): pass @@ -535,9 +522,7 @@ def history(self, assets, dts, field, is_perspective_after): ------- out : np.ndarray with shape(len(days between start, end), len(assets)) """ - block = self._ensure_sliding_windows( - assets, dts, field, is_perspective_after - ) + block = self._ensure_sliding_windows(assets, dts, field, is_perspective_after) end_ix = self._calendar.searchsorted(dts[-1]) return concatenate( diff --git a/src/zipline/data/session_bars.py b/src/zipline/data/session_bars.py index 0846749621..00893a985d 100644 --- a/src/zipline/data/session_bars.py +++ b/src/zipline/data/session_bars.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import abstractproperty, abstractmethod +from abc import abstractmethod from zipline.data.bar_reader import BarReader @@ -25,7 +25,8 @@ class SessionBarReader(BarReader): def data_frequency(self): return "session" - @abstractproperty + @property + @abstractmethod def sessions(self): """ Returns diff --git a/src/zipline/pipeline/loaders/earnings_estimates.py b/src/zipline/pipeline/loaders/earnings_estimates.py index 01a7692c82..6c52a26e0c 100644 --- a/src/zipline/pipeline/loaders/earnings_estimates.py +++ b/src/zipline/pipeline/loaders/earnings_estimates.py @@ -1,4 +1,4 @@ -from abc import abstractmethod, abstractproperty +from abc import abstractmethod from interface import implements import numpy as np @@ -182,7 +182,8 @@ def create_overwrite_for_estimate( ): raise NotImplementedError("create_overwrite_for_estimate") - @abstractproperty + @property + @abstractmethod def searchsorted_side(self): return NotImplementedError("searchsorted_side") diff --git a/src/zipline/pipeline/term.py b/src/zipline/pipeline/term.py index b3dff30c46..ab16651d48 100644 --- a/src/zipline/pipeline/term.py +++ b/src/zipline/pipeline/term.py @@ -1,7 +1,7 @@ """ Base class for Filters, Factors and Classifiers """ -from abc import ABCMeta, abstractproperty, abstractmethod +from abc import ABCMeta, abstractmethod from bisect import insort from collections.abc import Mapping from weakref import WeakValueDictionary @@ -353,21 +353,24 @@ def compute_extra_rows(self, all_dates, start_date, end_date, min_extra_rows): """ return min_extra_rows - @abstractproperty + @property + @abstractmethod def inputs(self): """ A tuple of other Terms needed as inputs for ``self``. """ raise NotImplementedError("inputs") - @abstractproperty + @property + @abstractmethod def windowed(self): """ Boolean indicating whether this term is a trailing-window computation. """ raise NotImplementedError("windowed") - @abstractproperty + @property + @abstractmethod def mask(self): """ A :class:`~zipline.pipeline.Filter` representing asset/date pairs to @@ -375,7 +378,8 @@ def mask(self): """ raise NotImplementedError("mask") - @abstractproperty + @property + @abstractmethod def dependencies(self): """ A dictionary mapping terms that must be computed before `self` to the diff --git a/src/zipline/testing/core.py b/src/zipline/testing/core.py index 4a0cff3731..7987e384b3 100644 --- a/src/zipline/testing/core.py +++ b/src/zipline/testing/core.py @@ -1,4 +1,4 @@ -from abc import ABCMeta, abstractmethod, abstractproperty +from abc import ABCMeta, abstractmethod from contextlib import contextmanager import gzip from itertools import ( @@ -1406,7 +1406,8 @@ class _TmpBarReader(tmp_dir, metaclass=ABCMeta): will be a unique name. """ - @abstractproperty + @property + @abstractmethod def _reader_cls(self): raise NotImplementedError("_reader") From 5190417201f35876736b29cd9bbf2d40f651b084 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Thu, 24 Nov 2022 16:17:57 +0100 Subject: [PATCH 02/15] removed deprecated __unicode__ --- src/zipline/errors.py | 1 - src/zipline/finance/order.py | 6 ------ 2 files changed, 7 deletions(-) diff --git a/src/zipline/errors.py b/src/zipline/errors.py index 5ddbe91a55..21a88ef99c 100644 --- a/src/zipline/errors.py +++ b/src/zipline/errors.py @@ -31,7 +31,6 @@ def __str__(self): msg = self.msg.format(**self.kwargs) return msg - __unicode__ = __str__ __repr__ = __str__ diff --git a/src/zipline/finance/order.py b/src/zipline/finance/order.py index e837db4f22..74ddedb647 100644 --- a/src/zipline/finance/order.py +++ b/src/zipline/finance/order.py @@ -286,9 +286,3 @@ def __repr__(self): String representation for this object. """ return "Order(%s)" % self.to_dict().__repr__() - - def __unicode__(self): - """ - Unicode representation for this object. - """ - return str(repr(self)) From c91aa9f4a7d0c302c5a0edf5f3d4f8645f9854e0 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Thu, 24 Nov 2022 16:30:00 +0100 Subject: [PATCH 03/15] bump up pytest to >= 7.2.0 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e4c336a37c..fca9be2218 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,7 +84,7 @@ build-backend = 'setuptools.build_meta' [project.optional-dependencies] test = [ 'tox', - 'pytest==6.2.5', + 'pytest>=7.2.0', 'pytest-cov >=3.0.0', 'pytest-xdist >=2.5.0', 'pytest-timeout >=1.4.2', From c5ad515d87a89076b79f9563cf0bd7d724e4d50a Mon Sep 17 00:00:00 2001 From: MBounouar Date: Thu, 24 Nov 2022 17:04:47 +0100 Subject: [PATCH 04/15] remove unecessary object class declaration --- src/zipline/_protocol.pyx | 2 +- src/zipline/algorithm.py | 2 +- src/zipline/assets/asset_writer.py | 6 +-- src/zipline/assets/assets.py | 2 +- src/zipline/assets/continuous_futures.pyx | 4 +- src/zipline/assets/exchange_info.py | 2 +- src/zipline/country.py | 2 +- src/zipline/currency.py | 2 +- src/zipline/data/adjustments.py | 4 +- src/zipline/data/bcolz_daily_bars.py | 2 +- src/zipline/data/data_portal.py | 2 +- src/zipline/data/fx/hdf5.py | 2 +- src/zipline/data/hdf5_daily_bars.py | 2 +- src/zipline/data/history_loader.py | 6 +-- src/zipline/data/minute_bars.py | 6 +-- src/zipline/data/resample.py | 2 +- src/zipline/extensions.py | 4 +- src/zipline/finance/ledger.py | 4 +- src/zipline/finance/metrics/metric.py | 34 ++++++++--------- src/zipline/finance/metrics/tracker.py | 2 +- src/zipline/finance/order.py | 2 +- src/zipline/finance/position.py | 2 +- src/zipline/finance/trading.py | 2 +- src/zipline/finance/transaction.py | 2 +- src/zipline/gens/tradesimulation.py | 2 +- src/zipline/lib/adjusted_array.py | 2 +- src/zipline/lib/labelarray.py | 2 +- src/zipline/pipeline/data/dataset.py | 21 ++++------- src/zipline/pipeline/factors/factor.py | 2 +- src/zipline/pipeline/graph.py | 2 +- src/zipline/pipeline/hooks/progress.py | 7 ++-- src/zipline/pipeline/pipeline.py | 2 +- src/zipline/protocol.py | 10 ++--- src/zipline/sources/benchmark_source.py | 2 +- src/zipline/sources/test_source.py | 2 +- src/zipline/testing/core.py | 6 +-- src/zipline/testing/fixtures.py | 2 +- src/zipline/testing/pipeline_terms.py | 2 +- src/zipline/testing/predicates.py | 45 +++++++++-------------- src/zipline/utils/api_support.py | 2 +- src/zipline/utils/argcheck.py | 6 +-- src/zipline/utils/cache.py | 8 ++-- src/zipline/utils/classproperty.py | 2 +- src/zipline/utils/context_tricks.py | 6 +-- src/zipline/utils/data.py | 6 +-- src/zipline/utils/dummy.py | 2 +- src/zipline/utils/events.py | 8 ++-- src/zipline/utils/exploding_object.py | 2 +- src/zipline/utils/functional.py | 2 +- src/zipline/utils/idbox.py | 2 +- src/zipline/utils/input_validation.py | 2 +- src/zipline/utils/memoize.py | 12 ++++-- src/zipline/utils/numpy_utils.py | 14 ++----- src/zipline/utils/run_algo.py | 2 +- src/zipline/utils/security_list.py | 4 +- src/zipline/utils/sentinel.py | 2 +- tests/pipeline/test_pipeline_algo.py | 2 +- tests/test_memoize.py | 2 +- tests/test_registration_manager.py | 4 +- tests/test_restrictions.py | 2 +- tests/test_testing.py | 2 +- tests/test_tradesimulation.py | 2 +- tests/utils/test_argcheck.py | 2 +- tests/utils/test_preprocess.py | 8 ++-- 64 files changed, 144 insertions(+), 171 deletions(-) diff --git a/src/zipline/_protocol.pyx b/src/zipline/_protocol.pyx index d1bc12da8f..6497354827 100644 --- a/src/zipline/_protocol.pyx +++ b/src/zipline/_protocol.pyx @@ -32,7 +32,7 @@ from zipline.zipline_warnings import ZiplineDeprecationWarning cdef bool _is_iterable(obj): return isinstance(obj, Iterable) and not isinstance(obj, str) -cdef class check_parameters(object): +cdef class check_parameters: """ Asserts that the keywords passed into the wrapped function are included in those passed into this decorator. If not, raise a TypeError with a diff --git a/src/zipline/algorithm.py b/src/zipline/algorithm.py index 3a2f88c6b9..17ac353918 100644 --- a/src/zipline/algorithm.py +++ b/src/zipline/algorithm.py @@ -140,7 +140,7 @@ def __init__(self): ) -class TradingAlgorithm(object): +class TradingAlgorithm: """A class that represents a trading strategy and parameters to execute the strategy. diff --git a/src/zipline/assets/asset_writer.py b/src/zipline/assets/asset_writer.py index 74f5fd18c0..f9cd2ccd7a 100644 --- a/src/zipline/assets/asset_writer.py +++ b/src/zipline/assets/asset_writer.py @@ -458,11 +458,7 @@ def write_version_info(conn, version_table, version_value): conn.execute(sa.insert(version_table, values={"version": version_value})) -class _empty(object): - columns = () - - -class AssetDBWriter(object): +class AssetDBWriter: """Class used to write data to an assets db. Parameters diff --git a/src/zipline/assets/assets.py b/src/zipline/assets/assets.py index 80518cea38..1391f2a231 100644 --- a/src/zipline/assets/assets.py +++ b/src/zipline/assets/assets.py @@ -255,7 +255,7 @@ def _encode_continuous_future_sid(root_symbol, offset, roll_style, adjustment_st Lifetimes = namedtuple("Lifetimes", "sid start end") -class AssetFinder(object): +class AssetFinder: """ An AssetFinder is an interface to a database of Asset metadata written by an ``AssetDBWriter``. diff --git a/src/zipline/assets/continuous_futures.pyx b/src/zipline/assets/continuous_futures.pyx index 228a01b9bb..1f3f2e09bc 100644 --- a/src/zipline/assets/continuous_futures.pyx +++ b/src/zipline/assets/continuous_futures.pyx @@ -279,7 +279,7 @@ cdef class ContinuousFuture: return calendar.is_open_on_minute(dt_minute) -cdef class ContractNode(object): +cdef class ContractNode: cdef readonly object contract cdef public object prev @@ -307,7 +307,7 @@ cdef class ContractNode(object): return curr -cdef class OrderedContracts(object): +cdef class OrderedContracts: """ A container for aligned values of a future contract chain, in sorted order of their occurrence. diff --git a/src/zipline/assets/exchange_info.py b/src/zipline/assets/exchange_info.py index ceab63d897..df4a7c21c8 100644 --- a/src/zipline/assets/exchange_info.py +++ b/src/zipline/assets/exchange_info.py @@ -1,7 +1,7 @@ from zipline.utils.calendar_utils import get_calendar -class ExchangeInfo(object): +class ExchangeInfo: """An exchange where assets are traded. Parameters diff --git a/src/zipline/country.py b/src/zipline/country.py index dfc18ae20c..16179d587f 100644 --- a/src/zipline/country.py +++ b/src/zipline/country.py @@ -7,7 +7,7 @@ def code(name): return countries_by_name[name].alpha2 -class CountryCode(object): +class CountryCode: """A simple namespace of iso3166 alpha2 country codes.""" ARGENTINA = code("ARGENTINA") diff --git a/src/zipline/currency.py b/src/zipline/currency.py index bb46232c77..78bfdeefa7 100644 --- a/src/zipline/currency.py +++ b/src/zipline/currency.py @@ -5,7 +5,7 @@ @total_ordering -class Currency(object): +class Currency: """A currency identifier, as defined by ISO-4217. Parameters diff --git a/src/zipline/data/adjustments.py b/src/zipline/data/adjustments.py index 625e0de22e..afeb23117b 100644 --- a/src/zipline/data/adjustments.py +++ b/src/zipline/data/adjustments.py @@ -79,7 +79,7 @@ def specialize_any_integer(d): return out -class SQLiteAdjustmentReader(object): +class SQLiteAdjustmentReader: """ Loads adjustments based on corporate actions from a SQLite database. @@ -339,7 +339,7 @@ def _df_dtypes(self, table_name, convert_dates): return out -class SQLiteAdjustmentWriter(object): +class SQLiteAdjustmentWriter: """ Writer for data to be read by SQLiteAdjustmentReader diff --git a/src/zipline/data/bcolz_daily_bars.py b/src/zipline/data/bcolz_daily_bars.py index 18656e3e42..ccc89237c3 100644 --- a/src/zipline/data/bcolz_daily_bars.py +++ b/src/zipline/data/bcolz_daily_bars.py @@ -124,7 +124,7 @@ def winsorise_uint32(df, invalid_data_behavior, column, *columns): return df -class BcolzDailyBarWriter(object): +class BcolzDailyBarWriter: """ Class capable of writing daily OHLCV data to disk in a format that can be read efficiently by BcolzDailyOHLCVReader. diff --git a/src/zipline/data/data_portal.py b/src/zipline/data/data_portal.py index ab4eb5f330..fa0085a133 100644 --- a/src/zipline/data/data_portal.py +++ b/src/zipline/data/data_portal.py @@ -87,7 +87,7 @@ _DEF_D_HIST_PREFETCH = DEFAULT_DAILY_HISTORY_PREFETCH -class DataPortal(object): +class DataPortal: """Interface to all of the data that a zipline simulation needs. This is used by the simulation runner to answer questions about the data, diff --git a/src/zipline/data/fx/hdf5.py b/src/zipline/data/fx/hdf5.py index 523792a100..325f28d53b 100644 --- a/src/zipline/data/fx/hdf5.py +++ b/src/zipline/data/fx/hdf5.py @@ -240,7 +240,7 @@ def get_rates(self, rate, quote, bases, dts): return out.transpose() -class HDF5FXRateWriter(object): +class HDF5FXRateWriter: """Writer class for HDF5 files consumed by HDF5FXRateReader.""" def __init__(self, group, date_chunk_size=HDF5_FX_DEFAULT_CHUNK_SIZE): diff --git a/src/zipline/data/hdf5_daily_bars.py b/src/zipline/data/hdf5_daily_bars.py index ed8965de36..085fdd6854 100644 --- a/src/zipline/data/hdf5_daily_bars.py +++ b/src/zipline/data/hdf5_daily_bars.py @@ -205,7 +205,7 @@ def days_and_sids_for_frames(frames): return frames[0].index.values, frames[0].columns.values -class HDF5DailyBarWriter(object): +class HDF5DailyBarWriter: """ Class capable of writing daily OHLCV data to disk in a format that can be read efficiently by HDF5DailyBarReader. diff --git a/src/zipline/data/history_loader.py b/src/zipline/data/history_loader.py index 0fe6e875c0..cce9a958e4 100644 --- a/src/zipline/data/history_loader.py +++ b/src/zipline/data/history_loader.py @@ -38,7 +38,7 @@ DEFAULT_ASSET_PRICE_DECIMALS = 3 -class HistoryCompatibleUSEquityAdjustmentReader(object): +class HistoryCompatibleUSEquityAdjustmentReader: def __init__(self, adjustment_reader): self._adjustments_reader = adjustment_reader @@ -132,7 +132,7 @@ def _get_adjustments_in_range(self, asset, dts, field): return adjs -class ContinuousFutureAdjustmentReader(object): +class ContinuousFutureAdjustmentReader: """ Calculates adjustments for continuous futures, based on the close and open of the contracts on the either side of each roll. @@ -222,7 +222,7 @@ def _get_adjustments_in_range(self, cf, dts, field): return adjs -class SlidingWindow(object): +class SlidingWindow: """ Wrapper around an AdjustedArrayWindow which supports monotonically increasing (by datetime) requests for a sized window of data. diff --git a/src/zipline/data/minute_bars.py b/src/zipline/data/minute_bars.py index 1f3152022b..cf07c4e817 100644 --- a/src/zipline/data/minute_bars.py +++ b/src/zipline/data/minute_bars.py @@ -178,7 +178,7 @@ def convert_cols(cols, scale_factor, sid, invalid_data_behavior): return opens, highs, lows, closes, volumes -class BcolzMinuteBarMetadata(object): +class BcolzMinuteBarMetadata: """ Parameters ---------- @@ -325,7 +325,7 @@ def write(self, rootdir): json.dump(metadata, fp) -class BcolzMinuteBarWriter(object): +class BcolzMinuteBarWriter: """ Class capable of writing minute OHLCV data to disk into bcolz format. @@ -1290,7 +1290,7 @@ def read(self, dts, sids): raise NotImplementedError() -class H5MinuteBarUpdateWriter(object): +class H5MinuteBarUpdateWriter: """ Writer for files containing minute bar updates for consumption by a writer for a ``MinuteBarReader`` format. diff --git a/src/zipline/data/resample.py b/src/zipline/data/resample.py index f5aac88cfa..3d47c50511 100644 --- a/src/zipline/data/resample.py +++ b/src/zipline/data/resample.py @@ -102,7 +102,7 @@ def minute_to_session(column, close_locs, data, out): return out -class DailyHistoryAggregator(object): +class DailyHistoryAggregator: """ Converts minute pricing data into a daily summary, to be used for the last slot in a call to history with a frequency of `1d`. diff --git a/src/zipline/extensions.py b/src/zipline/extensions.py index 16e0caf75e..238e1ccda5 100644 --- a/src/zipline/extensions.py +++ b/src/zipline/extensions.py @@ -83,13 +83,13 @@ def update_namespace(namespace, path, name): update_namespace(getattr(namespace, path[0]), path[1:], name) -class Namespace(object): +class Namespace: """ A placeholder object representing a namespace level """ -class Registry(object): +class Registry: """ Responsible for managing all instances of custom subclasses of a given abstract base class - only one instance needs to be created diff --git a/src/zipline/finance/ledger.py b/src/zipline/finance/ledger.py index bbde8ef69b..a51370f2fb 100644 --- a/src/zipline/finance/ledger.py +++ b/src/zipline/finance/ledger.py @@ -34,7 +34,7 @@ log = logbook.Logger("Performance") -class PositionTracker(object): +class PositionTracker: """The current state of the positions held. Parameters @@ -311,7 +311,7 @@ def stats(self): ) -class Ledger(object): +class Ledger: """The ledger tracks all orders and transactions as well as the current state of the portfolio and positions. diff --git a/src/zipline/finance/metrics/metric.py b/src/zipline/finance/metrics/metric.py index d37045a128..1e3e8e75b4 100644 --- a/src/zipline/finance/metrics/metric.py +++ b/src/zipline/finance/metrics/metric.py @@ -25,7 +25,7 @@ from zipline.finance._finance_ext import minute_annual_volatility -class SimpleLedgerField(object): +class SimpleLedgerField: """Emit the current value of a ledger field every bar or every session. Parameters @@ -55,7 +55,7 @@ def end_of_session(self, packet, ledger, session, session_ix, data_portal): ) -class DailyLedgerField(object): +class DailyLedgerField: """Like :class:`~zipline.finance.metrics.metric.SimpleLedgerField` but also puts the current value in the ``cumulative_perf`` section. @@ -88,7 +88,7 @@ def end_of_session(self, packet, ledger, session, session_ix, data_portal): ] = self._get_ledger_field(ledger) -class StartOfPeriodLedgerField(object): +class StartOfPeriodLedgerField: """Keep track of the value of a ledger field at the start of the period. Parameters @@ -127,7 +127,7 @@ def end_of_session(self, packet, ledger, session, session_ix, data_portal): self._end_of_period("daily_perf", packet, ledger) -class Returns(object): +class Returns: """Tracks the daily and cumulative returns of the algorithm.""" def _end_of_period(field, packet, ledger, dt, session_ix, data_portal): @@ -141,7 +141,7 @@ def _end_of_period(field, packet, ledger, dt, session_ix, data_portal): end_of_session = partial(_end_of_period, "daily_perf") -class BenchmarkReturnsAndVolatility(object): +class BenchmarkReturnsAndVolatility: """Tracks daily and cumulative returns for the benchmark as well as the volatility of the benchmark returns. """ @@ -205,7 +205,7 @@ def end_of_session(self, packet, ledger, session, session_ix, data_portal): packet["cumulative_risk_metrics"]["benchmark_volatility"] = v -class PNL(object): +class PNL: """Tracks daily and cumulative PNL.""" def start_of_simulation( @@ -228,7 +228,7 @@ def end_of_session(self, packet, ledger, session, session_ix, data_portal): self._end_of_period("daily_perf", packet, ledger) -class CashFlow(object): +class CashFlow: """Tracks daily and cumulative cash flow. Notes @@ -253,7 +253,7 @@ def end_of_session(self, packet, ledger, session, session_ix, data_portal): self._previous_cash_flow = cash_flow -class Orders(object): +class Orders: """Tracks daily orders.""" def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): @@ -263,7 +263,7 @@ def end_of_session(self, packet, ledger, dt, session_ix, data_portal): packet["daily_perf"]["orders"] = ledger.orders() -class Transactions(object): +class Transactions: """Tracks daily transactions.""" def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): @@ -273,7 +273,7 @@ def end_of_session(self, packet, ledger, dt, session_ix, data_portal): packet["daily_perf"]["transactions"] = ledger.transactions() -class Positions(object): +class Positions: """Tracks daily positions.""" def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): @@ -283,7 +283,7 @@ def end_of_session(self, packet, ledger, dt, session_ix, data_portal): packet["daily_perf"]["positions"] = ledger.positions() -class ReturnsStatistic(object): +class ReturnsStatistic: """A metric that reports an end of simulation scalar or time series computed from the algorithm returns. @@ -312,7 +312,7 @@ def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): end_of_session = end_of_bar -class AlphaBeta(object): +class AlphaBeta: """End of simulation alpha and beta to the benchmark.""" def start_of_simulation( @@ -341,7 +341,7 @@ def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): end_of_session = end_of_bar -class MaxLeverage(object): +class MaxLeverage: """Tracks the maximum account leverage.""" def start_of_simulation(self, *args): @@ -354,7 +354,7 @@ def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): end_of_session = end_of_bar -class NumTradingDays(object): +class NumTradingDays: """Report the number of trading days.""" def start_of_simulation(self, *args): @@ -369,7 +369,7 @@ def end_of_bar(self, packet, ledger, dt, session_ix, data_portal): end_of_session = end_of_bar -class _ConstantCumulativeRiskMetric(object): +class _ConstantCumulativeRiskMetric: """A metric which does not change, ever. Notes @@ -389,7 +389,7 @@ def end_of_session(self, packet, *args): packet["cumulative_risk_metrics"][self._field] = self._value -class PeriodLabel(object): +class PeriodLabel: """Backwards compat, please kill me.""" def start_of_session(self, ledger, session, data_portal): @@ -401,7 +401,7 @@ def end_of_bar(self, packet, *args): end_of_session = end_of_bar -class _ClassicRiskMetrics(object): +class _ClassicRiskMetrics: """Produces original risk packet.""" def start_of_simulation( diff --git a/src/zipline/finance/metrics/tracker.py b/src/zipline/finance/metrics/tracker.py index be59d51434..7ef1313b2d 100644 --- a/src/zipline/finance/metrics/tracker.py +++ b/src/zipline/finance/metrics/tracker.py @@ -21,7 +21,7 @@ log = logbook.Logger(__name__) -class MetricsTracker(object): +class MetricsTracker: """The algorithm's interface to the registered risk and performance metrics. diff --git a/src/zipline/finance/order.py b/src/zipline/finance/order.py index 74ddedb647..4e0b300ee0 100644 --- a/src/zipline/finance/order.py +++ b/src/zipline/finance/order.py @@ -41,7 +41,7 @@ ORDER_FIELDS_TO_IGNORE = {"type", "direction", "_status", "asset"} -class Order(object): +class Order: # using __slots__ to save on memory usage. Simulations can create many # Order objects and we keep them all in memory, so it's worthwhile trying # to cut down on the memory footprint of this object. diff --git a/src/zipline/finance/position.py b/src/zipline/finance/position.py index 5227c5d0d4..82b4bf288e 100644 --- a/src/zipline/finance/position.py +++ b/src/zipline/finance/position.py @@ -41,7 +41,7 @@ log = logbook.Logger("Performance") -class Position(object): +class Position: __slots__ = "inner_position", "protocol_position" def __init__( diff --git a/src/zipline/finance/trading.py b/src/zipline/finance/trading.py index fe25aac9ce..0572761049 100644 --- a/src/zipline/finance/trading.py +++ b/src/zipline/finance/trading.py @@ -25,7 +25,7 @@ DEFAULT_CAPITAL_BASE = 1e5 -class SimulationParameters(object): +class SimulationParameters: def __init__( self, start_session, diff --git a/src/zipline/finance/transaction.py b/src/zipline/finance/transaction.py index aa93adadb4..6db8f1dc95 100644 --- a/src/zipline/finance/transaction.py +++ b/src/zipline/finance/transaction.py @@ -19,7 +19,7 @@ from zipline.utils.input_validation import expect_types -class Transaction(object): +class Transaction: @expect_types(asset=Asset) def __init__(self, asset, amount, dt, price, order_id): self.asset = asset diff --git a/src/zipline/gens/tradesimulation.py b/src/zipline/gens/tradesimulation.py index ad072ec3cf..9cf041e682 100644 --- a/src/zipline/gens/tradesimulation.py +++ b/src/zipline/gens/tradesimulation.py @@ -30,7 +30,7 @@ log = Logger("Trade Simulation") -class AlgorithmSimulator(object): +class AlgorithmSimulator: EMISSION_TO_PERF_KEY_MAP = {"minute": "minute_perf", "daily": "daily_perf"} def __init__( diff --git a/src/zipline/lib/adjusted_array.py b/src/zipline/lib/adjusted_array.py index 78ebb50f02..3387f22790 100644 --- a/src/zipline/lib/adjusted_array.py +++ b/src/zipline/lib/adjusted_array.py @@ -176,7 +176,7 @@ def _merge_simple(adjustment_lists, front_idx, back_idx): } -class AdjustedArray(object): +class AdjustedArray: """ An array that can be iterated with a variable-length window, and which can provide different views on data from different perspectives. diff --git a/src/zipline/lib/labelarray.py b/src/zipline/lib/labelarray.py index d5c2ddebb1..1ec763bd0c 100644 --- a/src/zipline/lib/labelarray.py +++ b/src/zipline/lib/labelarray.py @@ -807,7 +807,7 @@ def element_of(self, container): @instance # This makes _sortable_sentinel a singleton instance. @total_ordering -class _sortable_sentinel(object): +class _sortable_sentinel: """Dummy object that sorts before any other python object.""" def __eq__(self, other): diff --git a/src/zipline/pipeline/data/dataset.py b/src/zipline/pipeline/data/dataset.py index c4a2787c4d..70065c5a85 100644 --- a/src/zipline/pipeline/data/dataset.py +++ b/src/zipline/pipeline/data/dataset.py @@ -31,7 +31,7 @@ IsSpecialization = sentinel("IsSpecialization") -class Column(object): +class Column: """ An abstract column of data, not yet associated with a dataset. """ @@ -73,7 +73,7 @@ def bind(self, name): ) -class _BoundColumnDescr(object): +class _BoundColumnDescr: """ Intermediate class that sits on `DataSet` objects and returns memoized `BoundColumn` objects when requested. @@ -82,9 +82,7 @@ class _BoundColumnDescr(object): parent classes. """ - def __init__( - self, dtype, missing_value, name, doc, metadata, currency_aware - ): + def __init__(self, dtype, missing_value, name, doc, metadata, currency_aware): # Validating and calculating default missing values here guarantees # that we fail quickly if the user passes an unsupporte dtype or fails # to provide a missing value for a dtype that requires one @@ -529,9 +527,7 @@ def _create_specialization(self, domain): @property def columns(self): - return frozenset( - getattr(self, colname) for colname in self._column_names - ) + return frozenset(getattr(self, colname) for colname in self._column_names) @property def qualname(self): @@ -716,7 +712,7 @@ def __str__(self): ) -class _DataSetFamilyColumn(object): +class _DataSetFamilyColumn: """Descriptor used to raise a helpful error when a column is accessed on a DataSetFamily instead of on the result of a slice. @@ -759,10 +755,7 @@ def __new__(cls, name, bases, dict_): if not is_abstract: self.extra_dims = extra_dims = OrderedDict( - [ - (k, frozenset(v)) - for k, v in OrderedDict(self.extra_dims).items() - ] + [(k, frozenset(v)) for k, v in OrderedDict(self.extra_dims).items()] ) if not extra_dims: raise ValueError( @@ -872,7 +865,7 @@ class SomeDataSet(DataSetFamily): _SliceType = DataSetFamilySlice @type.__call__ - class extra_dims(object): + class extra_dims: """OrderedDict[str, frozenset] of dimension name -> unique values May be defined on subclasses as an iterable of pairs: the diff --git a/src/zipline/pipeline/factors/factor.py b/src/zipline/pipeline/factors/factor.py index 27b467c610..52f2840552 100644 --- a/src/zipline/pipeline/factors/factor.py +++ b/src/zipline/pipeline/factors/factor.py @@ -398,7 +398,7 @@ def mathfunc(self): ) -class summary_funcs(object): +class summary_funcs: """Namespace of functions meant to be used with DailySummary.""" @staticmethod diff --git a/src/zipline/pipeline/graph.py b/src/zipline/pipeline/graph.py index 10ea88c8dd..273b99f8c6 100644 --- a/src/zipline/pipeline/graph.py +++ b/src/zipline/pipeline/graph.py @@ -23,7 +23,7 @@ class CyclicDependency(Exception): SCREEN_NAME = "screen_" + uuid.uuid4().hex -class TermGraph(object): +class TermGraph: """ An abstract representation of Pipeline Term dependencies. diff --git a/src/zipline/pipeline/hooks/progress.py b/src/zipline/pipeline/hooks/progress.py index c2bfb61e21..f26f3a8208 100644 --- a/src/zipline/pipeline/hooks/progress.py +++ b/src/zipline/pipeline/hooks/progress.py @@ -110,7 +110,7 @@ def computing_term(self, term): self._publish() -class ProgressModel(object): +class ProgressModel: """ Model object for tracking progress of a Pipeline execution. @@ -273,7 +273,6 @@ class ProgressBarContainer(ipywidgets.VBox): def __repr__(self): return "" - except ImportError: HAVE_WIDGETS = False @@ -287,7 +286,7 @@ def __repr__(self): # XXX: This class is currently untested, because we don't require ipywidgets as # a test dependency. Be careful if you make changes to this. -class IPythonWidgetProgressPublisher(object): +class IPythonWidgetProgressPublisher: """A progress publisher that publishes to an IPython/Jupyter widget.""" def __init__(self): @@ -458,7 +457,7 @@ def maybe_s(n): return "{seconds:.2f} Seconds".format(seconds=seconds) -class TestingProgressPublisher(object): +class TestingProgressPublisher: """A progress publisher that records a trace of model states for testing.""" TraceState = namedtuple( diff --git a/src/zipline/pipeline/pipeline.py b/src/zipline/pipeline/pipeline.py index 3e5e4426a6..3307e53e64 100644 --- a/src/zipline/pipeline/pipeline.py +++ b/src/zipline/pipeline/pipeline.py @@ -11,7 +11,7 @@ from .term import AssetExists, ComputableTerm, Term -class Pipeline(object): +class Pipeline: """ A Pipeline object represents a collection of named expressions to be compiled and executed by a PipelineEngine. diff --git a/src/zipline/protocol.py b/src/zipline/protocol.py index 72a9b7b6ac..a296d4881a 100644 --- a/src/zipline/protocol.py +++ b/src/zipline/protocol.py @@ -19,7 +19,7 @@ from ._protocol import BarData, InnerPosition # noqa -class MutableView(object): +class MutableView: """A mutable view over an "immutable" object. Parameters @@ -87,7 +87,7 @@ def __repr__(self): ] -class Event(object): +class Event: def __init__(self, initial_values=None): if initial_values: self.__dict__.update(initial_values) @@ -112,7 +112,7 @@ class Order(Event): pass -class Portfolio(object): +class Portfolio: """Object providing read-only access to current portfolio state. Parameters @@ -181,7 +181,7 @@ def current_portfolio_weights(self): return position_values / self.portfolio_value -class Account(object): +class Account: """ The account object tracks information about the trading account. The values are updated as the algorithm runs and its keys remain unchanged. @@ -216,7 +216,7 @@ def __repr__(self): return "Account({0})".format(self.__dict__) -class Position(object): +class Position: """ A position held by an algorithm. diff --git a/src/zipline/sources/benchmark_source.py b/src/zipline/sources/benchmark_source.py index 39d2f1a2a3..5c0f512987 100644 --- a/src/zipline/sources/benchmark_source.py +++ b/src/zipline/sources/benchmark_source.py @@ -22,7 +22,7 @@ ) -class BenchmarkSource(object): +class BenchmarkSource: def __init__( self, benchmark_asset, diff --git a/src/zipline/sources/test_source.py b/src/zipline/sources/test_source.py index 20d39c7ef2..a62c4140e2 100644 --- a/src/zipline/sources/test_source.py +++ b/src/zipline/sources/test_source.py @@ -82,7 +82,7 @@ def advance_current(cur): cur = advance_current(cur) -class SpecificEquityTrades(object): +class SpecificEquityTrades: """ Yields all events in event_list that match the given sid_filter. If no event_list is specified, generates an internal stream of events diff --git a/src/zipline/testing/core.py b/src/zipline/testing/core.py index 7987e384b3..117407ae8d 100644 --- a/src/zipline/testing/core.py +++ b/src/zipline/testing/core.py @@ -365,7 +365,7 @@ class UnexpectedAttributeAccess(Exception): pass -class ExplodingObject(object): +class ExplodingObject: """ Object that will raise an exception on any attribute access. @@ -739,7 +739,7 @@ def _get_minute_window_for_asset(self, asset, field, minutes_for_window): return np.arange(minutes_for_window, dtype=np.float64) -class tmp_assets_db(object): +class tmp_assets_db: """Create a temporary assets sqlite database. This is meant to be used as a context manager. @@ -948,7 +948,7 @@ def wrapped(*args, **kwargs): return dec -class MockDailyBarReader(object): +class MockDailyBarReader: def __init__(self, dates): self.sessions = pd.DatetimeIndex(dates) diff --git a/src/zipline/testing/fixtures.py b/src/zipline/testing/fixtures.py index 762cc63298..1de8775c43 100644 --- a/src/zipline/testing/fixtures.py +++ b/src/zipline/testing/fixtures.py @@ -266,7 +266,7 @@ def alias(attr_name): Examples -------- - >>> class C(object): + >>> class C: ... attr = 1 ... >>> class D(C): diff --git a/src/zipline/testing/pipeline_terms.py b/src/zipline/testing/pipeline_terms.py index a90957fac0..78dca8643e 100644 --- a/src/zipline/testing/pipeline_terms.py +++ b/src/zipline/testing/pipeline_terms.py @@ -7,7 +7,7 @@ from .predicates import assert_equal -class CheckWindowsMixin(object): +class CheckWindowsMixin: params = ("expected_windows",) def compute(self, today, assets, out, input_, expected_windows): diff --git a/src/zipline/testing/predicates.py b/src/zipline/testing/predicates.py index 500a408e79..1e93e4e663 100644 --- a/src/zipline/testing/predicates.py +++ b/src/zipline/testing/predicates.py @@ -31,7 +31,7 @@ @instance @ensure_doctest -class wildcard(object): +class wildcard: """An object that compares equal to any other object. This is useful when using :func:`~zipline.testing.predicates.assert_equal` @@ -63,7 +63,7 @@ def __repr__(self): return "<%s>" % type(self).__name__ -class instance_of(object): +class instance_of: """An object that compares equal to any instance of a given type or types. Parameters @@ -98,11 +98,7 @@ def __repr__(self): typenames = tuple(t.__name__ for t in self.types) return "%s(%s%s)" % ( type(self).__name__, - ( - typenames[0] - if len(typenames) == 1 - else "(%s)" % ", ".join(typenames) - ), + (typenames[0] if len(typenames) == 1 else "(%s)" % ", ".join(typenames)), ", exact=True" if self.exact else "", ) @@ -379,9 +375,7 @@ def assert_ordereddict_equal(result, expected, path=(), **kwargs): def assert_sequence_equal(result, expected, path=(), msg="", **kwargs): result_len = len(result) expected_len = len(expected) - assert ( - result_len == expected_len - ), "%s%s lengths do not match: %d != %d\n%s" % ( + assert result_len == expected_len, "%s%s lengths do not match: %d != %d\n%s" % ( _fmt_msg(msg), type(result).__name__, result_len, @@ -389,9 +383,7 @@ def assert_sequence_equal(result, expected, path=(), msg="", **kwargs): _fmt_path(path), ) for n, (resultv, expectedv) in enumerate(zip(result, expected)): - assert_equal( - resultv, expectedv, path=path + ("[%d]" % n,), msg=msg, **kwargs - ) + assert_equal(resultv, expectedv, path=path + ("[%d]" % n,), msg=msg, **kwargs) @assert_equal.register(set, set) @@ -422,8 +414,7 @@ def assert_array_equal( assert result_dtype == expected_dtype, ( "\nType mismatch:\n\n" "result dtype: %s\n" - "expected dtype: %s\n%s" - % (result_dtype, expected_dtype, _fmt_path(path)) + "expected dtype: %s\n%s" % (result_dtype, expected_dtype, _fmt_path(path)) ) f = partial( @@ -552,19 +543,19 @@ def assert_timestamp_and_datetime_equal( Returns raises unless ``allow_datetime_coercions`` is passed as True. """ - assert allow_datetime_coercions or type(result) == type( - expected - ), "%sdatetime types (%s, %s) don't match and " "allow_datetime_coercions was not set.\n%s" % ( - _fmt_msg(msg), - type(result), - type(expected), - _fmt_path(path), + assert allow_datetime_coercions or type(result) == type(expected), ( + "%sdatetime types (%s, %s) don't match and " + "allow_datetime_coercions was not set.\n%s" + % ( + _fmt_msg(msg), + type(result), + type(expected), + _fmt_path(path), + ) ) if isinstance(result, pd.Timestamp) and isinstance(expected, pd.Timestamp): - assert_equal( - result.tz, expected.tz, path=path + (".tz",), msg=msg, **kwargs - ) + assert_equal(result.tz, expected.tz, path=path + (".tz",), msg=msg, **kwargs) result = pd.Timestamp(result) expected = pd.Timestamp(expected) @@ -651,9 +642,7 @@ def assert_messages_equal(result, expected, msg=""): def index_of_first_difference(left, right): """Get the index of the first difference between two strings.""" - difflocs = ( - i for (i, (lc, rc)) in enumerate(zip_longest(left, right)) if lc != rc - ) + difflocs = (i for (i, (lc, rc)) in enumerate(zip_longest(left, right)) if lc != rc) try: return next(difflocs) except StopIteration: diff --git a/src/zipline/utils/api_support.py b/src/zipline/utils/api_support.py index 838a50a9ab..7a7e0231dc 100644 --- a/src/zipline/utils/api_support.py +++ b/src/zipline/utils/api_support.py @@ -18,7 +18,7 @@ from zipline.utils.algo_instance import get_algo_instance, set_algo_instance -class ZiplineAPI(object): +class ZiplineAPI: """ Context manager for making an algorithm instance available to zipline API functions within a scoped block. diff --git a/src/zipline/utils/argcheck.py b/src/zipline/utils/argcheck.py index d7749bfb02..33faebee58 100644 --- a/src/zipline/utils/argcheck.py +++ b/src/zipline/utils/argcheck.py @@ -34,7 +34,7 @@ def getinstance(): @singleton -class Ignore(object): +class Ignore: def __str__(self): return "Argument.ignore" @@ -42,7 +42,7 @@ def __str__(self): @singleton -class NoDefault(object): +class NoDefault: def __str__(self): return "Argument.no_default" @@ -50,7 +50,7 @@ def __str__(self): @singleton -class AnyDefault(object): +class AnyDefault: def __str__(self): return "Argument.any_default" diff --git a/src/zipline/utils/cache.py b/src/zipline/utils/cache.py index a19e20174b..933ecee6db 100644 --- a/src/zipline/utils/cache.py +++ b/src/zipline/utils/cache.py @@ -25,7 +25,7 @@ class Expired(Exception): AlwaysExpired = sentinel("AlwaysExpired") -class CachedObject(object): +class CachedObject: """ A simple struct for maintaining a cached object with an expiration date. @@ -86,7 +86,7 @@ def _unsafe_get_value(self): return self._value -class ExpiringCache(object): +class ExpiringCache: """ A cache of multiple CachedObjects, which returns the wrapped the value or raises and deletes the CachedObject if the value has expired. @@ -285,7 +285,7 @@ def __repr__(self): ) -class working_file(object): +class working_file: """A context manager for managing a temporary file that will be moved to a non-temporary location if no exceptions are raised in the context. @@ -328,7 +328,7 @@ def __exit__(self, *exc_info): self._commit() -class working_dir(object): +class working_dir: """A context manager for managing a temporary directory that will be moved to a non-temporary location if no exceptions are raised in the context. diff --git a/src/zipline/utils/classproperty.py b/src/zipline/utils/classproperty.py index 1ae963cc61..6498c070f2 100644 --- a/src/zipline/utils/classproperty.py +++ b/src/zipline/utils/classproperty.py @@ -1,4 +1,4 @@ -class classproperty(object): +class classproperty: """Class property""" def __init__(self, fget): diff --git a/src/zipline/utils/context_tricks.py b/src/zipline/utils/context_tricks.py index e1109bdab5..de67187920 100644 --- a/src/zipline/utils/context_tricks.py +++ b/src/zipline/utils/context_tricks.py @@ -1,5 +1,5 @@ @object.__new__ -class nop_context(object): +class nop_context: """A nop context manager.""" def __enter__(self): @@ -13,7 +13,7 @@ def _nop(*args, **kwargs): pass -class CallbackManager(object): +class CallbackManager: """Create a context manager from a pre-execution callback and a post-execution callback. @@ -68,7 +68,7 @@ def __exit__(self, *excinfo): self.post() -class _ManagedCallbackContext(object): +class _ManagedCallbackContext: def __init__(self, pre, post, args, kwargs): self._pre = pre self._post = post diff --git a/src/zipline/utils/data.py b/src/zipline/utils/data.py index bfe0aabf30..8c33ea3fca 100644 --- a/src/zipline/utils/data.py +++ b/src/zipline/utils/data.py @@ -26,7 +26,7 @@ def _ensure_index(x): return x -class RollingPanel(object): +class RollingPanel: """ Preallocation strategies for rolling window over expanding data set @@ -218,9 +218,7 @@ def convert_datelike_to_long(dt): ) elif values.ndim == 2: - return pd.DataFrame( - values, major_axis, self.minor_axis, dtype=self.dtype - ) + return pd.DataFrame(values, major_axis, self.minor_axis, dtype=self.dtype) def set_current(self, panel): """ diff --git a/src/zipline/utils/dummy.py b/src/zipline/utils/dummy.py index 4eec292c21..873b14fb58 100644 --- a/src/zipline/utils/dummy.py +++ b/src/zipline/utils/dummy.py @@ -1,4 +1,4 @@ -class DummyMapping(object): +class DummyMapping: """ Dummy object used to provide a mapping interface for singular values. """ diff --git a/src/zipline/utils/events.py b/src/zipline/utils/events.py index 975f713c00..98abd20a89 100644 --- a/src/zipline/utils/events.py +++ b/src/zipline/utils/events.py @@ -171,7 +171,7 @@ def lossless_float_to_int(funcname, func, argname, arg): raise TypeError(arg) -class EventManager(object): +class EventManager: """Manages a list of Event objects. This manages the logic for checking the rules and dispatching to the handle_data function of the Events. @@ -604,7 +604,7 @@ def should_trigger(self, dt): # Factory API -class date_rules(object): +class date_rules: """ Factories for date-based :func:`~zipline.api.schedule_function` rules. @@ -689,7 +689,7 @@ def week_end(days_offset=0): return NDaysBeforeLastTradingDayOfWeek(n=days_offset) -class time_rules(object): +class time_rules: """Factories for time-based :func:`~zipline.api.schedule_function` rules. See Also @@ -766,7 +766,7 @@ def market_close(offset=None, hours=None, minutes=None): every_minute = Always -class calendars(object): +class calendars: US_EQUITIES = sentinel("US_EQUITIES") US_FUTURES = sentinel("US_FUTURES") diff --git a/src/zipline/utils/exploding_object.py b/src/zipline/utils/exploding_object.py index 7071ab5750..1bc0378e3d 100644 --- a/src/zipline/utils/exploding_object.py +++ b/src/zipline/utils/exploding_object.py @@ -1,4 +1,4 @@ -class NamedExplodingObject(object): +class NamedExplodingObject: """An object which has no attributes but produces a more informative error message when accessed. diff --git a/src/zipline/utils/functional.py b/src/zipline/utils/functional.py index 68b663ce68..86f686e6a4 100644 --- a/src/zipline/utils/functional.py +++ b/src/zipline/utils/functional.py @@ -277,7 +277,7 @@ def getattrs(value, attrs, default=_no_default): Examples -------- - >>> class EmptyObject(object): + >>> class EmptyObject: ... pass ... >>> obj = EmptyObject() diff --git a/src/zipline/utils/idbox.py b/src/zipline/utils/idbox.py index cffcc4b0bd..5b30646d8c 100644 --- a/src/zipline/utils/idbox.py +++ b/src/zipline/utils/idbox.py @@ -1,4 +1,4 @@ -class IDBox(object): +class IDBox: """A wrapper that hashs to the id of the underlying object and compares equality on the id of the underlying. diff --git a/src/zipline/utils/input_validation.py b/src/zipline/utils/input_validation.py index 28c6a00f84..da9889dd34 100644 --- a/src/zipline/utils/input_validation.py +++ b/src/zipline/utils/input_validation.py @@ -840,7 +840,7 @@ def _coerce(types): return preprocess(**valmap(_coerce, kwargs)) -class error_keywords(object): +class error_keywords: def __init__(self, *args, **kwargs): self.messages = kwargs diff --git a/src/zipline/utils/memoize.py b/src/zipline/utils/memoize.py index 480efacd59..bf09a01514 100644 --- a/src/zipline/utils/memoize.py +++ b/src/zipline/utils/memoize.py @@ -20,7 +20,7 @@ class lazyval(property): ------- >>> from zipline.utils.memoize import lazyval - >>> class C(object): + >>> class C: ... def __init__(self): ... self.count = 0 ... @lazyval @@ -75,7 +75,7 @@ class classlazyval(lazyval): ------- >>> from zipline.utils.memoize import classlazyval - >>> class C(object): + >>> class C: ... count = 0 ... @classlazyval ... def val(cls): @@ -108,7 +108,9 @@ def _weak_lru_cache(maxsize=100): to allow the implementation to change. """ - def decorating_function(user_function, tuple=tuple, sorted=sorted, KeyError=KeyError): + def decorating_function( + user_function, tuple=tuple, sorted=sorted, KeyError=KeyError + ): hits = misses = 0 kwd_mark = (object(),) # separates positional and keyword args @@ -217,7 +219,9 @@ def _try_ref(item, callback): @property def alive(self): - return all(item() is not None for item in compress(self._items, self._selectors)) + return all( + item() is not None for item in compress(self._items, self._selectors) + ) def __eq__(self, other): return self._items == other._items diff --git a/src/zipline/utils/numpy_utils.py b/src/zipline/utils/numpy_utils.py index d28e5ce709..597001f25a 100644 --- a/src/zipline/utils/numpy_utils.py +++ b/src/zipline/utils/numpy_utils.py @@ -121,9 +121,7 @@ def unsigned_int_dtype_with_size_in_bytes(size): try: return UNSIGNED_INT_DTYPES_BY_SIZE_BYTES[size] except KeyError: - raise ValueError( - "No unsigned integral dtype whose size is %d bytes." % size - ) + raise ValueError("No unsigned integral dtype whose size is %d bytes." % size) class NoDefaultMissingValue(Exception): @@ -163,9 +161,7 @@ def coerce_to_dtype(dtype, value): elif name == "datetime64[ns]": return make_datetime64ns(value) else: - raise TypeError( - "Don't know how to coerce values of dtype %s" % dtype - ) + raise TypeError("Don't know how to coerce values of dtype %s" % dtype) return dtype.type(value) @@ -176,9 +172,7 @@ def default_missing_value_for_dtype(dtype): try: return _FILLVALUE_DEFAULTS[dtype] except KeyError: - raise NoDefaultMissingValue( - "No default value registered for dtype %s." % dtype - ) + raise NoDefaultMissingValue("No default value registered for dtype %s." % dtype) def repeat_first_axis(array, count): @@ -411,7 +405,7 @@ def busday_count_mask_NaT(begindates, enddates, out=None): return out -class WarningContext(object): +class WarningContext: """ Re-usable contextmanager for contextually managing warnings. """ diff --git a/src/zipline/utils/run_algo.py b/src/zipline/utils/run_algo.py index 508b5dba1a..c779a220e5 100644 --- a/src/zipline/utils/run_algo.py +++ b/src/zipline/utils/run_algo.py @@ -420,7 +420,7 @@ def run_algorithm( ) -class BenchmarkSpec(object): +class BenchmarkSpec: """ Helper for different ways we can get benchmark data for the Zipline CLI and zipline.utils.run_algo.run_algorithm. diff --git a/src/zipline/utils/security_list.py b/src/zipline/utils/security_list.py index b6209da5e1..82ec6c42ab 100644 --- a/src/zipline/utils/security_list.py +++ b/src/zipline/utils/security_list.py @@ -17,7 +17,7 @@ SECURITY_LISTS_DIR = os.path.join(zipline_dir, "resources", "security_lists") -class SecurityList(object): +class SecurityList: def __init__(self, data, current_date_func, asset_finder): """ data: a nested dictionary: @@ -88,7 +88,7 @@ def update_current(self, effective_date, symbols, change_func): change_func(asset.sid) -class SecurityListSet(object): +class SecurityListSet: # provide a cut point to substitute other security # list implementations. security_list_type = SecurityList diff --git a/src/zipline/utils/sentinel.py b/src/zipline/utils/sentinel.py index d6dfa3db35..6c8b6cdc6c 100644 --- a/src/zipline/utils/sentinel.py +++ b/src/zipline/utils/sentinel.py @@ -7,7 +7,7 @@ from textwrap import dedent -class _Sentinel(object): +class _Sentinel: """Base class for Sentinel objects.""" __slots__ = ("__weakref__",) diff --git a/tests/pipeline/test_pipeline_algo.py b/tests/pipeline/test_pipeline_algo.py index 353dc7bd17..9e4dce022c 100644 --- a/tests/pipeline/test_pipeline_algo.py +++ b/tests/pipeline/test_pipeline_algo.py @@ -412,7 +412,7 @@ def initialize(context): algo.run() -class MockDailyBarSpotReader(object): +class MockDailyBarSpotReader: """ A BcolzDailyBarReader which returns a constant value for spot price. """ diff --git a/tests/test_memoize.py b/tests/test_memoize.py index 9726aafb79..1480194ced 100644 --- a/tests/test_memoize.py +++ b/tests/test_memoize.py @@ -36,7 +36,7 @@ def func(x): def test_remember_last_method(self): call_count = defaultdict(int) - class clz(object): + class clz: @remember_last def func(self, x): call_count[(self, x)] += 1 diff --git a/tests/test_registration_manager.py b/tests/test_registration_manager.py index 6f1d737d3a..a5df7b78ad 100644 --- a/tests/test_registration_manager.py +++ b/tests/test_registration_manager.py @@ -52,7 +52,7 @@ def check_registered(): with pytest.raises(ValueError, match=msg): @rm.register("ayy-lmao") - class Fake(object): + class Fake: pass # assert excinfo.value.args == msg @@ -91,7 +91,7 @@ def check_registered(): # Check that we successfully registered. check_registered() - class Fake(object): + class Fake: pass # Try and fail to register with the same key again. diff --git a/tests/test_restrictions.py b/tests/test_restrictions.py index 1c6cc4804e..5352feeb7c 100644 --- a/tests/test_restrictions.py +++ b/tests/test_restrictions.py @@ -224,7 +224,7 @@ def test_security_list_restrictions(self): """ # A mock SecurityList object filled with fake data - class SecurityList(object): + class SecurityList: def __init__(self, assets_by_dt): self.assets_by_dt = assets_by_dt diff --git a/tests/test_testing.py b/tests/test_testing.py index 3a868bef45..77e543408c 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -171,7 +171,7 @@ def test_instance_of(self): assert "foo" == instance_of((str, int)) def test_instance_of_exact(self): - class Foo(object): + class Foo: pass class Bar(Foo): diff --git a/tests/test_tradesimulation.py b/tests/test_tradesimulation.py index 66fecf2ed4..4ae3c41953 100644 --- a/tests/test_tradesimulation.py +++ b/tests/test_tradesimulation.py @@ -83,7 +83,7 @@ def before_trading_start(algo, data): assert bts_times == expected_times[:num_sessions] -class BeforeTradingStartsOnlyClock(object): +class BeforeTradingStartsOnlyClock: def __init__(self, bts_minute): self.bts_minute = bts_minute diff --git a/tests/utils/test_argcheck.py b/tests/utils/test_argcheck.py index 554767b4c1..b3aa48c00b 100644 --- a/tests/utils/test_argcheck.py +++ b/tests/utils/test_argcheck.py @@ -269,7 +269,7 @@ def h(c=1): verify_callable_argspec(h, expected_args) def test_bound_method(self): - class C(object): + class C: def f(self, a, b): pass diff --git a/tests/utils/test_preprocess.py b/tests/utils/test_preprocess.py index cf3fa3d060..08e86e9588 100644 --- a/tests/utils/test_preprocess.py +++ b/tests/utils/test_preprocess.py @@ -169,7 +169,7 @@ def test_preprocess_on_method(self, args, kwargs): for decorator in decorators: - class Foo(object): + class Foo: @decorator def method(self, a, b, c=3): return a, b, c @@ -210,7 +210,7 @@ def foo(a, b, c): foo(not_int(1), not_int(2), 3) def test_expect_types_custom_funcname(self): - class Foo(object): + class Foo: @expect_types(__funcname="ArgleBargle", a=int) def __init__(self, a): self.a = a @@ -287,7 +287,7 @@ def test_expect_element_custom_funcname(self): set_ = {"a", "b"} - class Foo(object): + class Foo: @expect_element(__funcname="ArgleBargle", a=set_) def __init__(self, a): self.a = a @@ -356,7 +356,7 @@ def test_expect_dtypes_custom_funcname(self): allowed_dtypes = (np.dtype("datetime64[ns]"), np.dtype("float")) - class Foo(object): + class Foo: @expect_dtypes(__funcname="Foo", a=allowed_dtypes) def __init__(self, a): self.a = a From 79c62309868c13a3063f9b7167b6465cabdf6c5c Mon Sep 17 00:00:00 2001 From: MBounouar Date: Thu, 24 Nov 2022 17:26:23 +0100 Subject: [PATCH 05/15] cleanup metaclass declaration --- src/zipline/assets/assets.py | 6 +-- src/zipline/assets/roll_finder.py | 4 +- src/zipline/data/bar_reader.py | 4 +- src/zipline/data/dispatch_bar_reader.py | 4 +- src/zipline/data/history_loader.py | 4 +- src/zipline/data/minute_bars.py | 4 +- src/zipline/data/resample.py | 4 +- src/zipline/finance/asset_restrictions.py | 8 +--- src/zipline/finance/blotter/blotter.py | 4 +- .../finance/blotter/simulation_blotter.py | 15 ++------ src/zipline/finance/commission.py | 13 ++++--- src/zipline/finance/execution.py | 4 +- src/zipline/finance/slippage.py | 18 +++------ src/zipline/lib/labelarray.py | 38 ++++++------------- src/zipline/pipeline/downsample_helpers.py | 4 +- src/zipline/pipeline/engine.py | 4 +- src/zipline/pipeline/factors/basic.py | 4 +- src/zipline/pipeline/factors/statistical.py | 6 +-- src/zipline/pipeline/term.py | 4 +- src/zipline/sources/requests_csv.py | 12 ++---- src/zipline/utils/events.py | 4 +- src/zipline/utils/final.py | 4 +- src/zipline/utils/pandas_utils.py | 4 +- 23 files changed, 69 insertions(+), 107 deletions(-) diff --git a/src/zipline/assets/assets.py b/src/zipline/assets/assets.py index 1391f2a231..59ae968bda 100644 --- a/src/zipline/assets/assets.py +++ b/src/zipline/assets/assets.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta +from abc import ABC import array import binascii from collections import deque, namedtuple @@ -1493,7 +1493,7 @@ def equities_sids_for_country_code(self, country_code): return tuple(sids.tolist()) -class AssetConvertible(metaclass=ABCMeta): +class AssetConvertible(ABC): """ ABC for types that are convertible to integer-representations of Assets. @@ -1513,7 +1513,7 @@ class NotAssetConvertible(ValueError): pass -class PricingDataAssociable(metaclass=ABCMeta): +class PricingDataAssociable(ABC): """ ABC for types that can be associated with pricing data. diff --git a/src/zipline/assets/roll_finder.py b/src/zipline/assets/roll_finder.py index 4ac6fd4b6d..2c0016bb17 100644 --- a/src/zipline/assets/roll_finder.py +++ b/src/zipline/assets/roll_finder.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod # Number of days over which to compute rolls when finding the current contract # for a volume-rolling contract chain. For more details on why this is needed, @@ -20,7 +20,7 @@ ROLL_DAYS_FOR_CURRENT_CONTRACT = 90 -class RollFinder(object, metaclass=ABCMeta): +class RollFinder(ABC): """ Abstract base class for calculating when futures contracts are the active contract. diff --git a/src/zipline/data/bar_reader.py b/src/zipline/data/bar_reader.py index 9a2752e5a7..9b294d2248 100644 --- a/src/zipline/data/bar_reader.py +++ b/src/zipline/data/bar_reader.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod class NoDataOnDate(Exception): @@ -41,7 +41,7 @@ class NoDataForSid(Exception): OHLCV = ("open", "high", "low", "close", "volume") -class BarReader(object, metaclass=ABCMeta): +class BarReader(ABC): @property @abstractmethod def data_frequency(self): diff --git a/src/zipline/data/dispatch_bar_reader.py b/src/zipline/data/dispatch_bar_reader.py index e57bab3c2d..a82977240a 100644 --- a/src/zipline/data/dispatch_bar_reader.py +++ b/src/zipline/data/dispatch_bar_reader.py @@ -12,14 +12,14 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod from numpy import full, nan, int64, zeros from zipline.utils.memoize import lazyval -class AssetDispatchBarReader(metaclass=ABCMeta): +class AssetDispatchBarReader(ABC): """ Parameters diff --git a/src/zipline/data/history_loader.py b/src/zipline/data/history_loader.py index cce9a958e4..795e0c1f75 100644 --- a/src/zipline/data/history_loader.py +++ b/src/zipline/data/history_loader.py @@ -13,7 +13,7 @@ # limitations under the License. from abc import ( - ABCMeta, + ABC, abstractmethod, ) @@ -260,7 +260,7 @@ def get(self, end_ix): return self.current -class HistoryLoader(metaclass=ABCMeta): +class HistoryLoader(ABC): """ Loader for sliding history windows, with support for adjustments. diff --git a/src/zipline/data/minute_bars.py b/src/zipline/data/minute_bars.py index cf07c4e817..4245faa7a4 100644 --- a/src/zipline/data/minute_bars.py +++ b/src/zipline/data/minute_bars.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod import json import os from glob import glob @@ -1265,7 +1265,7 @@ def load_raw_arrays(self, fields, start_dt, end_dt, sids): return results -class MinuteBarUpdateReader(object, metaclass=ABCMeta): +class MinuteBarUpdateReader(ABC): """ Abstract base class for minute update readers. """ diff --git a/src/zipline/data/resample.py b/src/zipline/data/resample.py index 3d47c50511..560f50c377 100644 --- a/src/zipline/data/resample.py +++ b/src/zipline/data/resample.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from collections import OrderedDict -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod import numpy as np import pandas as pd @@ -599,7 +599,7 @@ def get_last_traded_dt(self, asset, dt): return self.trading_calendar.minute_to_session_label(last_dt) -class ReindexBarReader(metaclass=ABCMeta): +class ReindexBarReader(ABC): """ A base class for readers which reindexes results, filling in the additional indices with empty data. diff --git a/src/zipline/finance/asset_restrictions.py b/src/zipline/finance/asset_restrictions.py index 74cc79bb2f..865f80a4ef 100644 --- a/src/zipline/finance/asset_restrictions.py +++ b/src/zipline/finance/asset_restrictions.py @@ -105,9 +105,7 @@ def __or__(self, other_restriction): def is_restricted(self, assets, dt): if isinstance(assets, Asset): - return any( - r.is_restricted(assets, dt) for r in self.sub_restrictions - ) + return any(r.is_restricted(assets, dt) for r in self.sub_restrictions) return reduce( operator.or_, @@ -167,9 +165,7 @@ def __init__(self, restrictions): # A dict mapping each asset to its restrictions, which are sorted by # ascending order of effective_date self._restrictions_by_asset = { - asset: sorted( - restrictions_for_asset, key=lambda x: x.effective_date - ) + asset: sorted(restrictions_for_asset, key=lambda x: x.effective_date) for asset, restrictions_for_asset in groupby( lambda x: x.asset, restrictions ).items() diff --git a/src/zipline/finance/blotter/blotter.py b/src/zipline/finance/blotter/blotter.py index bd28801f5a..4a2583556c 100644 --- a/src/zipline/finance/blotter/blotter.py +++ b/src/zipline/finance/blotter/blotter.py @@ -12,13 +12,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod from zipline.extensions import extensible from zipline.finance.cancel_policy import NeverCancel @extensible -class Blotter(metaclass=ABCMeta): +class Blotter(ABC): def __init__(self, cancel_policy=None): self.cancel_policy = cancel_policy if cancel_policy else NeverCancel() self.current_dt = None diff --git a/src/zipline/finance/blotter/simulation_blotter.py b/src/zipline/finance/blotter/simulation_blotter.py index ba43a83742..7125f6d314 100644 --- a/src/zipline/finance/blotter/simulation_blotter.py +++ b/src/zipline/finance/blotter/simulation_blotter.py @@ -138,9 +138,7 @@ def order(self, asset, amount, style, order_id=None): elif amount > self.max_shares: # Arbitrary limit of 100 billion (US) shares will never be # exceeded except by a buggy algorithm. - raise OverflowError( - "Can't order more than %d shares" % self.max_shares - ) + raise OverflowError("Can't order more than %d shares" % self.max_shares) is_buy = amount > 0 order = Order( @@ -271,8 +269,7 @@ def execute_daily_cancel_policy(self, event): order_amt=order.amount, order_sym=order.asset.symbol, order_filled=-1 * order.filled, - order_failed=-1 - * (order.amount - order.filled), + order_failed=-1 * (order.amount - order.filled), ) ) else: @@ -289,9 +286,7 @@ def execute_cancel_policy(self, event): if self.cancel_policy.should_cancel(event): warn = self.cancel_policy.warn_on_cancel for asset in copy(self.open_orders): - self.cancel_all_orders_for_asset( - asset, warn, relay_status=False - ) + self.cancel_all_orders_for_asset(asset, warn, relay_status=False) def reject(self, order_id, reason=""): """ @@ -395,9 +390,7 @@ def get_transactions(self, bar_data): for asset, asset_orders in self.open_orders.items(): slippage = self.slippage_models[type(asset)] - for order, txn in slippage.simulate( - bar_data, asset, asset_orders - ): + for order, txn in slippage.simulate(bar_data, asset, asset_orders): commission = self.commission_models[type(asset)] additional_commission = commission.calculate(order, txn) diff --git a/src/zipline/finance/commission.py b/src/zipline/finance/commission.py index e408417069..eab183c68c 100644 --- a/src/zipline/finance/commission.py +++ b/src/zipline/finance/commission.py @@ -256,11 +256,14 @@ def __repr__(self): else: exchange_fee = "" - return "{class_name}(cost_per_contract={cost_per_contract}, " "exchange_fee={exchange_fee}, min_trade_cost={min_trade_cost})".format( - class_name=self.__class__.__name__, - cost_per_contract=cost_per_contract, - exchange_fee=exchange_fee, - min_trade_cost=self.min_trade_cost, + return ( + "{class_name}(cost_per_contract={cost_per_contract}, " + "exchange_fee={exchange_fee}, min_trade_cost={min_trade_cost})".format( + class_name=self.__class__.__name__, + cost_per_contract=cost_per_contract, + exchange_fee=exchange_fee, + min_trade_cost=self.min_trade_cost, + ) ) def calculate(self, order, transaction): diff --git a/src/zipline/finance/execution.py b/src/zipline/finance/execution.py index c7b42d835e..31c50f5c87 100644 --- a/src/zipline/finance/execution.py +++ b/src/zipline/finance/execution.py @@ -185,9 +185,9 @@ def asymmetric_round_price(price, prefer_round_down, tick_size, diff=0.95): If not prefer_round_down: (.0005, X.0105] -> round to X.01. """ precision = zp_math.number_of_decimal_places(tick_size) - multiplier = int(tick_size * (10 ** precision)) + multiplier = int(tick_size * (10**precision)) diff -= 0.5 # shift the difference down - diff *= 10 ** -precision # adjust diff to precision of tick size + diff *= 10**-precision # adjust diff to precision of tick size diff *= multiplier # adjust diff to value of tick_size # Subtracting an epsilon from diff to enforce the open-ness of the upper diff --git a/src/zipline/finance/slippage.py b/src/zipline/finance/slippage.py index 6f2cc974a5..7fe7bc60d5 100644 --- a/src/zipline/finance/slippage.py +++ b/src/zipline/finance/slippage.py @@ -191,9 +191,7 @@ def simulate(self, data, asset, orders_for_asset): txn = None try: - execution_price, execution_volume = self.process_order( - data, order - ) + execution_price, execution_volume = self.process_order(data, order) if execution_price is not None: txn = create_transaction( @@ -332,7 +330,7 @@ def process_order(self, data, order): # END simulated_impact = ( - volume_share ** 2 + volume_share**2 * math.copysign(self.price_impact, order.direction) * price ) @@ -449,9 +447,7 @@ def process_order(self, data, order): if not volume: return None, None - txn_volume = int( - min(self.get_txn_volume(data, order), abs(order.open_amount)) - ) + txn_volume = int(min(self.get_txn_volume(data, order), abs(order.open_amount))) # If the computed transaction volume is zero or a decimal value, 'int' # will round it down to zero. In that case just bail. @@ -472,9 +468,7 @@ def process_order(self, data, order): volatility=volatility, ) - impacted_price = price + math.copysign( - simulated_impact, order.direction - ) + impacted_price = price + math.copysign(simulated_impact, order.direction) if fill_price_worse_than_limit_price(impacted_price, order): return None, None @@ -688,9 +682,7 @@ def process_order(self, data, order): max_volume = int(self.volume_limit * volume) price = data.current(order.asset, "close") - shares_to_fill = min( - abs(order.open_amount), max_volume - self.volume_for_bar - ) + shares_to_fill = min(abs(order.open_amount), max_volume - self.volume_for_bar) if shares_to_fill == 0: raise LiquidityExceeded() diff --git a/src/zipline/lib/labelarray.py b/src/zipline/lib/labelarray.py index 1ec763bd0c..c7f9019ac6 100644 --- a/src/zipline/lib/labelarray.py +++ b/src/zipline/lib/labelarray.py @@ -37,16 +37,12 @@ def compare_arrays(left, right): "Eq check with a short-circuit for identical objects." - return left is right or ( - (left.shape == right.shape) and (left == right).all() - ) + return left is right or ((left.shape == right.shape) and (left == right).all()) def _make_unsupported_method(name): def method(*args, **kwargs): - raise NotImplementedError( - "Method %s is not supported on LabelArrays." % name - ) + raise NotImplementedError("Method %s is not supported on LabelArrays." % name) method.__name__ = name method.__doc__ = "Unsupported LabelArray Method: %s" % name @@ -294,9 +290,7 @@ def __array_finalize__(self, obj): responsible for copying over the parent array's category metadata. """ if obj is None: - raise TypeError( - "Direct construction of LabelArrays is not supported." - ) + raise TypeError("Direct construction of LabelArrays is not supported.") # See docstring for an explanation of when these will or will not be # set. @@ -346,9 +340,7 @@ def as_categorical_frame(self, index, columns, name=None): Coerce self into a pandas DataFrame of Categoricals. """ if len(self.shape) != 2: - raise ValueError( - "Can't convert a non-2D LabelArray into a DataFrame." - ) + raise ValueError("Can't convert a non-2D LabelArray into a DataFrame.") expected_shape = (len(index), len(columns)) if expected_shape != self.shape: @@ -443,17 +435,13 @@ def is_missing(self): """ Like isnan, but checks for locations where we store missing values. """ - return ( - self.as_int_array() == self.reverse_categories[self.missing_value] - ) + return self.as_int_array() == self.reverse_categories[self.missing_value] def not_missing(self): """ Like ~isnan, but checks for locations where we store missing values. """ - return ( - self.as_int_array() != self.reverse_categories[self.missing_value] - ) + return self.as_int_array() != self.reverse_categories[self.missing_value] def _equality_check(op): """ @@ -655,9 +643,7 @@ def map(self, f): else: allowed_outtypes = self.SUPPORTED_NON_NONE_SCALAR_TYPES - def f_to_use( - x, missing_value=self.missing_value, otypes=allowed_outtypes - ): + def f_to_use(x, missing_value=self.missing_value, otypes=allowed_outtypes): # Don't call f on the missing value; those locations don't exist # semantically. We return _sortable_sentinel rather than None @@ -685,9 +671,9 @@ def f_to_use( return ret - new_categories_with_duplicates = np.vectorize( - f_to_use, otypes=[object] - )(self.categories) + new_categories_with_duplicates = np.vectorize(f_to_use, otypes=[object])( + self.categories + ) # If f() maps multiple inputs to the same output, then we can end up # with the same code duplicated multiple times. Compress the categories @@ -821,9 +807,7 @@ def __lt__(self, other): def labelarray_where(cond, trues, falses): """LabelArray-aware implementation of np.where.""" if trues.missing_value != falses.missing_value: - raise ValueError( - "Can't compute where on arrays with different missing values." - ) + raise ValueError("Can't compute where on arrays with different missing values.") strs = np.where(cond, trues.as_string_array(), falses.as_string_array()) return LabelArray(strs, missing_value=trues.missing_value) diff --git a/src/zipline/pipeline/downsample_helpers.py b/src/zipline/pipeline/downsample_helpers.py index 20eea21bfa..bb561fa51c 100644 --- a/src/zipline/pipeline/downsample_helpers.py +++ b/src/zipline/pipeline/downsample_helpers.py @@ -56,6 +56,4 @@ def select_sampling_indices(dates, frequency): ``np.diff(dates.)`` to find dates where the sampling period has changed. """ - return changed_locations( - _dt_to_period[frequency](dates), include_first=True - ) + return changed_locations(_dt_to_period[frequency](dates), include_first=True) diff --git a/src/zipline/pipeline/engine.py b/src/zipline/pipeline/engine.py index 8fbb22d123..da99e09d0e 100644 --- a/src/zipline/pipeline/engine.py +++ b/src/zipline/pipeline/engine.py @@ -55,7 +55,7 @@ into "narrow" format, with output labels dictated by the Pipeline's screen. This logic lives in SimplePipelineEngine._to_narrow. """ -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod from functools import partial from numpy import array, arange @@ -80,7 +80,7 @@ from .term import AssetExists, InputDates, LoadableTerm -class PipelineEngine(metaclass=ABCMeta): +class PipelineEngine(ABC): @abstractmethod def run_pipeline(self, pipeline, start_date, end_date, hooks=None): """ diff --git a/src/zipline/pipeline/factors/basic.py b/src/zipline/pipeline/factors/basic.py index 3551f0ea44..0d5d28c3a8 100644 --- a/src/zipline/pipeline/factors/basic.py +++ b/src/zipline/pipeline/factors/basic.py @@ -435,7 +435,9 @@ def compute(self, today, assets, out, data, decay_rate): variance = average((data - mean) ** 2, axis=0, weights=weights) squared_weight_sum = np_sum(weights) ** 2 - bias_correction = squared_weight_sum / (squared_weight_sum - np_sum(weights**2)) + bias_correction = squared_weight_sum / ( + squared_weight_sum - np_sum(weights**2) + ) out[:] = sqrt(variance * bias_correction) diff --git a/src/zipline/pipeline/factors/statistical.py b/src/zipline/pipeline/factors/statistical.py index 1d05f8c982..93e03758db 100644 --- a/src/zipline/pipeline/factors/statistical.py +++ b/src/zipline/pipeline/factors/statistical.py @@ -630,7 +630,7 @@ def vectorized_beta(dependents, independent, allowed_missing, out=None): # column may have a different subset of the data dropped due to missing # data in the corresponding dependent column. # shape: (M,) - independent_variances = nanmean(ind_residual ** 2, axis=0) + independent_variances = nanmean(ind_residual**2, axis=0) # shape: (M,) np.divide(covariances, independent_variances, out=out) @@ -696,8 +696,8 @@ def vectorized_pearson_r(dependents, independents, allowed_missing, out=None): ind_residual = independents - mean(independents, axis=0) dep_residual = dependents - mean(dependents, axis=0) - ind_variance = mean(ind_residual ** 2, axis=0) - dep_variance = mean(dep_residual ** 2, axis=0) + ind_variance = mean(ind_residual**2, axis=0) + dep_variance = mean(dep_residual**2, axis=0) covariances = mean(ind_residual * dep_residual, axis=0) diff --git a/src/zipline/pipeline/term.py b/src/zipline/pipeline/term.py index ab16651d48..bb76de3503 100644 --- a/src/zipline/pipeline/term.py +++ b/src/zipline/pipeline/term.py @@ -1,7 +1,7 @@ """ Base class for Filters, Factors and Classifiers """ -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod from bisect import insort from collections.abc import Mapping from weakref import WeakValueDictionary @@ -47,7 +47,7 @@ from .sentinels import NotSpecified -class Term(object, metaclass=ABCMeta): +class Term(ABC): """ Base class for objects that can appear in the compute graph of a :class:`zipline.pipeline.Pipeline`. diff --git a/src/zipline/sources/requests_csv.py b/src/zipline/sources/requests_csv.py index 04454c3918..b0806ea371 100644 --- a/src/zipline/sources/requests_csv.py +++ b/src/zipline/sources/requests_csv.py @@ -1,4 +1,4 @@ -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod from collections import namedtuple import hashlib from textwrap import dedent @@ -110,9 +110,7 @@ def __init__(self, *args, **kwargs): def mask_requests_args(url, validating=False, params_checker=None, **kwargs): requests_kwargs = { - key: val - for (key, val) in kwargs.items() - if key in ALLOWED_REQUESTS_KWARGS + key: val for (key, val) in kwargs.items() if key in ALLOWED_REQUESTS_KWARGS } if params_checker is not None: url, s_params = params_checker(url) @@ -133,7 +131,7 @@ def mask_requests_args(url, validating=False, params_checker=None, **kwargs): return request_pair(requests_kwargs, url) -class PandasCSV(object, metaclass=ABCMeta): +class PandasCSV(ABC): def __init__( self, pre_func, @@ -241,9 +239,7 @@ def parse_date_str_series( def mask_pandas_args(self, kwargs): pandas_kwargs = { - key: val - for (key, val) in kwargs.items() - if key in ALLOWED_READ_CSV_KWARGS + key: val for (key, val) in kwargs.items() if key in ALLOWED_READ_CSV_KWARGS } if "usecols" in pandas_kwargs: usecols = pandas_kwargs["usecols"] diff --git a/src/zipline/utils/events.py b/src/zipline/utils/events.py index 98abd20a89..d95c9e4a47 100644 --- a/src/zipline/utils/events.py +++ b/src/zipline/utils/events.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABCMeta, abstractmethod +from abc import ABCMeta, ABC, abstractmethod from collections import namedtuple import inspect import warnings @@ -227,7 +227,7 @@ def handle_data(self, context, data, dt): self.callback(context, data) -class EventRule(metaclass=ABCMeta): +class EventRule(ABC): """A rule defining when a scheduled function should execute.""" # Instances of EventRule are assigned a calendar instance when scheduling diff --git a/src/zipline/utils/final.py b/src/zipline/utils/final.py index 47b263b8ad..073bd03ddb 100644 --- a/src/zipline/utils/final.py +++ b/src/zipline/utils/final.py @@ -1,4 +1,4 @@ -from abc import ABCMeta, abstractmethod +from abc import ABC, abstractmethod # Consistent error to be thrown in various cases regarding overriding # `final` attributes. @@ -61,7 +61,7 @@ class object. super(FinalMeta, self).__setattr__(name, value) -class final(object, metaclass=ABCMeta): +class final(ABC): """ An attribute that cannot be overridden. This is like the final modifier in Java. diff --git a/src/zipline/utils/pandas_utils.py b/src/zipline/utils/pandas_utils.py index 67c3ff4ad8..cf1126b07b 100644 --- a/src/zipline/utils/pandas_utils.py +++ b/src/zipline/utils/pandas_utils.py @@ -337,7 +337,5 @@ def check_indexes_all_same(indexes, message="Indexes are not equal."): bad_loc = np.flatnonzero(~same)[0] raise ValueError( "{}\nFirst difference is at index {}: " - "{} != {}".format( - message, bad_loc, first[bad_loc], other[bad_loc] - ), + "{} != {}".format(message, bad_loc, first[bad_loc], other[bad_loc]), ) From b997db454db5672749e9b0e349bd2e82eac131d2 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Sun, 27 Nov 2022 20:35:19 +0100 Subject: [PATCH 06/15] replace logbook with logging (#16) * fix B904 flak8-bugbear warnings * replaced logbook with logging --- pyproject.toml | 1 - src/zipline/__main__.py | 20 +- src/zipline/algorithm.py | 20 +- src/zipline/assets/asset_writer.py | 2 +- src/zipline/assets/assets.py | 24 +- src/zipline/assets/synthetic.py | 2 + src/zipline/currency.py | 6 +- src/zipline/data/adjustments.py | 32 ++- src/zipline/data/bcolz_daily_bars.py | 12 +- src/zipline/data/benchmarks.py | 6 +- src/zipline/data/bundles/core.py | 8 +- src/zipline/data/bundles/csvdir.py | 7 +- src/zipline/data/bundles/quandl.py | 6 +- src/zipline/data/data_portal.py | 8 +- src/zipline/data/fx/hdf5.py | 8 +- src/zipline/data/hdf5_daily_bars.py | 4 +- src/zipline/data/minute_bars.py | 14 +- src/zipline/examples/dual_ema_talib.py | 15 +- src/zipline/examples/dual_moving_average.py | 11 +- src/zipline/examples/olmar.py | 18 +- src/zipline/extensions.py | 12 +- .../finance/blotter/simulation_blotter.py | 12 +- src/zipline/finance/controls.py | 13 +- src/zipline/finance/execution.py | 4 +- src/zipline/finance/ledger.py | 4 +- src/zipline/finance/metrics/core.py | 8 +- src/zipline/finance/metrics/tracker.py | 14 +- src/zipline/finance/position.py | 4 +- src/zipline/finance/trading.py | 4 +- src/zipline/gens/tradesimulation.py | 11 +- src/zipline/lib/adjusted_array.py | 8 +- src/zipline/lib/labelarray.py | 4 +- .../pipeline/classifiers/classifier.py | 14 +- src/zipline/pipeline/data/dataset.py | 78 +++--- src/zipline/pipeline/domain.py | 4 +- src/zipline/pipeline/factors/factor.py | 10 +- src/zipline/pipeline/filters/filter.py | 6 +- .../pipeline/loaders/earnings_estimates.py | 4 +- src/zipline/pipeline/loaders/frame.py | 20 +- src/zipline/pipeline/loaders/synthetic.py | 4 +- src/zipline/pipeline/loaders/utils.py | 12 +- src/zipline/pipeline/mixins.py | 4 +- src/zipline/pipeline/term.py | 26 +- src/zipline/pipeline/visualize.py | 10 +- src/zipline/sources/requests_csv.py | 18 +- src/zipline/sources/test_source.py | 2 +- src/zipline/testing/__init__.py | 1 - src/zipline/testing/core.py | 24 -- src/zipline/testing/fixtures.py | 39 +-- src/zipline/testing/predicates.py | 14 +- src/zipline/utils/cache.py | 16 +- src/zipline/utils/calendar_utils.py | 6 +- src/zipline/utils/final.py | 12 +- src/zipline/utils/input_validation.py | 12 +- src/zipline/utils/numpy_utils.py | 16 +- src/zipline/utils/run_algo.py | 10 +- tests/data/test_adjustments.py | 54 ++-- tests/pipeline/test_slice.py | 8 +- tests/test_algorithm.py | 244 +++++++++--------- tests/test_benchmark.py | 28 +- tests/test_blotter.py | 3 +- tests/test_execution_styles.py | 5 +- 62 files changed, 502 insertions(+), 524 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fca9be2218..9c44e42611 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,6 @@ dependencies = [ 'intervaltree >=2.1.0', 'iso3166 >=2.1.1', 'iso4217 >=1.6.20180829', - 'logbook >=1.0', 'lru-dict >=1.1.4', 'multipledispatch >=0.6.0', 'networkx >=2.0', diff --git a/src/zipline/__main__.py b/src/zipline/__main__.py index 9769abc46e..0e4d054ee8 100644 --- a/src/zipline/__main__.py +++ b/src/zipline/__main__.py @@ -2,7 +2,7 @@ import os import click -import logbook +import logging import pandas as pd import zipline @@ -48,8 +48,14 @@ @click.pass_context def main(ctx, extension, strict_extensions, default_extension, x): """Top level zipline entry point.""" - # install a logbook handler before performing any other operations - logbook.StderrHandler().push_application() + # install a logging handler before performing any other operations + + logging.basicConfig( + format="[%(asctime)s-%(levelname)s][%(name)s]\n %(message)s", + level=logging.INFO, + datefmt="%Y-%m-%dT%H:%M:%S%z", + ) + create_args(x, zipline.extension_args) load_extensions( default_extension, @@ -357,11 +363,13 @@ def zipline_magic(line, cell=None): # don't use system exit and propogate errors to the caller standalone_mode=False, ) - except SystemExit as e: + except SystemExit as exc: # https://github.com/mitsuhiko/click/pull/533 # even in standalone_mode=False `--help` really wants to kill us ;_; - if e.code: - raise ValueError("main returned non-zero status code: %d" % e.code) + if exc.code: + raise ValueError( + "main returned non-zero status code: %d" % exc.code + ) from exc @main.command() diff --git a/src/zipline/algorithm.py b/src/zipline/algorithm.py index 17ac353918..966b1ad734 100644 --- a/src/zipline/algorithm.py +++ b/src/zipline/algorithm.py @@ -17,7 +17,7 @@ from copy import copy import warnings from datetime import tzinfo, time -import logbook +import logging import pytz import pandas as pd import numpy as np @@ -127,7 +127,7 @@ from zipline.sources.benchmark_source import BenchmarkSource from zipline.zipline_warnings import ZiplineDeprecationWarning -log = logbook.Logger("ZiplineLog") +log = logging.getLogger("ZiplineLog") # For creating and storing pipeline instances AttachedPipeline = namedtuple("AttachedPipeline", "pipe chunks eager") @@ -760,10 +760,10 @@ def get_environment(self, field="platform"): else: try: return env[field] - except KeyError: + except KeyError as exc: raise ValueError( "%r is not a valid field for get_environment" % field, - ) + ) from exc @api_method def fetch_csv( @@ -1195,7 +1195,7 @@ def _can_order_asset(self, asset): # If we are after the asset's end date or auto close date, warn # the user that they can't place an order for this asset, and # return None. - log.warn( + log.warning( "Cannot place order for {0}, as it has de-listed. " "Any existing positions for this asset will be " "liquidated on " @@ -1583,8 +1583,10 @@ def set_symbol_lookup_date(self, dt): self._symbol_lookup_date = pd.Timestamp(dt).tz_localize("UTC") except TypeError: self._symbol_lookup_date = pd.Timestamp(dt).tz_convert("UTC") - except ValueError: - raise UnsupportedDatetimeFormat(input=dt, method="set_symbol_lookup_date") + except ValueError as exc: + raise UnsupportedDatetimeFormat( + input=dt, method="set_symbol_lookup_date" + ) from exc @property def data_frequency(self): @@ -2228,11 +2230,11 @@ def pipeline_output(self, name): """ try: pipe, chunks, _ = self._pipelines[name] - except KeyError: + except KeyError as exc: raise NoSuchPipeline( name=name, valid=list(self._pipelines.keys()), - ) + ) from exc return self._pipeline_output(pipe, chunks, name) def _pipeline_output(self, pipeline, chunks, name): diff --git a/src/zipline/assets/asset_writer.py b/src/zipline/assets/asset_writer.py index f9cd2ccd7a..1e2cd92382 100644 --- a/src/zipline/assets/asset_writer.py +++ b/src/zipline/assets/asset_writer.py @@ -821,7 +821,7 @@ def write( def _write_df_to_table(self, tbl, df, txn, chunk_size): df = df.copy() - for column, dtype in df.dtypes.iteritems(): + for column, dtype in df.dtypes.items(): if dtype.kind == "M": df[column] = _dt_to_epoch_ns(df[column]) diff --git a/src/zipline/assets/assets.py b/src/zipline/assets/assets.py index 59ae968bda..d4c29079fa 100644 --- a/src/zipline/assets/assets.py +++ b/src/zipline/assets/assets.py @@ -21,7 +21,7 @@ from operator import itemgetter, attrgetter import struct -from logbook import Logger +import logging import numpy as np import pandas as pd from pandas import isnull @@ -77,7 +77,7 @@ from zipline.utils.preprocess import preprocess from zipline.utils.sqlite_utils import group_into_chunks, coerce_string_to_eng -log = Logger("assets.py") +log = logging.getLogger("assets.py") # A set of fields that need to be converted to strings before building an # Asset to avoid unicode fields @@ -789,9 +789,9 @@ def _lookup_symbol_strict(self, ownership_map, multi_country, symbol, as_of_date try: owners = ownership_map[company_symbol, share_class_symbol] assert owners, "empty owners list for %r" % symbol - except KeyError: + except KeyError as exc: # no equity has ever held this symbol - raise SymbolNotFound(symbol=symbol) + raise SymbolNotFound(symbol=symbol) from exc if not as_of_date: # exactly one equity has ever held this symbol, we may resolve @@ -848,9 +848,9 @@ def _lookup_symbol_fuzzy(self, ownership_map, multi_country, symbol, as_of_date) try: owners = ownership_map[company_symbol + share_class_symbol] assert owners, "empty owners list for %r" % symbol - except KeyError: + except KeyError as exc: # no equity has ever held a symbol matching the fuzzy symbol - raise SymbolNotFound(symbol=symbol) + raise SymbolNotFound(symbol=symbol) from exc if not as_of_date: if len(owners) == 1: @@ -1084,9 +1084,9 @@ def lookup_by_supplementary_field(self, field_name, value, as_of_date): field_name, value, ) - except KeyError: + except KeyError as exc: # no equity has ever held this value - raise ValueNotFoundForField(field=field_name, value=value) + raise ValueNotFoundForField(field=field_name, value=value) from exc if not as_of_date: if len(owners) > 1: @@ -1148,7 +1148,7 @@ def get_supplementary_field(self, sid, field_name, as_of_date): sid, ) except KeyError: - raise NoValueForSid(field=field_name, sid=sid) + raise NoValueForSid(field=field_name, sid=sid) from KeyError if not as_of_date: if len(periods) > 1: @@ -1363,9 +1363,9 @@ def lookup_generic(self, obj, as_of_date, country_code): return matches[0], missing except IndexError: if hasattr(obj, "__int__"): - raise SidsNotFound(sids=[obj]) + raise SidsNotFound(sids=[obj]) from IndexError else: - raise SymbolNotFound(symbol=obj) + raise SymbolNotFound(symbol=obj) from IndexError # Interpret input as iterable. try: @@ -1373,7 +1373,7 @@ def lookup_generic(self, obj, as_of_date, country_code): except TypeError: raise NotAssetConvertible( "Input was not a AssetConvertible " "or iterable of AssetConvertible." - ) + ) from TypeError for obj in iterator: self._lookup_generic_scalar( diff --git a/src/zipline/assets/synthetic.py b/src/zipline/assets/synthetic.py index 712d1601ee..ba9c749856 100644 --- a/src/zipline/assets/synthetic.py +++ b/src/zipline/assets/synthetic.py @@ -193,6 +193,8 @@ def make_jagged_equity_info( # Explicitly pass None to disable setting the auto_close_date column. if auto_close_delta is not None: + # TODO CHECK PerformanceWarning: Non-vectorized DateOffset + # being applied to Series or DatetimeIndex frame["auto_close_date"] = frame["end_date"] + auto_close_delta return frame diff --git a/src/zipline/currency.py b/src/zipline/currency.py index 78bfdeefa7..69ccfd3df4 100644 --- a/src/zipline/currency.py +++ b/src/zipline/currency.py @@ -30,8 +30,10 @@ def __new__(cls, code): else: try: name = ISO4217Currency(code).currency_name - except ValueError: - raise ValueError("{!r} is not a valid currency code.".format(code)) + except ValueError as exc: + raise ValueError( + "{!r} is not a valid currency code.".format(code) + ) from exc obj = _ALL_CURRENCIES[code] = super(Currency, cls).__new__(cls) obj._code = code diff --git a/src/zipline/data/adjustments.py b/src/zipline/data/adjustments.py index afeb23117b..5580aa9ea3 100644 --- a/src/zipline/data/adjustments.py +++ b/src/zipline/data/adjustments.py @@ -2,7 +2,7 @@ from errno import ENOENT from os import remove -from logbook import Logger +import logging import numpy as np from numpy import integer as any_integer import pandas as pd @@ -22,7 +22,7 @@ from zipline.utils.sqlite_utils import group_into_chunks, coerce_string_to_conn from ._adjustments import load_adjustments_from_sqlite -log = Logger(__name__) +log = logging.getLogger(__name__) SQLITE_ADJUSTMENT_TABLENAMES = frozenset(["splits", "dividends", "mergers"]) @@ -298,13 +298,13 @@ def unpack_db_to_component_dfs(self, convert_dates=False): def get_df_from_table(self, table_name, convert_dates=False): try: date_cols = self._datetime_int_cols[table_name] - except KeyError: + except KeyError as exc: raise ValueError( "Requested table {} not found.\n" "Available tables: {}\n".format( table_name, self._datetime_int_cols.keys() ) - ) + ) from exc # Dates are stored in second resolution as ints in adj.db tables. kwargs = ( @@ -517,22 +517,26 @@ def calc_dividend_ratios(self, dividends): non_nan_ratio_mask = ~np.isnan(ratio) for ix in np.flatnonzero(~non_nan_ratio_mask): - log.warn( + log.warning( "Couldn't compute ratio for dividend" - " sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}", - sid=input_sids[ix], - ex_date=pd.Timestamp(input_dates[ix]), - amount=amount[ix], + " sid=%(sid)s, ex_date=%(ex_date)s, amount=%(amount).3f", + { + "sid": input_sids[ix], + "ex_date": pd.Timestamp(input_dates[ix]).strftime("%Y-%m-%d"), + "amount": amount[ix], + }, ) positive_ratio_mask = ratio > 0 for ix in np.flatnonzero(~positive_ratio_mask & non_nan_ratio_mask): - log.warn( + log.warning( "Dividend ratio <= 0 for dividend" - " sid={sid}, ex_date={ex_date:%Y-%m-%d}, amount={amount:.3f}", - sid=input_sids[ix], - ex_date=pd.Timestamp(input_dates[ix]), - amount=amount[ix], + " sid=%(sid)s, ex_date=%(ex_date)s, amount=%(amount).3f", + { + "sid": input_sids[ix], + "ex_date": pd.Timestamp(input_dates[ix]).strftime("%Y-%m-%d"), + "amount": amount[ix], + }, ) valid_ratio_mask = non_nan_ratio_mask & positive_ratio_mask diff --git a/src/zipline/data/bcolz_daily_bars.py b/src/zipline/data/bcolz_daily_bars.py index ccc89237c3..f217622b7b 100644 --- a/src/zipline/data/bcolz_daily_bars.py +++ b/src/zipline/data/bcolz_daily_bars.py @@ -19,7 +19,7 @@ from bcolz import carray, ctable import numpy as np -import logbook +import logging from numpy import ( array, @@ -50,7 +50,7 @@ from zipline.utils.cli import maybe_show_progress from ._equities import _compute_row_slices, _read_bcolz_data -logger = logbook.Logger("UsEquityPricing") +logger = logging.getLogger("UsEquityPricing") OHLC = frozenset(["open", "high", "low", "close"]) US_EQUITY_PRICING_BCOLZ_COLUMNS = ( @@ -582,8 +582,8 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): def _load_raw_arrays_date_to_index(self, date): try: return self.sessions.get_loc(date) - except KeyError: - raise NoDataOnDate(date) + except KeyError as exc: + raise NoDataOnDate(date) from exc def _spot_col(self, colname): """ @@ -650,10 +650,10 @@ def sid_day_index(self, sid, day): """ try: day_loc = self.sessions.get_loc(day) - except Exception: + except Exception as exc: raise NoDataOnDate( "day={0} is outside of calendar={1}".format(day, self.sessions) - ) + ) from exc offset = day_loc - self._calendar_offsets[sid] if offset < 0: raise NoDataBeforeDate( diff --git a/src/zipline/data/benchmarks.py b/src/zipline/data/benchmarks.py index e88010b78b..dc99da80ab 100644 --- a/src/zipline/data/benchmarks.py +++ b/src/zipline/data/benchmarks.py @@ -12,11 +12,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import logbook +import logging import pandas as pd -log = logbook.Logger(__name__) +log = logging.getLogger(__name__) def get_benchmark_returns_from_file(filelike): @@ -33,7 +33,7 @@ def get_benchmark_returns_from_file(filelike): 2020-01-03 00:00:00+00:00,-0.02 """ - log.info("Reading benchmark returns from {}", filelike) + log.info("Reading benchmark returns from %s", filelike) df = pd.read_csv( filelike, diff --git a/src/zipline/data/bundles/core.py b/src/zipline/data/bundles/core.py index 9d2586052a..60504f99b0 100644 --- a/src/zipline/data/bundles/core.py +++ b/src/zipline/data/bundles/core.py @@ -5,7 +5,7 @@ import warnings import click -from logbook import Logger +import logging import pandas as pd from zipline.utils.calendar_utils import get_calendar from toolz import curry, complement, take @@ -28,7 +28,7 @@ import zipline.utils.paths as pth from zipline.utils.preprocess import preprocess -log = Logger(__name__) +log = logging.getLogger(__name__) def asset_db_path(bundle_name, timestr, environ=None, db_version=None): @@ -441,7 +441,7 @@ def ingest( "writers in order to downgrade the assets" " db." ) - log.info("Ingesting {}.", name) + log.info("Ingesting %s", name) bundle.ingest( environ, asset_db_writer, @@ -620,7 +620,7 @@ def should_clean(name): cleaned = set() for run in all_runs: if should_clean(run): - log.info("Cleaning {}.", run) + log.info("Cleaning %s.", run) path = pth.data_path([name, run], environ=environ) shutil.rmtree(path) cleaned.add(path) diff --git a/src/zipline/data/bundles/csvdir.py b/src/zipline/data/bundles/csvdir.py index b5ca7abde6..eb244c19b6 100644 --- a/src/zipline/data/bundles/csvdir.py +++ b/src/zipline/data/bundles/csvdir.py @@ -4,7 +4,7 @@ import os import sys -import logbook +import logging import numpy as np import pandas as pd from zipline.utils.calendar_utils import register_calendar_alias @@ -12,8 +12,9 @@ from . import core as bundles -handler = logbook.StreamHandler(sys.stdout, format_string=" | {record.message}") -logger = logbook.Logger(__name__) +handler = logging.StreamHandler() +# handler = logging.StreamHandler(sys.stdout, format_string=" | {record.message}") +logger = logging.getLogger(__name__) logger.handlers.append(handler) diff --git a/src/zipline/data/bundles/quandl.py b/src/zipline/data/bundles/quandl.py index 256bafeb1a..2ceac7c948 100644 --- a/src/zipline/data/bundles/quandl.py +++ b/src/zipline/data/bundles/quandl.py @@ -6,7 +6,7 @@ from zipfile import ZipFile from click import progressbar -from logbook import Logger +import logging import pandas as pd import requests from urllib.parse import urlencode @@ -15,7 +15,7 @@ from . import core as bundles import numpy as np -log = Logger(__name__) +log = logging.getLogger(__name__) ONE_MEGABYTE = 1024 * 1024 QUANDL_DATA_URL = "https://www.quandl.com/api/v3/datatables/WIKI/PRICES.csv?" @@ -308,7 +308,7 @@ def quantopian_quandl_bundle( with tarfile.open("r", fileobj=data) as tar: if show_progress: - log.info("Writing data to %s." % output_dir) + log.info("Writing data to %s.", output_dir) tar.extractall(output_dir) diff --git a/src/zipline/data/data_portal.py b/src/zipline/data/data_portal.py index fa0085a133..fefb4959ef 100644 --- a/src/zipline/data/data_portal.py +++ b/src/zipline/data/data_portal.py @@ -14,7 +14,7 @@ # limitations under the License. from operator import mul -from logbook import Logger +import logging import numpy as np from numpy import float64, int64, nan @@ -58,7 +58,7 @@ from zipline.errors import HistoryWindowStartsBeforeData -log = Logger("DataPortal") +log = logging.getLogger("DataPortal") BASE_FIELDS = frozenset( [ @@ -492,10 +492,10 @@ def get_spot_value(self, assets, field, dt, data_frequency): # an iterable. try: iter(assets) - except TypeError: + except TypeError as exc: raise TypeError( "Unexpected 'assets' value of type {}.".format(type(assets)) - ) + ) from exc session_label = self.trading_calendar.minute_to_session_label(dt) diff --git a/src/zipline/data/fx/hdf5.py b/src/zipline/data/fx/hdf5.py index 325f28d53b..abbb2ac359 100644 --- a/src/zipline/data/fx/hdf5.py +++ b/src/zipline/data/fx/hdf5.py @@ -94,7 +94,7 @@ """ from interface import implements import h5py -from logbook import Logger +import logging import numpy as np import pandas as pd @@ -113,7 +113,7 @@ DTS = "dts" RATES = "rates" -log = Logger(__name__) +log = logging.getLogger(__name__) class HDF5FXRateReader(implements(FXRateReader)): @@ -195,12 +195,12 @@ def get_rates(self, rate, quote, bases, dts): try: dataset = self._group[DATA][rate][quote][RATES] - except KeyError: + except KeyError as exc: raise ValueError( "FX rates not available for rate={}, quote_currency={}.".format( rate, quote ) - ) + ) from exc # OPTIMIZATION: Column indices correspond to dates, which must be in # sorted order. Rather than reading the entire dataset from h5, we can diff --git a/src/zipline/data/hdf5_daily_bars.py b/src/zipline/data/hdf5_daily_bars.py index 085fdd6854..0d566d51c1 100644 --- a/src/zipline/data/hdf5_daily_bars.py +++ b/src/zipline/data/hdf5_daily_bars.py @@ -100,7 +100,7 @@ from functools import partial import h5py -import logbook +import logging import numpy as np import pandas as pd from functools import reduce @@ -116,7 +116,7 @@ from zipline.utils.numpy_utils import bytes_array_to_native_str_object_array from zipline.utils.pandas_utils import check_indexes_all_same -log = logbook.Logger("HDF5DailyBars") +log = logging.getLogger("HDF5DailyBars") VERSION = 0 diff --git a/src/zipline/data/minute_bars.py b/src/zipline/data/minute_bars.py index 4245faa7a4..5c0ee144a6 100644 --- a/src/zipline/data/minute_bars.py +++ b/src/zipline/data/minute_bars.py @@ -22,7 +22,7 @@ import bcolz from bcolz import ctable from intervaltree import IntervalTree -import logbook +import logging import numpy as np import pandas as pd from pandas import HDFStore @@ -42,7 +42,7 @@ from zipline.utils.compat import mappingproxy from zipline.utils.memoize import lazyval -logger = logbook.Logger("MinuteBars") +logger = logging.getLogger("MinuteBars") US_EQUITIES_MINUTES_PER_DAY = 390 FUTURES_MINUTES_PER_DAY = 1440 @@ -149,7 +149,7 @@ def convert_cols(cols, scale_factor, sid, invalid_data_behavior): raise if invalid_data_behavior == "warn": - logger.warn( + logger.warning( "Values for sid={}, col={} contain some too large for " "uint32 (max={}), filtering them out", sid, @@ -1053,8 +1053,8 @@ def _open_minute_file(self, field, sid): rootdir=self._get_carray_path(sid, field), mode="r", ) - except IOError: - raise NoDataForSid("No minute data for sid {}.".format(sid)) + except IOError as exc: + raise NoDataForSid("No minute data for sid {}.".format(sid)) from exc return carray @@ -1104,8 +1104,8 @@ def get_value(self, sid, dt, field): else: try: minute_pos = self._find_position_of_minute(dt) - except ValueError: - raise NoDataOnDate() + except ValueError as exc: + raise NoDataOnDate() from exc self._last_get_value_dt_value = dt.value self._last_get_value_dt_position = minute_pos diff --git a/src/zipline/examples/dual_ema_talib.py b/src/zipline/examples/dual_ema_talib.py index c70026792a..334a583882 100644 --- a/src/zipline/examples/dual_ema_talib.py +++ b/src/zipline/examples/dual_ema_talib.py @@ -30,14 +30,14 @@ # Import exponential moving average from talib wrapper try: from talib import EMA -except ImportError: +except ImportError as exc: msg = ( "Unable to import module TA-lib. Use `pip install TA-lib` to " "install. Note: if installation fails, you might need to install " "the underlying TA-lib library (more information can be found in " "the zipline installation documentation)." ) - raise ImportError(msg) + raise ImportError(msg) from exc def initialize(context): @@ -86,10 +86,15 @@ def handle_data(context, data): # this algorithm on quantopian.com def analyze(context=None, results=None): import matplotlib.pyplot as plt - import logbook + import logging - logbook.StderrHandler().push_application() - log = logbook.Logger("Algorithm") + logging.basicConfig( + format="[%(asctime)s-%(levelname)s][%(name)s]\n %(message)s", + level=logging.INFO, + datefmt="%Y-%m-%dT%H:%M:%S%z", + ) + + log = logging.getLogger("Algorithm") fig = plt.figure() ax1 = fig.add_subplot(211) diff --git a/src/zipline/examples/dual_moving_average.py b/src/zipline/examples/dual_moving_average.py index 0d2d6cbdd8..e4c4aea45d 100644 --- a/src/zipline/examples/dual_moving_average.py +++ b/src/zipline/examples/dual_moving_average.py @@ -70,10 +70,15 @@ def handle_data(context, data): # this algorithm on quantopian.com def analyze(context=None, results=None): import matplotlib.pyplot as plt - import logbook - logbook.StderrHandler().push_application() - log = logbook.Logger("Algorithm") + import logging + + logging.basicConfig( + format="[%(asctime)s-%(levelname)s][%(name)s]\n %(message)s", + level=logging.INFO, + datefmt="%Y-%m-%dT%H:%M:%S%z", + ) + log = logging.getLogger("Algorithm") fig = plt.figure() ax1 = fig.add_subplot(211) diff --git a/src/zipline/examples/olmar.py b/src/zipline/examples/olmar.py index b16cde5806..218cd5b45d 100644 --- a/src/zipline/examples/olmar.py +++ b/src/zipline/examples/olmar.py @@ -1,17 +1,17 @@ +import logging import sys -import logbook + import numpy as np from zipline.finance import commission, slippage -zipline_logging = logbook.NestedSetup( - [ - logbook.NullHandler(), - logbook.StreamHandler(sys.stdout, level=logbook.INFO), - logbook.StreamHandler(sys.stderr, level=logbook.ERROR), - ] -) -zipline_logging.push_application() +# zipline_logging = logging.getLogger("zipline_logging") +# zipline_logging.addHandler(logging.NullHandler()) +# zipline_logging.addHandler( +# logging.StreamHandler(sys.stdout).setLevel(logging.INFO), +# ) +# zipline_logging.addHandler(logging.StreamHandler(sys.stderr).setLevel(logging.ERROR)) + STOCKS = ["AMD", "CERN", "COST", "DELL", "GPS", "INTC", "MMM"] diff --git a/src/zipline/extensions.py b/src/zipline/extensions.py index 238e1ccda5..1cf2d4e162 100644 --- a/src/zipline/extensions.py +++ b/src/zipline/extensions.py @@ -118,11 +118,11 @@ def load(self, name): """ try: return self._factories[name]() - except KeyError: + except KeyError as exc: raise ValueError( "no %s factory registered under name %r, options are: %r" % (self.interface.__name__, name, sorted(self._factories)), - ) + ) from exc def is_registered(self, name): """Check whether we have a factory registered under ``name``.""" @@ -143,11 +143,11 @@ def register(self, name, factory): def unregister(self, name): try: del self._factories[name] - except KeyError: + except KeyError as exc: raise ValueError( "%s factory %r was not already registered" % (self.interface.__name__, name) - ) + ) from exc def clear(self): self._factories.clear() @@ -173,8 +173,8 @@ def get_registry(interface): """ try: return custom_types[interface] - except KeyError: - raise ValueError("class specified is not an extendable type") + except KeyError as exc: + raise ValueError("class specified is not an extendable type") from exc def load(interface, name): diff --git a/src/zipline/finance/blotter/simulation_blotter.py b/src/zipline/finance/blotter/simulation_blotter.py index 7125f6d314..8744f6f349 100644 --- a/src/zipline/finance/blotter/simulation_blotter.py +++ b/src/zipline/finance/blotter/simulation_blotter.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from logbook import Logger +import logging from collections import defaultdict from copy import copy @@ -33,8 +33,8 @@ ) from zipline.utils.input_validation import expect_types -log = Logger("Blotter") -warning_logger = Logger("AlgoWarning") +log = logging.getLogger("Blotter") +warning_logger = logging.getLogger("AlgoWarning") @register(Blotter, "default") @@ -194,7 +194,7 @@ def cancel_all_orders_for_asset(self, asset, warn=False, relay_status=True): # Message appropriately depending on whether there's # been a partial fill or not. if order.filled > 0: - warning_logger.warn( + warning_logger.warning( "Your order for {order_amt} shares of " "{order_sym} has been partially filled. " "{order_filled} shares were successfully " @@ -208,7 +208,7 @@ def cancel_all_orders_for_asset(self, asset, warn=False, relay_status=True): ) ) elif order.filled < 0: - warning_logger.warn( + warning_logger.warning( "Your order for {order_amt} shares of " "{order_sym} has been partially filled. " "{order_filled} shares were successfully " @@ -222,7 +222,7 @@ def cancel_all_orders_for_asset(self, asset, warn=False, relay_status=True): ) ) else: - warning_logger.warn( + warning_logger.warning( "Your order for {order_amt} shares of " "{order_sym} failed to fill by the end of day " "and was canceled.".format( diff --git a/src/zipline/finance/controls.py b/src/zipline/finance/controls.py index 9cc574d87b..03971efbba 100644 --- a/src/zipline/finance/controls.py +++ b/src/zipline/finance/controls.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import abc -import logbook +import logging from datetime import datetime import pandas as pd @@ -27,7 +27,7 @@ expect_types, ) -log = logbook.Logger("TradingControl") +log = logging.getLogger("TradingControl") class TradingControl(metaclass=abc.ABCMeta): @@ -83,12 +83,9 @@ def handle_violation(self, asset, amount, datetime, metadata=None): ) elif self.on_error == "log": log.error( - "Order for {amount} shares of {asset} at {dt} " - "violates trading constraint {constraint}", - amount=amount, - asset=asset, - dt=datetime, - constraint=constraint, + "Order for %(amount)s shares of %(asset)s at %(dt)s " + "violates trading constraint %(constraint)s", + dict(amount=amount, asset=asset, dt=datetime, constraint=constraint), ) def __repr__(self): diff --git a/src/zipline/finance/execution.py b/src/zipline/finance/execution.py index 31c50f5c87..8fe8a876cc 100644 --- a/src/zipline/finance/execution.py +++ b/src/zipline/finance/execution.py @@ -217,11 +217,11 @@ def check_stoplimit_prices(price, label): "of {}.".format(label, price) ) # This catches arbitrary objects - except TypeError: + except TypeError as exc: raise BadOrderParameters( msg="Attempted to place an order with a {} price " "of {}.".format(label, type(price)) - ) + ) from exc if price < 0: raise BadOrderParameters( diff --git a/src/zipline/finance/ledger.py b/src/zipline/finance/ledger.py index a51370f2fb..6b081f68b8 100644 --- a/src/zipline/finance/ledger.py +++ b/src/zipline/finance/ledger.py @@ -16,7 +16,7 @@ from functools import partial from math import isnan -import logbook +import logging import numpy as np import pandas as pd @@ -31,7 +31,7 @@ update_position_last_sale_prices, ) -log = logbook.Logger("Performance") +log = logging.getLogger("Performance") class PositionTracker: diff --git a/src/zipline/finance/metrics/core.py b/src/zipline/finance/metrics/core.py index 1f09b80022..e8b58a6843 100644 --- a/src/zipline/finance/metrics/core.py +++ b/src/zipline/finance/metrics/core.py @@ -70,10 +70,10 @@ def unregister(name): """ try: del _metrics_sets[name] - except KeyError: + except KeyError as exc: raise ValueError( "metrics set %r was not already registered" % name, - ) + ) from exc def load(name): """Return an instance of the metrics set registered with the given name. @@ -90,14 +90,14 @@ def load(name): """ try: function = _metrics_sets[name] - except KeyError: + except KeyError as exc: raise ValueError( "no metrics set registered as %r, options are: %r" % ( name, sorted(_metrics_sets), ), - ) + ) from exc return function() diff --git a/src/zipline/finance/metrics/tracker.py b/src/zipline/finance/metrics/tracker.py index 7ef1313b2d..38d07bd287 100644 --- a/src/zipline/finance/metrics/tracker.py +++ b/src/zipline/finance/metrics/tracker.py @@ -12,13 +12,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import logbook +import logging from ..ledger import Ledger from zipline.utils.exploding_object import NamedExplodingObject -log = logbook.Logger(__name__) +log = logging.getLogger(__name__) class MetricsTracker: @@ -334,10 +334,12 @@ def handle_simulation_end(self, data_portal): and send it out on the results socket. """ log.info( - "Simulated {} trading days\n" "first open: {}\n" "last close: {}", - self._session_count, - self._trading_calendar.session_open(self._first_session), - self._trading_calendar.session_close(self._last_session), + "Simulated %(days)s trading days\n first open: %(first)s\n last close: %(last)s", + dict( + days=self._session_count, + first=self._trading_calendar.session_open(self._first_session), + last=self._trading_calendar.session_close(self._last_session), + ), ) packet = {} diff --git a/src/zipline/finance/position.py b/src/zipline/finance/position.py index 82b4bf288e..d309b484b3 100644 --- a/src/zipline/finance/position.py +++ b/src/zipline/finance/position.py @@ -33,12 +33,12 @@ from math import copysign import numpy as np -import logbook +import logging from zipline.assets import Future import zipline.protocol as zp -log = logbook.Logger("Performance") +log = logging.getLogger("Performance") class Position: diff --git a/src/zipline/finance/trading.py b/src/zipline/finance/trading.py index 0572761049..321d3302af 100644 --- a/src/zipline/finance/trading.py +++ b/src/zipline/finance/trading.py @@ -13,13 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logbook +import logging import pandas as pd from zipline.utils.memoize import remember_last from zipline.utils.pandas_utils import normalize_date -log = logbook.Logger("Trading") +log = logging.getLogger("Trading") DEFAULT_CAPITAL_BASE = 1e5 diff --git a/src/zipline/gens/tradesimulation.py b/src/zipline/gens/tradesimulation.py index 9cf041e682..cf95eabe60 100644 --- a/src/zipline/gens/tradesimulation.py +++ b/src/zipline/gens/tradesimulation.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from copy import copy -from logbook import Logger, Processor +import logging from zipline.finance.order import ORDER_STATUS from zipline.protocol import BarData from zipline.utils.api_support import ZiplineAPI @@ -27,7 +27,7 @@ BEFORE_TRADING_START_BAR, ) -log = Logger("Trade Simulation") +log = logging.getLogger("Trade Simulation") class AlgorithmSimulator: @@ -78,11 +78,9 @@ def __init__( # Processor function for injecting the algo_dt into # user prints/logs. - def inject_algo_dt(record): - if "algo_dt" not in record.extra: - record.extra["algo_dt"] = self.simulation_dt - self.processor = Processor(inject_algo_dt) + # TODO CHECK: Disabled the old logbook mechanism, + # didn't replace with an equivalent `logging` approach. def get_simulation_dt(self): return self.simulation_dt @@ -192,7 +190,6 @@ def on_exit(): with ExitStack() as stack: stack.callback(on_exit) - stack.enter_context(self.processor) stack.enter_context(ZiplineAPI(self.algo)) if algo.data_frequency == "minute": diff --git a/src/zipline/lib/adjusted_array.py b/src/zipline/lib/adjusted_array.py index 3387f22790..9940f1ace5 100644 --- a/src/zipline/lib/adjusted_array.py +++ b/src/zipline/lib/adjusted_array.py @@ -122,13 +122,13 @@ def _normalize_array(data, missing_value): try: outarray = data.astype("datetime64[ns]", copy=False).view("int64") return outarray, {"dtype": datetime64ns_dtype} - except OverflowError: + except OverflowError as exc: raise ValueError( "AdjustedArray received a datetime array " "not representable as datetime64[ns].\n" "Min Date: %s\n" "Max Date: %s\n" % (data.min(), data.max()) - ) + ) from exc else: raise TypeError( "Don't know how to construct AdjustedArray " @@ -238,11 +238,11 @@ def update_adjustments(self, adjustments, method): """ try: merge_func = _merge_methods[method] - except KeyError: + except KeyError as exc: raise ValueError( "Invalid merge method %s\n" "Valid methods are: %s" % (method, ", ".join(_merge_methods)) - ) + ) from exc self.adjustments = merge_with( merge_func, diff --git a/src/zipline/lib/labelarray.py b/src/zipline/lib/labelarray.py index c7f9019ac6..7746935e7a 100644 --- a/src/zipline/lib/labelarray.py +++ b/src/zipline/lib/labelarray.py @@ -413,8 +413,8 @@ def set_scalar(self, indexer, value): """ try: value_code = self.reverse_categories[value] - except KeyError: - raise ValueError("%r is not in LabelArray categories." % value) + except KeyError as exc: + raise ValueError("%r is not in LabelArray categories." % value) from exc self.as_int_array()[indexer] = value_code diff --git a/src/zipline/pipeline/classifiers/classifier.py b/src/zipline/pipeline/classifiers/classifier.py index 82967c8e4f..7266cbbb23 100644 --- a/src/zipline/pipeline/classifiers/classifier.py +++ b/src/zipline/pipeline/classifiers/classifier.py @@ -264,12 +264,12 @@ def element_of(self, choices): """ try: choices = frozenset(choices) - except Exception as e: + except Exception as exc: raise TypeError( "Expected `choices` to be an iterable of hashable values," " but got {} instead.\n" - "This caused the following error: {!r}.".format(choices, e) - ) + "This caused the following error: {!r}.".format(choices, exc) + ) from exc if self.missing_value in choices: raise ValueError( @@ -319,7 +319,7 @@ def only_contains(type_, values): choices=choices, ) ) - assert False, "Unknown dtype in Classifier.element_of %s." % self.dtype + raise AssertionError(f"Unknown dtype in Classifier.element_of {self.dtype}.") def postprocess(self, data): if self.dtype == int64_dtype: @@ -525,19 +525,19 @@ class CustomClassifier( def _validate(self): try: super(CustomClassifier, self)._validate() - except UnsupportedDataType: + except UnsupportedDataType as exc: if self.dtype in FACTOR_DTYPES: raise UnsupportedDataType( typename=type(self).__name__, dtype=self.dtype, hint="Did you mean to create a CustomFactor?", - ) + ) from exc elif self.dtype in FILTER_DTYPES: raise UnsupportedDataType( typename=type(self).__name__, dtype=self.dtype, hint="Did you mean to create a CustomFilter?", - ) + ) from exc raise def _allocate_output(self, windows, shape): diff --git a/src/zipline/pipeline/data/dataset.py b/src/zipline/pipeline/data/dataset.py index 70065c5a85..e114e05e2a 100644 --- a/src/zipline/pipeline/data/dataset.py +++ b/src/zipline/pipeline/data/dataset.py @@ -94,7 +94,7 @@ def __init__(self, dtype, missing_value, name, doc, metadata, currency_aware): dtype=dtype, missing_value=missing_value, ) - except NoDefaultMissingValue: + except NoDefaultMissingValue as exc: # Re-raise with a more specific message. raise NoDefaultMissingValue( "Failed to create Column with name {name!r} and" @@ -102,7 +102,7 @@ def __init__(self, dtype, missing_value, name, doc, metadata, currency_aware): "Columns with dtype {dtype} require a missing_value.\n" "Please pass missing_value to Column() or use a different" " dtype.".format(dtype=dtype, name=name) - ) + ) from exc self.name = name self.doc = doc self.metadata = metadata @@ -394,7 +394,7 @@ class DataSetMeta(type): families of specialized dataset. """ - def __new__(mcls, name, bases, dict_): + def __new__(metacls, name, bases, dict_): if len(bases) != 1: # Disallowing multiple inheritance makes it easier for us to # determine whether a given dataset is the root for its family of @@ -404,7 +404,7 @@ def __new__(mcls, name, bases, dict_): # This marker is set in the class dictionary by `specialize` below. is_specialization = dict_.pop(IsSpecialization, False) - newtype = super(DataSetMeta, mcls).__new__(mcls, name, bases, dict_) + newtype = super(DataSetMeta, metacls).__new__(metacls, name, bases, dict_) if not isinstance(newtype.domain, Domain): raise TypeError( @@ -441,7 +441,7 @@ def __new__(mcls, name, bases, dict_): return newtype @expect_types(domain=Domain) - def specialize(self, domain): + def specialize(cls, domain): """ Specialize a generic DataSet to a concrete domain. @@ -457,97 +457,97 @@ def specialize(self, domain): same columns as ``self``, but specialized to ``domain``. """ # We're already the specialization to this domain, so just return self. - if domain == self.domain: - return self + if domain == cls.domain: + return cls try: - return self._domain_specializations[domain] - except KeyError: - if not self._can_create_new_specialization(domain): + return cls._domain_specializations[domain] + except KeyError as exc: + if not cls._can_create_new_specialization(domain): # This either means we're already a specialization and trying # to create a new specialization, or we're the generic version # of a root-specialized dataset, which we don't want to create # new specializations of. raise ValueError( "Can't specialize {dataset} from {current} to new domain {new}.".format( - dataset=self.__name__, - current=self.domain, + dataset=cls.__name__, + current=cls.domain, new=domain, ) - ) - new_type = self._create_specialization(domain) - self._domain_specializations[domain] = new_type + ) from exc + new_type = cls._create_specialization(domain) + cls._domain_specializations[domain] = new_type return new_type - def unspecialize(self): + def unspecialize(cls): """ Unspecialize a dataset to its generic form. This is equivalent to ``dataset.specialize(GENERIC)``. """ - return self.specialize(GENERIC) + return cls.specialize(GENERIC) - def _can_create_new_specialization(self, domain): + def _can_create_new_specialization(cls, domain): # Always allow specializing to a generic domain. if domain is GENERIC: return True - elif "_domain_specializations" in vars(self): + elif "_domain_specializations" in vars(cls): # This branch is True if we're the root of a family. # Allow specialization if we're generic. - return self.domain is GENERIC + return cls.domain is GENERIC else: # If we're not the root of a family, we can't create any new # specializations. return False - def _create_specialization(self, domain): + def _create_specialization(cls, domain): # These are all assertions because we should have handled these cases # already in specialize(). assert isinstance(domain, Domain) assert ( - domain not in self._domain_specializations + domain not in cls._domain_specializations ), "Domain specializations should be memoized!" if domain is not GENERIC: assert ( - self.domain is GENERIC + cls.domain is GENERIC ), "Can't specialize dataset with domain {} to domain {}.".format( - self.domain, + cls.domain, domain, ) # Create a new subclass of ``self`` with the given domain. # Mark that it's a specialization so that we know not to create a new # family for it. - name = self.__name__ - bases = (self,) + name = cls.__name__ + bases = (cls,) dict_ = {"domain": domain, IsSpecialization: True} out = type(name, bases, dict_) - out.__module__ = self.__module__ + out.__module__ = cls.__module__ return out @property - def columns(self): - return frozenset(getattr(self, colname) for colname in self._column_names) + def columns(cls): + return frozenset(getattr(cls, colname) for colname in cls._column_names) @property - def qualname(self): - if self.domain is GENERIC: + def qualname(cls): + if cls.domain is GENERIC: specialization_key = "" else: - specialization_key = "<" + self.domain.country_code + ">" + specialization_key = "<" + cls.domain.country_code + ">" - return self.__name__ + specialization_key + return cls.__name__ + specialization_key # NOTE: We used to use `functools.total_ordering` to account for all of the # other rich comparison methods, but it has issues in python 3 and # this method is only used for test purposes, so for now we will just # keep this in isolation. If we ever need any of the other comparison # methods we will have to implement them individually. - def __lt__(self, other): - return id(self) < id(other) + def __lt__(cls, other): + return id(cls) < id(other) - def __repr__(self): - return "" % (self.__name__, self.domain) + def __repr__(cls): + return "" % (cls.__name__, cls.domain) class DataSet(object, metaclass=DataSetMeta): @@ -655,7 +655,7 @@ def get_column(cls, name): maybe_column = clsdict[name] if not isinstance(maybe_column, _BoundColumnDescr): raise KeyError(name) - except KeyError: + except KeyError as exc: raise AttributeError( "{dset} has no column {colname!r}:\n\n" "Possible choices are:\n" @@ -667,7 +667,7 @@ def get_column(cls, name): max_count=10, ), ) - ) + ) from exc # Resolve column descriptor into a BoundColumn. return maybe_column.__get__(None, cls) diff --git a/src/zipline/pipeline/domain.py b/src/zipline/pipeline/domain.py index 945ef0c3d4..825b5fb31f 100644 --- a/src/zipline/pipeline/domain.py +++ b/src/zipline/pipeline/domain.py @@ -94,13 +94,13 @@ def roll_forward(self, dt): trading_days = self.all_sessions() try: return trading_days[trading_days.searchsorted(dt)] - except IndexError: + except IndexError as exc: raise ValueError( "Date {} was past the last session for domain {}. " "The last session for this domain is {}.".format( dt.date(), self, trading_days[-1].date() ) - ) + ) from exc Domain = implements(IDomain) diff --git a/src/zipline/pipeline/factors/factor.py b/src/zipline/pipeline/factors/factor.py index 52f2840552..776965efff 100644 --- a/src/zipline/pipeline/factors/factor.py +++ b/src/zipline/pipeline/factors/factor.py @@ -1721,19 +1721,19 @@ def compute(self, today, assets, out, close): def _validate(self): try: super(CustomFactor, self)._validate() - except UnsupportedDataType: + except UnsupportedDataType as exc: if self.dtype in CLASSIFIER_DTYPES: raise UnsupportedDataType( typename=type(self).__name__, dtype=self.dtype, hint="Did you mean to create a CustomClassifier?", - ) + ) from exc elif self.dtype in FILTER_DTYPES: raise UnsupportedDataType( typename=type(self).__name__, dtype=self.dtype, hint="Did you mean to create a CustomFilter?", - ) + ) from exc raise def __getattribute__(self, name): @@ -1745,7 +1745,7 @@ def __getattribute__(self, name): else: try: return super(CustomFactor, self).__getattribute__(name) - except AttributeError: + except AttributeError as exc: raise AttributeError( "Instance of {factor} has no output named {attr!r}. " "Possible choices are: {choices}.".format( @@ -1753,7 +1753,7 @@ def __getattribute__(self, name): attr=name, choices=self.outputs, ) - ) + ) from exc def __iter__(self): if self.outputs is NotSpecified: diff --git a/src/zipline/pipeline/filters/filter.py b/src/zipline/pipeline/filters/filter.py index f9313824fb..d223be5be9 100644 --- a/src/zipline/pipeline/filters/filter.py +++ b/src/zipline/pipeline/filters/filter.py @@ -541,19 +541,19 @@ def compute(self, today, assets, out, *inputs): def _validate(self): try: super(CustomFilter, self)._validate() - except UnsupportedDataType: + except UnsupportedDataType as exc: if self.dtype in CLASSIFIER_DTYPES: raise UnsupportedDataType( typename=type(self).__name__, dtype=self.dtype, hint="Did you mean to create a CustomClassifier?", - ) + ) from exc elif self.dtype in FACTOR_DTYPES: raise UnsupportedDataType( typename=type(self).__name__, dtype=self.dtype, hint="Did you mean to create a CustomFactor?", - ) + ) from exc raise diff --git a/src/zipline/pipeline/loaders/earnings_estimates.py b/src/zipline/pipeline/loaders/earnings_estimates.py index 6c52a26e0c..5c660bfe41 100644 --- a/src/zipline/pipeline/loaders/earnings_estimates.py +++ b/src/zipline/pipeline/loaders/earnings_estimates.py @@ -585,14 +585,14 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): groups = groupby( lambda col: col_to_datasets[col].num_announcements, col_to_datasets ) - except AttributeError: + except AttributeError as exc: raise AttributeError( "Datasets loaded via the " "EarningsEstimatesLoader must define a " "`num_announcements` attribute that defines " "how many quarters out the loader should load" " the data relative to `dates`." - ) + ) from exc if any(num_qtr < 0 for num_qtr in groups): raise ValueError( INVALID_NUM_QTRS_MESSAGE diff --git a/src/zipline/pipeline/loaders/frame.py b/src/zipline/pipeline/loaders/frame.py index 8d25ed324c..90649f6e32 100644 --- a/src/zipline/pipeline/loaders/frame.py +++ b/src/zipline/pipeline/loaders/frame.py @@ -8,18 +8,14 @@ ix_, zeros, ) -from pandas import ( - DataFrame, - DatetimeIndex, - Index, - Int64Index, -) +import pandas as pd + from zipline.lib.adjusted_array import AdjustedArray from zipline.lib.adjustment import make_adjustment_from_labels from zipline.utils.numpy_utils import as_column from .base import PipelineLoader -ADJUSTMENT_COLUMNS = Index( +ADJUSTMENT_COLUMNS = pd.Index( [ "sid", "value", @@ -68,8 +64,8 @@ def __init__(self, column, baseline, adjustments=None): self.assets = baseline.columns if adjustments is None: - adjustments = DataFrame( - index=DatetimeIndex([]), + adjustments = pd.DataFrame( + index=pd.DatetimeIndex([]), columns=ADJUSTMENT_COLUMNS, ) else: @@ -78,9 +74,9 @@ def __init__(self, column, baseline, adjustments=None): adjustments.sort_values(["apply_date", "sid"], inplace=True) self.adjustments = adjustments - self.adjustment_apply_dates = DatetimeIndex(adjustments.apply_date) - self.adjustment_end_dates = DatetimeIndex(adjustments.end_date) - self.adjustment_sids = Int64Index(adjustments.sid) + self.adjustment_apply_dates = pd.DatetimeIndex(adjustments.apply_date) + self.adjustment_end_dates = pd.DatetimeIndex(adjustments.end_date) + self.adjustment_sids = pd.Index(adjustments.sid, dtype="int64") def format_adjustments(self, dates, assets): """ diff --git a/src/zipline/pipeline/loaders/synthetic.py b/src/zipline/pipeline/loaders/synthetic.py index 068ef9944d..02a775cc9e 100644 --- a/src/zipline/pipeline/loaders/synthetic.py +++ b/src/zipline/pipeline/loaders/synthetic.py @@ -89,8 +89,8 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): loader = self._loaders.get(col) if loader is None: loader = self._loaders[col.unspecialize()] - except KeyError: - raise ValueError("Couldn't find loader for %s" % col) + except KeyError as exc: + raise ValueError("Couldn't find loader for %s" % col) from exc out.update(loader.load_adjusted_array(domain, [col], dates, sids, mask)) return out diff --git a/src/zipline/pipeline/loaders/utils.py b/src/zipline/pipeline/loaders/utils.py index b9c5b1baf2..1e44b6ea21 100644 --- a/src/zipline/pipeline/loaders/utils.py +++ b/src/zipline/pipeline/loaders/utils.py @@ -308,7 +308,7 @@ def shift_dates(dates, start_date, end_date, shift): """ try: start = dates.get_loc(start_date) - except KeyError: + except KeyError as exc: if start_date < dates[0]: raise NoFurtherDataError( msg=( @@ -318,9 +318,9 @@ def shift_dates(dates, start_date, end_date, shift): query_start=str(start_date), calendar_start=str(dates[0]), ) - ) + ) from exc else: - raise ValueError("Query start %s not in calendar" % start_date) + raise ValueError(f"Query start {start_date} not in calendar") from exc # Make sure that shifting doesn't push us out of the calendar. if start < shift: @@ -334,7 +334,7 @@ def shift_dates(dates, start_date, end_date, shift): try: end = dates.get_loc(end_date) - except KeyError: + except KeyError as exc: if end_date > dates[-1]: raise NoFurtherDataError( msg=( @@ -344,8 +344,8 @@ def shift_dates(dates, start_date, end_date, shift): query_end=end_date, calendar_end=dates[-1], ) - ) + ) from exc else: - raise ValueError("Query end %s not in calendar" % end_date) + raise ValueError("Query end %s not in calendar" % end_date) from exc return dates[start - shift : end - shift + 1] # +1 to be inclusive diff --git a/src/zipline/pipeline/mixins.py b/src/zipline/pipeline/mixins.py index f2041dc16a..4ff3da6785 100644 --- a/src/zipline/pipeline/mixins.py +++ b/src/zipline/pipeline/mixins.py @@ -458,7 +458,7 @@ def compute_extra_rows(self, all_dates, start_date, end_date, min_extra_rows): lookback_start=start_date, lookback_length=min_extra_rows, ) - except KeyError: + except KeyError as exc: before, after = nearest_unequal_elements(all_dates, start_date) raise ValueError( "Pipeline start_date {start_date} is not in calendar.\n" @@ -468,7 +468,7 @@ def compute_extra_rows(self, all_dates, start_date, end_date, min_extra_rows): before=before, after=after, ) - ) + ) from exc # Our possible target dates are all the dates on or before the current # starting position. diff --git a/src/zipline/pipeline/term.py b/src/zipline/pipeline/term.py index bb76de3503..0236327da1 100644 --- a/src/zipline/pipeline/term.py +++ b/src/zipline/pipeline/term.py @@ -204,13 +204,13 @@ def _pop_params(cls, kwargs): # Check here that the value is hashable so that we fail here # instead of trying to hash the param values tuple later. hash(value) - except KeyError: + except KeyError as exc: raise TypeError( "{typename} expected a keyword parameter {name!r}.".format( typename=cls.__name__, name=key ) - ) - except TypeError: + ) from exc + except TypeError as exc: # Value wasn't hashable. raise TypeError( "{typename} expected a hashable value for parameter " @@ -219,7 +219,7 @@ def _pop_params(cls, kwargs): name=key, value=value, ) - ) + ) from exc param_values.append((key, value)) return tuple(param_values) @@ -286,7 +286,7 @@ def _init(self, domain, dtype, missing_value, window_safe, ndim, params): self.window_safe = window_safe self.ndim = ndim - for name, value in params: + for name, _ in params: if hasattr(self, name): raise TypeError( "Parameter {name!r} conflicts with already-present" @@ -862,7 +862,7 @@ def fillna(self, fill_value): # dtype. try: fill_value = _coerce_to_dtype(fill_value, self.dtype) - except TypeError as e: + except TypeError as exc: raise TypeError( "Fill value {value!r} is not a valid choice " "for term {termname} with dtype {dtype}.\n\n" @@ -870,9 +870,9 @@ def fillna(self, fill_value): termname=type(self).__name__, value=fill_value, dtype=self.dtype, - error=e, + error=exc, ) - ) + ) from exc if_false = self._constant_type( const=fill_value, @@ -940,8 +940,8 @@ def validate_dtype(termname, dtype, missing_value): try: dtype = dtype_class(dtype) - except TypeError: - raise NotDType(dtype=dtype, termname=termname) + except TypeError as exc: + raise NotDType(dtype=dtype, termname=termname) from exc if not can_represent_dtype(dtype): raise UnsupportedDType(dtype=dtype, termname=termname) @@ -951,7 +951,7 @@ def validate_dtype(termname, dtype, missing_value): try: _coerce_to_dtype(missing_value, dtype) - except TypeError as e: + except TypeError as exc: raise TypeError( "Missing value {value!r} is not a valid choice " "for term {termname} with dtype {dtype}.\n\n" @@ -959,9 +959,9 @@ def validate_dtype(termname, dtype, missing_value): termname=termname, value=missing_value, dtype=dtype, - error=e, + error=exc, ) - ) + ) from exc return dtype, missing_value diff --git a/src/zipline/pipeline/visualize.py b/src/zipline/pipeline/visualize.py index 7f15352fd1..eba1e45e51 100644 --- a/src/zipline/pipeline/visualize.py +++ b/src/zipline/pipeline/visualize.py @@ -124,12 +124,12 @@ def _render(g, out, format_, include_asset_exists=False): cmd = ["dot", "-T", format_] try: proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) - except OSError as e: - if e.errno == errno.ENOENT: + except OSError as exc: + if exc.errno == errno.ENOENT: raise RuntimeError( "Couldn't find `dot` graph layout program. " "Make sure Graphviz is installed and `dot` is on your path." - ) + ) from exc else: raise @@ -149,8 +149,8 @@ def display_graph(g, format="svg", include_asset_exists=False): """ try: import IPython.display as display - except ImportError: - raise NoIPython("IPython is not installed. Can't display graph.") + except ImportError as exc: + raise NoIPython("IPython is not installed. Can't display graph.") from exc if format == "svg": display_cls = display.SVG diff --git a/src/zipline/sources/requests_csv.py b/src/zipline/sources/requests_csv.py index b0806ea371..fdc20e482b 100644 --- a/src/zipline/sources/requests_csv.py +++ b/src/zipline/sources/requests_csv.py @@ -4,7 +4,7 @@ from textwrap import dedent import warnings -from logbook import Logger +import logging import numpy import pandas as pd from pandas import read_csv @@ -15,7 +15,7 @@ from zipline.protocol import DATASOURCE_TYPE, Event from zipline.assets import Equity -logger = Logger("Requests Source Logger") +logger = logging.getLogger("Requests Source Logger") def roll_dts_to_midnight(dts, trading_day): @@ -208,7 +208,7 @@ def parse_date_str_series( # Explicitly ignoring this parameter. See note above. if format_str is not None: - logger.warn( + logger.warning( "The 'format_str' parameter to fetch_csv is deprecated. " "Ignoring and defaulting to pandas default date parsing." ) @@ -365,8 +365,8 @@ def load_df(self): df = df[df["sid"].notnull()] no_sid_count = length_before_drop - len(df) if no_sid_count: - logger.warn( - "Dropped {} rows from fetched csv.".format(no_sid_count), + logger.warning( + "Dropped %s rows from fetched csv.", no_sid_count, extra={"syslog": True}, ) @@ -532,8 +532,8 @@ def fetch_url(self, url): # pandas logic for decoding content try: response = requests.get(url, **self.requests_kwargs) - except requests.exceptions.ConnectionError: - raise Exception("Could not connect to %s" % url) + except requests.exceptions.ConnectionError as exc: + raise Exception("Could not connect to %s" % url) from exc if not response.ok: raise Exception("Problem reaching %s" % url) @@ -589,9 +589,9 @@ def fetch_data(self): frames_hash = hashlib.md5(str(fd.getvalue()).encode("utf-8")) self.fetch_hash = frames_hash.hexdigest() - except pd.parser.CParserError: + except pd.parser.CParserError as exc: # could not parse the data, raise exception - raise Exception("Error parsing remote CSV data.") + raise Exception("Error parsing remote CSV data.") from exc finally: fd.close() diff --git a/src/zipline/sources/test_source.py b/src/zipline/sources/test_source.py index a62c4140e2..d4dd25162a 100644 --- a/src/zipline/sources/test_source.py +++ b/src/zipline/sources/test_source.py @@ -74,7 +74,7 @@ def advance_current(cur): # during trading hours. while cur < end: if repeats: - for j in range(repeats): + for _ in range(repeats): yield cur else: yield cur diff --git a/src/zipline/testing/__init__.py b/src/zipline/testing/__init__.py index 84dece29f2..05db1441ea 100644 --- a/src/zipline/testing/__init__.py +++ b/src/zipline/testing/__init__.py @@ -26,7 +26,6 @@ empty_assets_db, make_alternating_boolean_array, make_cascading_boolean_array, - make_test_handler, make_trade_data_for_asset_info, parameter_space, patch_os_environment, diff --git a/src/zipline/testing/core.py b/src/zipline/testing/core.py index 117407ae8d..ab465214e6 100644 --- a/src/zipline/testing/core.py +++ b/src/zipline/testing/core.py @@ -15,7 +15,6 @@ import tempfile from traceback import format_exception -from logbook import TestHandler from mock import patch from numpy.testing import assert_allclose, assert_array_equal @@ -1307,29 +1306,6 @@ def permute_rows(seed, array): return np.apply_along_axis(rand.permutation, 1, array) -# @nottest -def make_test_handler(testcase, *args, **kwargs): - """ - Returns a TestHandler which will be used by the given testcase. This - handler can be used to test log messages. - - Parameters - ---------- - testcase: unittest.TestCase - The test class in which the log handler will be used. - *args, **kwargs - Forwarded to the new TestHandler object. - - Returns - ------- - handler: logbook.TestHandler - The handler to use for the test case. - """ - handler = TestHandler(*args, **kwargs) - testcase.addCleanup(handler.close) - return handler - - def write_compressed(path, content): """ Write a compressed (gzipped) file to `path`. diff --git a/src/zipline/testing/fixtures.py b/src/zipline/testing/fixtures.py index 1de8775c43..819c44064c 100644 --- a/src/zipline/testing/fixtures.py +++ b/src/zipline/testing/fixtures.py @@ -4,7 +4,6 @@ from unittest import TestCase import warnings -from logbook import NullHandler, Logger import numpy as np import pandas as pd from pandas.errors import PerformanceWarning @@ -88,13 +87,13 @@ class DebugMROMeta(FinalMeta): """Metaclass that helps debug MRO resolution errors.""" - def __new__(mcls, name, bases, clsdict): + def __new__(cls, name, bases, clsdict): try: - return super(DebugMROMeta, mcls).__new__(mcls, name, bases, clsdict) - except TypeError as e: - if "(MRO)" in str(e): + return super(DebugMROMeta, cls).__new__(cls, name, bases, clsdict) + except TypeError as exc: + if "(MRO)" in str(exc): msg = debug_mro_failure(name, bases) - raise TypeError(msg) + raise TypeError(msg) from exc else: raise @@ -307,32 +306,6 @@ class WithDefaultDateBounds(object, metaclass=DebugMROMeta): END_DATE = pd.Timestamp("2006-12-29", tz="utc") -class WithLogger: - """ - ZiplineTestCase mixin providing cls.log_handler as an instance-level - fixture. - - After init_instance_fixtures has been called `self.log_handler` will be a - new ``logbook.NullHandler``. - - Methods - ------- - make_log_handler() -> logbook.LogHandler - A class method which constructs the new log handler object. By default - this will construct a ``NullHandler``. - """ - - make_log_handler = NullHandler - - @classmethod - def init_class_fixtures(cls): - super(WithLogger, cls).init_class_fixtures() - cls.log = Logger() - cls.log_handler = cls.enter_class_context( - cls.make_log_handler().applicationbound(), - ) - - class WithAssetFinder(WithDefaultDateBounds): """ ZiplineTestCase mixin providing cls.asset_finder as a class-level fixture. @@ -1982,7 +1955,7 @@ def create_bardata(self, simulation_dt_func, restrictions=None): ) -class WithMakeAlgo(WithBenchmarkReturns, WithSimParams, WithLogger, WithDataPortal): +class WithMakeAlgo(WithBenchmarkReturns, WithSimParams, WithDataPortal): """ ZiplineTestCase mixin that provides a ``make_algo`` method. """ diff --git a/src/zipline/testing/predicates.py b/src/zipline/testing/predicates.py index 1e93e4e663..79e606ecf5 100644 --- a/src/zipline/testing/predicates.py +++ b/src/zipline/testing/predicates.py @@ -437,8 +437,8 @@ def assert_array_equal( verbose=array_verbose, err_msg=msg, ) - except AssertionError as e: - raise AssertionError("\n".join((str(e), _fmt_path(path)))) + except AssertionError as exc: + raise AssertionError("\n".join((str(exc), _fmt_path(path)))) from exc @assert_equal.register(LabelArray, LabelArray) @@ -477,10 +477,10 @@ def _register_assert_equal_wrapper(type_, assert_eq): def assert_ndframe_equal(result, expected, path=(), msg="", **kwargs): try: assert_eq(result, expected, **filter_kwargs(assert_eq, kwargs)) - except AssertionError as e: + except AssertionError as exc: raise AssertionError( - _fmt_msg(msg) + "\n".join((str(e), _fmt_path(path))), - ) + _fmt_msg(msg) + "\n".join((str(exc), _fmt_path(path))), + ) from exc return assert_ndframe_equal @@ -645,5 +645,5 @@ def index_of_first_difference(left, right): difflocs = (i for (i, (lc, rc)) in enumerate(zip_longest(left, right)) if lc != rc) try: return next(difflocs) - except StopIteration: - raise ValueError("Left was equal to right!") + except StopIteration as exc: + raise ValueError("Left was equal to right!") from exc diff --git a/src/zipline/utils/cache.py b/src/zipline/utils/cache.py index 933ecee6db..974bc50ae8 100644 --- a/src/zipline/utils/cache.py +++ b/src/zipline/utils/cache.py @@ -149,10 +149,10 @@ def get(self, key, dt): """ try: return self._cache[key].unwrap(dt) - except Expired: + except Expired as exc: self.cleanup(self._cache[key]._unsafe_get_value()) del self._cache[key] - raise KeyError(key) + raise KeyError(key) from exc def set(self, key, value, expiration_dt): """Adds a new key value pair to the cache. @@ -252,10 +252,10 @@ def __getitem__(self, key): try: with open(self._keypath(key), "rb") as f: return self.deserialize(f) - except IOError as e: - if e.errno != errno.ENOENT: + except IOError as exc: + if exc.errno != errno.ENOENT: raise - raise KeyError(key) + raise KeyError(key) from exc def __setitem__(self, key, value): with self.lock: @@ -265,10 +265,10 @@ def __delitem__(self, key): with self.lock: try: os.remove(self._keypath(key)) - except OSError as e: - if e.errno == errno.ENOENT: + except OSError as exc: + if exc.errno == errno.ENOENT: # raise a keyerror if this directory did not exist - raise KeyError(key) + raise KeyError(key) from exc # reraise the actual oserror otherwise raise diff --git a/src/zipline/utils/calendar_utils.py b/src/zipline/utils/calendar_utils.py index d1645768e3..9a845a6889 100644 --- a/src/zipline/utils/calendar_utils.py +++ b/src/zipline/utils/calendar_utils.py @@ -70,9 +70,11 @@ def _fabricate(self, name: str, **kwargs): aliases_to_names = global_calendar_dispatcher.aliases_to_names names_to_aliases = global_calendar_dispatcher.names_to_aliases -except ImportError: +except ImportError as exc: if PANDAS_VERSION > "1.2.5": - raise ImportError("For pandas >= 1.3 YOU MUST INSTALL exchange-calendars") + raise ImportError( + "For pandas >= 1.3 YOU MUST INSTALL exchange-calendars" + ) from exc else: from trading_calendars import ( register_calendar, diff --git a/src/zipline/utils/final.py b/src/zipline/utils/final.py index 073bd03ddb..2b31b5c26a 100644 --- a/src/zipline/utils/final.py +++ b/src/zipline/utils/final.py @@ -32,8 +32,8 @@ class FinalMeta(type): overriding some methods or attributes. """ - def __new__(mcls, name, bases, dict_): - for k, v in dict_.items(): + def __new__(metacls, name, bases, dict_): + for k, _ in dict_.items(): if is_final(k, bases): raise _type_error @@ -49,16 +49,16 @@ def __new__(mcls, name, bases, dict_): # users cannot just avoid the descriptor protocol. dict_["__setattr__"] = final(setattr_) - return super(FinalMeta, mcls).__new__(mcls, name, bases, dict_) + return super(FinalMeta, metacls).__new__(metacls, name, bases, dict_) - def __setattr__(self, name, value): + def __setattr__(metacls, name, value): """This stops the `final` attributes from being reassigned on the class object. """ - if is_final(name, self.__mro__): + if is_final(name, metacls.__mro__): raise _type_error - super(FinalMeta, self).__setattr__(name, value) + super(FinalMeta, metacls).__setattr__(name, value) class final(ABC): diff --git a/src/zipline/utils/input_validation.py b/src/zipline/utils/input_validation.py index da9889dd34..82f9be138c 100644 --- a/src/zipline/utils/input_validation.py +++ b/src/zipline/utils/input_validation.py @@ -141,7 +141,7 @@ def ensure_dtype(func, argname, arg): """ try: return dtype(arg) - except TypeError: + except TypeError as exc: raise TypeError( "{func}() couldn't convert argument " "{argname}={arg!r} to a numpy dtype.".format( @@ -149,7 +149,7 @@ def ensure_dtype(func, argname, arg): argname=argname, arg=arg, ), - ) + ) from exc def ensure_timezone(func, argname, arg): @@ -194,7 +194,7 @@ def ensure_timestamp(func, argname, arg): """ try: return pd.Timestamp(arg) - except ValueError as e: + except ValueError as exc: raise TypeError( "{func}() couldn't convert argument " "{argname}={arg!r} to a pandas Timestamp.\n" @@ -202,10 +202,10 @@ def ensure_timestamp(func, argname, arg): func=_qualified_name(func), argname=argname, arg=arg, - t=_qualified_name(type(e)), - e=e, + t=_qualified_name(type(exc)), + e=exc, ), - ) + ) from exc def expect_dtypes(__funcname=_qualified_name, **named): diff --git a/src/zipline/utils/numpy_utils.py b/src/zipline/utils/numpy_utils.py index 597001f25a..60af54f3f5 100644 --- a/src/zipline/utils/numpy_utils.py +++ b/src/zipline/utils/numpy_utils.py @@ -113,15 +113,17 @@ def NaT_for_dtype(dtype): def int_dtype_with_size_in_bytes(size): try: return INT_DTYPES_BY_SIZE_BYTES[size] - except KeyError: - raise ValueError("No integral dtype whose size is %d bytes." % size) + except KeyError as exc: + raise ValueError("No integral dtype whose size is %d bytes." % size) from exc def unsigned_int_dtype_with_size_in_bytes(size): try: return UNSIGNED_INT_DTYPES_BY_SIZE_BYTES[size] - except KeyError: - raise ValueError("No unsigned integral dtype whose size is %d bytes." % size) + except KeyError as exc: + raise ValueError( + "No unsigned integral dtype whose size is %d bytes." % size + ) from exc class NoDefaultMissingValue(Exception): @@ -171,8 +173,10 @@ def default_missing_value_for_dtype(dtype): """ try: return _FILLVALUE_DEFAULTS[dtype] - except KeyError: - raise NoDefaultMissingValue("No default value registered for dtype %s." % dtype) + except KeyError as exc: + raise NoDefaultMissingValue( + "No default value registered for dtype %s." % dtype + ) from exc def repeat_first_axis(array, count): diff --git a/src/zipline/utils/run_algo.py b/src/zipline/utils/run_algo.py index c779a220e5..f375beef3e 100644 --- a/src/zipline/utils/run_algo.py +++ b/src/zipline/utils/run_algo.py @@ -11,7 +11,7 @@ PYGMENTS = True except ImportError: PYGMENTS = False -import logbook +import logging import pandas as pd from toolz import concatv from zipline.utils.calendar_utils import get_calendar @@ -30,7 +30,7 @@ from zipline.algorithm import TradingAlgorithm, NoBenchmark from zipline.finance.blotter import Blotter -log = logbook.Logger(__name__) +log = logging.getLogger(__name__) class _RunAlgoError(click.ClickException, ValueError): @@ -535,14 +535,14 @@ def resolve(self, asset_finder, start_date, end_date): end_date=end_date, ) else: - log.warn( + log.warning( "No benchmark configured. " "Assuming algorithm calls set_benchmark." ) - log.warn( + log.warning( "Pass --benchmark-sid, --benchmark-symbol, or" " --benchmark-file to set a source of benchmark returns." ) - log.warn( + log.warning( "Pass --no-benchmark to use a dummy benchmark " "of zero returns.", ) benchmark_sid = None diff --git a/tests/data/test_adjustments.py b/tests/data/test_adjustments.py index f2b469668a..13b6d25ddd 100644 --- a/tests/data/test_adjustments.py +++ b/tests/data/test_adjustments.py @@ -1,4 +1,5 @@ -import logbook +import logging +import pytest import numpy as np import pandas as pd @@ -15,7 +16,6 @@ from zipline.testing.fixtures import ( WithInstanceTmpDir, WithTradingCalendars, - WithLogger, ZiplineTestCase, ) @@ -23,9 +23,11 @@ class TestSQLiteAdjustmentsWriter( - WithTradingCalendars, WithInstanceTmpDir, WithLogger, ZiplineTestCase + WithTradingCalendars, WithInstanceTmpDir, ZiplineTestCase ): - make_log_handler = logbook.TestHandler + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + self._caplog = caplog def init_instance_fixtures(self): super(TestSQLiteAdjustmentsWriter, self).init_instance_fixtures() @@ -174,26 +176,30 @@ def T(n): dividend_ratios.reset_index(drop=True, inplace=True) assert_frame_equal(dividend_ratios, expected_dividend_ratios) - assert self.log_handler.has_warning( - "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-18," - " amount=10.000", - ) - assert self.log_handler.has_warning( - "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-19," - " amount=0.100", - ) - assert self.log_handler.has_warning( - "Couldn't compute ratio for dividend sid=2, ex_date=1990-11-01," - " amount=0.100", - ) - assert self.log_handler.has_warning( - "Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-17," - " amount=0.510", - ) - assert self.log_handler.has_warning( - "Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-18," - " amount=0.400", - ) + with self._caplog.at_level(logging.WARNING): + assert ( + "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-18, amount=10.000" + in self._caplog.messages + ) + assert ( + "Couldn't compute ratio for dividend sid=2, ex_date=1990-10-19, amount=0.100" + in self._caplog.messages + ) + + assert ( + "Couldn't compute ratio for dividend sid=2, ex_date=1990-11-01, amount=0.100" + in self._caplog.messages + ) + + assert ( + "Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-17, amount=0.510" + in self._caplog.messages + ) + + assert ( + "Dividend ratio <= 0 for dividend sid=1, ex_date=1990-10-18, amount=0.400" + in self._caplog.messages + ) def _test_identity(self, name): sids = np.arange(5) diff --git a/tests/pipeline/test_slice.py b/tests/pipeline/test_slice.py index 07f8609ac0..834d8f8b12 100644 --- a/tests/pipeline/test_slice.py +++ b/tests/pipeline/test_slice.py @@ -2,7 +2,7 @@ Tests for slicing pipeline terms. """ from numpy import where -from pandas import Int64Index, Timestamp +import pandas as pd from pandas.testing import assert_frame_equal from zipline.assets import Asset, ExchangeInfo @@ -39,9 +39,9 @@ class SliceTestCase(WithSeededRandomPipelineEngine, ZiplineTestCase): - sids = ASSET_FINDER_EQUITY_SIDS = Int64Index([1, 2, 3]) - START_DATE = Timestamp("2015-01-31", tz="UTC") - END_DATE = Timestamp("2015-03-01", tz="UTC") + sids = ASSET_FINDER_EQUITY_SIDS = pd.Index([1, 2, 3], dtype="int64") + START_DATE = pd.Timestamp("2015-01-31", tz="UTC") + END_DATE = pd.Timestamp("2015-03-01", tz="UTC") ASSET_FINDER_COUNTRY_CODE = "US" SEEDED_RANDOM_PIPELINE_DEFAULT_DOMAIN = US_EQUITIES diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 1c66355b0c..ed948b4ddb 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -12,33 +12,28 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import pytest -import warnings import datetime +import logging +import warnings +from copy import deepcopy from datetime import timedelta from functools import partial from textwrap import dedent -from copy import deepcopy - -import logbook -import toolz -from logbook import TestHandler, WARNING -from parameterized import parameterized -from testfixtures import TempDirectory import numpy as np import pandas as pd +import pytest import pytz -from zipline.utils.calendar_utils import get_calendar, register_calendar +import toolz +from parameterized import parameterized +from testfixtures import TempDirectory import zipline.api +import zipline.testing.fixtures as zf from zipline.api import FixedSlippage -from zipline.assets import Equity, Future, Asset +from zipline.assets import Asset, Equity, Future from zipline.assets.continuous_futures import ContinuousFuture -from zipline.assets.synthetic import ( - make_jagged_equity_info, - make_simple_equity_info, -) +from zipline.assets.synthetic import make_jagged_equity_info, make_simple_equity_info from zipline.errors import ( AccountControlViolation, CannotOrderDelistedAsset, @@ -52,81 +47,80 @@ UnsupportedDatetimeFormat, ZeroCapitalError, ) - -from zipline.finance.commission import PerShare, PerTrade -from zipline.finance.execution import LimitOrder -from zipline.finance.order import ORDER_STATUS -from zipline.finance.trading import SimulationParameters from zipline.finance.asset_restrictions import ( - Restriction, + RESTRICTION_STATES, HistoricalRestrictions, + Restriction, StaticRestrictions, - RESTRICTION_STATES, ) +from zipline.finance.commission import PerShare, PerTrade from zipline.finance.controls import AssetDateBounds -from zipline.testing import ( - FakeDataPortal, - create_daily_df_for_asset, - create_data_portal_from_trade_history, - create_minute_df_for_asset, - make_test_handler, - make_trade_data_for_asset_info, - parameter_space, - str_to_seconds, - to_utc, -) -from zipline.testing import RecordBatchBlotter -import zipline.testing.fixtures as zf +from zipline.finance.execution import LimitOrder +from zipline.finance.order import ORDER_STATUS +from zipline.finance.trading import SimulationParameters from zipline.test_algorithms import ( access_account_in_init, access_portfolio_in_init, api_algo, api_get_environment_algo, api_symbol_algo, - handle_data_api, - handle_data_noop, - initialize_api, - initialize_noop, - noop_algo, - record_float_magic, - record_variables, - call_with_kwargs, - call_without_kwargs, - call_with_bad_kwargs_current, - call_with_bad_kwargs_history, - bad_type_history_assets, - bad_type_history_fields, - bad_type_history_bar_count, - bad_type_history_frequency, - bad_type_history_assets_kwarg_list, + bad_type_can_trade_assets, bad_type_current_assets, + bad_type_current_assets_kwarg, bad_type_current_fields, - bad_type_can_trade_assets, - bad_type_is_stale_assets, + bad_type_current_fields_kwarg, + bad_type_history_assets, bad_type_history_assets_kwarg, - bad_type_history_fields_kwarg, + bad_type_history_assets_kwarg_list, + bad_type_history_bar_count, bad_type_history_bar_count_kwarg, + bad_type_history_fields, + bad_type_history_fields_kwarg, + bad_type_history_frequency, bad_type_history_frequency_kwarg, - bad_type_current_assets_kwarg, - bad_type_current_fields_kwarg, + bad_type_is_stale_assets, + call_with_bad_kwargs_current, call_with_bad_kwargs_get_open_orders, + call_with_bad_kwargs_history, call_with_good_kwargs_get_open_orders, + call_with_kwargs, call_with_no_kwargs_get_open_orders, + call_without_kwargs, empty_positions, + handle_data_api, + handle_data_noop, + initialize_api, + initialize_noop, no_handle_data, + noop_algo, + record_float_magic, + record_variables, +) +from zipline.testing import ( + FakeDataPortal, + RecordBatchBlotter, + create_daily_df_for_asset, + create_data_portal_from_trade_history, + create_minute_df_for_asset, + # make_test_handler, + make_trade_data_for_asset_info, + parameter_space, + str_to_seconds, + to_utc, ) from zipline.testing.predicates import assert_equal +from zipline.utils import factory from zipline.utils.api_support import ZiplineAPI +from zipline.utils.calendar_utils import get_calendar, register_calendar from zipline.utils.context_tricks import CallbackManager, nop_context from zipline.utils.events import ( - date_rules, - time_rules, Always, ComposedRule, Never, OncePerDay, + date_rules, + time_rules, ) -from zipline.utils import factory from zipline.utils.pandas_utils import PerformanceWarning # Because test cases appear to reuse some resources. @@ -2776,6 +2770,10 @@ class TestTradingControls(zf.WithMakeAlgo, zf.ZiplineTestCase): SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = True + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + self._caplog = caplog + @classmethod def init_class_fixtures(cls): super(TestTradingControls, cls).init_class_fixtures() @@ -2928,13 +2926,13 @@ def handle_data(algo, data): initialize=initialize, handle_data=handle_data, ) - with make_test_handler(self) as log_catcher: - self.check_algo_succeeds(algo) - logs = [r.message for r in log_catcher.records] + + self.check_algo_succeeds(algo) + assert ( "Order for 100 shares of Equity(133 [A]) at " "2006-01-03 21:00:00+00:00 violates trading constraint " - "RestrictedListOrder({})" in logs + "RestrictedListOrder({})" in self._caplog.messages ) assert not algo.could_trade @@ -3653,6 +3651,11 @@ def handle_data(context, data): """, ) + # https://stackoverflow.com/questions/50373916/pytest-to-insert-caplog-fixture-in-test-method + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + self._caplog = caplog + @classmethod def make_equity_minute_bar_data(cls): asset_minutes = cls.trading_calendar.minutes_for_sessions_in_range( @@ -3721,85 +3724,78 @@ def test_eod_order_cancel_minute(self, direction, minute_emission): minute_emission=minute_emission, ) - log_catcher = TestHandler() - with log_catcher: - results = algo.run() + results = algo.run() - for daily_positions in results.positions: - assert 1 == len(daily_positions) - assert np.copysign(389, direction) == daily_positions[0]["amount"] - assert 1 == results.positions[0][0]["sid"] + for daily_positions in results.positions: + assert 1 == len(daily_positions) + assert np.copysign(389, direction) == daily_positions[0]["amount"] + assert 1 == results.positions[0][0]["sid"] - # should be an order on day1, but no more orders afterwards - np.testing.assert_array_equal([1, 0, 0], list(map(len, results.orders))) + # should be an order on day1, but no more orders afterwards + np.testing.assert_array_equal([1, 0, 0], list(map(len, results.orders))) - # should be 389 txns on day 1, but no more afterwards - np.testing.assert_array_equal( - [389, 0, 0], list(map(len, results.transactions)) - ) + # should be 389 txns on day 1, but no more afterwards + np.testing.assert_array_equal([389, 0, 0], list(map(len, results.transactions))) - the_order = results.orders[0][0] + the_order = results.orders[0][0] - assert ORDER_STATUS.CANCELLED == the_order["status"] - assert np.copysign(389, direction) == the_order["filled"] + assert ORDER_STATUS.CANCELLED == the_order["status"] + assert np.copysign(389, direction) == the_order["filled"] - warnings = [ - record for record in log_catcher.records if record.level == WARNING - ] + with self._caplog.at_level(logging.WARNING): - assert 1 == len(warnings) + assert 1 == len(self._caplog.messages) if direction == 1: - assert ( + expected = [ "Your order for 1000 shares of ASSET1 has been partially " "filled. 389 shares were successfully purchased. " "611 shares were not filled by the end of day and " - "were canceled." == str(warnings[0].message) - ) + "were canceled." + ] + assert expected == self._caplog.messages elif direction == -1: - assert ( + expected = [ "Your order for -1000 shares of ASSET1 has been partially " "filled. 389 shares were successfully sold. " "611 shares were not filled by the end of day and " - "were canceled." == str(warnings[0].message) - ) + "were canceled." + ] + assert expected == self._caplog.messages + self._caplog.clear() def test_default_cancelation_policy(self): algo = self.prep_algo("") - log_catcher = TestHandler() - with log_catcher: - results = algo.run() + results = algo.run() - # order stays open throughout simulation - np.testing.assert_array_equal([1, 1, 1], list(map(len, results.orders))) + # order stays open throughout simulation + np.testing.assert_array_equal([1, 1, 1], list(map(len, results.orders))) - # one txn per minute. 389 the first day (since no order until the - # end of the first minute). 390 on the second day. 221 on the - # the last day, sum = 1000. - np.testing.assert_array_equal( - [389, 390, 221], list(map(len, results.transactions)) - ) + # one txn per minute. 389 the first day (since no order until the + # end of the first minute). 390 on the second day. 221 on the + # the last day, sum = 1000. + np.testing.assert_array_equal( + [389, 390, 221], list(map(len, results.transactions)) + ) - assert not log_catcher.has_warnings + with self._caplog.at_level(logging.WARNING): + assert len(self._caplog.messages) == 0 def test_eod_order_cancel_daily(self): # in daily mode, EODCancel does nothing. algo = self.prep_algo("set_cancel_policy(cancel_policy.EODCancel())", "daily") - log_catcher = TestHandler() - with log_catcher: - results = algo.run() + results = algo.run() - # order stays open throughout simulation - np.testing.assert_array_equal([1, 1, 1], list(map(len, results.orders))) + # order stays open throughout simulation + np.testing.assert_array_equal([1, 1, 1], list(map(len, results.orders))) - # one txn per day - np.testing.assert_array_equal( - [0, 1, 1], list(map(len, results.transactions)) - ) + # one txn per day + np.testing.assert_array_equal([0, 1, 1], list(map(len, results.transactions))) - assert not log_catcher.has_warnings + with self._caplog.at_level(logging.WARNING): + assert len(self._caplog.messages) == 0 class TestDailyEquityAutoClose(zf.WithMakeAlgo, zf.ZiplineTestCase): @@ -4368,6 +4364,10 @@ class TestOrderAfterDelist(zf.WithMakeAlgo, zf.ZiplineTestCase): # FIXME: Pass a benchmark source here. BENCHMARK_SID = None + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + self._caplog = caplog + @classmethod def make_equity_info(cls): return pd.DataFrame.from_dict( @@ -4442,21 +4442,21 @@ def handle_data(context, data): data_frequency="minute", ), ) - with make_test_handler(self) as log_catcher: - algo.run() - warnings = [r for r in log_catcher.records if r.level == logbook.WARNING] + algo.run() + + with self._caplog.at_level(logging.WARNING): # one warning per order on the second day - assert 6 * 390 == len(warnings) + assert 6 * 390 == len(self._caplog.messages) - for w in warnings: - expected_message = ( - "Cannot place order for ASSET{sid}, as it has de-listed. " - "Any existing positions for this asset will be liquidated " - "on {date}.".format(sid=sid, date=asset.auto_close_date) - ) - assert expected_message == w.message + expected_message = ( + "Cannot place order for ASSET{sid}, as it has de-listed. " + "Any existing positions for this asset will be liquidated " + "on {date}.".format(sid=sid, date=asset.auto_close_date) + ) + for w in self._caplog.messages: + assert expected_message == w class AlgoInputValidationTestCase(zf.WithMakeAlgo, zf.ZiplineTestCase): diff --git a/tests/test_benchmark.py b/tests/test_benchmark.py index 8d894b914e..d7d33c4043 100644 --- a/tests/test_benchmark.py +++ b/tests/test_benchmark.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import logbook +import logging import numpy as np import pandas as pd from pandas.testing import assert_series_equal @@ -42,7 +42,6 @@ WithTradingCalendars, ZiplineTestCase, ) -from zipline.testing.core import make_test_handler import pytest import re @@ -242,6 +241,10 @@ def test_no_stock_dividends_allowed(self): class BenchmarkSpecTestCase(WithTmpDir, WithAssetFinder, ZiplineTestCase): + @pytest.fixture(autouse=True) + def inject_fixtures(self, caplog): + self._caplog = caplog + @classmethod def init_class_fixtures(cls): super(BenchmarkSpecTestCase, cls).init_class_fixtures() @@ -256,7 +259,6 @@ def init_class_fixtures(cls): def init_instance_fixtures(self): super(BenchmarkSpecTestCase, self).init_instance_fixtures() - self.log_handler = self.enter_instance_context(make_test_handler(self)) @classmethod def make_equity_info(cls): @@ -301,13 +303,13 @@ def test_no_benchmark(self): assert sid is None assert returns is None - warnings = self.logs_at_level(logbook.WARNING) expected = [ "No benchmark configured. Assuming algorithm calls set_benchmark.", "Pass --benchmark-sid, --benchmark-symbol, or --benchmark-file to set a source of benchmark returns.", # noqa "Pass --no-benchmark to use a dummy benchmark of zero returns.", ] - assert_equal(warnings, expected) + with self._caplog.at_level(logging.WARNING): + assert_equal(self._caplog.messages, expected) def test_no_benchmark_explicitly_disabled(self): """Test running with no benchmark provided, with no_benchmark flag.""" @@ -323,9 +325,9 @@ def test_no_benchmark_explicitly_disabled(self): assert sid is None assert_series_equal(returns, self.zero_returns) - warnings = self.logs_at_level(logbook.WARNING) expected = [] - assert_equal(warnings, expected) + with self._caplog.at_level(logging.WARNING): + assert_equal(self._caplog.messages, expected) @parameter_space(case=[("A", 1), ("B", 2)]) def test_benchmark_symbol(self, case): @@ -344,9 +346,9 @@ def test_benchmark_symbol(self, case): assert_equal(sid, expected_sid) assert returns is None - warnings = self.logs_at_level(logbook.WARNING) expected = [] - assert_equal(warnings, expected) + with self._caplog.at_level(logging.WARNING): + assert_equal(self._caplog.messages, expected) @parameter_space(input_sid=[1, 2]) def test_benchmark_sid(self, input_sid): @@ -363,9 +365,9 @@ def test_benchmark_sid(self, input_sid): assert_equal(sid, input_sid) assert returns is None - warnings = self.logs_at_level(logbook.WARNING) expected = [] - assert_equal(warnings, expected) + with self._caplog.at_level(logging.WARNING): + assert_equal(self._caplog.messages, expected) def test_benchmark_file(self): """Test running with a benchmark file.""" @@ -400,6 +402,6 @@ def test_benchmark_file(self): assert_series_equal(returns, expected_returns, check_names=False) - warnings = self.logs_at_level(logbook.WARNING) expected = [] - assert_equal(warnings, expected) + with self._caplog.at_level(logging.WARNING): + assert_equal(self._caplog.messages, expected) diff --git a/tests/test_blotter.py b/tests/test_blotter.py index 9c172d5c60..3e1e7cc1c6 100644 --- a/tests/test_blotter.py +++ b/tests/test_blotter.py @@ -36,7 +36,6 @@ from zipline.testing.fixtures import ( WithCreateBarData, WithDataPortal, - WithLogger, WithSimParams, ZiplineTestCase, ) @@ -44,7 +43,7 @@ class BlotterTestCase( - WithCreateBarData, WithLogger, WithDataPortal, WithSimParams, ZiplineTestCase + WithCreateBarData, WithDataPortal, WithSimParams, ZiplineTestCase ): START_DATE = pd.Timestamp("2006-01-05", tz="utc") END_DATE = pd.Timestamp("2006-01-06", tz="utc") diff --git a/tests/test_execution_styles.py b/tests/test_execution_styles.py index 81e17836ad..0782a9d728 100644 --- a/tests/test_execution_styles.py +++ b/tests/test_execution_styles.py @@ -23,7 +23,6 @@ StopOrder, ) from zipline.testing.fixtures import ( - WithLogger, ZiplineTestCase, WithConstantFutureMinuteBarData, ) @@ -32,9 +31,7 @@ import pytest -class ExecutionStyleTestCase( - WithConstantFutureMinuteBarData, WithLogger, ZiplineTestCase -): +class ExecutionStyleTestCase(WithConstantFutureMinuteBarData, ZiplineTestCase): """ Tests for zipline ExecutionStyle classes. """ From c56395f76d8b40121d4e7369dfdd609ed724bbc5 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Mon, 28 Nov 2022 13:40:30 +0100 Subject: [PATCH 07/15] replace logbook with logging (#17) * fix B904 flak8-bugbear warnings * change import style for logbook * replaced with logging * replaced logbook with logging * changes to utils path --- src/zipline/utils/paths.py | 119 +++++++++-------------------- tests/pipeline/test_statistical.py | 6 +- 2 files changed, 41 insertions(+), 84 deletions(-) diff --git a/src/zipline/utils/paths.py b/src/zipline/utils/paths.py index c4b8bcd87f..b9421e0e62 100644 --- a/src/zipline/utils/paths.py +++ b/src/zipline/utils/paths.py @@ -4,14 +4,14 @@ Paths are rooted at $ZIPLINE_ROOT if that environment variable is set. Otherwise default to expanduser(~/.zipline) """ -from errno import EEXIST import os -from os.path import exists, expanduser, join +from pathlib import Path +from typing import Any, Iterable, Mapping, Optional import pandas as pd -def hidden(path): +def hidden(path: str) -> bool: """Check if a path is hidden. Parameters @@ -19,35 +19,27 @@ def hidden(path): path : str A filepath. """ - return os.path.split(path)[1].startswith(".") + # return os.path.split(path)[1].startswith(".") + return Path(path).stem.startswith(".") -def ensure_directory(path): - """ - Ensure that a directory named "path" exists. - """ - try: - os.makedirs(path) - except OSError as exc: - if exc.errno == EEXIST and os.path.isdir(path): - return - raise +def ensure_directory(path: str) -> None: + """Ensure that a directory named "path" exists.""" + Path(path).mkdir(parents=True, exist_ok=True) -def ensure_directory_containing(path): - """ - Ensure that the directory containing `path` exists. +def ensure_directory_containing(path: str) -> None: + """Ensure that the directory containing `path` exists. This is just a convenience wrapper for doing:: ensure_directory(os.path.dirname(path)) """ - ensure_directory(os.path.dirname(path)) + ensure_directory(str(Path(path).parent)) -def ensure_file(path): - """ - Ensure that a file exists. This will create any parent directories needed +def ensure_file(path: str) -> None: + """Ensure that a file exists. This will create any parent directories needed and create an empty file if it does not exist. Parameters @@ -56,35 +48,16 @@ def ensure_file(path): The file path to ensure exists. """ ensure_directory_containing(path) - open(path, "a+").close() # touch the file + Path(path).touch(exist_ok=True) -def update_modified_time(path, times=None): - """ - Updates the modified time of an existing file. This will create any - parent directories needed and create an empty file if it does not exist. +def last_modified_time(path: str) -> pd.Timestamp: + """Get the last modified time of path as a Timestamp.""" + return pd.Timestamp(Path(path).stat().st_mtime, unit="s", tz="UTC") - Parameters - ---------- - path : str - The file path to update. - times : tuple - A tuple of size two; access time and modified time - """ - ensure_directory_containing(path) - os.utime(path, times) - -def last_modified_time(path): - """ - Get the last modified time of path as a Timestamp. - """ - return pd.Timestamp(os.path.getmtime(path), unit="s", tz="UTC") - - -def modified_since(path, dt): - """ - Check whether `path` was modified since `dt`. +def modified_since(path: str, dt: pd.Timestamp) -> bool: + """Check whether `path` was modified since `dt`. Returns False if path doesn't exist. @@ -101,12 +74,11 @@ def modified_since(path, dt): Will be ``False`` if path doesn't exists, or if its last modified date is earlier than or equal to `dt` """ - return exists(path) and last_modified_time(path) > dt + return Path(path).exists() and last_modified_time(path) > dt -def zipline_root(environ=None): - """ - Get the root directory for all zipline-managed files. +def zipline_root(environ: Optional[Mapping[Any, Any]] = None) -> str: + """Get the root directory for all zipline-managed files. For testing purposes, this accepts a dictionary to interpret as the os environment. @@ -126,14 +98,13 @@ def zipline_root(environ=None): root = environ.get("ZIPLINE_ROOT", None) if root is None: - root = expanduser("~/.zipline") + root = str(Path.expanduser(Path("~/.zipline"))) return root -def zipline_path(paths, environ=None): - """ - Get a path relative to the zipline root. +def zipline_path(paths: list[str], environ: Optional[Mapping[Any, Any]] = None) -> str: + """Get a path relative to the zipline root. Parameters ---------- @@ -147,12 +118,11 @@ def zipline_path(paths, environ=None): newpath : str The requested path joined with the zipline root. """ - return join(zipline_root(environ=environ), *paths) + return str(Path(zipline_root(environ=environ) / Path(*paths))) -def default_extension(environ=None): - """ - Get the path to the default zipline extension file. +def default_extension(environ: Optional[Mapping[Any, Any]] = None) -> str: + """Get the path to the default zipline extension file. Parameters ---------- @@ -167,9 +137,8 @@ def default_extension(environ=None): return zipline_path(["extension.py"], environ=environ) -def data_root(environ=None): - """ - The root directory for zipline data files. +def data_root(environ: Optional[Mapping[Any, Any]] = None) -> str: + """The root directory for zipline data files. Parameters ---------- @@ -184,16 +153,8 @@ def data_root(environ=None): return zipline_path(["data"], environ=environ) -def ensure_data_root(environ=None): - """ - Ensure that the data root exists. - """ - ensure_directory(data_root(environ=environ)) - - -def data_path(paths, environ=None): - """ - Get a path relative to the zipline data directory. +def data_path(paths: Iterable[str], environ: Optional[Mapping[Any, Any]] = None) -> str: + """Get a path relative to the zipline data directory. Parameters ---------- @@ -210,9 +171,8 @@ def data_path(paths, environ=None): return zipline_path(["data"] + list(paths), environ=environ) -def cache_root(environ=None): - """ - The root directory for zipline cache files. +def cache_root(environ: Optional[Mapping[Any, Any]] = None) -> str: + """The root directory for zipline cache files. Parameters ---------- @@ -227,16 +187,13 @@ def cache_root(environ=None): return zipline_path(["cache"], environ=environ) -def ensure_cache_root(environ=None): - """ - Ensure that the data root exists. - """ +def ensure_cache_root(environ: Optional[Mapping[Any, Any]] = None) -> None: + """Ensure that the data root exists.""" ensure_directory(cache_root(environ=environ)) -def cache_path(paths, environ=None): - """ - Get a path relative to the zipline cache directory. +def cache_path(paths: Iterable[str], environ: Optional[dict] = None) -> str: + """Get a path relative to the zipline cache directory. Parameters ---------- diff --git a/tests/pipeline/test_statistical.py b/tests/pipeline/test_statistical.py index eb9764f875..7efbde816c 100644 --- a/tests/pipeline/test_statistical.py +++ b/tests/pipeline/test_statistical.py @@ -293,7 +293,7 @@ def test_regression_of_returns_factor(self, returns_length, regression_length): for day in range(num_days): todays_returns = returns_results.iloc[day : day + regression_length] my_asset_returns = todays_returns.iloc[:, my_asset_column] - for asset, other_asset_returns in todays_returns.iteritems(): + for asset, other_asset_returns in todays_returns.items(): asset_column = int(asset) - 1 expected_regression_results = linregress( y=other_asset_returns, @@ -801,7 +801,7 @@ def test_factor_correlation_methods_two_factors(self, correlation_length): for day in range(num_days): todays_returns_5 = returns_5_results.iloc[day : day + correlation_length] todays_returns_10 = returns_10_results.iloc[day : day + correlation_length] - for asset, asset_returns_5 in todays_returns_5.iteritems(): + for asset, asset_returns_5 in todays_returns_5.items(): asset_column = int(asset) - 1 asset_returns_10 = todays_returns_10[asset] expected_pearson_results[day, asset_column] = pearsonr( @@ -905,7 +905,7 @@ def test_factor_regression_method_two_factors(self, regression_length): for day in range(num_days): todays_returns_5 = returns_5_results.iloc[day : day + regression_length] todays_returns_10 = returns_10_results.iloc[day : day + regression_length] - for asset, asset_returns_5 in todays_returns_5.iteritems(): + for asset, asset_returns_5 in todays_returns_5.items(): asset_column = int(asset) - 1 asset_returns_10 = todays_returns_10[asset] expected_regression_results = linregress( From 9587f375aefdba49efbc47815bb5a8e4b42ceade Mon Sep 17 00:00:00 2001 From: MBounouar Date: Fri, 2 Dec 2022 11:36:28 +0100 Subject: [PATCH 08/15] wip sqlalchemy2.0 --- .devcontainer/requirements.txt | 1 - pyproject.toml | 7 +- src/zipline/assets/asset_db_migrations.py | 141 +++-- src/zipline/assets/asset_db_schema.py | 43 +- src/zipline/assets/asset_writer.py | 50 +- src/zipline/assets/assets.py | 147 +++-- src/zipline/data/adjustments.py | 4 +- .../{minute_bars.py => bcolz_minute_bars.py} | 0 src/zipline/data/bundles/core.py | 2 +- src/zipline/data/bundles/csvdir.py | 4 +- src/zipline/data/data_portal.py | 2 +- src/zipline/data/resample.py | 2 +- src/zipline/lib/adjustment.pyx | 6 +- src/zipline/pipeline/loaders/events.py | 2 +- src/zipline/pipeline/loaders/frame.py | 2 +- src/zipline/testing/core.py | 45 +- src/zipline/testing/fixtures.py | 2 +- src/zipline/utils/sqlite_utils.py | 2 +- tests/__init__.py | 1 - tests/conftest.py | 174 ++++++ tests/data/bundles/test_core.py | 44 +- tests/data/bundles/test_csvdir.py | 4 +- tests/data/bundles/test_quandl.py | 8 +- tests/data/test_minute_bars.py | 2 +- tests/finance/test_commissions.py | 76 ++- tests/finance/test_risk.py | 60 +- tests/metrics/test_core.py | 4 +- tests/pipeline/base.py | 14 +- tests/pipeline/test_engine.py | 6 +- tests/pipeline/test_events.py | 24 +- tests/pipeline/test_factor.py | 43 +- tests/pipeline/test_pipeline_algo.py | 22 +- tests/pipeline/test_statistical.py | 208 ++++--- tests/resources/pipeline_inputs/generate.py | 9 +- tests/test_assets.py | 587 +++++++++--------- tests/test_benchmark.py | 145 ++--- tests/test_continuous_futures.py | 224 +++---- tests/test_data_portal.py | 4 +- tests/test_examples.py | 57 +- tests/test_finance.py | 96 ++- 40 files changed, 1306 insertions(+), 968 deletions(-) rename src/zipline/data/{minute_bars.py => bcolz_minute_bars.py} (100%) create mode 100644 tests/conftest.py diff --git a/.devcontainer/requirements.txt b/.devcontainer/requirements.txt index 19cef2a7e9..313f9bb2d8 100644 --- a/.devcontainer/requirements.txt +++ b/.devcontainer/requirements.txt @@ -28,7 +28,6 @@ iso3166==2.1.1 iso4217==1.11.20220401 kiwisolver==1.4.4 korean-lunar-calendar==0.3.1 -Logbook==1.5.3 lru-dict==1.1.8 lxml==4.9.1 Mako==1.2.4 diff --git a/pyproject.toml b/pyproject.toml index 9c44e42611..32e46316c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,12 +96,15 @@ test = [ 'pandas-datareader >=0.2.1', 'click <8.1.0', 'coverage', - 'pytest-rerunfailures' + 'pytest-rerunfailures', + 'psycopg2 ==2.9.4', + 'pytest-postgresql ==3.1.3' ] dev = [ 'flake8 >=3.9.1', 'black', - 'pre-commit >=2.12.1' + 'pre-commit >=2.12.1', + 'Cython>=0.29.21,<3', ] docs = [ diff --git a/src/zipline/assets/asset_db_migrations.py b/src/zipline/assets/asset_db_migrations.py index aeb40e0bee..fc3a5ea980 100644 --- a/src/zipline/assets/asset_db_migrations.py +++ b/src/zipline/assets/asset_db_migrations.py @@ -1,11 +1,11 @@ +import sqlalchemy as sa from alembic.migration import MigrationContext from alembic.operations import Operations -import sqlalchemy as sa from toolz.curried import do, operator from zipline.assets.asset_writer import write_version_info -from zipline.utils.compat import wraps from zipline.errors import AssetDBImpossibleDowngrade +from zipline.utils.compat import wraps from zipline.utils.preprocess import preprocess from zipline.utils.sqlite_utils import coerce_string_to_eng @@ -44,22 +44,22 @@ def alter_columns(op, name, *columns, **kwargs): # fail to create the table because the indices will already be present. # When we create the table below, the indices that we want to preserve # will just get recreated. - for table in name, tmp_name: + for table in (name, tmp_name): try: - op.drop_index("ix_%s_%s" % (table, column.name)) + op.execute(f"DROP INDEX IF EXISTS ix_{table}_{column.name}") except sa.exc.OperationalError: pass op.create_table(name, *columns) op.execute( - "insert into %s select %s from %s" - % ( - name, - selection_string, - tmp_name, - ), + f"INSERT INTO {name} SELECT {selection_string} FROM {tmp_name}", ) - op.drop_table(tmp_name) + + if op.impl.dialect.name == "postgresql": + op.execute(f"ALTER TABLE {tmp_name} DISABLE TRIGGER ALL;") + op.execute(f"DROP TABLE {tmp_name} CASCADE;") + else: + op.drop_table(tmp_name) @preprocess(engine=coerce_string_to_eng(require_exists=True)) @@ -123,7 +123,12 @@ def _pragma_foreign_keys(connection, on): If true, PRAGMA foreign_keys will be set to ON. Otherwise, the PRAGMA foreign_keys will be set to OFF. """ - connection.execute("PRAGMA foreign_keys=%s" % ("ON" if on else "OFF")) + if connection.engine.name == "sqlite": + connection.execute(sa.text(f"PRAGMA foreign_keys={'ON' if on else 'OFF'}")) + # elif connection.engine.name == "postgresql": + # connection.execute( + # f"SET session_replication_role = {'origin' if on else 'replica'};" + # ) # This dict contains references to downgrade methods that can be applied to an @@ -231,7 +236,7 @@ def _downgrade_v3(op): "_new_equities", sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, @@ -241,10 +246,10 @@ def _downgrade_v3(op): sa.Column("share_class_symbol", sa.Text), sa.Column("fuzzy_symbol", sa.Text), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer, nullable=False), - sa.Column("auto_close_date", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger, nullable=False), + sa.Column("auto_close_date", sa.BigInteger), sa.Column("exchange", sa.Text), ) op.execute( @@ -297,7 +302,7 @@ def _downgrade_v5(op): "_new_equities", sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, @@ -307,18 +312,18 @@ def _downgrade_v5(op): sa.Column("share_class_symbol", sa.Text), sa.Column("fuzzy_symbol", sa.Text), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer), - sa.Column("auto_close_date", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger), + sa.Column("auto_close_date", sa.BigInteger), sa.Column("exchange", sa.Text), sa.Column("exchange_full", sa.Text), ) op.execute( """ - insert into _new_equities - select + INSERT INTO _new_equities + SELECT equities.sid as sid, sym.symbol as symbol, sym.company_symbol as company_symbol, @@ -331,26 +336,23 @@ def _downgrade_v5(op): equities.auto_close_date as auto_close_date, equities.exchange as exchange, equities.exchange_full as exchange_full - from + FROM equities - inner join - -- Select the last held symbol for each equity sid from the - -- symbol_mappings table. Selecting max(end_date) causes - -- SQLite to take the other values from the same row that contained - -- the max end_date. See https://www.sqlite.org/lang_select.html#resultset. # noqa - (select - sid, symbol, company_symbol, share_class_symbol, max(end_date) - from - equity_symbol_mappings - group by sid) as 'sym' + INNER JOIN + -- Select the last held symbol (end_date) for each equity sid from the + (SELECT + sid, symbol, company_symbol, share_class_symbol, end_date + FROM (SELECT *, RANK() OVER (PARTITION BY sid ORDER BY end_date DESC) max_end_date + FROM equity_symbol_mappings) ranked WHERE max_end_date=1 + ) as sym on - equities.sid == sym.sid + equities.sid = sym.sid """, ) op.drop_table("equity_symbol_mappings") op.drop_table("equities") op.rename_table("_new_equities", "equities") - # we need to make sure the indicies have the proper names after the rename + # we need to make sure the indices have the proper names after the rename op.create_index( "ix_equities_company_symbol", "equities", @@ -375,25 +377,25 @@ def _downgrade_v7(op): tmp_name, sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, ), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer), - sa.Column("auto_close_date", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger), + sa.Column("auto_close_date", sa.BigInteger), # remove foreign key to exchange sa.Column("exchange", sa.Text), # add back exchange full column sa.Column("exchange_full", sa.Text), ) op.execute( - """ + f""" insert into - _new_equities + {tmp_name} select eq.sid, eq.asset_name, @@ -408,12 +410,27 @@ def _downgrade_v7(op): inner join exchanges ex on - eq.exchange == ex.exchange + eq.exchange = ex.exchange where ex.country_code in ('US', '??') """, ) - op.drop_table("equities") + # if op.impl.dialect.name == "postgresql": + # for table_name, col_name in [ + # ("equities", "exchange"), + # ("equity_symbol_mappings", "sid"), + # ("equity_supplementary_mappings", "sid"), + # ]: + # op.drop_constraint( + # f"{table_name}_{col_name}_fkey", + # f"{table_name}", + # type_="foreignkey", + # ) + if op.impl.dialect.name == "postgresql": + op.execute("ALTER TABLE equities DISABLE TRIGGER ALL;") + op.execute("DROP TABLE equities CASCADE;") + else: + op.drop_table("equities") op.rename_table(tmp_name, "equities") # rebuild all tables without a foreign key to ``exchanges`` @@ -427,7 +444,7 @@ def _downgrade_v7(op): nullable=False, primary_key=True, ), - sa.Column("root_symbol_id", sa.Integer), + sa.Column("root_symbol_id", sa.BigInteger), sa.Column("sector", sa.Text), sa.Column("description", sa.Text), sa.Column("exchange", sa.Text), @@ -437,7 +454,7 @@ def _downgrade_v7(op): "futures_contracts", sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, @@ -445,13 +462,13 @@ def _downgrade_v7(op): sa.Column("symbol", sa.Text, unique=True, index=True), sa.Column("root_symbol", sa.Text, index=True), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger), sa.Column("exchange", sa.Text), - sa.Column("notice_date", sa.Integer, nullable=False), - sa.Column("expiration_date", sa.Integer, nullable=False), - sa.Column("auto_close_date", sa.Integer, nullable=False), + sa.Column("notice_date", sa.BigInteger, nullable=False), + sa.Column("expiration_date", sa.BigInteger, nullable=False), + sa.Column("auto_close_date", sa.BigInteger, nullable=False), sa.Column("multiplier", sa.Float), sa.Column("tick_size", sa.Float), ) @@ -485,7 +502,7 @@ def _downgrade_v7(op): nullable=False, primary_key=True, ), - sa.Column("root_symbol_id", sa.Integer), + sa.Column("root_symbol_id", sa.BigInteger), sa.Column("sector", sa.Text), sa.Column("description", sa.Text), sa.Column( @@ -499,7 +516,7 @@ def _downgrade_v7(op): "futures_contracts", sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, @@ -512,17 +529,17 @@ def _downgrade_v7(op): index=True, ), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger), sa.Column( "exchange", sa.Text, sa.ForeignKey("futures_exchanges.exchange"), ), - sa.Column("notice_date", sa.Integer, nullable=False), - sa.Column("expiration_date", sa.Integer, nullable=False), - sa.Column("auto_close_date", sa.Integer, nullable=False), + sa.Column("notice_date", sa.BigInteger, nullable=False), + sa.Column("expiration_date", sa.BigInteger, nullable=False), + sa.Column("auto_close_date", sa.BigInteger, nullable=False), sa.Column("multiplier", sa.Float), sa.Column("tick_size", sa.Float), ) diff --git a/src/zipline/assets/asset_db_schema.py b/src/zipline/assets/asset_db_schema.py index 43edb9a349..2dd7c8e84f 100644 --- a/src/zipline/assets/asset_db_schema.py +++ b/src/zipline/assets/asset_db_schema.py @@ -1,6 +1,5 @@ import sqlalchemy as sa - # Define a version number for the database generated by these writers # Increment this version number any time a change is made to the schema of the # assets database @@ -44,16 +43,16 @@ metadata, sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, ), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer), - sa.Column("auto_close_date", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger), + sa.Column("auto_close_date", sa.BigInteger), sa.Column("exchange", sa.Text, sa.ForeignKey(exchanges.c.exchange)), ) @@ -62,14 +61,14 @@ metadata, sa.Column( "id", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, ), sa.Column( "sid", - sa.Integer, + sa.BigInteger, sa.ForeignKey(equities.c.sid), nullable=False, index=True, @@ -90,12 +89,12 @@ ), sa.Column( "start_date", - sa.Integer, + sa.BigInteger, nullable=False, ), sa.Column( "end_date", - sa.Integer, + sa.BigInteger, nullable=False, ), ) @@ -105,14 +104,14 @@ metadata, sa.Column( "sid", - sa.Integer, + sa.BigInteger, sa.ForeignKey(equities.c.sid), nullable=False, primary_key=True, ), sa.Column("field", sa.Text, nullable=False, primary_key=True), - sa.Column("start_date", sa.Integer, nullable=False, primary_key=True), - sa.Column("end_date", sa.Integer, nullable=False), + sa.Column("start_date", sa.BigInteger, nullable=False, primary_key=True), + sa.Column("end_date", sa.BigInteger, nullable=False), sa.Column("value", sa.Text, nullable=False), ) @@ -126,7 +125,7 @@ nullable=False, primary_key=True, ), - sa.Column("root_symbol_id", sa.Integer), + sa.Column("root_symbol_id", sa.BigInteger), sa.Column("sector", sa.Text), sa.Column("description", sa.Text), sa.Column( @@ -141,7 +140,7 @@ metadata, sa.Column( "sid", - sa.Integer, + sa.BigInteger, unique=True, nullable=False, primary_key=True, @@ -154,17 +153,17 @@ index=True, ), sa.Column("asset_name", sa.Text), - sa.Column("start_date", sa.Integer, default=0, nullable=False), - sa.Column("end_date", sa.Integer, nullable=False), - sa.Column("first_traded", sa.Integer), + sa.Column("start_date", sa.BigInteger, default=0, nullable=False), + sa.Column("end_date", sa.BigInteger, nullable=False), + sa.Column("first_traded", sa.BigInteger), sa.Column( "exchange", sa.Text, sa.ForeignKey(exchanges.c.exchange), ), - sa.Column("notice_date", sa.Integer, nullable=False), - sa.Column("expiration_date", sa.Integer, nullable=False), - sa.Column("auto_close_date", sa.Integer, nullable=False), + sa.Column("notice_date", sa.BigInteger, nullable=False), + sa.Column("expiration_date", sa.BigInteger, nullable=False), + sa.Column("auto_close_date", sa.BigInteger, nullable=False), sa.Column("multiplier", sa.Float), sa.Column("tick_size", sa.Float), ) @@ -172,7 +171,7 @@ asset_router = sa.Table( "asset_router", metadata, - sa.Column("sid", sa.Integer, unique=True, nullable=False, primary_key=True), + sa.Column("sid", sa.BigInteger, unique=True, nullable=False, primary_key=True), sa.Column("asset_type", sa.Text), ) diff --git a/src/zipline/assets/asset_writer.py b/src/zipline/assets/asset_writer.py index 1e2cd92382..d3e4659037 100644 --- a/src/zipline/assets/asset_writer.py +++ b/src/zipline/assets/asset_writer.py @@ -12,28 +12,28 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from collections import namedtuple import re +from collections import namedtuple import numpy as np import pandas as pd import sqlalchemy as sa from toolz import first -from zipline.errors import AssetDBVersionError from zipline.assets.asset_db_schema import ( ASSET_DB_VERSION, asset_db_table_names, asset_router, - equities as equities_table, - equity_symbol_mappings, +) +from zipline.assets.asset_db_schema import equities as equities_table +from zipline.assets.asset_db_schema import ( equity_supplementary_mappings as equity_supplementary_mappings_table, - futures_contracts as futures_contracts_table, - exchanges as exchanges_table, - futures_root_symbols, - metadata, - version_info, ) +from zipline.assets.asset_db_schema import equity_symbol_mappings +from zipline.assets.asset_db_schema import exchanges as exchanges_table +from zipline.assets.asset_db_schema import futures_contracts as futures_contracts_table +from zipline.assets.asset_db_schema import futures_root_symbols, metadata, version_info +from zipline.errors import AssetDBVersionError from zipline.utils.compat import ExitStack from zipline.utils.preprocess import preprocess from zipline.utils.range import from_tuple, intersecting_ranges @@ -385,7 +385,7 @@ def _split_symbol_mappings(df, exchanges): ) -def _dt_to_epoch_ns(dt_series): +def _dt_to_epoch_ns(dt_series: pd.Series) -> pd.Index: """Convert a timeseries into an Int64Index of nanoseconds since the epoch. Parameters @@ -395,7 +395,7 @@ def _dt_to_epoch_ns(dt_series): Returns ------- - idx : pd.Int64Index + idx : pd.Index The index converted to nanoseconds since the epoch. """ index = pd.to_datetime(dt_series.values) @@ -406,7 +406,7 @@ def _dt_to_epoch_ns(dt_series): return index.view(np.int64) -def check_version_info(conn, version_table, expected_version): +def check_version_info(conn, version_table, expected_version: int): """ Checks for a version value in the version table. @@ -426,9 +426,7 @@ def check_version_info(conn, version_table, expected_version): """ # Read the version out of the table - version_from_table = conn.execute( - sa.select((version_table.c.version,)), - ).scalar() + version_from_table = conn.execute(sa.select(version_table.c.version)).scalar() # A db without a version is considered v0 if version_from_table is None: @@ -455,7 +453,9 @@ def write_version_info(conn, version_table, version_value): The version to write in to the database """ - conn.execute(sa.insert(version_table, values={"version": version_value})) + if conn.engine.name == "postgresql": + conn.execute(sa.text("ALTER SEQUENCE version_info_id_seq RESTART WITH 1")) + conn.execute(version_table.insert().values(version=version_value)) class AssetDBWriter: @@ -825,9 +825,12 @@ def _write_df_to_table(self, tbl, df, txn, chunk_size): if dtype.kind == "M": df[column] = _dt_to_epoch_ns(df[column]) + if txn.dialect.name == "postgresql": + txn.execute(sa.text(f"ALTER TABLE {tbl.name} DISABLE TRIGGER ALL;")) + df.to_sql( tbl.name, - txn.connection, + txn, index=True, index_label=first(tbl.primary_key.columns).name, if_exists="append", @@ -866,7 +869,7 @@ def _write_assets(self, asset_type, assets, txn, chunk_size, mapping_data=None): } ).to_sql( asset_router.name, - txn.connection, + txn, if_exists="append", index=False, chunksize=chunk_size, @@ -886,11 +889,12 @@ def _all_tables_present(self, txn): has_tables : bool True if any tables are present, otherwise False. """ - conn = txn.connect() + # conn = txn.connect() for table_name in asset_db_table_names: - if txn.dialect.has_table(conn, table_name): - return True - return False + return sa.inspect(txn).has_table(table_name) + # if txn.dialect.has_table(conn, table_name): + # return True + # return False def init_db(self, txn=None): """Connect to database and create tables. @@ -898,7 +902,7 @@ def init_db(self, txn=None): Parameters ---------- txn : sa.engine.Connection, optional - The transaction to execute in. If this is not provided, a new + The transaction block to execute in. If this is not provided, a new transaction will be started with the engine provided. Returns diff --git a/src/zipline/assets/assets.py b/src/zipline/assets/assets.py index d4c29079fa..50612e15a3 100644 --- a/src/zipline/assets/assets.py +++ b/src/zipline/assets/assets.py @@ -12,19 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +# import array +# import binascii +# import struct from abc import ABC -import array -import binascii from collections import deque, namedtuple from functools import partial from numbers import Integral -from operator import itemgetter, attrgetter -import struct +from operator import attrgetter, itemgetter import logging import numpy as np import pandas as pd -from pandas import isnull import sqlalchemy as sa from toolz import ( compose, @@ -46,15 +45,25 @@ MultipleValuesFoundForField, MultipleValuesFoundForSid, NoValueForSid, - ValueNotFoundForField, SameSymbolUsedAcrossCountries, SidsNotFound, SymbolNotFound, + ValueNotFoundForField, ) -from . import ( - Asset, - Equity, - Future, +from zipline.utils.functional import invert +from zipline.utils.memoize import lazyval +from zipline.utils.numpy_utils import as_column +from zipline.utils.preprocess import preprocess +from zipline.utils.sqlite_utils import coerce_string_to_eng, group_into_chunks + +from . import Asset, Equity, Future +from .asset_db_schema import ASSET_DB_VERSION +from .asset_writer import ( + SQLITE_MAX_VARIABLE_NUMBER, + asset_db_table_names, + check_version_info, + split_delimited_symbol, + symbol_columns, ) from .continuous_futures import ( ADJUSTMENT_STYLES, @@ -62,32 +71,19 @@ ContinuousFuture, OrderedContracts, ) -from .asset_writer import ( - check_version_info, - split_delimited_symbol, - asset_db_table_names, - symbol_columns, - SQLITE_MAX_VARIABLE_NUMBER, -) -from .asset_db_schema import ASSET_DB_VERSION from .exchange_info import ExchangeInfo -from zipline.utils.functional import invert -from zipline.utils.memoize import lazyval -from zipline.utils.numpy_utils import as_column -from zipline.utils.preprocess import preprocess -from zipline.utils.sqlite_utils import group_into_chunks, coerce_string_to_eng log = logging.getLogger("assets.py") # A set of fields that need to be converted to strings before building an # Asset to avoid unicode fields -_asset_str_fields = frozenset( - { - "symbol", - "asset_name", - "exchange", - } -) +# _asset_str_fields = frozenset( +# { +# "symbol", +# "asset_name", +# "exchange", +# } +# ) # A set of fields that need to be converted to timestamps in UTC _asset_timestamp_fields = frozenset( @@ -219,7 +215,7 @@ def _convert_asset_timestamp_fields(dict_): """ for key in _asset_timestamp_fields & dict_.keys(): value = pd.Timestamp(dict_[key], tz="UTC") - dict_[key] = None if isnull(value) else value + dict_[key] = None if pd.isnull(value) else value return dict_ @@ -609,34 +605,30 @@ def _select_most_recent_symbols_chunk(self, sid_group): data_cols = (cols.sid,) + tuple(cols[name] for name in symbol_columns) # Also select the max of end_date so that all non-grouped fields take - # on the value associated with the max end_date. The SQLite docs say - # this: - # - # When the min() or max() aggregate functions are used in an aggregate - # query, all bare columns in the result set take values from the input - # row which also contains the minimum or maximum. Only the built-in - # min() and max() functions work this way. - # - # See https://www.sqlite.org/lang_select.html#resultset, for more info. - to_select = data_cols + (sa.func.max(cols.end_date),) + # on the value associated with the max end_date. + # to_select = data_cols + (sa.func.max(cols.end_date),) + func_rank = ( + sa.func.rank() + .over(order_by=cols.end_date.desc(), partition_by=cols.sid) + .label("rnk") + ) + to_select = data_cols + (func_rank,) - return ( - sa.select( - to_select, - ) - .where(cols.sid.in_(map(int, sid_group))) - .group_by( - cols.sid, - ) + subquery = ( + sa.select(to_select).where(cols.sid.in_(map(int, sid_group))).subquery("sq") + ) + query = ( + sa.select(subquery.columns) + .filter(subquery.c.rnk == 1) + .select_from(subquery) ) + return query def _lookup_most_recent_symbols(self, sids): return { row.sid: {c: row[c] for c in symbol_columns} for row in concat( - self.engine.execute( - self._select_most_recent_symbols_chunk(sid_group), - ).fetchall() + self._select_most_recent_symbols_chunk(sid_group).execute().fetchall() for sid_group in partition_all(SQLITE_MAX_VARIABLE_NUMBER, sids) ) } @@ -1254,7 +1246,10 @@ def _(self): return tuple( map( itemgetter("sid"), - sa.select((getattr(self, tblattr).c.sid,)).execute().fetchall(), + sa.select((getattr(self, tblattr).c.sid,)) + .order_by(getattr(self, tblattr).c.sid) + .execute() + .fetchall(), ) ) @@ -1372,7 +1367,7 @@ def lookup_generic(self, obj, as_of_date, country_code): iterator = iter(obj) except TypeError: raise NotAssetConvertible( - "Input was not a AssetConvertible " "or iterable of AssetConvertible." + "Input was not a AssetConvertible or iterable of AssetConvertible." ) from TypeError for obj in iterator: @@ -1386,13 +1381,17 @@ def lookup_generic(self, obj, as_of_date, country_code): return matches, missing - def _compute_asset_lifetimes(self, country_codes): - """ - Compute and cache a recarray of asset lifetimes. - """ + def _compute_asset_lifetimes(self, **kwargs): + """Compute and cache a recarray of asset lifetimes""" sids = starts = ends = [] equities_cols = self.equities.c - if country_codes: + exchanges_cols = self.exchanges.c + if len(kwargs) == 1: + if "country_codes" in kwargs.keys(): + condt = exchanges_cols.country_code.in_(kwargs["country_codes"]) + if "exchange_names" in kwargs.keys(): + condt = exchanges_cols.exchange.in_(kwargs["exchange_names"]) + results = ( sa.select( ( @@ -1401,10 +1400,7 @@ def _compute_asset_lifetimes(self, country_codes): equities_cols.end_date, ) ) - .where( - (self.exchanges.c.exchange == equities_cols.exchange) - & (self.exchanges.c.country_code.in_(country_codes)) - ) + .where((exchanges_cols.exchange == equities_cols.exchange) & (condt)) .execute() .fetchall() ) @@ -1465,7 +1461,7 @@ def lifetimes(self, dates, include_start_date, country_codes): if lifetimes is None: self._asset_lifetimes[ country_codes - ] = lifetimes = self._compute_asset_lifetimes(country_codes) + ] = lifetimes = self._compute_asset_lifetimes(country_codes=country_codes) raw_dates = as_column(dates.asi8) if include_start_date: @@ -1489,7 +1485,22 @@ def equities_sids_for_country_code(self, country_code): tuple[int] The sids whose exchanges are in this country. """ - sids = self._compute_asset_lifetimes([country_code]).sid + sids = self._compute_asset_lifetimes(country_codes=[country_code]).sid + return tuple(sids.tolist()) + + def equities_sids_for_exchange_name(self, exchange_name): + """Return all of the sids for a given exchange_name. + + Parameters + ---------- + exchange_name : str + + Returns + ------- + tuple[int] + The sids whose exchanges are in this country. + """ + sids = self._compute_asset_lifetimes(exchange_names=[exchange_name]).sid return tuple(sids.tolist()) @@ -1514,8 +1525,7 @@ class NotAssetConvertible(ValueError): class PricingDataAssociable(ABC): - """ - ABC for types that can be associated with pricing data. + """ABC for types that can be associated with pricing data. Includes Asset, Future, ContinuousFuture """ @@ -1529,8 +1539,7 @@ class PricingDataAssociable(ABC): def was_active(reference_date_value, asset): - """ - Whether or not `asset` was active at the time corresponding to + """Whether or not `asset` was active at the time corresponding to `reference_date_value`. Parameters diff --git a/src/zipline/data/adjustments.py b/src/zipline/data/adjustments.py index 5580aa9ea3..d198466bdb 100644 --- a/src/zipline/data/adjustments.py +++ b/src/zipline/data/adjustments.py @@ -497,8 +497,8 @@ def calc_dividend_ratios(self, dividends): (close,) = pricing_reader.load_raw_arrays( ["close"], - pd.Timestamp(dates[0], tz="UTC"), - pd.Timestamp(dates[-1], tz="UTC"), + pd.Timestamp(dates[0]).tz_localize("UTC"), + pd.Timestamp(dates[-1]).tz_localize("UTC"), unique_sids, ) date_ix = np.searchsorted(dates, dividends.ex_date.values) diff --git a/src/zipline/data/minute_bars.py b/src/zipline/data/bcolz_minute_bars.py similarity index 100% rename from src/zipline/data/minute_bars.py rename to src/zipline/data/bcolz_minute_bars.py diff --git a/src/zipline/data/bundles/core.py b/src/zipline/data/bundles/core.py index 60504f99b0..a56379962d 100644 --- a/src/zipline/data/bundles/core.py +++ b/src/zipline/data/bundles/core.py @@ -12,7 +12,7 @@ from ..adjustments import SQLiteAdjustmentReader, SQLiteAdjustmentWriter from ..bcolz_daily_bars import BcolzDailyBarReader, BcolzDailyBarWriter -from ..minute_bars import ( +from ..bcolz_minute_bars import ( BcolzMinuteBarReader, BcolzMinuteBarWriter, ) diff --git a/src/zipline/data/bundles/csvdir.py b/src/zipline/data/bundles/csvdir.py index eb244c19b6..a31e9ef0a3 100644 --- a/src/zipline/data/bundles/csvdir.py +++ b/src/zipline/data/bundles/csvdir.py @@ -236,7 +236,7 @@ def _pricing_iter(csvdir, symbols, metadata, divs_splits, show_progress): range(splits.shape[0], splits.shape[0] + split.shape[0]) ) split.set_index(index, inplace=True) - divs_splits["splits"] = splits.append(split) + divs_splits["splits"] = pd.concat([splits, split], axis=0) if "dividend" in dfr.columns: # ex_date amount sid record_date declared_date pay_date @@ -251,7 +251,7 @@ def _pricing_iter(csvdir, symbols, metadata, divs_splits, show_progress): divs = divs_splits["divs"] ind = pd.Index(range(divs.shape[0], divs.shape[0] + div.shape[0])) div.set_index(ind, inplace=True) - divs_splits["divs"] = divs.append(div) + divs_splits["divs"] = pd.concat([divs, div], axis=0) yield sid, dfr diff --git a/src/zipline/data/data_portal.py b/src/zipline/data/data_portal.py index fefb4959ef..89ac6cabdf 100644 --- a/src/zipline/data/data_portal.py +++ b/src/zipline/data/data_portal.py @@ -376,7 +376,7 @@ def handle_extra_source(self, source_df, sim_params): # Append to extra_source_df the reindexed dataframe for the single # sid - extra_source_df = extra_source_df.append(df) + extra_source_df = pd.concat([extra_source_df, df], axis=0) self._extra_source_df = extra_source_df diff --git a/src/zipline/data/resample.py b/src/zipline/data/resample.py index 560f50c377..f6b40bf394 100644 --- a/src/zipline/data/resample.py +++ b/src/zipline/data/resample.py @@ -25,7 +25,7 @@ _minute_to_session_volume, ) from zipline.data.bar_reader import NoDataOnDate -from zipline.data.minute_bars import MinuteBarReader +from zipline.data.bcolz_minute_bars import MinuteBarReader from zipline.data.session_bars import SessionBarReader from zipline.utils.memoize import lazyval from zipline.utils.math_utils import nanmax, nanmin diff --git a/src/zipline/lib/adjustment.pyx b/src/zipline/lib/adjustment.pyx index 23391e926c..bd0e4794ea 100644 --- a/src/zipline/lib/adjustment.pyx +++ b/src/zipline/lib/adjustment.pyx @@ -221,13 +221,13 @@ cpdef tuple get_adjustment_locs(DatetimeIndex_t dates_index, start_date_loc = 0 else: # Location of earliest date on or after start_date. - start_date_loc = dates_index.get_loc(start_date, method='bfill') + start_date_loc = dates_index.get_indexer([start_date], method='bfill')[0] return ( start_date_loc, # Location of latest date on or before start_date. - dates_index.get_loc(end_date, method='ffill'), - assets_index.get_loc(asset_id), # Must be exact match. + dates_index.get_indexer([end_date], method='ffill')[0], + assets_index.get_indexer([asset_id])[0], # Must be exact match. ) diff --git a/src/zipline/pipeline/loaders/events.py b/src/zipline/pipeline/loaders/events.py index 8391371bea..b9cf324778 100644 --- a/src/zipline/pipeline/loaders/events.py +++ b/src/zipline/pipeline/loaders/events.py @@ -97,7 +97,7 @@ def __init__(self, events, next_value_columns, previous_value_columns): # so we coerce from a frame to a dict of arrays here. self.events = { name: np.asarray(series) - for name, series in (events.sort_values(EVENT_DATE_FIELD_NAME).iteritems()) + for name, series in (events.sort_values(EVENT_DATE_FIELD_NAME).items()) } # Columns to load with self.load_next_events. diff --git a/src/zipline/pipeline/loaders/frame.py b/src/zipline/pipeline/loaders/frame.py index 90649f6e32..2518d3cabb 100644 --- a/src/zipline/pipeline/loaders/frame.py +++ b/src/zipline/pipeline/loaders/frame.py @@ -133,7 +133,7 @@ def format_adjustments(self, dates, assets): apply_date, sid, value, kind, start_date, end_date = row if apply_date != previous_apply_date: # Get the next apply date if no exact match. - row_loc = dates.get_loc(apply_date, method="bfill") + row_loc = dates.get_indexer([apply_date], method="bfill")[0] current_date_adjustments = out[row_loc] = [] previous_apply_date = apply_date diff --git a/src/zipline/testing/core.py b/src/zipline/testing/core.py index ab465214e6..0c24f09928 100644 --- a/src/zipline/testing/core.py +++ b/src/zipline/testing/core.py @@ -1,41 +1,32 @@ from abc import ABCMeta, abstractmethod from contextlib import contextmanager import gzip -from itertools import ( - combinations, - count, - product, -) import json import operator import os -from os.path import abspath, dirname, join, realpath import shutil import sys import tempfile +from itertools import combinations, count, product +from os.path import abspath, dirname, join, realpath from traceback import format_exception +import numpy as np +import pandas as pd from mock import patch - from numpy.testing import assert_allclose, assert_array_equal -import pandas as pd from sqlalchemy import create_engine from testfixtures import TempDirectory from toolz import concat, curry -from zipline.utils.calendar_utils import get_calendar -from zipline.assets import AssetFinder, AssetDBWriter +from zipline.assets import AssetDBWriter, AssetFinder from zipline.assets.synthetic import make_simple_equity_info -from zipline.utils.compat import getargspec, wraps +from zipline.data.bcolz_daily_bars import BcolzDailyBarReader, BcolzDailyBarWriter from zipline.data.data_portal import DataPortal -from zipline.data.minute_bars import ( +from zipline.data.bcolz_minute_bars import ( + US_EQUITIES_MINUTES_PER_DAY, BcolzMinuteBarReader, BcolzMinuteBarWriter, - US_EQUITIES_MINUTES_PER_DAY, -) -from zipline.data.bcolz_daily_bars import ( - BcolzDailyBarReader, - BcolzDailyBarWriter, ) from zipline.finance.blotter import SimulationBlotter from zipline.finance.order import ORDER_STATUS @@ -46,14 +37,13 @@ from zipline.pipeline.factors import CustomFactor from zipline.pipeline.loaders.testing import make_seeded_random_loader from zipline.utils import security_list +from zipline.utils.calendar_utils import get_calendar +from zipline.utils.compat import getargspec, wraps from zipline.utils.input_validation import expect_dimensions from zipline.utils.numpy_utils import as_column, isnat from zipline.utils.pandas_utils import timedelta_to_integral_seconds from zipline.utils.sentinel import sentinel -import numpy as np -from numpy import float64 - EPOCH = pd.Timestamp(0, tz="UTC") @@ -268,8 +258,8 @@ def make_trade_data_for_asset_info( trade_data = {} sids = asset_info.index - price_sid_deltas = np.arange(len(sids), dtype=float64) * price_step_by_sid - price_date_deltas = np.arange(len(dates), dtype=float64) * price_step_by_date + price_sid_deltas = np.arange(len(sids), dtype=np.float64) * price_step_by_sid + price_date_deltas = np.arange(len(dates), dtype=np.float64) * price_step_by_date prices = (price_sid_deltas + as_column(price_date_deltas)) + price_start volume_sid_deltas = np.arange(len(sids)) * volume_step_by_sid @@ -728,15 +718,6 @@ def get_spot_value(self, asset, field, dt, data_frequency): # otherwise just return a fixed value return int(asset) - # XXX: These aren't actually the methods that are used by the superclasses, - # so these don't do anything, and this class will likely produce unexpected - # results for history(). - def _get_daily_window_for_sid(self, asset, field, days_in_window, extra_slot=True): - return np.arange(days_in_window, dtype=np.float64) - - def _get_minute_window_for_asset(self, asset, field, minutes_for_window): - return np.arange(minutes_for_window, dtype=np.float64) - class tmp_assets_db: """Create a temporary assets sqlite database. @@ -775,7 +756,7 @@ def __init__(self, url="sqlite:///:memory:", equities=_default_equities, **frame self._eng = None # set in enter and exit def __enter__(self): - self._eng = eng = create_engine(self._url) + self._eng = eng = create_engine(self._url, future=False) AssetDBWriter(eng).write(**self._frames) return eng diff --git a/src/zipline/testing/fixtures.py b/src/zipline/testing/fixtures.py index 819c44064c..610ea1469f 100644 --- a/src/zipline/testing/fixtures.py +++ b/src/zipline/testing/fixtures.py @@ -64,7 +64,7 @@ HDF5DailyBarWriter, MultiCountryDailyBarReader, ) -from ..data.minute_bars import ( +from ..data.bcolz_minute_bars import ( BcolzMinuteBarReader, BcolzMinuteBarWriter, US_EQUITIES_MINUTES_PER_DAY, diff --git a/src/zipline/utils/sqlite_utils.py b/src/zipline/utils/sqlite_utils.py index 3eba2ee301..6ed9c3bf27 100644 --- a/src/zipline/utils/sqlite_utils.py +++ b/src/zipline/utils/sqlite_utils.py @@ -42,7 +42,7 @@ def check_and_create_connection(path, require_exists): def check_and_create_engine(path, require_exists): if require_exists: verify_sqlite_path_exists(path) - return sa.create_engine("sqlite:///" + path) + return sa.create_engine("sqlite:///" + path, future=False) def coerce_string_to_conn(require_exists): diff --git a/tests/__init__.py b/tests/__init__.py index d652aae469..e69de29bb2 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +0,0 @@ -from zipline import setup, teardown # noqa For nosetests diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000000..185dd385b3 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,174 @@ +import warnings +import pandas as pd +import pytest +from zipline.utils.calendar_utils import get_calendar +import sqlalchemy as sa + +from zipline.assets import ( + AssetDBWriter, + AssetFinder, + Equity, + Future, +) + + +DEFAULT_DATE_BOUNDS = { + "START_DATE": pd.Timestamp("2006-01-03", tz="utc"), + "END_DATE": pd.Timestamp("2006-12-29", tz="utc"), +} + + +@pytest.fixture(scope="function") +def sql_db(request): + url = "sqlite:///:memory:" + request.cls.engine = sa.create_engine(url, future=False) + yield request.cls.engine + request.cls.engine.dispose() + request.cls.engine = None + + +@pytest.fixture(scope="class") +def sql_db_class(request): + url = "sqlite:///:memory:" + request.cls.engine = sa.create_engine(url, future=False) + yield request.cls.engine + request.cls.engine.dispose() + request.cls.engine = None + + +@pytest.fixture(scope="function") +def empty_assets_db(sql_db, request): + AssetDBWriter(sql_db).write(None) + request.cls.metadata = sa.MetaData(sql_db) + request.cls.metadata.reflect(bind=sql_db) + + +@pytest.fixture(scope="class") +def with_trading_calendars(request): + """ + fixture providing cls.trading_calendar, + cls.all_trading_calendars, cls.trading_calendar_for_asset_type as a + class-level fixture. + + - `cls.trading_calendar` is populated with a default of the nyse trading + calendar for compatibility with existing tests + - `cls.all_trading_calendars` is populated with the trading calendars + keyed by name, + - `cls.trading_calendar_for_asset_type` is populated with the trading + calendars keyed by the asset type which uses the respective calendar. + + Attributes + ---------- + TRADING_CALENDAR_STRS : iterable + iterable of identifiers of the calendars to use. + TRADING_CALENDAR_FOR_ASSET_TYPE : dict + A dictionary which maps asset type names to the calendar associated + with that asset type. + """ + + request.cls.TRADING_CALENDAR_STRS = ("NYSE",) + request.cls.TRADING_CALENDAR_FOR_ASSET_TYPE = {Equity: "NYSE", Future: "us_futures"} + # For backwards compatibility, exisitng tests and fixtures refer to + # `trading_calendar` with the assumption that the value is the NYSE + # calendar. + request.cls.TRADING_CALENDAR_PRIMARY_CAL = "NYSE" + + request.cls.trading_calendars = {} + # Silence `pandas.errors.PerformanceWarning: Non-vectorized DateOffset + # being applied to Series or DatetimeIndex` in trading calendar + with warnings.catch_warnings(): + warnings.simplefilter("ignore", pd.errors.PerformanceWarning) + for cal_str in set(request.cls.TRADING_CALENDAR_STRS) | { + request.cls.TRADING_CALENDAR_PRIMARY_CAL + }: + # Set name to allow aliasing. + calendar = get_calendar(cal_str) + setattr(request.cls, "{0}_calendar".format(cal_str.lower()), calendar) + request.cls.trading_calendars[cal_str] = calendar + + type_to_cal = request.cls.TRADING_CALENDAR_FOR_ASSET_TYPE.items() + for asset_type, cal_str in type_to_cal: + calendar = get_calendar(cal_str) + request.cls.trading_calendars[asset_type] = calendar + + request.cls.trading_calendar = request.cls.trading_calendars[ + request.cls.TRADING_CALENDAR_PRIMARY_CAL + ] + + +@pytest.fixture(scope="class") +def set_trading_calendar(): + TRADING_CALENDAR_STRS = ("NYSE",) + TRADING_CALENDAR_FOR_ASSET_TYPE = {Equity: "NYSE", Future: "us_futures"} + # For backwards compatibility, exisitng tests and fixtures refer to + # `trading_calendar` with the assumption that the value is the NYSE + # calendar. + TRADING_CALENDAR_PRIMARY_CAL = "NYSE" + + trading_calendars = {} + # Silence `pandas.errors.PerformanceWarning: Non-vectorized DateOffset + # being applied to Series or DatetimeIndex` in trading calendar + with warnings.catch_warnings(): + warnings.simplefilter("ignore", pd.errors.PerformanceWarning) + for cal_str in set(TRADING_CALENDAR_STRS) | {TRADING_CALENDAR_PRIMARY_CAL}: + # Set name to allow aliasing. + calendar = get_calendar(cal_str) + # setattr(request.cls, "{0}_calendar".format(cal_str.lower()), calendar) + trading_calendars[cal_str] = calendar + + type_to_cal = TRADING_CALENDAR_FOR_ASSET_TYPE.items() + for asset_type, cal_str in type_to_cal: + calendar = get_calendar(cal_str) + trading_calendars[asset_type] = calendar + + return trading_calendars[TRADING_CALENDAR_PRIMARY_CAL] + + +@pytest.fixture(scope="class") +def with_asset_finder(sql_db_class): + def asset_finder(**kwargs): + AssetDBWriter(sql_db_class).write(**kwargs) + return AssetFinder(sql_db_class) + + return asset_finder + + +@pytest.fixture(scope="class") +def with_benchmark_returns(request): + from zipline.testing.fixtures import ( + read_checked_in_benchmark_data, + STATIC_BENCHMARK_PATH, + ) + + START_DATE = DEFAULT_DATE_BOUNDS["START_DATE"].date() + END_DATE = DEFAULT_DATE_BOUNDS["END_DATE"].date() + + benchmark_returns = read_checked_in_benchmark_data() + + # Zipline ordinarily uses cached benchmark returns data, but when + # running the zipline tests this cache is not always updated to include + # the appropriate dates required by both the futures and equity + # calendars. In order to create more reliable and consistent data + # throughout the entirety of the tests, we read static benchmark + # returns files from source. If a test using this fixture attempts to + # run outside of the static date range of the csv files, raise an + # exception warning the user to either update the csv files in source + # or to use a date range within the current bounds. + static_start_date = benchmark_returns.index[0].date() + static_end_date = benchmark_returns.index[-1].date() + warning_message = ( + "The WithBenchmarkReturns fixture uses static data between " + "{static_start} and {static_end}. To use a start and end date " + "of {given_start} and {given_end} you will have to update the " + "file in {benchmark_path} to include the missing dates.".format( + static_start=static_start_date, + static_end=static_end_date, + given_start=START_DATE, + given_end=END_DATE, + benchmark_path=STATIC_BENCHMARK_PATH, + ) + ) + if START_DATE < static_start_date or END_DATE > static_end_date: + raise AssertionError(warning_message) + + request.cls.BENCHMARK_RETURNS = benchmark_returns diff --git a/tests/data/bundles/test_core.py b/tests/data/bundles/test_core.py index c6566f0856..a7b25cbd06 100644 --- a/tests/data/bundles/test_core.py +++ b/tests/data/bundles/test_core.py @@ -337,7 +337,8 @@ def bundle_ingest_create_writers( to_bundle_ingest_dirname(ingestions[0]), # most recent self.environ, version, - ) + ), + future=False, ) metadata = sa.MetaData() metadata.reflect(eng) @@ -489,25 +490,19 @@ def test_clean_before_after(self): first }, "directory should not have changed (after)" - assert ( - self.clean( - "bundle", - before=self._ts_of_run(first) + _1_ns, - environ=self.environ, - ) - == {first} - ) + assert self.clean( + "bundle", + before=self._ts_of_run(first) + _1_ns, + environ=self.environ, + ) == {first} assert self._list_bundle() == set(), "directory now be empty (before)" second = self._empty_ingest() - assert ( - self.clean( - "bundle", - after=self._ts_of_run(second) - _1_ns, - environ=self.environ, - ) - == {second} - ) + assert self.clean( + "bundle", + after=self._ts_of_run(second) - _1_ns, + environ=self.environ, + ) == {second} assert self._list_bundle() == set(), "directory now be empty (after)" @@ -523,15 +518,12 @@ def test_clean_before_after(self): sixth, }, "larger set of ingestions did no happen correctly" - assert ( - self.clean( - "bundle", - before=self._ts_of_run(fourth), - after=self._ts_of_run(fifth), - environ=self.environ, - ) - == {third, sixth} - ) + assert self.clean( + "bundle", + before=self._ts_of_run(fourth), + after=self._ts_of_run(fifth), + environ=self.environ, + ) == {third, sixth} assert self._list_bundle() == { fourth, diff --git a/tests/data/bundles/test_csvdir.py b/tests/data/bundles/test_csvdir.py index 699c2b7f1b..3815fa6894 100644 --- a/tests/data/bundles/test_csvdir.py +++ b/tests/data/bundles/test_csvdir.py @@ -291,8 +291,8 @@ def test_bundle(self): sessions = self.calendar.all_sessions actual = bundle.equity_daily_bar_reader.load_raw_arrays( self.columns, - sessions[sessions.get_loc(self.asset_start, "bfill")], - sessions[sessions.get_loc(self.asset_end, "ffill")], + sessions[sessions.get_indexer([self.asset_start], "bfill")[0]], + sessions[sessions.get_indexer([self.asset_end], "ffill")[0]], sids, ) diff --git a/tests/data/bundles/test_quandl.py b/tests/data/bundles/test_quandl.py index 98e09b72f1..86dff09427 100644 --- a/tests/data/bundles/test_quandl.py +++ b/tests/data/bundles/test_quandl.py @@ -69,7 +69,9 @@ def pricing(): yield vs # the first index our written data will appear in the files on disk - start_idx = self.calendar.all_sessions.get_loc(self.start_date, "ffill") + 1 + start_idx = ( + self.calendar.all_sessions.get_indexer([self.start_date], "ffill")[0] + 1 + ) # convert an index into the raw dataframe into an index into the # final data @@ -220,8 +222,8 @@ def test_bundle(self): sessions = self.calendar.all_sessions actual = bundle.equity_daily_bar_reader.load_raw_arrays( self.columns, - sessions[sessions.get_loc(self.start_date, "bfill")], - sessions[sessions.get_loc(self.end_date, "ffill")], + sessions[sessions.get_indexer([self.start_date], "bfill")[0]], + sessions[sessions.get_indexer([self.end_date], "ffill")[0]], sids, ) expected_pricing, expected_adjustments = self._expected_data( diff --git a/tests/data/test_minute_bars.py b/tests/data/test_minute_bars.py index 97a7fdaab2..c033c0d9e7 100644 --- a/tests/data/test_minute_bars.py +++ b/tests/data/test_minute_bars.py @@ -21,7 +21,7 @@ from unittest import skip from zipline.data.bar_reader import NoDataForSid, NoDataOnDate -from zipline.data.minute_bars import ( +from zipline.data.bcolz_minute_bars import ( BcolzMinuteBarMetadata, BcolzMinuteBarWriter, BcolzMinuteBarReader, diff --git a/tests/finance/test_commissions.py b/tests/finance/test_commissions.py index d23a7f6f1c..5aa3e33e2d 100644 --- a/tests/finance/test_commissions.py +++ b/tests/finance/test_commissions.py @@ -1,7 +1,8 @@ from textwrap import dedent +import pandas as pd +import pytest from parameterized import parameterized -from pandas import DataFrame from zipline.assets import Equity, Future from zipline.errors import IncompatibleCommissionModel @@ -18,29 +19,64 @@ from zipline.finance.order import Order from zipline.finance.transaction import Transaction from zipline.testing import ZiplineTestCase -from zipline.testing.fixtures import WithAssetFinder, WithMakeAlgo -import pytest - +from zipline.testing.fixtures import WithMakeAlgo + + +@pytest.fixture(scope="class") +def set_test_commission_unit(request, with_asset_finder): + ASSET_FINDER_COUNTRY_CODE = "??" + + START_DATE = pd.Timestamp("2006-01-03", tz="utc") + END_DATE = pd.Timestamp("2006-12-29", tz="utc") + + equities = pd.DataFrame.from_dict( + { + 1: { + "symbol": "A", + "start_date": START_DATE, + "end_date": END_DATE + pd.Timedelta(days=1), + "exchange": "TEST", + }, + 2: { + "symbol": "B", + "start_date": START_DATE, + "end_date": END_DATE + pd.Timedelta(days=1), + "exchange": "TEST", + }, + }, + orient="index", + ) -class CommissionUnitTests(WithAssetFinder, ZiplineTestCase): - ASSET_FINDER_EQUITY_SIDS = 1, 2 + futures = pd.DataFrame( + { + "sid": [1000, 1001], + "root_symbol": ["CL", "FV"], + "symbol": ["CLF07", "FVF07"], + "start_date": [START_DATE, START_DATE], + "end_date": [END_DATE, END_DATE], + "notice_date": [END_DATE, END_DATE], + "expiration_date": [END_DATE, END_DATE], + "multiplier": [500, 500], + "exchange": ["CMES", "CMES"], + } + ) - @classmethod - def make_futures_info(cls): - return DataFrame( + exchange_names = [df["exchange"] for df in (futures, equities) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( { - "sid": [1000, 1001], - "root_symbol": ["CL", "FV"], - "symbol": ["CLF07", "FVF07"], - "start_date": [cls.START_DATE, cls.START_DATE], - "end_date": [cls.END_DATE, cls.END_DATE], - "notice_date": [cls.END_DATE, cls.END_DATE], - "expiration_date": [cls.END_DATE, cls.END_DATE], - "multiplier": [500, 500], - "exchange": ["CMES", "CMES"], + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, } ) + request.cls.asset_finder = with_asset_finder( + **dict(equities=equities, futures=futures, exchanges=exchanges) + ) + + +@pytest.mark.usefixtures("set_test_commission_unit") +class TestCommissionUnit: def generate_order_and_txns(self, sid, order_amount, fill_amounts): asset1 = self.asset_finder.retrieve_asset(sid) @@ -341,7 +377,7 @@ def handle_data(context, data): @classmethod def make_futures_info(cls): - return DataFrame( + return pd.DataFrame( { "sid": [1000, 1001], "root_symbol": ["CL", "FV"], @@ -362,7 +398,7 @@ def make_equity_daily_bar_data(cls, country_code, sids): cls.END_DATE, ) for sid in sids: - yield sid, DataFrame( + yield sid, pd.DataFrame( index=sessions, data={ "open": 10.0, diff --git a/tests/finance/test_risk.py b/tests/finance/test_risk.py index cc3748ec81..5fd480f9a3 100644 --- a/tests/finance/test_risk.py +++ b/tests/finance/test_risk.py @@ -14,15 +14,14 @@ # limitations under the License. import datetime -import pandas as pd -import numpy as np -from zipline.utils import factory - -from zipline.finance.trading import SimulationParameters -import zipline.testing.fixtures as zf +import numpy as np +import pandas as pd +import pytest from zipline.finance.metrics import _ClassicRiskMetrics as ClassicRiskMetrics +from zipline.finance.trading import SimulationParameters +from zipline.utils import factory RETURNS_BASE = 0.01 RETURNS = [RETURNS_BASE] * 251 @@ -39,28 +38,33 @@ ] -class TestRisk(zf.WithBenchmarkReturns, zf.ZiplineTestCase): - def init_instance_fixtures(self): - super(TestRisk, self).init_instance_fixtures() - self.start_session = pd.Timestamp("2006-01-01", tz="UTC") - self.end_session = self.trading_calendar.minute_to_session_label( - pd.Timestamp("2006-12-31", tz="UTC"), direction="previous" - ) - self.sim_params = SimulationParameters( - start_session=self.start_session, - end_session=self.end_session, - trading_calendar=self.trading_calendar, - ) - self.algo_returns = factory.create_returns_from_list(RETURNS, self.sim_params) - self.benchmark_returns = factory.create_returns_from_list( - BENCHMARK, self.sim_params - ) - self.metrics = ClassicRiskMetrics.risk_report( - algorithm_returns=self.algo_returns, - benchmark_returns=self.benchmark_returns, - algorithm_leverages=pd.Series(0.0, index=self.algo_returns.index), - ) - +@pytest.fixture(scope="class") +def set_test_risk(request, set_trading_calendar): + request.cls.trading_calendar = set_trading_calendar + request.cls.start_session = pd.Timestamp("2006-01-01", tz="UTC") + request.cls.end_session = request.cls.trading_calendar.minute_to_session_label( + pd.Timestamp("2006-12-31", tz="UTC"), direction="previous" + ) + request.cls.sim_params = SimulationParameters( + start_session=request.cls.start_session, + end_session=request.cls.end_session, + trading_calendar=request.cls.trading_calendar, + ) + request.cls.algo_returns = factory.create_returns_from_list( + RETURNS, request.cls.sim_params + ) + request.cls.benchmark_returns = factory.create_returns_from_list( + BENCHMARK, request.cls.sim_params + ) + request.cls.metrics = ClassicRiskMetrics.risk_report( + algorithm_returns=request.cls.algo_returns, + benchmark_returns=request.cls.benchmark_returns, + algorithm_leverages=pd.Series(0.0, index=request.cls.algo_returns.index), + ) + + +@pytest.mark.usefixtures("set_test_risk", "with_benchmark_returns") +class TestRisk: def test_factory(self): returns = [0.1] * 100 r_objects = factory.create_returns_from_list(returns, self.sim_params) diff --git a/tests/metrics/test_core.py b/tests/metrics/test_core.py index afb3c478c5..226869a64e 100644 --- a/tests/metrics/test_core.py +++ b/tests/metrics/test_core.py @@ -1,6 +1,8 @@ import re -import pytest from collections import namedtuple + +import pytest + from zipline.finance.metrics.core import _make_metrics_set_core from zipline.testing.predicates import assert_equal from zipline.utils.compat import mappingproxy diff --git a/tests/pipeline/base.py b/tests/pipeline/base.py index 3dcd8ff585..935b6d6c26 100644 --- a/tests/pipeline/base.py +++ b/tests/pipeline/base.py @@ -2,11 +2,9 @@ Base class for Pipeline API unit tests. """ import numpy as np -from numpy import arange, prod -from pandas import DataFrame, Timestamp +import pandas as pd from zipline.lib.labelarray import LabelArray -from zipline.utils.compat import wraps from zipline.pipeline import ExecutionPlan from zipline.pipeline.domain import US_EQUITIES from zipline.pipeline.engine import SimplePipelineEngine @@ -18,7 +16,7 @@ WithTradingSessions, ZiplineTestCase, ) - +from zipline.utils.compat import wraps from zipline.utils.functional import dzip_exact from zipline.utils.pandas_utils import explode @@ -56,8 +54,8 @@ def method(self, *args, **kwargs): class BaseUSEquityPipelineTestCase( WithTradingSessions, WithAssetFinder, ZiplineTestCase ): - START_DATE = Timestamp("2014", tz="UTC") - END_DATE = Timestamp("2014-12-31", tz="UTC") + START_DATE = pd.Timestamp("2014", tz="UTC") + END_DATE = pd.Timestamp("2014-12-31", tz="UTC") ASSET_FINDER_EQUITY_SIDS = list(range(20)) @classmethod @@ -155,7 +153,7 @@ def build_mask(self, array): array. """ ndates, nassets = array.shape - return DataFrame( + return pd.DataFrame( array, # Use the **last** N dates rather than the first N so that we have # space for lookbacks. @@ -169,7 +167,7 @@ def arange_data(self, shape, dtype=np.float64): """ Build a block of testing data from numpy.arange. """ - return arange(prod(shape), dtype=dtype).reshape(shape) + return np.arange(np.prod(shape), dtype=dtype).reshape(shape) @with_default_shape def randn_data(self, seed, shape): diff --git a/tests/pipeline/test_engine.py b/tests/pipeline/test_engine.py index 68da21ff3a..e433e9d559 100644 --- a/tests/pipeline/test_engine.py +++ b/tests/pipeline/test_engine.py @@ -633,7 +633,7 @@ def test_instance_of_factor_with_multiple_outputs(self): expected_values, index=dates, columns=assets, - dtype=np.float64, + # dtype=np.float64, ) multiple_outputs = MultipleOutputs() @@ -961,11 +961,11 @@ def write_nans(self, df): min_, max_ = index[[0, -1]] for asset in df.columns: if asset.start_date >= min_: - start = index.get_loc(asset.start_date, method="bfill") + start = index.get_indexer([asset.start_date], method="bfill")[0] # +1 to overwrite start_date: df.iloc[: start + 1, df.columns.get_loc(asset)] = np.nan if asset.end_date <= max_: - end = index.get_loc(asset.end_date) + end = index.get_indexer([asset.end_date])[0] # +1 to *not* overwrite end_date: df.iloc[end + 1 :, df.columns.get_loc(asset)] = np.nan diff --git a/tests/pipeline/test_events.py b/tests/pipeline/test_events.py index c14cfdc8b1..7edfb60035 100644 --- a/tests/pipeline/test_events.py +++ b/tests/pipeline/test_events.py @@ -1,34 +1,24 @@ """ Tests for setting up an EventsLoader. """ +import re from datetime import time from itertools import product from unittest import skipIf -import pytest -import re import numpy as np import pandas as pd +import pytest import pytz from zipline.pipeline import Pipeline, SimplePipelineEngine -from zipline.pipeline.common import ( - EVENT_DATE_FIELD_NAME, - TS_FIELD_NAME, - SID_FIELD_NAME, -) -from zipline.pipeline.data import DataSet, Column +from zipline.pipeline.common import EVENT_DATE_FIELD_NAME, SID_FIELD_NAME, TS_FIELD_NAME +from zipline.pipeline.data import Column, DataSet from zipline.pipeline.domain import US_EQUITIES, EquitySessionDomain from zipline.pipeline.loaders.events import EventsLoader -from zipline.pipeline.loaders.utils import ( - next_event_indexer, - previous_event_indexer, -) +from zipline.pipeline.loaders.utils import next_event_indexer, previous_event_indexer from zipline.testing import ZiplineTestCase -from zipline.testing.fixtures import ( - WithAssetFinder, - WithTradingSessions, -) +from zipline.testing.fixtures import WithAssetFinder, WithTradingSessions from zipline.testing.predicates import assert_equal from zipline.utils.numpy_utils import ( categorical_dtype, @@ -102,7 +92,7 @@ def make_null_event_date_events(all_sids, timestamp): { "sid": all_sids, "timestamp": timestamp, - "event_date": pd.Timestamp("NaT"), + "event_date": pd.NaT, "float": -9999.0, "int": -9999, "datetime": pd.Timestamp("1980"), diff --git a/tests/pipeline/test_factor.py b/tests/pipeline/test_factor.py index a2fb637ed0..aa969f2712 100644 --- a/tests/pipeline/test_factor.py +++ b/tests/pipeline/test_factor.py @@ -1,59 +1,44 @@ """ Tests for Factor terms. """ +import re from functools import partial from itertools import product -from parameterized import parameterized from unittest import skipIf -from toolz import compose import numpy as np +import pandas as pd +import pytest from numpy import nan from numpy.random import randn, seed -import pandas as pd +from parameterized import parameterized from scipy.stats.mstats import winsorize as scipy_winsorize +from toolz import compose from zipline.errors import BadPercentileBounds, UnknownRankMethod from zipline.lib.labelarray import LabelArray -from zipline.lib.rank import masked_rankdata_2d from zipline.lib.normalize import naive_grouped_rowwise_apply as grouped_apply +from zipline.lib.rank import masked_rankdata_2d from zipline.pipeline import Classifier, Factor, Filter, Pipeline -from zipline.pipeline.data import DataSet, Column, EquityPricing -from zipline.pipeline.factors import ( - CustomFactor, - DailyReturns, - Returns, - PercentChange, -) -from zipline.pipeline.factors.factor import ( - summary_funcs, - winsorize as zp_winsorize, -) -from zipline.testing import ( - check_allclose, - check_arrays, - parameter_space, - permute_rows, -) -from zipline.testing.fixtures import ( - WithUSEquityPricingPipelineEngine, - ZiplineTestCase, -) +from zipline.pipeline.data import Column, DataSet, EquityPricing +from zipline.pipeline.factors import CustomFactor, DailyReturns, PercentChange, Returns +from zipline.pipeline.factors.factor import summary_funcs +from zipline.pipeline.factors.factor import winsorize as zp_winsorize +from zipline.testing import check_allclose, check_arrays, parameter_space, permute_rows +from zipline.testing.fixtures import WithUSEquityPricingPipelineEngine, ZiplineTestCase from zipline.testing.predicates import assert_equal +from zipline.utils.math_utils import nanmean, nanstd from zipline.utils.numpy_utils import ( + NaTns, as_column, categorical_dtype, datetime64ns_dtype, float64_dtype, int64_dtype, - NaTns, ) -from zipline.utils.math_utils import nanmean, nanstd from zipline.utils.pandas_utils import new_pandas, skip_pipeline_new_pandas from .base import BaseUSEquityPipelineTestCase -import pytest -import re class F(Factor): diff --git a/tests/pipeline/test_pipeline_algo.py b/tests/pipeline/test_pipeline_algo.py index 9e4dce022c..1e7702c374 100644 --- a/tests/pipeline/test_pipeline_algo.py +++ b/tests/pipeline/test_pipeline_algo.py @@ -1,11 +1,7 @@ """ Tests for Algorithms using the Pipeline API. """ -from os.path import ( - dirname, - join, - realpath, -) +from pathlib import Path from parameterized import parameterized import numpy as np @@ -44,11 +40,9 @@ from zipline.utils.pandas_utils import normalize_date import pytest -TEST_RESOURCE_PATH = join( - dirname(dirname(realpath(__file__))), # zipline_repo/tests - "resources", - "pipeline_inputs", -) + +# zipline_repo/tests/resources/pipeline_inputs +TEST_RESOURCE_PATH = Path(__file__).parent.parent / "resources" / "pipeline_inputs" def rolling_vwap(df, length): @@ -148,7 +142,7 @@ def init_class_fixtures(cls): "sid": cls.split_asset.sid, "value": cls.split_ratio, "kind": MULTIPLY, - "start_date": pd.Timestamp("NaT"), + "start_date": pd.NaT, "end_date": cls.split_date, "apply_date": cls.split_date, } @@ -447,9 +441,9 @@ class PipelineAlgorithmTestCase( @classmethod def make_equity_daily_bar_data(cls, country_code, sids): resources = { - cls.AAPL: join(TEST_RESOURCE_PATH, "AAPL.csv"), - cls.MSFT: join(TEST_RESOURCE_PATH, "MSFT.csv"), - cls.BRK_A: join(TEST_RESOURCE_PATH, "BRK-A.csv"), + cls.AAPL: TEST_RESOURCE_PATH / "AAPL.csv", + cls.MSFT: TEST_RESOURCE_PATH / "MSFT.csv", + cls.BRK_A: TEST_RESOURCE_PATH / "BRK-A.csv", } cls.raw_data = raw_data = { asset: pd.read_csv(path, parse_dates=["day"]).set_index("day") diff --git a/tests/pipeline/test_statistical.py b/tests/pipeline/test_statistical.py index 7efbde816c..965deb2def 100644 --- a/tests/pipeline/test_statistical.py +++ b/tests/pipeline/test_statistical.py @@ -1,17 +1,13 @@ """ Tests for statistical pipeline terms. """ -import re - import numpy as np import pandas as pd -import pytest -from empyrical.stats import beta_aligned as empyrical_beta -from numpy import nan from pandas.testing import assert_frame_equal from scipy.stats import linregress, pearsonr, spearmanr -import zipline.testing.fixtures as zf +from empyrical.stats import beta_aligned as empyrical_beta + from zipline.assets import Equity, ExchangeInfo from zipline.errors import IncompatibleTerms, NonExistentAssetInTimeFrame from zipline.pipeline import CustomFactor, Pipeline @@ -40,6 +36,7 @@ make_cascading_boolean_array, parameter_space, ) +import zipline.testing.fixtures as zf from zipline.testing.predicates import assert_equal from zipline.utils.numpy_utils import ( as_column, @@ -47,83 +44,117 @@ datetime64ns_dtype, float64_dtype, ) +import pytest +import re -class StatisticalBuiltInsTestCase( - zf.WithAssetFinder, zf.WithTradingCalendars, zf.ZiplineTestCase -): +@pytest.fixture(scope="class") +def set_test_statistical_built_ins(request, with_asset_finder, with_trading_calendars): sids = ASSET_FINDER_EQUITY_SIDS = pd.Index([1, 2, 3], dtype="int64") START_DATE = pd.Timestamp("2015-01-31", tz="UTC") END_DATE = pd.Timestamp("2015-03-01", tz="UTC") ASSET_FINDER_EQUITY_SYMBOLS = ("A", "B", "C") ASSET_FINDER_COUNTRY_CODE = "US" - @classmethod - def init_class_fixtures(cls): - super(StatisticalBuiltInsTestCase, cls).init_class_fixtures() - - day = cls.trading_calendar.day - cls.dates = dates = pd.date_range( - "2015-02-01", - "2015-02-28", - freq=day, - tz="UTC", - ) - - # Using these start and end dates because they are a contigous span of - # 5 days (Monday - Friday) and they allow for plenty of days to look - # back on when computing correlations and regressions. - cls.start_date_index = start_date_index = 14 - cls.end_date_index = end_date_index = 18 - cls.pipeline_start_date = dates[start_date_index] - cls.pipeline_end_date = dates[end_date_index] - cls.num_days = num_days = end_date_index - start_date_index + 1 - - sids = cls.sids - cls.assets = assets = cls.asset_finder.retrieve_all(sids) - cls.my_asset_column = my_asset_column = 0 - cls.my_asset = assets[my_asset_column] - cls.num_assets = num_assets = len(assets) - - cls.raw_data = raw_data = pd.DataFrame( - data=np.arange(len(dates) * len(sids), dtype=float64_dtype).reshape( - len(dates), - len(sids), - ), - index=dates, - columns=assets, - ) - - # Using mock 'close' data here because the correlation and regression - # built-ins use USEquityPricing.close as the input to their `Returns` - # factors. Since there is no way to change that when constructing an - # instance of these built-ins, we need to test with mock 'close' data - # to most accurately reflect their true behavior and results. - close_loader = DataFrameLoader(USEquityPricing.close, raw_data) - - cls.run_pipeline = SimplePipelineEngine( - {USEquityPricing.close: close_loader}.__getitem__, - cls.asset_finder, - default_domain=US_EQUITIES, - ).run_pipeline - - cls.cascading_mask = AssetIDPlusDay() < (sids[-1] + dates[start_date_index].day) - cls.expected_cascading_mask_result = make_cascading_boolean_array( - shape=(num_days, num_assets), - ) - cls.alternating_mask = (AssetIDPlusDay() % 2).eq(0) - cls.expected_alternating_mask_result = make_alternating_boolean_array( - shape=(num_days, num_assets), - ) - cls.expected_no_mask_result = np.full( - shape=(num_days, num_assets), - fill_value=True, - dtype=bool_dtype, + equities = pd.DataFrame( + list( + zip( + ASSET_FINDER_EQUITY_SIDS, + ASSET_FINDER_EQUITY_SYMBOLS, + [ + START_DATE, + ] + * 3, + [ + END_DATE, + ] + * 3, + [ + "NYSE", + ] + * 3, + ) + ), + columns=["sid", "symbol", "start_date", "end_date", "exchange"], + ) + + exchange_names = [df["exchange"] for df in (equities,) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( + { + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, + } ) - # todo: figure out why this fails on CI - @parameter_space(returns_length=[2, 3], correlation_length=[3, 4]) - @pytest.mark.skip(reason="Sometimes fails on CI") + request.cls.asset_finder = with_asset_finder( + **dict(equities=equities, exchanges=exchanges) + ) + day = request.cls.trading_calendar.day + request.cls.dates = dates = pd.date_range( + "2015-02-01", + "2015-02-28", + freq=day, + tz="UTC", + ) + + # Using these start and end dates because they are a contigous span of + # 5 days (Monday - Friday) and they allow for plenty of days to look + # back on when computing correlations and regressions. + request.cls.start_date_index = start_date_index = 14 + request.cls.end_date_index = end_date_index = 18 + request.cls.pipeline_start_date = dates[start_date_index] + request.cls.pipeline_end_date = dates[end_date_index] + request.cls.num_days = num_days = end_date_index - start_date_index + 1 + + request.cls.assets = assets = request.cls.asset_finder.retrieve_all(sids) + request.cls.my_asset_column = my_asset_column = 0 + request.cls.my_asset = assets[my_asset_column] + request.cls.num_assets = num_assets = len(assets) + + request.cls.raw_data = raw_data = pd.DataFrame( + data=np.arange(len(dates) * len(sids), dtype=float64_dtype).reshape( + len(dates), + len(sids), + ), + index=dates, + columns=assets, + ) + + # Using mock 'close' data here because the correlation and regression + # built-ins use USEquityPricing.close as the input to their `Returns` + # factors. Since there is no way to change that when constructing an + # instance of these built-ins, we need to test with mock 'close' data + # to most accurately reflect their true behavior and results. + close_loader = DataFrameLoader(USEquityPricing.close, raw_data) + + request.cls.run_pipeline = SimplePipelineEngine( + {USEquityPricing.close: close_loader}.__getitem__, + request.cls.asset_finder, + default_domain=US_EQUITIES, + ).run_pipeline + + request.cls.cascading_mask = AssetIDPlusDay() < ( + sids[-1] + dates[start_date_index].day + ) + request.cls.expected_cascading_mask_result = make_cascading_boolean_array( + shape=(num_days, num_assets), + ) + request.cls.alternating_mask = (AssetIDPlusDay() % 2).eq(0) + request.cls.expected_alternating_mask_result = make_alternating_boolean_array( + shape=(num_days, num_assets), + ) + request.cls.expected_no_mask_result = np.full( + shape=(num_days, num_assets), + fill_value=True, + dtype=bool_dtype, + ) + + +@pytest.mark.usefixtures("set_test_statistical_built_ins") +class TestStatisticalBuiltIns: + @pytest.mark.parametrize("returns_length", [2, 3]) + @pytest.mark.parametrize("correlation_length", [3, 4]) def test_correlation_factors(self, returns_length, correlation_length): """ Tests for the built-in factors `RollingPearsonOfReturns` and @@ -191,12 +222,12 @@ def test_correlation_factors(self, returns_length, correlation_length): # On each day, calculate the expected correlation coefficients # between the asset we are interested in and each other asset. Each # correlation is calculated over `correlation_length` days. - expected_pearson_results = np.full_like(pearson_results, nan) - expected_spearman_results = np.full_like(spearman_results, nan) + expected_pearson_results = np.full_like(pearson_results, np.nan) + expected_spearman_results = np.full_like(spearman_results, np.nan) for day in range(num_days): todays_returns = returns_results.iloc[day : day + correlation_length] my_asset_returns = todays_returns.iloc[:, my_asset_column] - for asset, other_asset_returns in todays_returns.iteritems(): + for asset, other_asset_returns in todays_returns.items(): asset_column = int(asset) - 1 expected_pearson_results[day, asset_column] = pearsonr( my_asset_returns, @@ -208,20 +239,21 @@ def test_correlation_factors(self, returns_length, correlation_length): )[0] expected_pearson_results = pd.DataFrame( - data=np.where(expected_mask, expected_pearson_results, nan), + data=np.where(expected_mask, expected_pearson_results, np.nan), index=dates[start_date_index : end_date_index + 1], columns=assets, ) assert_frame_equal(pearson_results, expected_pearson_results) expected_spearman_results = pd.DataFrame( - data=np.where(expected_mask, expected_spearman_results, nan), + data=np.where(expected_mask, expected_spearman_results, np.nan), index=dates[start_date_index : end_date_index + 1], columns=assets, ) assert_frame_equal(spearman_results, expected_spearman_results) - @parameter_space(returns_length=[2, 3], regression_length=[3, 4]) + @pytest.mark.parametrize("returns_length", [2, 3]) + @pytest.mark.parametrize("regression_length", [3, 4]) def test_regression_of_returns_factor(self, returns_length, regression_length): """ Tests for the built-in factor `RollingLinearRegressionOfReturns`. @@ -272,7 +304,7 @@ def test_regression_of_returns_factor(self, returns_length, regression_length): output_results[output] = results[output].unstack() expected_output_results[output] = np.full_like( output_results[output], - nan, + np.nan, ) # Run a separate pipeline that calculates returns starting @@ -307,7 +339,7 @@ def test_regression_of_returns_factor(self, returns_length, regression_length): for output in outputs: output_result = output_results[output] expected_output_result = pd.DataFrame( - np.where(expected_mask, expected_output_results[output], nan), + np.where(expected_mask, expected_output_results[output], np.nan), index=dates[start_date_index : end_date_index + 1], columns=assets, ) @@ -641,9 +673,7 @@ def compute(self, today, assets, out): correlation_length=correlation_length, ) - # todo: figure out why this sometimes fails on CI @parameter_space(returns_length=[2, 3], regression_length=[3, 4]) - @pytest.mark.skip(reason="Sometimes fails on CI") def test_factor_regression_method(self, returns_length, regression_length): """ Ensure that `Factor.linear_regression` is consistent with the built-in @@ -796,8 +826,8 @@ def test_factor_correlation_methods_two_factors(self, correlation_length): # On each day, calculate the expected correlation coefficients # between each asset's 5 and 10 day rolling returns. Each correlation # is calculated over `correlation_length` days. - expected_pearson_results = np.full_like(pearson_results, nan) - expected_spearman_results = np.full_like(spearman_results, nan) + expected_pearson_results = np.full_like(pearson_results, np.nan) + expected_spearman_results = np.full_like(spearman_results, np.nan) for day in range(num_days): todays_returns_5 = returns_5_results.iloc[day : day + correlation_length] todays_returns_10 = returns_10_results.iloc[day : day + correlation_length] @@ -882,7 +912,7 @@ def test_factor_regression_method_two_factors(self, regression_length): output_results[output] = results[output].unstack() expected_output_results[output] = np.full_like( output_results[output], - nan, + np.nan, ) # Run a separate pipeline that calculates returns starting @@ -968,8 +998,8 @@ def test_nan_handling_matches_empyrical(self, seed, pct_dependent, pct_independe dependents = 1.0 + true_betas * independent + noise # Fill 20% of the input arrays with nans randomly. - dependents[rand.uniform(0, 1, dependents.shape) < pct_dependent] = nan - independent[independent > np.nanmean(independent)] = nan + dependents[rand.uniform(0, 1, dependents.shape) < pct_dependent] = np.nan + independent[independent > np.nanmean(independent)] = np.nan # Sanity check that we actually inserted some nans. # self.assertTrue(np.count_nonzero(np.isnan(dependents)) > 0) @@ -1009,7 +1039,7 @@ def test_produce_nans_when_too_much_missing_data(self, nan_offset): for allowed_missing in range(7): results = vectorized_beta(dependents, independent, allowed_missing) - for i, expected in enumerate(true_betas): + for i, _ in enumerate(true_betas): result = results[i] expect_nan = num_nans[i] > allowed_missing true_beta = true_betas[i] diff --git a/tests/resources/pipeline_inputs/generate.py b/tests/resources/pipeline_inputs/generate.py index 8c168086a0..986ecd0f3e 100644 --- a/tests/resources/pipeline_inputs/generate.py +++ b/tests/resources/pipeline_inputs/generate.py @@ -1,13 +1,10 @@ """ Quick and dirty script to generate test case inputs. """ -from os.path import ( - dirname, - join, -) +from pathlib import Path from pandas_datareader.data import DataReader -here = join(dirname(__file__)) +TESTPATH = Path(__file__).parent def main(): @@ -32,7 +29,7 @@ def main(): ) del data["Adj Close"] - dest = join(here, symbol + ".csv") + dest = TESTPATH / f"{symbol}.csv" print("Writing %s -> %s" % (symbol, dest)) data.to_csv(dest, index_label="day") diff --git a/tests/test_assets.py b/tests/test_assets.py index 53f066a74f..1a39bb512d 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -31,24 +31,23 @@ import pandas as pd import pytest import sqlalchemy as sa -from parameterized import parameterized -from toolz import valmap, concat +from toolz import concat, valmap from zipline.assets import ( Asset, - ExchangeInfo, - Equity, - Future, AssetDBWriter, AssetFinder, + Equity, + ExchangeInfo, + Future, ) from zipline.assets.asset_db_migrations import downgrade from zipline.assets.asset_db_schema import ASSET_DB_VERSION from zipline.assets.asset_writer import ( + SQLITE_MAX_VARIABLE_NUMBER, + _futures_defaults, check_version_info, write_version_info, - _futures_defaults, - SQLITE_MAX_VARIABLE_NUMBER, ) from zipline.assets.assets import OwnershipPeriod from zipline.assets.synthetic import ( @@ -57,6 +56,8 @@ make_simple_equity_info, ) from zipline.errors import ( + AssetDBImpossibleDowngrade, + AssetDBVersionError, EquitiesNotFound, FutureContractsNotFound, MultipleSymbolsFound, @@ -64,31 +65,21 @@ MultipleValuesFoundForField, MultipleValuesFoundForSid, NoValueForSid, - AssetDBVersionError, SameSymbolUsedAcrossCountries, SidsNotFound, SymbolNotFound, - AssetDBImpossibleDowngrade, ValueNotFoundForField, ) -from zipline.testing import ( - all_subindices, - empty_assets_db, - parameter_space, - powerset, - tmp_assets_db, - tmp_asset_finder, -) -from zipline.testing.fixtures import ( - WithAssetFinder, - ZiplineTestCase, - WithTradingCalendars, -) -from zipline.testing.predicates import assert_index_equal, assert_frame_equal +from zipline.testing import all_subindices, powerset, tmp_asset_finder, tmp_assets_db +from zipline.testing.predicates import assert_frame_equal, assert_index_equal -Case = namedtuple("Case", "finder inputs as_of country_code expected") +CASE = namedtuple("CASE", "finder inputs as_of country_code expected") +MINUTE = pd.Timedelta(minutes=1) -minute = pd.Timedelta(minutes=1) +if sys.platform == "win32": + DBS = ["sqlite"] +else: + DBS = ["sqlite", "postgresql"] def build_lookup_generic_cases(): @@ -193,7 +184,7 @@ def build_lookup_generic_cases(): with temp_db as assets_db: finder = AssetFinder(assets_db) - case = partial(Case, finder) + case = partial(CASE, finder) equities = finder.retrieve_all(range(5)) dupe_old, dupe_new, unique, dupe_us, dupe_ca = equities @@ -220,7 +211,7 @@ def build_lookup_generic_cases(): yield case("DUPLICATED_IN_US", dupe_old_start, country, dupe_old) yield case( "DUPLICATED_IN_US", - dupe_new_start - minute, + dupe_new_start - MINUTE, country, dupe_old, ) @@ -228,7 +219,7 @@ def build_lookup_generic_cases(): yield case("DUPLICATED_IN_US", dupe_new_start, country, dupe_new) yield case( "DUPLICATED_IN_US", - dupe_new_start + minute, + dupe_new_start + MINUTE, country, dupe_new, ) @@ -303,7 +294,7 @@ def build_lookup_generic_cases(): @pytest.fixture(scope="function") -def set_asset(request): +def set_test_asset(request): # Dynamically list the Asset properties we want to test. request.cls.asset_attrs = [ name @@ -336,7 +327,112 @@ def set_asset(request): ) -@pytest.mark.usefixtures("set_asset") +@pytest.fixture(scope="class") +def set_test_futures(request, with_asset_finder): + ASSET_FINDER_COUNTRY_CODE = "??" + futures = pd.DataFrame.from_dict( + { + 2468: { + "symbol": "OMH15", + "root_symbol": "OM", + "notice_date": pd.Timestamp("2014-01-20", tz="UTC"), + "expiration_date": pd.Timestamp("2014-02-20", tz="UTC"), + "auto_close_date": pd.Timestamp("2014-01-18", tz="UTC"), + "tick_size": 0.01, + "multiplier": 500.0, + "exchange": "TEST", + }, + 0: { + "symbol": "CLG06", + "root_symbol": "CL", + "start_date": pd.Timestamp("2005-12-01", tz="UTC"), + "notice_date": pd.Timestamp("2005-12-20", tz="UTC"), + "expiration_date": pd.Timestamp("2006-01-20", tz="UTC"), + "multiplier": 1.0, + "exchange": "TEST", + }, + }, + orient="index", + ) + + exchange_names = [df["exchange"] for df in (futures,) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( + { + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, + } + ) + + request.cls.asset_finder = with_asset_finder( + **dict(futures=futures, exchanges=exchanges) + ) + + +@pytest.fixture(scope="class") +def set_test_vectorized_symbol_lookup(request, with_asset_finder): + ASSET_FINDER_COUNTRY_CODE = "??" + T = partial(pd.Timestamp, tz="UTC") + + def asset(sid, symbol, start_date, end_date): + return dict( + sid=sid, + symbol=symbol, + start_date=T(start_date), + end_date=T(end_date), + exchange="NYSE", + ) + + records = [ + asset(1, "A", "2014-01-02", "2014-01-31"), + asset(2, "A", "2014-02-03", "2015-01-02"), + asset(3, "B", "2014-01-02", "2014-01-15"), + asset(4, "B", "2014-01-17", "2015-01-02"), + asset(5, "C", "2001-01-02", "2015-01-02"), + asset(6, "D", "2001-01-02", "2015-01-02"), + asset(7, "FUZZY", "2001-01-02", "2015-01-02"), + ] + equities = pd.DataFrame.from_records(records) + + exchange_names = [df["exchange"] for df in (equities,) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( + { + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, + } + ) + + request.cls.asset_finder = with_asset_finder( + **dict(equities=equities, exchanges=exchanges) + ) + + +# @pytest.fixture(scope="function") +# def set_test_write(request, tmp_path): +# request.cls.assets_db_path = path = os.path.join( +# str(tmp_path), +# "assets.db", +# ) +# request.cls.writer = AssetDBWriter(path) + + +@pytest.fixture(scope="function") +def set_test_write(request, sql_db): + request.cls.assets_db_path = sql_db + request.cls.writer = AssetDBWriter(sql_db) + + +@pytest.fixture(scope="function") +def asset_finder(sql_db): + def asset_finder(**kwargs): + AssetDBWriter(sql_db).write(**kwargs) + return AssetFinder(sql_db) + + return asset_finder + + +@pytest.mark.usefixtures("set_test_asset") class TestAsset: def test_asset_object(self): the_asset = Asset( @@ -420,66 +516,39 @@ def test_type_mismatch(self): "a" < self.asset3 -class TestFuture(WithAssetFinder, ZiplineTestCase): - @classmethod - def make_futures_info(cls): - return pd.DataFrame.from_dict( - { - 2468: { - "symbol": "OMH15", - "root_symbol": "OM", - "notice_date": pd.Timestamp("2014-01-20", tz="UTC"), - "expiration_date": pd.Timestamp("2014-02-20", tz="UTC"), - "auto_close_date": pd.Timestamp("2014-01-18", tz="UTC"), - "tick_size": 0.01, - "multiplier": 500.0, - "exchange": "TEST", - }, - 0: { - "symbol": "CLG06", - "root_symbol": "CL", - "start_date": pd.Timestamp("2005-12-01", tz="UTC"), - "notice_date": pd.Timestamp("2005-12-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-01-20", tz="UTC"), - "multiplier": 1.0, - "exchange": "TEST", - }, - }, - orient="index", - ) - - @classmethod - def init_class_fixtures(cls): - super(TestFuture, cls).init_class_fixtures() - cls.future = cls.asset_finder.lookup_future_symbol("OMH15") - cls.future2 = cls.asset_finder.lookup_future_symbol("CLG06") - +@pytest.mark.usefixtures("set_test_futures") +class TestFuture: def test_repr(self): - reprd = repr(self.future) + future_symbol = self.asset_finder.lookup_future_symbol("OMH15") + reprd = repr(future_symbol) assert "Future(2468 [OMH15])" == reprd def test_reduce(self): + future_symbol = self.asset_finder.lookup_future_symbol("OMH15") assert ( - pickle.loads(pickle.dumps(self.future)).to_dict() == self.future.to_dict() + pickle.loads(pickle.dumps(future_symbol)).to_dict() + == future_symbol.to_dict() ) def test_to_and_from_dict(self): - dictd = self.future.to_dict() + future_symbol = self.asset_finder.lookup_future_symbol("OMH15") + dictd = future_symbol.to_dict() for field in _futures_defaults.keys(): assert field in dictd from_dict = Future.from_dict(dictd) assert isinstance(from_dict, Future) - assert self.future == from_dict + assert future_symbol == from_dict def test_root_symbol(self): - assert "OM" == self.future.root_symbol + future_symbol = self.asset_finder.lookup_future_symbol("OMH15") + assert "OM" == future_symbol.root_symbol def test_lookup_future_symbol(self): """ Test the lookup_future_symbol method. """ - om = TestFuture.asset_finder.lookup_future_symbol("OMH15") + om = self.asset_finder.lookup_future_symbol("OMH15") assert om.sid == 2468 assert om.symbol == "OMH15" assert om.root_symbol == "OM" @@ -487,7 +556,7 @@ def test_lookup_future_symbol(self): assert om.expiration_date == pd.Timestamp("2014-02-20", tz="UTC") assert om.auto_close_date == pd.Timestamp("2014-01-18", tz="UTC") - cl = TestFuture.asset_finder.lookup_future_symbol("CLG06") + cl = self.asset_finder.lookup_future_symbol("CLG06") assert cl.sid == 0 assert cl.symbol == "CLG06" assert cl.root_symbol == "CL" @@ -496,32 +565,21 @@ def test_lookup_future_symbol(self): assert cl.expiration_date == pd.Timestamp("2006-01-20", tz="UTC") with pytest.raises(SymbolNotFound): - TestFuture.asset_finder.lookup_future_symbol("") + self.asset_finder.lookup_future_symbol("") with pytest.raises(SymbolNotFound): - TestFuture.asset_finder.lookup_future_symbol("#&?!") + self.asset_finder.lookup_future_symbol("#&?!") with pytest.raises(SymbolNotFound): - TestFuture.asset_finder.lookup_future_symbol("FOOBAR") + self.asset_finder.lookup_future_symbol("FOOBAR") with pytest.raises(SymbolNotFound): - TestFuture.asset_finder.lookup_future_symbol("XXX99") + self.asset_finder.lookup_future_symbol("XXX99") -class AssetFinderTestCase(WithTradingCalendars, ZiplineTestCase): - asset_finder_type = AssetFinder - - def write_assets(self, **kwargs): - self._asset_writer.write(**kwargs) - - def init_instance_fixtures(self): - super(AssetFinderTestCase, self).init_instance_fixtures() - - conn = self.enter_instance_context(empty_assets_db()) - self._asset_writer = AssetDBWriter(conn) - self.asset_finder = self.asset_finder_type(conn) - - def test_blocked_lookup_symbol_query(self): +@pytest.mark.usefixtures("with_trading_calendars") +class TestAssetFinder: + def test_blocked_lookup_symbol_query(self, asset_finder): # we will try to query for more variables than sqlite supports # to make sure we are properly chunking on the client side as_of = pd.Timestamp("2013-01-01", tz="UTC") @@ -540,11 +598,13 @@ def test_blocked_lookup_symbol_query(self): for sid in sids ] ) - self.write_assets(equities=frame) - assets = self.asset_finder.retrieve_equities(sids) + asset_finder = asset_finder(equities=frame) + # self.write_assets(equities=frame) + assets = asset_finder.retrieve_equities(sids) + # assets = self.asset_finder.retrieve_equities(sids) assert assets.keys() == set(sids) - def test_lookup_symbol_delimited(self): + def test_lookup_symbol_delimited(self, asset_finder): as_of = pd.Timestamp("2013-01-01", tz="UTC") frame = pd.DataFrame.from_records( [ @@ -559,12 +619,11 @@ def test_lookup_symbol_delimited(self): for i in range(3) ] ) - self.write_assets(equities=frame) - finder = self.asset_finder + finder = asset_finder(equities=frame) asset_0, asset_1, asset_2 = (finder.retrieve_asset(i) for i in range(3)) # we do it twice to catch caching bugs - for i in range(2): + for _ in range(2): with pytest.raises(SymbolNotFound): finder.lookup_symbol("TEST", as_of) with pytest.raises(SymbolNotFound): @@ -577,7 +636,7 @@ def test_lookup_symbol_delimited(self): for fuzzy_char in ["-", "/", "_", "."]: assert asset_1 == finder.lookup_symbol("TEST%s1" % fuzzy_char, as_of) - def test_lookup_symbol_fuzzy(self): + def test_lookup_symbol_fuzzy(self, asset_finder): metadata = pd.DataFrame.from_records( [ {"symbol": "PRTY_HRD", "exchange": "TEST"}, @@ -585,8 +644,7 @@ def test_lookup_symbol_fuzzy(self): {"symbol": "BRK_A", "exchange": "TEST"}, ] ) - self.write_assets(equities=metadata) - finder = self.asset_finder + finder = asset_finder(equities=metadata) dt = pd.Timestamp("2013-01-01", tz="UTC") # Try combos of looking up PRTYHRD with and without a time or fuzzy @@ -617,7 +675,7 @@ def test_lookup_symbol_fuzzy(self): assert 2 == finder.lookup_symbol("BRK_A", None, fuzzy=True) assert 2 == finder.lookup_symbol("BRK_A", dt, fuzzy=True) - def test_lookup_symbol_change_ticker(self): + def test_lookup_symbol_change_ticker(self, asset_finder): T = partial(pd.Timestamp, tz="utc") metadata = pd.DataFrame.from_records( [ @@ -654,8 +712,7 @@ def test_lookup_symbol_change_ticker(self): ], index=[0, 0, 1, 1], ) - self.write_assets(equities=metadata) - finder = self.asset_finder + finder = asset_finder(equities=metadata) # note: these assertions walk forward in time, starting at assertions # about ownership before the start_date and ending with assertions @@ -710,7 +767,7 @@ def test_lookup_symbol_change_ticker(self): assert A_result.symbol == "A" assert A_result.asset_name == "Asset A" - def test_lookup_symbol(self): + def test_lookup_symbol(self, asset_finder): # Incrementing by two so that start and end dates for each # generated Asset don't overlap (each Asset's end_date is the @@ -728,8 +785,7 @@ def test_lookup_symbol(self): for i, date in enumerate(dates) ] ) - self.write_assets(equities=df) - finder = self.asset_finder + finder = asset_finder(equities=df) for _ in range(2): # Run checks twice to test for caching bugs. with pytest.raises(SymbolNotFound): finder.lookup_symbol("NON_EXISTING", dates[0]) @@ -744,7 +800,7 @@ def test_lookup_symbol(self): assert result.symbol == "EXISTING" assert result.sid == i - def test_fail_to_write_overlapping_data(self): + def test_fail_to_write_overlapping_data(self, asset_finder): df = pd.DataFrame.from_records( [ { @@ -786,7 +842,7 @@ def test_fail_to_write_overlapping_data(self): " 3 2011-01-01 2012-01-01" ) with pytest.raises(ValueError, match=re.escape(expected_error_msg)): - self.write_assets(equities=df) + asset_finder(equities=df) def test_lookup_generic(self): """ @@ -795,7 +851,8 @@ def test_lookup_generic(self): cases = build_lookup_generic_cases() # Make sure we clean up temp resources in the generator if we don't # consume the whole thing because of a failure. - self.add_instance_callback(cases.close) + # Pytest has not instance call back DISABLED + # self.add_instance_callback(cases.close) for finder, inputs, reference_date, country, expected in cases: results, missing = finder.lookup_generic( inputs, @@ -805,16 +862,17 @@ def test_lookup_generic(self): assert results == expected assert missing == [] - def test_lookup_none_raises(self): + def test_lookup_none_raises(self, asset_finder): """ If lookup_symbol is vectorized across multiple symbols, and one of them is None, want to raise a TypeError. """ with pytest.raises(TypeError): - self.asset_finder.lookup_symbol(None, pd.Timestamp("2013-01-01")) + asset_finder = asset_finder(None) + asset_finder.lookup_symbol(None, pd.Timestamp("2013-01-01")) - def test_lookup_mult_are_one(self): + def test_lookup_mult_are_one(self, asset_finder): """ Ensure that multiple symbols that return the same sid are collapsed to a single returned asset. @@ -834,20 +892,18 @@ def test_lookup_mult_are_one(self): for symbol in ("FOOB", "FOO_B") ] ) - self.write_assets(equities=df) - finder = self.asset_finder + finder = asset_finder(equities=df) # If we are able to resolve this with any result, means that we did not # raise a MultipleSymbolError. result = finder.lookup_symbol("FOO/B", date + timedelta(1), fuzzy=True) assert result.sid == 1 - def test_endless_multiple_resolves(self): + def test_endless_multiple_resolves(self, asset_finder): """ Situation: 1. Asset 1 w/ symbol FOOB changes to FOO_B, and then is delisted. 2. Asset 2 is listed with symbol FOO_B. - If someone asks for FOO_B with fuzzy matching after 2 has been listed, they should be able to correctly get 2. """ @@ -879,15 +935,14 @@ def test_endless_multiple_resolves(self): }, ] ) - self.write_assets(equities=df) - finder = self.asset_finder + finder = asset_finder(equities=df) # If we are able to resolve this with any result, means that we did not # raise a MultipleSymbolError. result = finder.lookup_symbol("FOO/B", date + timedelta(days=90), fuzzy=True) assert result.sid == 2 - def test_lookup_generic_handle_missing(self): + def test_lookup_generic_handle_missing(self, asset_finder): data = pd.DataFrame.from_records( [ { @@ -924,8 +979,7 @@ def test_lookup_generic_handle_missing(self): }, ] ) - self.write_assets(equities=data) - finder = self.asset_finder + finder = asset_finder(equities=data) results, missing = finder.lookup_generic( ["REAL", 1, "FAKE", "REAL_BUT_OLD", "REAL_BUT_IN_THE_FUTURE"], pd.Timestamp("2013-02-01", tz="UTC"), @@ -944,7 +998,7 @@ def test_lookup_generic_handle_missing(self): assert missing[0] == "FAKE" assert missing[1] == "REAL_BUT_IN_THE_FUTURE" - def test_lookup_generic_multiple_symbols_across_countries(self): + def test_lookup_generic_multiple_symbols_across_countries(self, asset_finder): data = pd.DataFrame.from_records( [ { @@ -970,41 +1024,40 @@ def test_lookup_generic_multiple_symbols_across_countries(self): ] ) - self.write_assets(equities=data, exchanges=exchanges) - + asset_finder = asset_finder(equities=data, exchanges=exchanges) # looking up a symbol shared by two assets across countries should # raise a SameSymbolUsedAcrossCountries if a country code is not passed with pytest.raises(SameSymbolUsedAcrossCountries): - self.asset_finder.lookup_generic( + asset_finder.lookup_generic( "real", as_of_date=pd.Timestamp("2014-1-1", tz="UTC"), country_code=None, ) with pytest.raises(SameSymbolUsedAcrossCountries): - self.asset_finder.lookup_generic( + asset_finder.lookup_generic( "real", as_of_date=None, country_code=None, ) - matches, missing = self.asset_finder.lookup_generic( + matches, missing = asset_finder.lookup_generic( "real", as_of_date=pd.Timestamp("2014-1-1", tz="UTC"), country_code="US", ) - assert [matches] == [self.asset_finder.retrieve_asset(0)] + assert [matches] == [asset_finder.retrieve_asset(0)] assert missing == [] - matches, missing = self.asset_finder.lookup_generic( + matches, missing = asset_finder.lookup_generic( "real", as_of_date=pd.Timestamp("2014-1-1", tz="UTC"), country_code="CA", ) - assert [matches] == [self.asset_finder.retrieve_asset(1)] + assert [matches] == [asset_finder.retrieve_asset(1)] assert missing == [] - def test_compute_lifetimes(self): + def test_compute_lifetimes(self, asset_finder): assets_per_exchange = 4 trading_day = self.trading_calendar.day first_start = pd.Timestamp("2015-04-01", tz="UTC") @@ -1050,8 +1103,7 @@ def test_compute_lifetimes(self): "CA": equities.index[2 * assets_per_exchange : 3 * assets_per_exchange], "JP": equities.index[3 * assets_per_exchange :], } - self.write_assets(equities=equities, exchanges=exchanges) - finder = self.asset_finder + finder = asset_finder(equities=equities, exchanges=exchanges) all_dates = pd.date_range( start=first_start, @@ -1129,18 +1181,18 @@ def test_compute_lifetimes(self): result = result[permuted_sids] assert_frame_equal(result, expected_no_start) - def test_sids(self): + def test_sids(self, asset_finder): # Ensure that the sids property of the AssetFinder is functioning - self.write_assets( + asset_finder = asset_finder( equities=make_simple_equity_info( [0, 1, 2], pd.Timestamp("2014-01-01"), pd.Timestamp("2014-01-02"), ) ) - assert {0, 1, 2} == set(self.asset_finder.sids) + assert {0, 1, 2} == set(asset_finder.sids) - def test_lookup_by_supplementary_field(self): + def test_lookup_by_supplementary_field(self, asset_finder): equities = pd.DataFrame.from_records( [ { @@ -1200,13 +1252,11 @@ def test_lookup_by_supplementary_field(self): ] ) - self.write_assets( + af = asset_finder( equities=equities, equity_supplementary_mappings=equity_supplementary_mappings, ) - af = self.asset_finder - # Before sid 0 has changed ALT_ID. dt = pd.Timestamp("2013-6-28", tz="UTC") @@ -1247,7 +1297,7 @@ def test_lookup_by_supplementary_field(self): with pytest.raises(MultipleValuesFoundForField, match=expected_in_repr): af.lookup_by_supplementary_field("ALT_ID", "100000000", None) - def test_get_supplementary_field(self): + def test_get_supplementary_field(self, asset_finder): equities = pd.DataFrame.from_records( [ { @@ -1307,11 +1357,10 @@ def test_get_supplementary_field(self): ] ) - self.write_assets( + finder = asset_finder( equities=equities, equity_supplementary_mappings=equity_supplementary_mappings, ) - finder = self.asset_finder # Before sid 0 has changed ALT_ID and sid 2 has started. dt = pd.Timestamp("2013-6-28", tz="UTC") @@ -1343,7 +1392,7 @@ def test_get_supplementary_field(self): ): finder.get_supplementary_field(0, "ALT_ID", None), - def test_group_by_type(self): + def test_group_by_type(self, asset_finder): equities = make_simple_equity_info( range(5), start_date=pd.Timestamp("2014-01-01"), @@ -1361,22 +1410,24 @@ def test_group_by_type(self): ([0, 2, 3], [7, 10]), (list(equities.index), list(futures.index)), ] - self.write_assets( + finder = asset_finder( equities=equities, futures=futures, ) - finder = self.asset_finder for equity_sids, future_sids in queries: results = finder.group_by_type(equity_sids + future_sids) assert results == {"equity": set(equity_sids), "future": set(future_sids)} - @parameterized.expand( + @pytest.mark.parametrize( + "type_, lookup_name, failure_type", [ (Equity, "retrieve_equities", EquitiesNotFound), (Future, "retrieve_futures_contracts", FutureContractsNotFound), - ] + ], ) - def test_retrieve_specific_type(self, type_, lookup_name, failure_type): + def test_retrieve_specific_type( + self, type_, lookup_name, failure_type, asset_finder + ): equities = make_simple_equity_info( range(5), start_date=pd.Timestamp("2014-01-01"), @@ -1397,11 +1448,10 @@ def test_retrieve_specific_type(self, type_, lookup_name, failure_type): fail_sids = equity_sids success_sids = future_sids - self.write_assets( + finder = asset_finder( equities=equities, futures=futures, ) - finder = self.asset_finder # Run twice to exercise caching. lookup = getattr(finder, lookup_name) for _ in range(2): @@ -1416,7 +1466,7 @@ def test_retrieve_specific_type(self, type_, lookup_name, failure_type): # Should fail if **any** of the assets are bad. lookup([success_sids[0], fail_sids[0]]) - def test_retrieve_all(self): + def test_retrieve_all(self, asset_finder): equities = make_simple_equity_info( range(5), start_date=pd.Timestamp("2014-01-01"), @@ -1428,11 +1478,10 @@ def test_retrieve_all(self): root_symbols=["CL"], years=[2014], ) - self.write_assets( + finder = asset_finder( equities=equities, futures=futures, ) - finder = self.asset_finder all_sids = finder.sids assert len(all_sids) == len(equities) + len(futures) queries = [ @@ -1464,12 +1513,13 @@ def test_retrieve_all(self): + list(futures.symbol.loc[future_sids]) ) == list(asset.symbol for asset in results) - @parameterized.expand( + @pytest.mark.parametrize( + "error_type, singular, plural", [ (EquitiesNotFound, "equity", "equities"), (FutureContractsNotFound, "future contract", "future contracts"), (SidsNotFound, "asset", "assets"), - ] + ], ) def test_error_message_plurality(self, error_type, singular, plural): try: @@ -1482,22 +1532,16 @@ def test_error_message_plurality(self, error_type, singular, plural): assert str(e) == "No {plural} found for sids: [1, 2].".format(plural=plural) -class AssetFinderMultipleCountries(WithTradingCalendars, ZiplineTestCase): +@pytest.mark.usefixtures("with_trading_calendars") +class TestAssetFinderMultipleCountries: def write_assets(self, **kwargs): self._asset_writer.write(**kwargs) - def init_instance_fixtures(self): - super(AssetFinderMultipleCountries, self).init_instance_fixtures() - - conn = self.enter_instance_context(empty_assets_db()) - self._asset_writer = AssetDBWriter(conn) - self.asset_finder = AssetFinder(conn) - @staticmethod def country_code(n): return "A" + chr(ord("A") + n) - def test_lookup_symbol_delimited(self): + def test_lookup_symbol_delimited(self, asset_finder): as_of = pd.Timestamp("2013-01-01", tz="UTC") num_assets = 3 sids = list(range(num_assets)) @@ -1521,8 +1565,7 @@ def test_lookup_symbol_delimited(self): "country_code": [self.country_code(n) for n in range(num_assets)], } ) - self.write_assets(equities=frame, exchanges=exchanges) - finder = self.asset_finder + finder = asset_finder(equities=frame, exchanges=exchanges) assets = finder.retrieve_all(sids) def shouldnt_resolve(ticker): @@ -1560,7 +1603,7 @@ def shouldnt_resolve(ticker): n ) - def test_lookup_symbol_fuzzy(self): + def test_lookup_symbol_fuzzy(self, asset_finder): num_countries = 3 metadata = pd.DataFrame.from_records( [ @@ -1575,8 +1618,7 @@ def test_lookup_symbol_fuzzy(self): "country_code": list(map(self.country_code, range(num_countries))), } ) - self.write_assets(equities=metadata, exchanges=exchanges) - finder = self.asset_finder + finder = asset_finder(equities=metadata, exchanges=exchanges) dt = pd.Timestamp("2013-01-01", tz="UTC") # Try combos of looking up PRTYHRD with and without a time or fuzzy @@ -1638,7 +1680,7 @@ def check_sid(expected_sid, ticker, country_code): check_sid(n * 3 + 1, "BRKA", self.country_code(n)) check_sid(n * 3 + 2, "BRK_A", self.country_code(n)) - def test_lookup_symbol_change_ticker(self): + def test_lookup_symbol_change_ticker(self, asset_finder): T = partial(pd.Timestamp, tz="utc") num_countries = 3 metadata = pd.DataFrame.from_records( @@ -1683,8 +1725,7 @@ def test_lookup_symbol_change_ticker(self): "country_code": [self.country_code(n) for n in range(num_countries)], } ) - self.write_assets(equities=metadata, exchanges=exchanges) - finder = self.asset_finder + finder = asset_finder(equities=metadata, exchanges=exchanges) def assert_doesnt_resolve(symbol, as_of_date): # check across all countries @@ -1787,7 +1828,7 @@ def assert_resolves_in_each_country( expected_name="Asset A", ) - def test_lookup_symbol(self): + def test_lookup_symbol(self, asset_finder): num_countries = 3 # Incrementing by two so that start and end dates for each # generated Asset don't overlap (each Asset's end_date is the @@ -1812,8 +1853,7 @@ def test_lookup_symbol(self): "country_code": [self.country_code(n) for n in range(num_countries)], } ) - self.write_assets(equities=df, exchanges=exchanges) - finder = self.asset_finder + finder = asset_finder(equities=df, exchanges=exchanges) for _ in range(2): # Run checks twice to test for caching bugs. with pytest.raises(SymbolNotFound): finder.lookup_symbol("NON_EXISTING", dates[0]) @@ -1852,7 +1892,7 @@ def test_lookup_symbol(self): expected_sid = n * len(dates) + i assert result.sid == expected_sid - def test_fail_to_write_overlapping_data(self): + def test_fail_to_write_overlapping_data(self, asset_finder): num_countries = 3 df = pd.DataFrame.from_records( concat( @@ -1925,14 +1965,13 @@ def test_fail_to_write_overlapping_data(self): ) ) with pytest.raises(ValueError, match=re.escape(expected_error_msg)): - self.write_assets(equities=df, exchanges=exchanges) + asset_finder(equities=df, exchanges=exchanges) - def test_endless_multiple_resolves(self): + def test_endless_multiple_resolves(self, asset_finder): """ Situation: 1. Asset 1 w/ symbol FOOB changes to FOO_B, and then is delisted. 2. Asset 2 is listed with symbol FOO_B. - If someone asks for FOO_B with fuzzy matching after 2 has been listed, they should be able to correctly get 2. """ @@ -1973,8 +2012,7 @@ def test_endless_multiple_resolves(self): "country_code": [self.country_code(n) for n in range(num_countries)], } ) - self.write_assets(equities=df, exchanges=exchanges) - finder = self.asset_finder + finder = asset_finder(equities=df, exchanges=exchanges) with pytest.raises(MultipleSymbolsFoundForFuzzySymbol): finder.lookup_symbol( @@ -1993,10 +2031,16 @@ def test_endless_multiple_resolves(self): assert result.sid == n * 2 + 1 -@pytest.fixture(scope="function") -def sql_db(request): - url = "sqlite:///:memory:" - request.cls.engine = sa.create_engine(url) +@pytest.fixture(scope="function", params=DBS) +def sql_db(request, postgresql): + if request.param == "sqlite": + connection = "sqlite:///:memory:" + elif request.param == "postgresql": + connection = f"postgresql://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}" + request.cls.engine = sa.create_engine( + connection, + future=False, + ) yield request.cls.engine request.cls.engine.dispose() request.cls.engine = None @@ -2012,67 +2056,71 @@ def setup_empty_assets_db(sql_db, request): @pytest.mark.usefixtures("sql_db", "setup_empty_assets_db") class TestAssetDBVersioning: def test_check_version(self): + version_table = self.metadata.tables["version_info"] - # This should not raise an error - check_version_info(self.engine, version_table, ASSET_DB_VERSION) + with self.engine.begin() as conn: + # This should not raise an error + check_version_info(conn, version_table, ASSET_DB_VERSION) - # This should fail because the version is too low - with pytest.raises(AssetDBVersionError): - check_version_info( - self.engine, - version_table, - ASSET_DB_VERSION - 1, - ) + # This should fail because the version is too low + with pytest.raises(AssetDBVersionError): + check_version_info( + conn, + version_table, + ASSET_DB_VERSION - 1, + ) - # This should fail because the version is too high - with pytest.raises(AssetDBVersionError): - check_version_info( - self.engine, - version_table, - ASSET_DB_VERSION + 1, - ) + # This should fail because the version is too high + with pytest.raises(AssetDBVersionError): + check_version_info( + conn, + version_table, + ASSET_DB_VERSION + 1, + ) def test_write_version(self): version_table = self.metadata.tables["version_info"] - version_table.delete().execute() + with self.engine.begin() as conn: + conn.execute(version_table.delete()) - # Assert that the version is not present in the table - assert sa.select((version_table.c.version,)).scalar() is None + # Assert that the version is not present in the table + assert conn.execute(sa.select(version_table.c.version)).scalar() is None - # This should fail because the table has no version info and is, - # therefore, consdered v0 - with pytest.raises(AssetDBVersionError): - check_version_info(self.engine, version_table, -2) + # This should fail because the table has no version info and is, + # therefore, consdered v0 + with pytest.raises(AssetDBVersionError): + check_version_info(conn, version_table, -2) - # This should not raise an error because the version has been written - write_version_info(self.engine, version_table, -2) - check_version_info(self.engine, version_table, -2) + # This should not raise an error because the version has been written + write_version_info(conn, version_table, -2) + check_version_info(conn, version_table, -2) - # Assert that the version is in the table and correct - assert sa.select((version_table.c.version,)).scalar() == -2 + # Assert that the version is in the table and correct + assert conn.execute(sa.select(version_table.c.version)).scalar() == -2 - # Assert that trying to overwrite the version fails - with pytest.raises(sa.exc.IntegrityError): - write_version_info(self.engine, version_table, -3) + # Assert that trying to overwrite the version fails + with pytest.raises(sa.exc.IntegrityError): + write_version_info(conn, version_table, -3) def test_finder_checks_version(self): version_table = self.metadata.tables["version_info"] - version_table.delete().execute() - write_version_info(self.engine, version_table, -2) - check_version_info(self.engine, version_table, -2) + with self.engine.begin() as conn: + conn.execute(version_table.delete()) + write_version_info(conn, version_table, -2) + check_version_info(conn, version_table, -2) - # Assert that trying to build a finder with a bad db raises an error - with pytest.raises(AssetDBVersionError): - AssetFinder(engine=self.engine) + # Assert that trying to build a finder with a bad db raises an error + with pytest.raises(AssetDBVersionError): + AssetFinder(engine=conn) - # Change the version number of the db to the correct version - version_table.delete().execute() - write_version_info(self.engine, version_table, ASSET_DB_VERSION) - check_version_info(self.engine, version_table, ASSET_DB_VERSION) + # Change the version number of the db to the correct version + conn.execute(version_table.delete()) + write_version_info(conn, version_table, ASSET_DB_VERSION) + check_version_info(conn, version_table, ASSET_DB_VERSION) - # Now that the versions match, this Finder should succeed - AssetFinder(engine=self.engine) + # Now that the versions match, this Finder should succeed + AssetFinder(engine=conn) def test_downgrade(self): # Attempt to downgrade a current assets db all the way down to v0 @@ -2135,12 +2183,15 @@ def select_fields(r): (1, "B", "B", T("2014-01-01").value, T("2014-01-02").value), (2, "B", "C", T("2014-01-01").value, T("2014-01-04").value), } - actual_data = set( - map( - select_fields, - sa.select(metadata.tables["equities"].c).execute(), + + with self.engine.begin() as conn: + + actual_data = set( + map( + select_fields, + conn.execute(sa.select(metadata.tables["equities"].c)), + ) ) - ) assert expected_data == actual_data @@ -2172,52 +2223,29 @@ def test_v7_to_v6_only_keeps_US(self): metadata.reflect() expected_sids = {0, 2} - actual_sids = set( - map( - lambda r: r.sid, - sa.select(metadata.tables["equities"].c).execute(), + + with self.engine.begin() as conn: + actual_sids = set( + map( + lambda r: r.sid, + conn.execute(sa.select(metadata.tables["equities"].c)), + ) ) - ) assert expected_sids == actual_sids -class TestVectorizedSymbolLookup(WithAssetFinder, ZiplineTestCase): - @classmethod - def make_equity_info(cls): - T = partial(pd.Timestamp, tz="UTC") - - def asset(sid, symbol, start_date, end_date): - return dict( - sid=sid, - symbol=symbol, - start_date=T(start_date), - end_date=T(end_date), - exchange="NYSE", - ) - - records = [ - asset(1, "A", "2014-01-02", "2014-01-31"), - asset(2, "A", "2014-02-03", "2015-01-02"), - asset(3, "B", "2014-01-02", "2014-01-15"), - asset(4, "B", "2014-01-17", "2015-01-02"), - asset(5, "C", "2001-01-02", "2015-01-02"), - asset(6, "D", "2001-01-02", "2015-01-02"), - asset(7, "FUZZY", "2001-01-02", "2015-01-02"), - ] - return pd.DataFrame.from_records(records) - - @parameter_space( - as_of=pd.to_datetime( - [ - "2014-01-02", - "2014-01-15", - "2014-01-17", - "2015-01-02", - ], - utc=True, - ), - symbols=[ +@pytest.mark.usefixtures("set_test_vectorized_symbol_lookup") +class TestVectorizedSymbolLookup: + @pytest.mark.parametrize( + "as_of", + pd.to_datetime( + ["2014-01-02", "2014-01-15", "2014-01-17", "2015-01-02"], utc=True + ).to_list(), + ) + @pytest.mark.parametrize( + "symbols", + ( [], ["A"], ["B"], @@ -2226,7 +2254,7 @@ def asset(sid, symbol, start_date, end_date): list("ABCD"), list("ABCDDCBA"), list("AABBAABBACABD"), - ], + ), ) def test_lookup_symbols(self, as_of, symbols): af = self.asset_finder @@ -2344,16 +2372,7 @@ def test_read_from_asset_finder(self): assert asset.exchange_info == expected_exchange_info -@pytest.fixture(scope="function") -def _setup(request, tmp_path): - request.cls.assets_db_path = path = os.path.join( - str(tmp_path), - "assets.db", - ) - request.cls.writer = AssetDBWriter(path) - - -@pytest.mark.usefixtures("_setup") +@pytest.mark.usefixtures("set_test_write") class TestWrite: def new_asset_finder(self): return AssetFinder(self.assets_db_path) diff --git a/tests/test_benchmark.py b/tests/test_benchmark.py index d7d33c4043..703e9415f4 100644 --- a/tests/test_benchmark.py +++ b/tests/test_benchmark.py @@ -13,8 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging +import re + import numpy as np import pandas as pd +import pytest from pandas.testing import assert_series_equal from zipline.data.data_portal import DataPortal @@ -23,27 +26,69 @@ BenchmarkAssetNotAvailableTooLate, InvalidBenchmarkAsset, ) - from zipline.sources.benchmark_source import BenchmarkSource -from zipline.utils.run_algo import BenchmarkSpec - from zipline.testing import ( MockDailyBarReader, create_minute_bar_data, - parameter_space, tmp_bcolz_equity_minute_bar_reader, ) -from zipline.testing.predicates import assert_equal from zipline.testing.fixtures import ( - WithAssetFinder, WithDataPortal, WithSimParams, - WithTmpDir, WithTradingCalendars, ZiplineTestCase, ) -import pytest -import re +from zipline.testing.predicates import assert_equal +from zipline.utils.run_algo import BenchmarkSpec + + +@pytest.fixture(scope="class") +def set_test_benchmark_spec(request, with_asset_finder): + ASSET_FINDER_COUNTRY_CODE = "??" + START_DATE = pd.Timestamp("2006-01-03", tz="utc") + END_DATE = pd.Timestamp("2006-12-29", tz="utc") + request.cls.START_DATE = START_DATE + request.cls.END_DATE = END_DATE + + zero_returns_index = pd.date_range( + request.cls.START_DATE, + request.cls.END_DATE, + freq="D", + tz="utc", + ) + request.cls.zero_returns = pd.Series(index=zero_returns_index, data=0.0) + + equities = pd.DataFrame.from_dict( + { + 1: { + "symbol": "A", + "start_date": START_DATE, + "end_date": END_DATE + pd.Timedelta(days=1), + "exchange": "TEST", + }, + 2: { + "symbol": "B", + "start_date": START_DATE, + "end_date": END_DATE + pd.Timedelta(days=1), + "exchange": "TEST", + }, + }, + orient="index", + ) + + equities = equities + exchange_names = [df["exchange"] for df in (equities,) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( + { + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, + } + ) + + request.cls.asset_finder = with_asset_finder( + **dict(equities=equities, exchanges=exchanges) + ) class TestBenchmark( @@ -240,53 +285,12 @@ def test_no_stock_dividends_allowed(self): ) -class BenchmarkSpecTestCase(WithTmpDir, WithAssetFinder, ZiplineTestCase): - @pytest.fixture(autouse=True) - def inject_fixtures(self, caplog): - self._caplog = caplog - - @classmethod - def init_class_fixtures(cls): - super(BenchmarkSpecTestCase, cls).init_class_fixtures() - - zero_returns_index = pd.date_range( - cls.START_DATE, - cls.END_DATE, - freq="D", - tz="utc", - ) - cls.zero_returns = pd.Series(index=zero_returns_index, data=0.0) - - def init_instance_fixtures(self): - super(BenchmarkSpecTestCase, self).init_instance_fixtures() - - @classmethod - def make_equity_info(cls): - return pd.DataFrame.from_dict( - { - 1: { - "symbol": "A", - "start_date": cls.START_DATE, - "end_date": cls.END_DATE + pd.Timedelta(days=1), - "exchange": "TEST", - }, - 2: { - "symbol": "B", - "start_date": cls.START_DATE, - "end_date": cls.END_DATE + pd.Timedelta(days=1), - "exchange": "TEST", - }, - }, - orient="index", - ) - - def logs_at_level(self, level): - return [r.message for r in self.log_handler.records if r.level == level] - +@pytest.mark.usefixtures("set_test_benchmark_spec") +class TestBenchmarkSpec: def resolve_spec(self, spec): return spec.resolve(self.asset_finder, self.START_DATE, self.END_DATE) - def test_no_benchmark(self): + def test_no_benchmark(self, caplog): """Test running with no benchmark provided. We should have no benchmark sid and have a returns series of all zeros. @@ -308,10 +312,11 @@ def test_no_benchmark(self): "Pass --benchmark-sid, --benchmark-symbol, or --benchmark-file to set a source of benchmark returns.", # noqa "Pass --no-benchmark to use a dummy benchmark of zero returns.", ] - with self._caplog.at_level(logging.WARNING): - assert_equal(self._caplog.messages, expected) - def test_no_benchmark_explicitly_disabled(self): + with caplog.at_level(logging.WARNING): + assert_equal(caplog.messages, expected) + + def test_no_benchmark_explicitly_disabled(self, caplog): """Test running with no benchmark provided, with no_benchmark flag.""" spec = BenchmarkSpec.from_cli_params( no_benchmark=True, @@ -326,11 +331,11 @@ def test_no_benchmark_explicitly_disabled(self): assert_series_equal(returns, self.zero_returns) expected = [] - with self._caplog.at_level(logging.WARNING): - assert_equal(self._caplog.messages, expected) + with caplog.at_level(logging.WARNING): + assert_equal(caplog.messages, expected) - @parameter_space(case=[("A", 1), ("B", 2)]) - def test_benchmark_symbol(self, case): + @pytest.mark.parametrize("case", [("A", 1), ("B", 2)]) + def test_benchmark_symbol(self, case, caplog): """Test running with no benchmark provided, with no_benchmark flag.""" symbol, expected_sid = case @@ -347,11 +352,11 @@ def test_benchmark_symbol(self, case): assert returns is None expected = [] - with self._caplog.at_level(logging.WARNING): - assert_equal(self._caplog.messages, expected) + with caplog.at_level(logging.WARNING): + assert_equal(caplog.messages, expected) - @parameter_space(input_sid=[1, 2]) - def test_benchmark_sid(self, input_sid): + @pytest.mark.parametrize("input_sid", [1, 2]) + def test_benchmark_sid(self, input_sid, caplog): """Test running with no benchmark provided, with no_benchmark flag.""" spec = BenchmarkSpec.from_cli_params( no_benchmark=False, @@ -366,12 +371,12 @@ def test_benchmark_sid(self, input_sid): assert returns is None expected = [] - with self._caplog.at_level(logging.WARNING): - assert_equal(self._caplog.messages, expected) + with caplog.at_level(logging.WARNING): + assert_equal(caplog.messages, expected) - def test_benchmark_file(self): + def test_benchmark_file(self, tmp_path, caplog): """Test running with a benchmark file.""" - csv_file_path = self.tmpdir.getpath("b.csv") + csv_file_path = tmp_path / "b.csv" with open(csv_file_path, "w") as csv_file: csv_file.write( "date,return\n" @@ -403,5 +408,5 @@ def test_benchmark_file(self): assert_series_equal(returns, expected_returns, check_names=False) expected = [] - with self._caplog.at_level(logging.WARNING): - assert_equal(self._caplog.messages, expected) + with caplog.at_level(logging.WARNING): + assert_equal(caplog.messages, expected) diff --git a/tests/test_continuous_futures.py b/tests/test_continuous_futures.py index ccf486c696..b511efd807 100644 --- a/tests/test_continuous_futures.py +++ b/tests/test_continuous_futures.py @@ -17,18 +17,124 @@ from textwrap import dedent import numpy as np -from numpy.testing import assert_almost_equal import pandas as pd +import pytest +from numpy.testing import assert_almost_equal +import zipline.testing.fixtures as zf from zipline.assets.continuous_futures import OrderedContracts, delivery_predicate -from zipline.assets.roll_finder import ( - ROLL_DAYS_FOR_CURRENT_CONTRACT, - VolumeRollFinder, -) -from zipline.data.minute_bars import FUTURES_MINUTES_PER_DAY +from zipline.assets.roll_finder import ROLL_DAYS_FOR_CURRENT_CONTRACT, VolumeRollFinder +from zipline.data.bcolz_minute_bars import FUTURES_MINUTES_PER_DAY from zipline.errors import SymbolNotFound -import zipline.testing.fixtures as zf -import pytest + + +@pytest.fixture(scope="class") +def set_test_ordered_futures_contracts(request, with_asset_finder): + ASSET_FINDER_COUNTRY_CODE = "??" + + root_symbols = pd.DataFrame( + { + "root_symbol": ["FO", "BA", "BZ"], + "root_symbol_id": [1, 2, 3], + "exchange": ["CMES", "CMES", "CMES"], + } + ) + + fo_frame = pd.DataFrame( + { + "root_symbol": ["FO"] * 4, + "asset_name": ["Foo"] * 4, + "symbol": ["FOF16", "FOG16", "FOH16", "FOJ16"], + "sid": range(1, 5), + "start_date": pd.date_range("2015-01-01", periods=4, tz="UTC"), + "end_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), + "notice_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), + "expiration_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), + "auto_close_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), + "tick_size": [0.001] * 4, + "multiplier": [1000.0] * 4, + "exchange": ["CMES"] * 4, + } + ) + # BA is set up to test a quarterly roll, to test Eurodollar-like + # behavior + # The roll should go from BAH16 -> BAM16 + ba_frame = pd.DataFrame( + { + "root_symbol": ["BA"] * 3, + "asset_name": ["Bar"] * 3, + "symbol": ["BAF16", "BAG16", "BAH16"], + "sid": range(5, 8), + "start_date": pd.date_range("2015-01-01", periods=3, tz="UTC"), + "end_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), + "notice_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), + "expiration_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), + "auto_close_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), + "tick_size": [0.001] * 3, + "multiplier": [1000.0] * 3, + "exchange": ["CMES"] * 3, + } + ) + # BZ is set up to test the case where the first contract in a chain has + # an auto close date before its start date. It also tests the case + # where a contract in the chain has a start date after the auto close + # date of the previous contract, leaving a gap with no active contract. + bz_frame = pd.DataFrame( + { + "root_symbol": ["BZ"] * 4, + "asset_name": ["Baz"] * 4, + "symbol": ["BZF15", "BZG15", "BZH15", "BZJ16"], + "sid": range(8, 12), + "start_date": [ + pd.Timestamp("2015-01-02", tz="UTC"), + pd.Timestamp("2015-01-03", tz="UTC"), + pd.Timestamp("2015-02-23", tz="UTC"), + pd.Timestamp("2015-02-24", tz="UTC"), + ], + "end_date": pd.date_range( + "2015-02-01", + periods=4, + freq="MS", + tz="UTC", + ), + "notice_date": [ + pd.Timestamp("2014-12-31", tz="UTC"), + pd.Timestamp("2015-02-18", tz="UTC"), + pd.Timestamp("2015-03-18", tz="UTC"), + pd.Timestamp("2015-04-17", tz="UTC"), + ], + "expiration_date": pd.date_range( + "2015-02-01", + periods=4, + freq="MS", + tz="UTC", + ), + "auto_close_date": [ + pd.Timestamp("2014-12-29", tz="UTC"), + pd.Timestamp("2015-02-16", tz="UTC"), + pd.Timestamp("2015-03-16", tz="UTC"), + pd.Timestamp("2015-04-15", tz="UTC"), + ], + "tick_size": [0.001] * 4, + "multiplier": [1000.0] * 4, + "exchange": ["CMES"] * 4, + } + ) + + futures = pd.concat([fo_frame, ba_frame, bz_frame]) + + exchange_names = [df["exchange"] for df in (futures,) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( + { + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, + } + ) + + request.cls.asset_finder = with_asset_finder( + **dict(futures=futures, exchanges=exchanges, root_symbols=root_symbols) + ) class ContinuousFuturesTestCase( @@ -48,7 +154,7 @@ class ContinuousFuturesTestCase( } @classmethod - def make_root_symbols_info(self): + def make_root_symbols_info(cls): return pd.DataFrame( { "root_symbol": ["FOOBAR", "BZ", "MA", "DF"], @@ -58,7 +164,7 @@ def make_root_symbols_info(self): ) @classmethod - def make_futures_info(self): + def make_futures_info(cls): fo_frame = pd.DataFrame( { "symbol": [ @@ -1765,102 +1871,8 @@ def test_get_contract_center(self): ) == asset_finder.retrieve_asset(2000) -class OrderedContractsTestCase(zf.WithAssetFinder, zf.ZiplineTestCase): - @classmethod - def make_root_symbols_info(self): - return pd.DataFrame( - { - "root_symbol": ["FOOBAR", "BA", "BZ"], - "root_symbol_id": [1, 2, 3], - "exchange": ["CMES", "CMES", "CMES"], - } - ) - - @classmethod - def make_futures_info(self): - fo_frame = pd.DataFrame( - { - "root_symbol": ["FOOBAR"] * 4, - "asset_name": ["Foo"] * 4, - "symbol": ["FOOBARF16", "FOOBARG16", "FOOBARH16", "FOOBARJ16"], - "sid": range(1, 5), - "start_date": pd.date_range("2015-01-01", periods=4, tz="UTC"), - "end_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "notice_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "expiration_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "auto_close_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "tick_size": [0.001] * 4, - "multiplier": [1000.0] * 4, - "exchange": ["CMES"] * 4, - } - ) - # BA is set up to test a quarterly roll, to test Eurodollar-like - # behavior - # The roll should go from BAH16 -> BAM16 - ba_frame = pd.DataFrame( - { - "root_symbol": ["BA"] * 3, - "asset_name": ["Bar"] * 3, - "symbol": ["BAF16", "BAG16", "BAH16"], - "sid": range(5, 8), - "start_date": pd.date_range("2015-01-01", periods=3, tz="UTC"), - "end_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "notice_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "expiration_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "auto_close_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "tick_size": [0.001] * 3, - "multiplier": [1000.0] * 3, - "exchange": ["CMES"] * 3, - } - ) - # BZ is set up to test the case where the first contract in a chain has - # an auto close date before its start date. It also tests the case - # where a contract in the chain has a start date after the auto close - # date of the previous contract, leaving a gap with no active contract. - bz_frame = pd.DataFrame( - { - "root_symbol": ["BZ"] * 4, - "asset_name": ["Baz"] * 4, - "symbol": ["BZF15", "BZG15", "BZH15", "BZJ16"], - "sid": range(8, 12), - "start_date": [ - pd.Timestamp("2015-01-02", tz="UTC"), - pd.Timestamp("2015-01-03", tz="UTC"), - pd.Timestamp("2015-02-23", tz="UTC"), - pd.Timestamp("2015-02-24", tz="UTC"), - ], - "end_date": pd.date_range( - "2015-02-01", - periods=4, - freq="MS", - tz="UTC", - ), - "notice_date": [ - pd.Timestamp("2014-12-31", tz="UTC"), - pd.Timestamp("2015-02-18", tz="UTC"), - pd.Timestamp("2015-03-18", tz="UTC"), - pd.Timestamp("2015-04-17", tz="UTC"), - ], - "expiration_date": pd.date_range( - "2015-02-01", - periods=4, - freq="MS", - tz="UTC", - ), - "auto_close_date": [ - pd.Timestamp("2014-12-29", tz="UTC"), - pd.Timestamp("2015-02-16", tz="UTC"), - pd.Timestamp("2015-03-16", tz="UTC"), - pd.Timestamp("2015-04-15", tz="UTC"), - ], - "tick_size": [0.001] * 4, - "multiplier": [1000.0] * 4, - "exchange": ["CMES"] * 4, - } - ) - - return pd.concat([fo_frame, ba_frame, bz_frame]) - +@pytest.mark.usefixtures("set_test_ordered_futures_contracts") +class TestOrderedContracts: def test_contract_at_offset(self): contract_sids = np.array([1, 2, 3, 4], dtype=np.int64) start_dates = pd.date_range("2015-01-01", periods=4, tz="UTC") diff --git a/tests/test_data_portal.py b/tests/test_data_portal.py index 951bebe7f7..8681352b93 100644 --- a/tests/test_data_portal.py +++ b/tests/test_data_portal.py @@ -20,7 +20,7 @@ from zipline.assets import Equity, Future from zipline.data.data_portal import HISTORY_FREQUENCIES, OHLCV_FIELDS -from zipline.data.minute_bars import ( +from zipline.data.bcolz_minute_bars import ( FUTURES_MINUTES_PER_DAY, US_EQUITIES_MINUTES_PER_DAY, ) @@ -53,7 +53,7 @@ class DataPortalTestBase(WithDataPortal, WithTradingSessions): OHLC_RATIOS_PER_SID = {10001: 100000} @classmethod - def make_root_symbols_info(self): + def make_root_symbols_info(cls): return pd.DataFrame( { "root_symbol": ["BAR", "BUZ"], diff --git a/tests/test_examples.py b/tests/test_examples.py index a490ecf6e1..10439478fb 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -15,6 +15,7 @@ import pytest import warnings from functools import partial +from itertools import combinations from operator import itemgetter import tarfile from os import listdir @@ -49,7 +50,7 @@ def _no_benchmark_expectations_applied(expected_perf): return expected_perf -def _stored_pd_data(skip_vers=["0-18-1", "0-19-2"]): +def _stored_pd_data(skip_vers=["0-18-1", "0-19-2", "0-22-0", "1-1-3", "1-2-3"]): with tarfile.open(join(TEST_RESOURCE_PATH, "example_data.tar.gz")) as tar: pd_versions = { n.split("/")[2] @@ -61,6 +62,7 @@ def _stored_pd_data(skip_vers=["0-18-1", "0-19-2"]): STORED_DATA_VERSIONS = _stored_pd_data() +COMBINED_DATA_VERSIONS = list(combinations(STORED_DATA_VERSIONS, 2)) @pytest.fixture(scope="class") @@ -152,3 +154,56 @@ def test_example(self, example_name, benchmark_returns): expected_perf["positions"].apply(sorted, key=itemgetter("sid")), actual_perf["positions"].apply(sorted, key=itemgetter("sid")), ) + + +@pytest.mark.usefixtures("_setup_class") +class TestsStoredDataCheck: + def expected_perf(self, pd_version): + return dataframe_cache( + join( + str(self.tmp_path), + "example_data", + f"expected_perf/{pd_version}", + ), + serialization="pickle", + ) + + @pytest.mark.parametrize( + "benchmark_returns", [read_checked_in_benchmark_data(), None] + ) + @pytest.mark.parametrize("example_name", sorted(EXAMPLE_MODULES)) + @pytest.mark.parametrize("pd_versions", COMBINED_DATA_VERSIONS, ids=str) + def test_compare_stored_data(self, example_name, benchmark_returns, pd_versions): + + if benchmark_returns is not None: + expected_perf_a = self.expected_perf(pd_versions[0])[example_name] + expected_perf_b = self.expected_perf(pd_versions[1])[example_name] + else: + expected_perf_a = { + example_name: _no_benchmark_expectations_applied(expected_perf.copy()) + for example_name, expected_perf in self.expected_perf( + pd_versions[0] + ).items() + }[example_name] + expected_perf_b = { + example_name: _no_benchmark_expectations_applied(expected_perf.copy()) + for example_name, expected_perf in self.expected_perf( + pd_versions[1] + ).items() + }[example_name] + + # Exclude positions column as the positions do not always have the + # same order + columns = [ + column for column in examples._cols_to_check if column != "positions" + ] + + assert_equal( + expected_perf_a[columns], + expected_perf_b[columns], + ) + # Sort positions by SID before comparing + assert_equal( + expected_perf_a["positions"].apply(sorted, key=itemgetter("sid")), + expected_perf_b["positions"].apply(sorted, key=itemgetter("sid")), + ) diff --git a/tests/test_finance.py b/tests/test_finance.py index 4774e03ad6..5358de741d 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -16,31 +16,27 @@ """ Tests for the zipline.finance package """ +from pathlib import Path from datetime import datetime, timedelta -import os - +from functools import partial import numpy as np import pandas as pd +import pytest import pytz +import zipline.utils.factory as factory from testfixtures import TempDirectory - -from zipline.finance.blotter.simulation_blotter import SimulationBlotter -from zipline.finance.execution import MarketOrder, LimitOrder -from zipline.finance.metrics import MetricsTracker, load as load_metrics_set -from zipline.finance.trading import SimulationParameters -from zipline.data.bcolz_daily_bars import ( - BcolzDailyBarReader, - BcolzDailyBarWriter, -) -from zipline.data.minute_bars import BcolzMinuteBarReader +from zipline.data.bcolz_daily_bars import BcolzDailyBarReader, BcolzDailyBarWriter from zipline.data.data_portal import DataPortal -from zipline.finance.slippage import FixedSlippage, FixedBasisPointsSlippage +from zipline.data.bcolz_minute_bars import BcolzMinuteBarReader, BcolzMinuteBarWriter from zipline.finance.asset_restrictions import NoRestrictions +from zipline.finance.blotter.simulation_blotter import SimulationBlotter +from zipline.finance.execution import LimitOrder, MarketOrder +from zipline.finance.metrics import MetricsTracker +from zipline.finance.metrics import load as load_metrics_set +from zipline.finance.slippage import FixedBasisPointsSlippage, FixedSlippage +from zipline.finance.trading import SimulationParameters from zipline.protocol import BarData from zipline.testing import write_bcolz_minute_data -import zipline.testing.fixtures as zf -import zipline.utils.factory as factory -import pytest DEFAULT_TIMEOUT = 15 # seconds EXTENDED_TIMEOUT = 90 @@ -48,21 +44,59 @@ _multiprocess_can_split_ = False -class FinanceTestCase(zf.WithAssetFinder, zf.WithTradingCalendars, zf.ZiplineTestCase): - ASSET_FINDER_EQUITY_SIDS = 1, 2, 133 - start = START_DATE = pd.Timestamp("2006-01-01", tz="utc") - end = END_DATE = pd.Timestamp("2006-12-31", tz="utc") +@pytest.fixture(scope="class") +def set_test_finance(request, with_asset_finder): + ASSET_FINDER_COUNTRY_CODE = "??" + + T = partial(pd.Timestamp, tz="UTC") + + START_DATES = [ + T("2006-01-03", tz="UTC"), + ] * 3 + END_DATES = [ + T("2006-12-29", tz="UTC"), + ] * 3 + + equities = pd.DataFrame( + list( + zip( + [1, 2, 133], + ["A", "B", "C"], + START_DATES, + END_DATES, + [ + "NYSE", + ] + * 3, + ) + ), + columns=["sid", "symbol", "start_date", "end_date", "exchange"], + ) + + exchange_names = [df["exchange"] for df in (equities,) if df is not None] + if exchange_names: + exchanges = pd.DataFrame( + { + "exchange": pd.concat(exchange_names).unique(), + "country_code": ASSET_FINDER_COUNTRY_CODE, + } + ) + + request.cls.asset_finder = with_asset_finder( + **dict(equities=equities, exchanges=exchanges) + ) + - def init_instance_fixtures(self): - super(FinanceTestCase, self).init_instance_fixtures() - self.zipline_test_config = {"sid": 133} +@pytest.mark.usefixtures("set_test_finance", "with_trading_calendars") +class TestFinance: + start = pd.Timestamp("2006-01-01", tz="utc") + end = pd.Timestamp("2006-12-31", tz="utc") # TODO: write tests for short sales # TODO: write a test to do massive buying or shorting. @pytest.mark.timeout(DEFAULT_TIMEOUT) def test_partially_filled_orders(self): - # create a scenario where order size and trade size are equal # so that orders must be spread out over several trades. params = { @@ -97,7 +131,7 @@ def test_partially_filled_orders(self): self.transaction_sim(**params2) - @pytest.mark.timeout(DEFAULT_TIMEOUT) + # @pytest.mark.timeout(DEFAULT_TIMEOUT) def test_collapsing_orders(self): # create a scenario where order.amount <<< trade.volume # to test that several orders can be covered properly by one trade, @@ -243,7 +277,7 @@ def transaction_sim(self, **params): ) } - path = os.path.join(tempdir.path, "testdata.bcolz") + path = Path(tempdir.path) / "testdata.bcolz" BcolzDailyBarWriter( path, self.trading_calendar, days[0], days[-1] ).write(assets.items()) @@ -266,10 +300,7 @@ def transaction_sim(self, **params): start_date = sim_params.first_open - if alternate: - alternator = -1 - else: - alternator = 1 + alternator = -1 if alternate else 1 tracker = MetricsTracker( trading_calendar=self.trading_calendar, @@ -329,7 +360,7 @@ def transaction_sim(self, **params): for i in range(order_count): order = order_list[i] assert order.asset == asset1 - assert order.amount == order_amount * alternator ** i + assert order.amount == order_amount * alternator**i if complete_fill: assert len(transactions) == len(order_list) @@ -393,7 +424,8 @@ def test_blotter_processes_splits(self): assert 2 == asset2_order.asset -class SimParamsTestCase(zf.WithTradingCalendars, zf.ZiplineTestCase): +@pytest.mark.usefixtures("with_trading_calendars") +class TestSimulationParameters: """ Tests for date management utilities in zipline.finance.trading. """ From 91305e44e1778e25b0f26da3e8ddb804c6e35cb9 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 08:37:46 +0100 Subject: [PATCH 09/15] exchange-calendar 4 --- pyproject.toml | 9 +- src/zipline/_protocol.pyx | 35 +- src/zipline/algorithm.py | 121 ++- src/zipline/assets/assets.py | 67 +- src/zipline/assets/continuous_futures.pyx | 41 +- src/zipline/assets/roll_finder.py | 33 +- src/zipline/assets/synthetic.py | 15 +- src/zipline/data/_adjustments.pyx | 6 +- src/zipline/data/_equities.pyx | 3 +- src/zipline/data/_minute_bar_internal.pyx | 12 +- src/zipline/data/adjustments.py | 45 +- src/zipline/data/bcolz_daily_bars.py | 126 ++- src/zipline/data/bcolz_minute_bars.py | 127 +-- src/zipline/data/benchmarks.py | 7 +- src/zipline/data/continuous_future_reader.py | 28 +- src/zipline/data/data_portal.py | 105 +-- src/zipline/data/fx/hdf5.py | 6 +- src/zipline/data/fx/in_memory.py | 8 +- src/zipline/data/hdf5_daily_bars.py | 26 +- src/zipline/data/history_loader.py | 53 +- src/zipline/data/in_memory_daily_bars.py | 7 +- src/zipline/data/resample.py | 65 +- src/zipline/data/session_bars.py | 8 +- src/zipline/errors.py | 198 ++-- src/zipline/examples/__init__.py | 4 +- src/zipline/examples/buy_and_hold.py | 5 +- src/zipline/examples/buyapple.py | 6 +- src/zipline/examples/buyapple_ide.py | 4 +- src/zipline/examples/dual_ema_talib.py | 5 +- src/zipline/examples/dual_moving_average.py | 5 +- src/zipline/examples/momentum_pipeline.py | 4 +- src/zipline/examples/olmar.py | 5 +- src/zipline/finance/asset_restrictions.py | 37 +- src/zipline/finance/controls.py | 78 +- src/zipline/finance/metrics/metric.py | 13 +- src/zipline/finance/metrics/tracker.py | 17 +- src/zipline/finance/slippage.py | 38 +- src/zipline/finance/trading.py | 29 +- src/zipline/gens/tradesimulation.py | 2 + src/zipline/pipeline/domain.py | 59 +- src/zipline/pipeline/engine.py | 42 +- src/zipline/pipeline/factors/statistical.py | 12 +- src/zipline/pipeline/graph.py | 2 +- .../pipeline/loaders/earnings_estimates.py | 86 +- .../pipeline/loaders/equity_pricing_loader.py | 2 +- src/zipline/pipeline/loaders/frame.py | 23 +- src/zipline/pipeline/loaders/synthetic.py | 139 ++- src/zipline/pipeline/loaders/utils.py | 9 +- src/zipline/sources/benchmark_source.py | 16 +- src/zipline/sources/test_source.py | 29 +- src/zipline/testing/core.py | 117 +-- src/zipline/testing/fixtures.py | 162 ++-- src/zipline/testing/predicates.py | 3 +- src/zipline/utils/cache.py | 10 +- src/zipline/utils/calendar_utils.py | 107 +-- src/zipline/utils/date_utils.py | 4 +- src/zipline/utils/events.py | 52 +- src/zipline/utils/factory.py | 6 +- src/zipline/utils/final.py | 4 +- src/zipline/utils/pandas_utils.py | 32 +- src/zipline/utils/run_algo.py | 2 +- src/zipline/utils/security_list.py | 12 +- tests/conftest.py | 7 +- tests/data/bundles/test_core.py | 13 +- tests/data/bundles/test_csvdir.py | 6 +- tests/data/bundles/test_quandl.py | 8 +- tests/data/test_adjustments.py | 34 +- tests/data/test_daily_bars.py | 58 +- tests/data/test_dispatch_bar_reader.py | 22 +- tests/data/test_fx.py | 11 +- tests/data/test_hdf5_daily_bars.py | 6 +- tests/data/test_minute_bars.py | 220 ++--- tests/data/test_resample.py | 136 ++- tests/events/test_events.py | 53 +- tests/events/test_events_nyse.py | 20 +- tests/finance/test_commissions.py | 4 +- tests/finance/test_risk.py | 22 +- tests/finance/test_slippage.py | 33 +- tests/history/generate_csvs.py | 2 +- tests/metrics/test_metrics.py | 157 ++-- tests/pipeline/base.py | 4 +- tests/pipeline/test_domain.py | 53 +- tests/pipeline/test_downsampling.py | 21 +- tests/pipeline/test_engine.py | 24 +- tests/pipeline/test_events.py | 15 +- tests/pipeline/test_frameload.py | 4 +- tests/pipeline/test_hooks.py | 4 +- tests/pipeline/test_international_markets.py | 17 +- .../pipeline/test_multidimensional_dataset.py | 23 +- tests/pipeline/test_pipeline.py | 9 +- tests/pipeline/test_pipeline_algo.py | 62 +- tests/pipeline/test_quarters_estimates.py | 246 ++--- tests/pipeline/test_slice.py | 28 +- tests/pipeline/test_statistical.py | 55 +- tests/pipeline/test_technical.py | 22 +- tests/pipeline/test_term.py | 10 +- .../pipeline/test_us_equity_pricing_loader.py | 22 +- tests/resources/yahoo_samples/rebuild_samples | 2 +- tests/test_algorithm.py | 851 +++++++++--------- tests/test_api_shim.py | 4 +- tests/test_assets.py | 199 ++-- tests/test_bar_data.py | 157 ++-- tests/test_benchmark.py | 30 +- tests/test_blotter.py | 4 +- tests/test_clock.py | 30 +- tests/test_continuous_futures.py | 482 +++++----- tests/test_data_portal.py | 44 +- tests/test_fetcher.py | 43 +- tests/test_finance.py | 38 +- tests/test_history.py | 219 ++--- tests/test_labelarray.py | 4 +- tests/test_memoize.py | 4 +- tests/test_ordering.py | 16 +- tests/test_restrictions.py | 22 +- tests/test_security_list.py | 15 +- tests/test_testing.py | 6 +- tests/test_tradesimulation.py | 4 +- tests/utils/test_date_utils.py | 22 +- tests/utils/test_final.py | 8 +- 119 files changed, 2536 insertions(+), 3432 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 32e46316c9..70326cdff0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,7 @@ dependencies = [ 'networkx >=2.0', 'numexpr >=2.6.1', 'numpy >=1.14.5, <1.24', - 'pandas >=1.1.0, <1.6', + 'pandas >=1.3, <1.6', 'patsy >=0.4.0', 'python-dateutil >=2.4.2', 'python-interface >=1.5.3', @@ -61,8 +61,7 @@ dependencies = [ 'ta-lib >=0.4.09', 'tables >=3.4.3', 'toolz >=0.8.2', - 'trading-calendars >=1.6.1', - 'exchange-calendars <=3.3' + 'exchange-calendars >=4.2.4' ] [project.urls] @@ -88,7 +87,7 @@ test = [ 'pytest-xdist >=2.5.0', 'pytest-timeout >=1.4.2', 'parameterized >=0.6.1', - 'mock >=2.0.0p', + 'mock >=4.0.3', 'testfixtures >=4.1.2', 'flake8 >=3.9.1', 'matplotlib >=1.5.3', @@ -194,8 +193,6 @@ setenv = changedir = tmp extras = test deps = - pandas11: pandas>=1.1.0,<1.2 - pandas12: pandas>=1.2.0,<1.3 pandas13: pandas>=1.3.0,<1.4 pandas14: pandas>=1.4.0,<1.5 pandas15: pandas>=1.5.0,<1.6 diff --git a/src/zipline/_protocol.pyx b/src/zipline/_protocol.pyx index 6497354827..25926e56c8 100644 --- a/src/zipline/_protocol.pyx +++ b/src/zipline/_protocol.pyx @@ -26,7 +26,6 @@ from zipline.assets import ( ) from zipline.assets._assets cimport Asset from zipline.assets.continuous_futures import ContinuousFuture -from zipline.utils.pandas_utils import normalize_date from zipline.zipline_warnings import ZiplineDeprecationWarning cdef bool _is_iterable(obj): @@ -115,8 +114,7 @@ def handle_non_market_minutes(bar_data): bar_data._handle_non_market_minutes = False cdef class BarData: - """ - Provides methods for accessing minutely and daily price/volume data from + """Provides methods for accessing minutely and daily price/volume data from Algorithm API functions. Also provides utility methods to determine if an asset is alive, and if it @@ -166,8 +164,7 @@ cdef class BarData: self._is_restricted = restrictions.is_restricted cdef _get_current_minute(self): - """ - Internal utility method to get the current simulation time. + """Internal utility method to get the current simulation time. Possible answers are: - whatever the algorithm's get_datetime() method returns (this is what @@ -180,21 +177,19 @@ cdef class BarData: dt = self.simulation_dt_func() if self._adjust_minutes: - dt = \ - self.data_portal.trading_calendar.previous_minute(dt) + dt = self.data_portal.trading_calendar.previous_minute(dt) if self._daily_mode: # if we're in daily mode, take the given dt (which is the last # minute of the session) and get the session label for it. - dt = self.data_portal.trading_calendar.minute_to_session_label(dt) + dt = self.data_portal.trading_calendar.minute_to_session(dt) return dt @check_parameters(('assets', 'fields'), ((Asset, ContinuousFuture, str), (str,))) def current(self, assets, fields): - """ - Returns the "current" value of the given fields for the given assets + """Returns the "current" value of the given fields for the given assets at the current simulation time. Parameters @@ -380,8 +375,7 @@ cdef class BarData: @check_parameters(('assets',), (Asset,)) def can_trade(self, assets): - """ - For the given asset or iterable of assets, returns True if all of the + """For the given asset or iterable of assets, returns True if all of the following are true: 1. The asset is alive for the session of the current simulation time @@ -444,7 +438,7 @@ cdef class BarData: if self._is_restricted(asset, adjusted_dt): return False - session_label = self._trading_calendar.minute_to_session_label(dt) + session_label = self._trading_calendar.minute_to_session(dt) if not asset.is_alive_for_session(session_label): # asset isn't alive @@ -459,8 +453,7 @@ cdef class BarData: if self._trading_calendar.is_open_on_minute(dt): dt_to_use_for_exchange_check = dt else: - dt_to_use_for_exchange_check = \ - self._trading_calendar.next_open(dt) + dt_to_use_for_exchange_check = self._trading_calendar.next_open(dt) if not asset.is_exchange_open(dt_to_use_for_exchange_check): return False @@ -474,8 +467,7 @@ cdef class BarData: @check_parameters(('assets',), (Asset,)) def is_stale(self, assets): - """ - For the given asset or iterable of assets, returns True if the asset + """For the given asset or iterable of assets, returns True if the asset is alive and there is no trade data for the current simulation time. If the asset has never traded, returns False. @@ -516,7 +508,7 @@ cdef class BarData: }) cdef bool _is_stale_for_asset(self, asset, dt, adjusted_dt, data_portal): - session_label = normalize_date(dt) # FIXME + session_label = dt.normalize() # FIXME if not asset.is_alive_for_session(session_label): return False @@ -543,8 +535,7 @@ cdef class BarData: int, (str,))) def history(self, assets, fields, bar_count, frequency): - """ - Returns a trailing window of length ``bar_count`` with data for + """Returns a trailing window of length ``bar_count`` with data for the given assets, fields, and frequency, adjusted for splits, dividends, and mergers as of the current simulation time. @@ -685,14 +676,14 @@ cdef class BarData: property current_session: def __get__(self): - return self._trading_calendar.minute_to_session_label( + return self._trading_calendar.minute_to_session( self.simulation_dt_func(), direction="next" ) property current_session_minutes: def __get__(self): - return self._trading_calendar.minutes_for_session( + return self._trading_calendar.session_minutes( self.current_session ) diff --git a/src/zipline/algorithm.py b/src/zipline/algorithm.py index 966b1ad734..cf4f4c5d7e 100644 --- a/src/zipline/algorithm.py +++ b/src/zipline/algorithm.py @@ -100,7 +100,6 @@ optionally, ) from zipline.utils.numpy_utils import int64_dtype -from zipline.utils.pandas_utils import normalize_date from zipline.utils.cache import ExpiringCache import zipline.utils.events @@ -404,8 +403,7 @@ def noop(*args, **kwargs): self.restrictions = NoRestrictions() def init_engine(self, get_loader): - """ - Construct and store a PipelineEngine from loader. + """Construct and store a PipelineEngine from loader. If get_loader is None, constructs an ExplodingPipelineEngine """ @@ -419,8 +417,7 @@ def init_engine(self, get_loader): self.engine = ExplodingPipelineEngine() def initialize(self, *args, **kwargs): - """ - Call self._initialize with `self` made available to Zipline API + """Call self._initialize with `self` made available to Zipline API functions. """ with ZiplineAPI(self): @@ -453,8 +450,7 @@ def analyze(self, perf): self._analyze(self, perf) def __repr__(self): - """ - N.B. this does not yet represent a string that can be used + """N.B. this does not yet represent a string that can be used to instantiate an exact copy of an algorithm. However, it is getting close, and provides some value as something @@ -481,15 +477,14 @@ def __repr__(self): ) def _create_clock(self): - """ - If the clock property is not set, then create one based on frequency. - """ - trading_o_and_c = self.trading_calendar.schedule.loc[self.sim_params.sessions] - market_closes = trading_o_and_c["market_close"] + """If the clock property is not set, then create one based on frequency.""" + market_closes = self.trading_calendar.schedule.loc[ + self.sim_params.sessions, "close" + ] + market_opens = self.trading_calendar.first_minutes.loc[self.sim_params.sessions] minutely_emission = False if self.sim_params.data_frequency == "minute": - market_opens = trading_o_and_c["market_open"] minutely_emission = self.sim_params.emission_rate == "minute" # The calendar's execution times are the minutes over which we @@ -499,23 +494,34 @@ def _create_clock(self): # a subset of the full 24 hour calendar, so the execution times # dictate a market open time of 6:31am US/Eastern and a close of # 5:00pm US/Eastern. - execution_opens = self.trading_calendar.execution_time_from_open( - market_opens - ) - execution_closes = self.trading_calendar.execution_time_from_close( - market_closes - ) + if self.trading_calendar.name == "us_futures": + execution_opens = self.trading_calendar.execution_time_from_open( + market_opens + ) + execution_closes = self.trading_calendar.execution_time_from_close( + market_closes + ) + else: + execution_opens = market_opens + execution_closes = market_closes else: # in daily mode, we want to have one bar per session, timestamped # as the last minute of the session. - execution_closes = self.trading_calendar.execution_time_from_close( - market_closes - ) - execution_opens = execution_closes + if self.trading_calendar.name == "us_futures": + execution_closes = self.trading_calendar.execution_time_from_close( + market_closes + ) + execution_opens = execution_closes + else: + execution_closes = market_closes + execution_opens = market_closes # FIXME generalize these values before_trading_start_minutes = days_at_time( - self.sim_params.sessions, time(8, 45), "US/Eastern" + self.sim_params.sessions, + time(8, 45), + "US/Eastern", + day_offset=0, ) return MinuteSimulationClock( @@ -583,16 +589,13 @@ def _create_generator(self, sim_params): return self.trading_client.transform() def compute_eager_pipelines(self): - """ - Compute any pipelines attached with eager=True. - """ + """Compute any pipelines attached with eager=True.""" for name, pipe in self._pipelines.items(): if pipe.eager: self.pipeline_output(name) def get_generator(self): - """ - Override this method to add new logic to the construction + """Override this method to add new logic to the construction of the generator. Overrides can use the _create_generator method to get a standard construction generator. """ @@ -656,8 +659,7 @@ def _create_daily_stats(self, perfs): def calculate_capital_changes( self, dt, emission_rate, is_interday, portfolio_value_adjustment=0.0 ): - """ - If there is a capital change for a given dt, this means the the change + """If there is a capital change for a given dt, this means the the change occurs before `handle_data` on the given dt. In the case of the change being a target value, the change will be computed on the portfolio value according to prices at the given dt @@ -666,6 +668,9 @@ def calculate_capital_changes( portfolio_value of the cumulative performance when calculating deltas from target capital changes. """ + + # CHECK is try/catch faster than search? + try: capital_change = self.capital_changes[dt] except KeyError: @@ -879,8 +884,7 @@ def schedule_function( half_days=True, calendar=None, ): - """ - Schedule a function to be called repeatedly in the future. + """Schedule a function to be called repeatedly in the future. Parameters ---------- @@ -1142,14 +1146,13 @@ def future_symbol(self, symbol): return self.asset_finder.lookup_future_symbol(symbol) def _calculate_order_value_amount(self, asset, value): - """ - Calculates how many shares/contracts to order based on the type of + """Calculates how many shares/contracts to order based on the type of asset being ordered. """ # Make sure the asset exists, and that there is a last price for it. # FIXME: we should use BarData's can_trade logic here, but I haven't # yet found a good way to do that. - normalized_date = normalize_date(self.datetime) + normalized_date = self.trading_calendar.minute_to_session(self.datetime) if normalized_date < asset.start_date: raise CannotOrderDelistedAsset( @@ -1189,7 +1192,8 @@ def _can_order_asset(self, asset): ) if asset.auto_close_date: - day = normalize_date(self.get_datetime()) + # TODO FIXME TZ MESS + day = self.trading_calendar.minute_to_session(self.get_datetime()) if day > min(asset.end_date, asset.auto_close_date): # If we are after the asset's end date or auto close date, warn @@ -1273,8 +1277,7 @@ def _calculate_order( @staticmethod def round_order(amount): - """ - Convert number of shares to an integer. + """Convert number of shares to an integer. By default, truncates to the integer share count that's either within .0001 of amount or closer to zero. @@ -1317,8 +1320,7 @@ def validate_order_params(self, asset, amount, limit_price, stop_price, style): @staticmethod def __convert_order_params_for_blotter(asset, limit_price, stop_price, style): - """ - Helper method for converting deprecated limit_price and stop_price + """Helper method for converting deprecated limit_price and stop_price arguments into ExecutionStyle instances. This function assumes that either style == None or (limit_price, @@ -1339,8 +1341,7 @@ def __convert_order_params_for_blotter(asset, limit_price, stop_price, style): @api_method @disallowed_in_before_trading_start(OrderInBeforeTradingStart()) def order_value(self, asset, value, limit_price=None, stop_price=None, style=None): - """ - Place an order for a fixed amount of money. + """Place an order for a fixed amount of money. Equivalent to ``order(asset, value / data.current(asset, 'price'))``. @@ -1428,8 +1429,7 @@ def set_logger(self, logger): self.logger = logger def on_dt_changed(self, dt): - """ - Callback triggered by the simulation loop whenever the current dt + """Callback triggered by the simulation loop whenever the current dt changes. Any logic that should happen exactly once at the start of each datetime @@ -1442,8 +1442,7 @@ def on_dt_changed(self, dt): @preprocess(tz=coerce_string(pytz.timezone)) @expect_types(tz=optional(tzinfo)) def get_datetime(self, tz=None): - """ - Returns the current simulation datetime. + """Returns the current simulation datetime. Parameters ---------- @@ -1463,8 +1462,7 @@ def get_datetime(self, tz=None): @api_method def set_slippage(self, us_equities=None, us_futures=None): - """ - Set the slippage models for the simulation. + """Set the slippage models for the simulation. Parameters ---------- @@ -2204,8 +2202,7 @@ def attach_pipeline(self, pipeline, name, chunks=None, eager=True): @api_method @require_initialized(PipelineOutputDuringInitialize()) def pipeline_output(self, name): - """ - Get results of the pipeline attached by with name ``name``. + """Get results of the pipeline attached by with name ``name``. Parameters ---------- @@ -2238,10 +2235,9 @@ def pipeline_output(self, name): return self._pipeline_output(pipe, chunks, name) def _pipeline_output(self, pipeline, chunks, name): - """ - Internal implementation of `pipeline_output`. - """ - today = normalize_date(self.get_datetime()) + """Internal implementation of `pipeline_output`.""" + # TODO FIXME TZ MESS + today = self.get_datetime().normalize().tz_localize(None) try: data = self._pipeline_cache.get(name, today) except KeyError: @@ -2262,8 +2258,7 @@ def _pipeline_output(self, pipeline, chunks, name): return pd.DataFrame(index=[], columns=data.columns) def run_pipeline(self, pipeline, start_session, chunksize): - """ - Compute `pipeline`, providing values for at least `start_date`. + """Compute `pipeline`, providing values for at least `start_date`. Produces a DataFrame containing data for days between `start_date` and `end_date`, where `end_date` is defined by: @@ -2279,7 +2274,7 @@ def run_pipeline(self, pipeline, start_session, chunksize): -------- PipelineEngine.run_pipeline """ - sessions = self.trading_calendar.all_sessions + sessions = self.trading_calendar.sessions # Load data starting from the previous trading day... start_date_loc = sessions.get_loc(start_session) @@ -2299,8 +2294,7 @@ def run_pipeline(self, pipeline, start_session, chunksize): @staticmethod def default_pipeline_domain(calendar): - """ - Get a default pipeline domain for algorithms running on ``calendar``. + """Get a default pipeline domain for algorithms running on ``calendar``. This will be used to infer a domain for pipelines that only use generic datasets when running in the context of a TradingAlgorithm. @@ -2309,8 +2303,7 @@ def default_pipeline_domain(calendar): @staticmethod def default_fetch_csv_country_code(calendar): - """ - Get a default country_code to use for fetch_csv symbol lookups. + """Get a default country_code to use for fetch_csv symbol lookups. This will be used to disambiguate symbol lookups for fetch_csv calls if our asset db contains entries with the same ticker spread across @@ -2324,9 +2317,7 @@ def default_fetch_csv_country_code(calendar): @classmethod def all_api_methods(cls): - """ - Return a list of all the TradingAlgorithm API methods. - """ + """Return a list of all the TradingAlgorithm API methods.""" return [fn for fn in vars(cls).values() if getattr(fn, "is_api_method", False)] diff --git a/src/zipline/assets/assets.py b/src/zipline/assets/assets.py index 50612e15a3..babf4d8f17 100644 --- a/src/zipline/assets/assets.py +++ b/src/zipline/assets/assets.py @@ -101,8 +101,7 @@ def merge_ownership_periods(mappings): - """ - Given a dict of mappings where the values are lists of + """Given a dict of mappings where the values are lists of OwnershipPeriod objects, returns a dict with the same structure with new OwnershipPeriod objects adjusted so that the periods have no gaps. @@ -127,7 +126,7 @@ def merge_ownership_periods(mappings): # end date be max timestamp [ OwnershipPeriod( - pd.Timestamp.max.tz_localize("utc"), + pd.Timestamp.max, None, None, None, @@ -145,8 +144,11 @@ def _build_ownership_map_from_rows(rows, key_from_row, value_from_row): for row in rows: mappings.setdefault(key_from_row(row), [],).append( OwnershipPeriod( - pd.Timestamp(row.start_date, unit="ns", tz="utc"), - pd.Timestamp(row.end_date, unit="ns", tz="utc"), + # TODO FIX TZ MESS + # pd.Timestamp(row.start_date, unit="ns", tz="utc"), + # pd.Timestamp(row.end_date, unit="ns", tz="utc"), + pd.Timestamp(row.start_date, unit="ns", tz=None), + pd.Timestamp(row.end_date, unit="ns", tz=None), row.sid, value_from_row(row), ), @@ -156,9 +158,7 @@ def _build_ownership_map_from_rows(rows, key_from_row, value_from_row): def build_ownership_map(table, key_from_row, value_from_row): - """ - Builds a dict mapping to lists of OwnershipPeriods, from a db table. - """ + """Builds a dict mapping to lists of OwnershipPeriods, from a db table.""" return _build_ownership_map_from_rows( sa.select(table.c).execute().fetchall(), key_from_row, @@ -167,8 +167,7 @@ def build_ownership_map(table, key_from_row, value_from_row): def build_grouped_ownership_map(table, key_from_row, value_from_row, group_key): - """ - Builds a dict mapping group keys to maps of keys to lists of + """Builds a dict mapping group keys to maps of keys to lists of OwnershipPeriods, from a db table. """ grouped_rows = groupby( @@ -210,11 +209,11 @@ def _filter_kwargs(names, dict_): def _convert_asset_timestamp_fields(dict_): - """ - Takes in a dict of Asset init args and converts dates to pd.Timestamps - """ + """Takes in a dict of Asset init args and converts dates to pd.Timestamps""" for key in _asset_timestamp_fields & dict_.keys(): - value = pd.Timestamp(dict_[key], tz="UTC") + # TODO FIX TZ MESS + # value = pd.Timestamp(dict_[key], tz="UTC") + value = pd.Timestamp(dict_[key], tz=None) dict_[key] = None if pd.isnull(value) else value return dict_ @@ -252,8 +251,7 @@ def _encode_continuous_future_sid(root_symbol, offset, roll_style, adjustment_st class AssetFinder: - """ - An AssetFinder is an interface to a database of Asset metadata written by + """An AssetFinder is an interface to a database of Asset metadata written by an ``AssetDBWriter``. This class provides methods for looking up assets by unique integer id or @@ -387,8 +385,7 @@ def equity_supplementary_map_by_sid(self): ) def lookup_asset_types(self, sids): - """ - Retrieve asset types for a list of sids. + """Retrieve asset types for a list of sids. Parameters ---------- @@ -427,8 +424,7 @@ def lookup_asset_types(self, sids): return found def group_by_type(self, sids): - """ - Group a list of sids by asset type. + """Group a list of sids by asset type. Parameters ---------- @@ -455,8 +451,7 @@ def retrieve_asset(self, sid, default_none=False): return self.retrieve_all((sid,), default_none=default_none)[0] def retrieve_all(self, sids, default_none=False): - """ - Retrieve all assets in `sids`. + """Retrieve all assets in `sids`. Parameters ---------- @@ -519,8 +514,7 @@ def retrieve_all(self, sids, default_none=False): return [hits[sid] for sid in sids] def retrieve_equities(self, sids): - """ - Retrieve Equity objects for a list of sids. + """Retrieve Equity objects for a list of sids. Users generally shouldn't need to this method (instead, they should prefer the more general/friendly `retrieve_assets`), but it has a @@ -545,8 +539,7 @@ def _retrieve_equity(self, sid): return self.retrieve_equities((sid,))[sid] def retrieve_futures_contracts(self, sids): - """ - Retrieve Future objects for an iterable of sids. + """Retrieve Future objects for an iterable of sids. Users generally shouldn't need to this method (instead, they should prefer the more general/friendly `retrieve_assets`), but it has a @@ -665,8 +658,7 @@ def mkdict(row, exchanges=self.exchange_info): yield _convert_asset_timestamp_fields(mkdict(row)) def _retrieve_assets(self, sids, asset_tbl, asset_type): - """ - Internal function for loading assets from a table. + """Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. @@ -716,8 +708,7 @@ def _retrieve_assets(self, sids, asset_tbl, asset_type): return hits def _lookup_symbol_strict(self, ownership_map, multi_country, symbol, as_of_date): - """ - Resolve a symbol to an asset object without fuzzy matching. + """Resolve a symbol to an asset object without fuzzy matching. Parameters ---------- @@ -979,8 +970,7 @@ def lookup_symbol(self, symbol, as_of_date, fuzzy=False, country_code=None): ) def lookup_symbols(self, symbols, as_of_date, fuzzy=False, country_code=None): - """ - Lookup a list of equities by symbol. + """Lookup a list of equities by symbol. Equivalent to:: @@ -1210,8 +1200,8 @@ def get_ordered_contracts(self, root_symbol): def create_continuous_future(self, root_symbol, offset, roll_style, adjustment): if adjustment not in ADJUSTMENT_STYLES: raise ValueError( - "Invalid adjustment style {!r}. Allowed adjustment styles are " - "{}.".format(adjustment, list(ADJUSTMENT_STYLES)) + f"Invalid adjustment style {adjustment!r}. Allowed adjustment styles are " + f"{list(ADJUSTMENT_STYLES)}." ) oc = self.get_ordered_contracts(root_symbol) @@ -1314,8 +1304,7 @@ def _lookup_generic_scalar_helper(self, obj, as_of_date, country_code): raise NotAssetConvertible("Input was %s, not AssetConvertible." % obj) def lookup_generic(self, obj, as_of_date, country_code): - """ - Convert an object into an Asset or sequence of Assets. + """Convert an object into an Asset or sequence of Assets. This method exists primarily as a convenience for implementing user-facing APIs that can handle multiple kinds of input. It should @@ -1415,8 +1404,7 @@ def _compute_asset_lifetimes(self, **kwargs): return Lifetimes(sid, start.astype("i8"), end.astype("i8")) def lifetimes(self, dates, include_start_date, country_codes): - """ - Compute a DataFrame representing asset lifetimes for the specified date + """Compute a DataFrame representing asset lifetimes for the specified date range. Parameters @@ -1560,8 +1548,7 @@ def was_active(reference_date_value, asset): def only_active_assets(reference_date_value, assets): - """ - Filter an iterable of Asset objects down to just assets that were alive at + """Filter an iterable of Asset objects down to just assets that were alive at the time corresponding to `reference_date_value`. Parameters diff --git a/src/zipline/assets/continuous_futures.pyx b/src/zipline/assets/continuous_futures.pyx index 1f3f2e09bc..f833bac2bb 100644 --- a/src/zipline/assets/continuous_futures.pyx +++ b/src/zipline/assets/continuous_futures.pyx @@ -14,9 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" -Cythonized ContinuousFutures object. -""" +"""Cythonized ContinuousFutures object.""" + cimport cython from cpython.number cimport PyNumber_Index from cpython.object cimport ( @@ -76,8 +75,7 @@ ADJUSTMENT_STYLES = {'add', 'mul', None} cdef class ContinuousFuture: - """ - Represents a specifier for a chain of future contracts, where the + """Represents a specifier for a chain of future contracts, where the coordinates for the chain are: root_symbol : str The root symbol of the contracts. @@ -153,8 +151,8 @@ cdef class ContinuousFuture: return self.sid_hash def __richcmp__(x, y, int op): - """ - Cython rich comparison method. This is used in place of various + """Cython rich comparison method. + This is used in place of various equality checkers in pure python. """ cdef long_t x_as_int, y_as_int @@ -207,8 +205,7 @@ cdef class ContinuousFuture: return 'ContinuousFuture(%d, %s)' % (self.sid, params) cpdef __reduce__(self): - """ - Function used by pickle to determine how to serialize/deserialize this + """Function used by pickle to determine how to serialize/deserialize this class. Should return a tuple whose first element is self.__class__, and whose second element is a tuple of all the attributes that should be serialized/deserialized during pickling. @@ -222,9 +219,7 @@ cdef class ContinuousFuture: self.exchange)) cpdef to_dict(self): - """ - Convert to a python dict. - """ + """Convert to a python dict.""" return { 'sid': self.sid, 'root_symbol': self.root_symbol, @@ -237,14 +232,11 @@ cdef class ContinuousFuture: @classmethod def from_dict(cls, dict_): - """ - Build an ContinuousFuture instance from a dict. - """ + """Build an ContinuousFuture instance from a dict.""" return cls(**dict_) def is_alive_for_session(self, session_label): - """ - Returns whether the continuous future is alive at the given dt. + """Returns whether the continuous future is alive at the given dt. Parameters ---------- @@ -265,6 +257,7 @@ cdef class ContinuousFuture: def is_exchange_open(self, dt_minute): """ + Parameters ---------- dt_minute: pd.Timestamp (UTC, tz-aware) @@ -308,8 +301,7 @@ cdef class ContractNode: cdef class OrderedContracts: - """ - A container for aligned values of a future contract chain, in sorted order + """A container for aligned values of a future contract chain, in sorted order of their occurrence. Used to get answers about contracts in relation to their auto close dates and start dates. @@ -382,9 +374,7 @@ cdef class OrderedContracts: prev = curr cpdef long_t contract_before_auto_close(self, long_t dt_value): - """ - Get the contract with next upcoming auto close date. - """ + """Get the contract with next upcoming auto close date.""" curr = self._head_contract while curr.next is not None: if curr.contract.auto_close_date.value > dt_value: @@ -393,8 +383,7 @@ cdef class OrderedContracts: return curr.contract.sid cpdef contract_at_offset(self, long_t sid, Py_ssize_t offset, int64_t start_cap): - """ - Get the sid which is the given sid plus the offset distance. + """Get the sid which is the given sid plus the offset distance. An offset of 0 should be reflexive. """ cdef Py_ssize_t i @@ -423,8 +412,8 @@ cdef class OrderedContracts: property start_date: def __get__(self): - return Timestamp(self._start_date, tz='UTC') + return Timestamp(self._start_date) property end_date: def __get__(self): - return Timestamp(self._end_date, tz='UTC') + return Timestamp(self._end_date) diff --git a/src/zipline/assets/roll_finder.py b/src/zipline/assets/roll_finder.py index 2c0016bb17..71840b2c10 100644 --- a/src/zipline/assets/roll_finder.py +++ b/src/zipline/assets/roll_finder.py @@ -21,8 +21,7 @@ class RollFinder(ABC): - """ - Abstract base class for calculating when futures contracts are the active + """Abstract base class for calculating when futures contracts are the active contract. """ @@ -31,12 +30,11 @@ def _active_contract(self, oc, front, back, dt): raise NotImplementedError def _get_active_contract_at_offset(self, root_symbol, dt, offset): - """ - For the given root symbol, find the contract that is considered active + """For the given root symbol, find the contract that is considered active on a specific date at a specific offset. """ oc = self.asset_finder.get_ordered_contracts(root_symbol) - session = self.trading_calendar.minute_to_session_label(dt) + session = self.trading_calendar.minute_to_session(dt) front = oc.contract_before_auto_close(session.value) back = oc.contract_at_offset(front, 1, dt.value) if back is None: @@ -46,6 +44,7 @@ def _get_active_contract_at_offset(self, root_symbol, dt, offset): def get_contract_center(self, root_symbol, dt, offset): """ + Parameters ---------- root_symbol : str @@ -64,8 +63,7 @@ def get_contract_center(self, root_symbol, dt, offset): return self._get_active_contract_at_offset(root_symbol, dt, offset) def get_rolls(self, root_symbol, start, end, offset): - """ - Get the rolls, i.e. the session at which to hop from contract to + """Get the rolls, i.e. the session at which to hop from contract to contract in the chain. Parameters @@ -91,7 +89,7 @@ def get_rolls(self, root_symbol, start, end, offset): front = self._get_active_contract_at_offset(root_symbol, end, 0) back = oc.contract_at_offset(front, 1, end.value) if back is not None: - end_session = self.trading_calendar.minute_to_session_label(end) + end_session = self.trading_calendar.minute_to_session(end) first = self._active_contract(oc, front, back, end_session) else: first = front @@ -99,7 +97,7 @@ def get_rolls(self, root_symbol, start, end, offset): rolls = [((first_contract >> offset).contract.sid, None)] tc = self.trading_calendar sessions = tc.sessions_in_range( - tc.minute_to_session_label(start), tc.minute_to_session_label(end) + tc.minute_to_session(start), tc.minute_to_session(end) ) freq = sessions.freq if first == front: @@ -115,12 +113,14 @@ def get_rolls(self, root_symbol, start, end, offset): curr = first_contract << 2 session = sessions[-1] + start = start.tz_localize(None) + while session > start and curr is not None: front = curr.contract.sid back = rolls[0][0] prev_c = curr.prev while session > start: - prev = session - freq + prev = (session - freq).tz_localize(None) if prev_c is not None: if prev < prev_c.contract.auto_close_date: break @@ -139,8 +139,7 @@ def get_rolls(self, root_symbol, start, end, offset): class CalendarRollFinder(RollFinder): - """ - The CalendarRollFinder calculates contract rolls based purely on the + """The CalendarRollFinder calculates contract rolls based purely on the contract's auto close date. """ @@ -156,8 +155,7 @@ def _active_contract(self, oc, front, back, dt): class VolumeRollFinder(RollFinder): - """ - The VolumeRollFinder calculates contract rolls based on when + """The VolumeRollFinder calculates contract rolls based on when volume activity transfers from one contract to another. """ @@ -231,8 +229,8 @@ def _active_contract(self, oc, front, back, dt): # date, and a volume flip happened during that period, return the back # contract as the active one. sessions = tc.sessions_in_range( - tc.minute_to_session_label(gap_start), - tc.minute_to_session_label(gap_end), + tc.minute_to_session(gap_start), + tc.minute_to_session(gap_end), ) for session in sessions: front_vol = get_value(front, session, "volume") @@ -243,6 +241,7 @@ def _active_contract(self, oc, front, back, dt): def get_contract_center(self, root_symbol, dt, offset): """ + Parameters ---------- root_symbol : str @@ -268,7 +267,7 @@ def get_contract_center(self, root_symbol, dt, offset): day = self.trading_calendar.day end_date = min( dt + (ROLL_DAYS_FOR_CURRENT_CONTRACT * day), - self.session_reader.last_available_dt, + self.session_reader.last_available_dt.tz_localize(dt.tzinfo), ) rolls = self.get_rolls( root_symbol=root_symbol, diff --git a/src/zipline/assets/synthetic.py b/src/zipline/assets/synthetic.py index ba9c749856..8da37673f7 100644 --- a/src/zipline/assets/synthetic.py +++ b/src/zipline/assets/synthetic.py @@ -15,8 +15,7 @@ def make_rotating_equity_info( asset_lifetime, exchange="TEST", ): - """ - Create a DataFrame representing lifetimes of assets that are constantly + """Create a DataFrame representing lifetimes of assets that are constantly rotating in and out of existence. Parameters @@ -63,8 +62,7 @@ def make_rotating_equity_info( def make_simple_equity_info( sids, start_date, end_date, symbols=None, names=None, exchange="TEST" ): - """ - Create a DataFrame representing assets that exist for the full duration + """Create a DataFrame representing assets that exist for the full duration between `start_date` and `end_date`. Parameters @@ -154,8 +152,7 @@ def make_simple_multi_country_equity_info( def make_jagged_equity_info( num_assets, start_date, first_end, frequency, periods_between_ends, auto_close_delta ): - """ - Create a DataFrame representing assets that all begin at the same start + """Create a DataFrame representing assets that all begin at the same start date, but have cascading end dates. Parameters @@ -210,8 +207,7 @@ def make_future_info( month_codes=None, multiplier=500, ): - """ - Create a DataFrame representing futures for `root_symbols` during `year`. + """Create a DataFrame representing futures for `root_symbols` during `year`. Generates a contract per triple of (symbol, year, month) supplied to `root_symbols`, `years`, and `month_codes`. @@ -284,8 +280,7 @@ def make_future_info( def make_commodity_future_info( first_sid, root_symbols, years, month_codes=None, multiplier=500 ): - """ - Make futures testing data that simulates the notice/expiration date + """Make futures testing data that simulates the notice/expiration date behavior of physical commodities like oil. Parameters diff --git a/src/zipline/data/_adjustments.pyx b/src/zipline/data/_adjustments.pyx index c56676fd90..a85ba462c1 100644 --- a/src/zipline/data/_adjustments.pyx +++ b/src/zipline/data/_adjustments.pyx @@ -59,8 +59,7 @@ cdef set _get_sids_from_table(object db, str tablename, int start_date, int end_date): - """ - Get the unique sids for all adjustments between start_date and end_date + """Get the unique sids for all adjustments between start_date and end_date from table `tablename`. Parameters @@ -167,8 +166,7 @@ cpdef load_adjustments_from_sqlite(object adjustments_db, bool should_include_mergers, bool should_include_dividends, str adjustment_type): - """ - Load a dictionary of Adjustment objects from adjustments_db. + """Load a dictionary of Adjustment objects from adjustments_db. Parameters ---------- diff --git a/src/zipline/data/_equities.pyx b/src/zipline/data/_equities.pyx index 16d8b9b659..a052804964 100644 --- a/src/zipline/data/_equities.pyx +++ b/src/zipline/data/_equities.pyx @@ -151,8 +151,7 @@ cpdef _read_bcolz_data(ctable_t table, intp_t[:] last_rows, intp_t[:] offsets, bool read_all): - """ - Load raw bcolz data for the given columns and indices. + """Load raw bcolz data for the given columns and indices. Parameters ---------- diff --git a/src/zipline/data/_minute_bar_internal.pyx b/src/zipline/data/_minute_bar_internal.pyx index 61818ae108..28460b2b6d 100644 --- a/src/zipline/data/_minute_bar_internal.pyx +++ b/src/zipline/data/_minute_bar_internal.pyx @@ -9,8 +9,7 @@ cdef inline int int_min(int a, int b): return a if a <= b else b def minute_value(ndarray[long_t, ndim=1] market_opens, Py_ssize_t pos, short minutes_per_day): - """ - Finds the value of the minute represented by `pos` in the given array of + """Finds the value of the minute represented by `pos` in the given array of market opens. Parameters @@ -40,8 +39,7 @@ def find_position_of_minute(ndarray[long_t, ndim=1] market_opens, long_t minute_val, short minutes_per_day, bool forward_fill): - """ - Finds the position of a given minute in the given array of market opens. + """Finds the position of a given minute in the given array of market opens. If not a market minute, adjusts to the last market minute. Parameters @@ -76,8 +74,7 @@ def find_position_of_minute(ndarray[long_t, ndim=1] market_opens, """ cdef Py_ssize_t market_open_loc, market_open, delta - market_open_loc = \ - searchsorted(market_opens, minute_val, side='right') - 1 + market_open_loc = searchsorted(market_opens, minute_val, side='right') - 1 market_open = market_opens[market_open_loc] market_close = market_closes[market_open_loc] @@ -96,8 +93,7 @@ def find_last_traded_position_internal( volumes, short minutes_per_day): - """ - Finds the position of the last traded minute for the given volumes array. + """Finds the position of the last traded minute for the given volumes array. Parameters ---------- diff --git a/src/zipline/data/adjustments.py b/src/zipline/data/adjustments.py index d198466bdb..bc378f77be 100644 --- a/src/zipline/data/adjustments.py +++ b/src/zipline/data/adjustments.py @@ -80,8 +80,7 @@ def specialize_any_integer(d): class SQLiteAdjustmentReader: - """ - Loads adjustments based on corporate actions from a SQLite database. + """Loads adjustments based on corporate actions from a SQLite database. Expects data written in the format output by `SQLiteAdjustmentWriter`. @@ -150,8 +149,7 @@ def load_adjustments( should_include_dividends, adjustment_type, ): - """ - Load collection of Adjustment objects from underlying adjustments db. + """Load collection of Adjustment objects from underlying adjustments db. Parameters ---------- @@ -175,6 +173,7 @@ def load_adjustments( A dictionary containing price and/or volume adjustment mappings from index to adjustment objects to apply at that index. """ + dates = dates.tz_localize("UTC") return load_adjustments_from_sqlite( self.conn, dates, @@ -218,7 +217,7 @@ def get_adjustments_for_sid(self, table_name, sid): c.close() return [ - [Timestamp(adjustment[0], unit="s", tz="UTC"), adjustment[1]] + [Timestamp(adjustment[0], unit="s"), adjustment[1]] for adjustment in adjustments_for_sid ] @@ -300,15 +299,14 @@ def get_df_from_table(self, table_name, convert_dates=False): date_cols = self._datetime_int_cols[table_name] except KeyError as exc: raise ValueError( - "Requested table {} not found.\n" - "Available tables: {}\n".format( - table_name, self._datetime_int_cols.keys() - ) + f"Requested table {table_name} not found.\n" + f"Available tables: {self._datetime_int_cols.keys()}\n" ) from exc # Dates are stored in second resolution as ints in adj.db tables. kwargs = ( - {"parse_dates": {col: {"unit": "s", "utc": True} for col in date_cols}} + # {"parse_dates": {col: {"unit": "s", "utc": True} for col in date_cols}} + {"parse_dates": {col: {"unit": "s"} for col in date_cols}} if convert_dates else {} ) @@ -340,8 +338,7 @@ def _df_dtypes(self, table_name, convert_dates): class SQLiteAdjustmentWriter: - """ - Writer for data to be read by SQLiteAdjustmentReader + """Writer for data to be read by SQLiteAdjustmentReader Parameters ---------- @@ -426,11 +423,7 @@ def _write(self, tablename, expected_dtypes, frame): def write_frame(self, tablename, frame): if tablename not in SQLITE_ADJUSTMENT_TABLENAMES: raise ValueError( - "Adjustment table %s not in %s" - % ( - tablename, - SQLITE_ADJUSTMENT_TABLENAMES, - ) + f"Adjustment table {tablename} not in {SQLITE_ADJUSTMENT_TABLENAMES}" ) if not (frame is None or frame.empty): frame = frame.copy() @@ -448,9 +441,7 @@ def write_frame(self, tablename, frame): ) def write_dividend_payouts(self, frame): - """ - Write dividend payout data to SQLite table `dividend_payouts`. - """ + """Write dividend payout data to SQLite table `dividend_payouts`.""" return self._write( "dividend_payouts", SQLITE_DIVIDEND_PAYOUT_COLUMN_DTYPES, @@ -465,8 +456,7 @@ def write_stock_dividend_payouts(self, frame): ) def calc_dividend_ratios(self, dividends): - """ - Calculate the ratios to apply to equities when looking back at pricing + """Calculate the ratios to apply to equities when looking back at pricing history so that the price is smoothed over the ex_date, when the market adjusts to the change in equity value due to upcoming dividend. @@ -497,8 +487,8 @@ def calc_dividend_ratios(self, dividends): (close,) = pricing_reader.load_raw_arrays( ["close"], - pd.Timestamp(dates[0]).tz_localize("UTC"), - pd.Timestamp(dates[-1]).tz_localize("UTC"), + pd.Timestamp(dates[0]), + pd.Timestamp(dates[-1]), unique_sids, ) date_ix = np.searchsorted(dates, dividends.ex_date.values) @@ -606,9 +596,7 @@ def _write_stock_dividends(self, stock_dividends): self.write_stock_dividend_payouts(stock_dividend_payouts) def write_dividend_data(self, dividends, stock_dividends=None): - """ - Write both dividend payouts and the derived price adjustment ratios. - """ + """Write both dividend payouts and the derived price adjustment ratios.""" # First write the dividend payouts. self._write_dividends(dividends) @@ -619,8 +607,7 @@ def write_dividend_data(self, dividends, stock_dividends=None): self.write_frame("dividends", dividend_ratios) def write(self, splits=None, mergers=None, dividends=None, stock_dividends=None): - """ - Writes data to a SQLite file to be read by SQLiteAdjustmentReader. + """Writes data to a SQLite file to be read by SQLiteAdjustmentReader. Parameters ---------- diff --git a/src/zipline/data/bcolz_daily_bars.py b/src/zipline/data/bcolz_daily_bars.py index f217622b7b..5d26104c0e 100644 --- a/src/zipline/data/bcolz_daily_bars.py +++ b/src/zipline/data/bcolz_daily_bars.py @@ -11,8 +11,8 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from functools import partial import warnings +from functools import partial with warnings.catch_warnings(): # noqa warnings.filterwarnings("ignore", category=DeprecationWarning) @@ -21,33 +21,17 @@ import logging -from numpy import ( - array, - full, - iinfo, - nan, -) -from pandas import ( - DatetimeIndex, - NaT, - read_csv, - to_datetime, - Timestamp, -) -from toolz import compose -from zipline.utils.calendar_utils import get_calendar +import pandas as pd +from zipline.data.bar_reader import NoDataAfterDate, NoDataBeforeDate, NoDataOnDate from zipline.data.session_bars import CurrencyAwareSessionBarReader -from zipline.data.bar_reader import ( - NoDataAfterDate, - NoDataBeforeDate, - NoDataOnDate, -) +from zipline.utils.calendar_utils import get_calendar +from zipline.utils.cli import maybe_show_progress from zipline.utils.functional import apply from zipline.utils.input_validation import expect_element -from zipline.utils.numpy_utils import iNaT, float64_dtype, uint32_dtype from zipline.utils.memoize import lazyval -from zipline.utils.cli import maybe_show_progress +from zipline.utils.numpy_utils import float64_dtype, iNaT, uint32_dtype + from ._equities import _compute_row_slices, _read_bcolz_data logger = logging.getLogger("UsEquityPricing") @@ -63,7 +47,7 @@ "id", ) -UINT32_MAX = iinfo(np.uint32).max +UINT32_MAX = np.iinfo(np.uint32).max def check_uint32_safe(value, colname): @@ -112,7 +96,7 @@ def winsorise_uint32(df, invalid_data_behavior, column, *columns): if invalid_data_behavior == "warn": warnings.warn( "Ignoring %d values because they are out of bounds for" - " uint32: %r" + " uint32:\n %r" % ( mv.sum(), df[mask.any(axis=1)], @@ -125,8 +109,7 @@ def winsorise_uint32(df, invalid_data_behavior, column, *columns): class BcolzDailyBarWriter: - """ - Class capable of writing daily OHLCV data to disk in a format that can + """Class capable of writing daily OHLCV data to disk in a format that can be read efficiently by BcolzDailyOHLCVReader. Parameters @@ -155,6 +138,8 @@ class BcolzDailyBarWriter: def __init__(self, filename, calendar, start_session, end_session): self._filename = filename + start_session = start_session.tz_localize(None) + end_session = end_session.tz_localize(None) if start_session != end_session: if not calendar.is_session(start_session): @@ -178,6 +163,7 @@ def write( self, data, assets=None, show_progress=False, invalid_data_behavior="warn" ): """ + Parameters ---------- data : iterable[tuple[int, pandas.DataFrame or bcolz.ctable]] @@ -223,7 +209,7 @@ def write_csvs(self, asset_map, show_progress=False, invalid_data_behavior="warn a uint32. """ read = partial( - read_csv, + pd.read_csv, parse_dates=["day"], index_col="day", dtype=self._csv_dtypes, @@ -236,8 +222,7 @@ def write_csvs(self, asset_map, show_progress=False, invalid_data_behavior="warn ) def _write_internal(self, iterator, assets): - """ - Internal implementation of write. + """Internal implementation of write. `iterator` should be an iterator yielding pairs of (asset, ctable). """ @@ -248,7 +233,7 @@ def _write_internal(self, iterator, assets): # Maps column name -> output carray. columns = { - k: carray(array([], dtype=uint32_dtype)) + k: carray(np.array([], dtype=uint32_dtype)) for k in US_EQUITY_PRICING_BCOLZ_COLUMNS } @@ -273,7 +258,7 @@ def iterator(iterator=iterator, assets=set(assets)): # We know what the content of this column is, so don't # bother reading it. columns["id"].append( - full((nrows,), asset_id, dtype="uint32"), + np.full((nrows,), asset_id, dtype="uint32"), ) continue @@ -295,41 +280,31 @@ def iterator(iterator=iterator, assets=set(assets)): last_row[asset_key] = total_rows + nrows - 1 total_rows += nrows - table_day_to_session = compose( - self._calendar.minute_to_session_label, - partial(Timestamp, unit="s", tz="UTC"), - ) - asset_first_day = table_day_to_session(table["day"][0]) - asset_last_day = table_day_to_session(table["day"][-1]) + asset_first_day = pd.Timestamp(table["day"][0], unit="s").normalize() + asset_last_day = pd.Timestamp(table["day"][-1], unit="s").normalize() asset_sessions = sessions[ sessions.slice_indexer(asset_first_day, asset_last_day) ] - assert len(table) == len(asset_sessions), ( - "Got {} rows for daily bars table with first day={}, last " - "day={}, expected {} rows.\n" - "Missing sessions: {}\n" - "Extra sessions: {}".format( - len(table), - asset_first_day.date(), - asset_last_day.date(), - len(asset_sessions), - asset_sessions.difference( - to_datetime( - np.array(table["day"]), - unit="s", - utc=True, - ) - ).tolist(), - to_datetime( - np.array(table["day"]), - unit="s", - utc=True, - ) + if len(table) != len(asset_sessions): + + missing_sessions = asset_sessions.difference( + pd.to_datetime(np.array(table["day"]), unit="s") + ).tolist() + + extra_sessions = ( + pd.to_datetime(np.array(table["day"]), unit="s") .difference(asset_sessions) - .tolist(), + .tolist() ) - ) + raise AssertionError( + f"Got {len(table)} rows for daily bars table with " + f"first day={asset_first_day.date()}, last " + f"day={asset_last_day.date()}, expected {len(asset_sessions)} rows.\n" + f"Missing sessions: {missing_sessions}\nExtra sessions: {extra_sessions}" + ) + + # assert len(table) == len(asset_sessions), ( # Calculate the number of trading days between the first date # in the stored data and the first date of **this** asset. This @@ -373,8 +348,7 @@ def to_ctable(self, raw_data, invalid_data_behavior): class BcolzDailyBarReader(CurrencyAwareSessionBarReader): - """ - Reader for raw pricing data written by BcolzDailyOHLCVWriter. + """Reader for raw pricing data written by BcolzDailyOHLCVWriter. Parameters ---------- @@ -465,14 +439,15 @@ def _table(self): def sessions(self): if "calendar" in self._table.attrs.attrs: # backwards compatibility with old formats, will remove - return DatetimeIndex(self._table.attrs["calendar"], tz="UTC") + return pd.DatetimeIndex(self._table.attrs["calendar"]) else: cal = get_calendar(self._table.attrs["calendar_name"]) start_session_ns = self._table.attrs["start_session_ns"] - start_session = Timestamp(start_session_ns, tz="UTC") + + start_session = pd.Timestamp(start_session_ns) end_session_ns = self._table.attrs["end_session_ns"] - end_session = Timestamp(end_session_ns, tz="UTC") + end_session = pd.Timestamp(end_session_ns) sessions = cal.sessions_in_range(start_session, end_session) @@ -502,7 +477,7 @@ def _calendar_offsets(self): @lazyval def first_trading_day(self): try: - return Timestamp(self._table.attrs["first_trading_day"], unit="s", tz="UTC") + return pd.Timestamp(self._table.attrs["first_trading_day"], unit="s") except KeyError: return None @@ -518,8 +493,7 @@ def last_available_dt(self): return self.sessions[-1] def _compute_slices(self, start_idx, end_idx, assets): - """ - Compute the raw row indices to load for each asset on a query for the + """Compute the raw row indices to load for each asset on a query for the given dates after applying a shift. Parameters @@ -581,13 +555,13 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): def _load_raw_arrays_date_to_index(self, date): try: + # TODO get_loc is deprecated but get_indexer doesnt raise and error return self.sessions.get_loc(date) except KeyError as exc: raise NoDataOnDate(date) from exc def _spot_col(self, colname): - """ - Get the colname from daily_bar_table and read all of it into memory, + """Get the colname from daily_bar_table and read all of it into memory, caching the result. Parameters @@ -616,24 +590,25 @@ def get_last_traded_dt(self, asset, day): try: ix = self.sid_day_index(asset, search_day) except NoDataBeforeDate: - return NaT + return pd.NaT except NoDataAfterDate: prev_day_ix = self.sessions.get_loc(search_day) - 1 if prev_day_ix > -1: search_day = self.sessions[prev_day_ix] continue except NoDataOnDate: - return NaT + return pd.NaT if volumes[ix] != 0: return search_day prev_day_ix = self.sessions.get_loc(search_day) - 1 if prev_day_ix > -1: search_day = self.sessions[prev_day_ix] else: - return NaT + return pd.NaT def sid_day_index(self, sid, day): """ + Parameters ---------- sid : int @@ -668,6 +643,7 @@ def sid_day_index(self, sid, day): def get_value(self, sid, dt, field): """ + Parameters ---------- sid : int @@ -690,7 +666,7 @@ def get_value(self, sid, dt, field): price = self._spot_col(field)[ix] if field != "volume": if price == 0: - return nan + return np.nan else: return price * 0.001 else: diff --git a/src/zipline/data/bcolz_minute_bars.py b/src/zipline/data/bcolz_minute_bars.py index 5c0ee144a6..fde027bfc5 100644 --- a/src/zipline/data/bcolz_minute_bars.py +++ b/src/zipline/data/bcolz_minute_bars.py @@ -11,33 +11,31 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -from abc import ABC, abstractmethod import json +import logging import os +from abc import ABC, abstractmethod from glob import glob from os.path import join -from textwrap import dedent -from lru import LRU import bcolz -from bcolz import ctable -from intervaltree import IntervalTree -import logging import numpy as np import pandas as pd +from bcolz import ctable +from intervaltree import IntervalTree +from lru import LRU from pandas import HDFStore from toolz import keymap, valmap -from zipline.utils.calendar_utils import get_calendar from zipline.data._minute_bar_internal import ( - minute_value, - find_position_of_minute, find_last_traded_position_internal, + find_position_of_minute, + minute_value, ) - -from zipline.gens.sim_engine import NANOS_IN_MINUTE from zipline.data.bar_reader import BarReader, NoDataForSid, NoDataOnDate from zipline.data.bcolz_daily_bars import check_uint32_safe +from zipline.gens.sim_engine import NANOS_IN_MINUTE +from zipline.utils.calendar_utils import get_calendar from zipline.utils.cli import maybe_show_progress from zipline.utils.compat import mappingproxy from zipline.utils.memoize import lazyval @@ -79,8 +77,7 @@ def _calc_minute_index(market_opens, minutes_per_day): def _sid_subdir_path(sid): - """ - Format subdir path to limit the number directories in any given + """Format subdir path to limit the number directories in any given subdirectory to 100. The number in each directory is designed to support at least 100000 @@ -180,6 +177,7 @@ def convert_cols(cols, scale_factor, sid, invalid_data_behavior): class BcolzMinuteBarMetadata: """ + Parameters ---------- ohlc_ratio : int @@ -226,16 +224,16 @@ def read(cls, rootdir): if version >= 2: calendar = get_calendar(raw_data["calendar_name"]) - start_session = pd.Timestamp(raw_data["start_session"], tz="UTC") - end_session = pd.Timestamp(raw_data["end_session"], tz="UTC") + start_session = pd.Timestamp(raw_data["start_session"]) + end_session = pd.Timestamp(raw_data["end_session"]) else: # No calendar info included in older versions, so # default to NYSE. calendar = get_calendar("XNYS") - start_session = pd.Timestamp(raw_data["first_trading_day"], tz="UTC") - end_session = calendar.minute_to_session_label( - pd.Timestamp(raw_data["market_closes"][-1], unit="m", tz="UTC") + start_session = pd.Timestamp(raw_data["first_trading_day"]) + end_session = calendar.minute_to_session( + pd.Timestamp(raw_data["market_closes"][-1], unit="m") ) if version >= 3: @@ -274,8 +272,7 @@ def __init__( self.version = version def write(self, rootdir): - """ - Write the metadata to a JSON file in the rootdir. + """Write the metadata to a JSON file in the rootdir. Values contained in the metadata are: @@ -303,15 +300,6 @@ def write(self, rootdir): session in the data set. """ - # calendar = self.calendar - # slicer = calendar.schedule.index.slice_indexer( - # self.start_session, - # self.end_session, - # ) - # schedule = calendar.schedule[slicer] - # market_opens = schedule.market_open - # market_closes = schedule.market_close - metadata = { "version": self.version, "ohlc_ratio": self.default_ohlc_ratio, @@ -326,8 +314,7 @@ def write(self, rootdir): class BcolzMinuteBarWriter: - """ - Class capable of writing minute OHLCV data to disk into bcolz format. + """Class capable of writing minute OHLCV data to disk into bcolz format. Parameters ---------- @@ -434,7 +421,9 @@ def __init__( self._start_session = start_session self._end_session = end_session self._calendar = calendar - slicer = calendar.schedule.index.slice_indexer(start_session, end_session) + slicer = calendar.schedule.index.slice_indexer( + self._start_session, self._end_session + ) self._schedule = calendar.schedule[slicer] self._session_labels = self._schedule.index self._minutes_per_day = minutes_per_day @@ -443,7 +432,7 @@ def __init__( self._ohlc_ratios_per_sid = ohlc_ratios_per_sid self._minute_index = _calc_minute_index( - self._schedule.market_open, self._minutes_per_day + calendar.first_minutes[slicer], self._minutes_per_day ) if write_metadata: @@ -459,8 +448,7 @@ def __init__( @classmethod def open(cls, rootdir, end_session=None): - """ - Open an existing ``rootdir`` for writing. + """Open an existing ``rootdir`` for writing. Parameters ---------- @@ -496,6 +484,7 @@ def ohlc_ratio_for_sid(self, sid): def sidpath(self, sid): """ + Parameters ---------- sid : int @@ -511,6 +500,7 @@ def sidpath(self, sid): def last_date_in_output_for_sid(self, sid): """ + Parameters ---------- sid : int @@ -537,8 +527,7 @@ def last_date_in_output_for_sid(self, sid): return self._session_labels[num_days - 1] def _init_ctable(self, path): - """ - Create empty ctable for given path. + """Create empty ctable for given path. Parameters ---------- @@ -589,8 +578,7 @@ def _zerofill(self, table, numdays): table.flush() def pad(self, sid, date): - """ - Fill sid container with empty data through the specified date. + """Fill sid container with empty data through the specified date. If the last recorded trade is not at the close, then that day will be padded with zeros until its close. Any day after that (up to and @@ -670,8 +658,7 @@ def write(self, data, show_progress=False, invalid_data_behavior="warn"): write_sid(*e, invalid_data_behavior=invalid_data_behavior) def write_sid(self, sid, df, invalid_data_behavior="warn"): - """ - Write the OHLCV data for the given sid. + """Write the OHLCV data for the given sid. If there is no bcolz ctable yet created for the sid, create it. If the length of the bcolz ctable is not exactly to the date before the first day provided, fill the ctable with 0s up to that date. @@ -703,8 +690,7 @@ def write_sid(self, sid, df, invalid_data_behavior="warn"): self._write_cols(sid, dts, cols, invalid_data_behavior) def write_cols(self, sid, dts, cols, invalid_data_behavior="warn"): - """ - Write the OHLCV data for the given sid. + """Write the OHLCV data for the given sid. If there is no bcolz ctable yet created for the sid, create it. If the length of the bcolz ctable is not exactly to the date before the first day provided, fill the ctable with 0s up to that date. @@ -737,8 +723,7 @@ def write_cols(self, sid, dts, cols, invalid_data_behavior="warn"): self._write_cols(sid, dts, cols, invalid_data_behavior) def _write_cols(self, sid, dts, cols, invalid_data_behavior): - """ - Internal method for `write_cols` and `write`. + """Internal method for `write_cols` and `write`. Parameters ---------- @@ -758,7 +743,7 @@ def _write_cols(self, sid, dts, cols, invalid_data_behavior): table = self._ensure_ctable(sid) tds = self._session_labels - input_first_day = self._calendar.minute_to_session_label( + input_first_day = self._calendar.minute_to_session( pd.Timestamp(dts[0]), direction="previous" ) @@ -787,11 +772,9 @@ def _write_cols(self, sid, dts, cols, invalid_data_behavior): last_recorded_minute = all_minutes[num_rec_mins - 1] if last_minute_to_write <= last_recorded_minute: raise BcolzMinuteOverlappingData( - dedent( - """ - Data with last_date={0} already includes input start={1} for - sid={2}""".strip() - ).format(last_date, input_first_day, sid) + f"Data with last_date={last_date} " + f"already includes input start={input_first_day} " + f"for\n sid={sid}" ) latest_min_count = all_minutes.get_loc(last_minute_to_write) @@ -826,8 +809,7 @@ def _write_cols(self, sid, dts, cols, invalid_data_behavior): table.flush() def data_len_for_day(self, day): - """ - Return the number of data points up to and including the + """Return the number of data points up to and including the provided day. """ day_ix = self._session_labels.get_loc(day) @@ -864,8 +846,7 @@ def truncate(self, date): class BcolzMinuteBarReader(MinuteBarReader): - """ - Reader for data written by BcolzMinuteBarWriter + """Reader for data written by BcolzMinuteBarWriter Parameters ---------- @@ -910,11 +891,11 @@ def __init__(self, rootdir, sid_cache_sizes=_default_proxy): self._end_session, ) self._schedule = self.calendar.schedule[slicer] - self._market_opens = self._schedule.market_open + self._market_opens = self.calendar.first_minutes[slicer] self._market_open_values = self._market_opens.values.astype( "datetime64[m]" ).astype(np.int64) - self._market_closes = self._schedule.market_close + self._market_closes = self._schedule.close self._market_close_values = self._market_closes.values.astype( "datetime64[m]" ).astype(np.int64) @@ -950,7 +931,7 @@ def trading_calendar(self): @lazyval def last_available_dt(self): - _, close = self.calendar.open_and_close_for_session(self._end_session) + close = self.calendar.session_close(self._end_session) return close @property @@ -969,8 +950,7 @@ def _ohlc_ratio_inverse_for_sid(self, sid): return self._default_ohlc_inverse def _minutes_to_exclude(self): - """ - Calculate the minutes which should be excluded when a window + """Calculate the minutes which should be excluded when a window occurs on days which had an early close, i.e. days where the close based on the regular period of minutes per day and the market close do not match. @@ -994,8 +974,7 @@ def _minutes_to_exclude(self): @lazyval def _minute_exclusion_tree(self): - """ - Build an interval tree keyed by the start and end of each range + """Build an interval tree keyed by the start and end of each range of positions should be dropped from windows. (These are the minutes between an early close and the minute which would be the close based on the regular period if there were no early close.) @@ -1022,6 +1001,7 @@ def _minute_exclusion_tree(self): def _exclusion_indices_for_range(self, start_idx, end_idx): """ + Returns ------- List of tuples of (start, stop) which represent the ranges of minutes @@ -1072,8 +1052,7 @@ def get_sid_attr(self, sid, name): return None def get_value(self, sid, dt, field): - """ - Retrieve the pricing info for the given sid, dt, and field. + """Retrieve the pricing info for the given sid, dt, and field. Parameters ---------- @@ -1174,8 +1153,7 @@ def _pos_to_minute(self, pos): return pd.Timestamp(minute_epoch, tz="UTC", unit="m") def _find_position_of_minute(self, minute_dt): - """ - Internal method that returns the position of the given minute in the + """Internal method that returns the position of the given minute in the list of every trading minute since market open of the first trading day. Adjusts non market minutes to the last close. @@ -1202,6 +1180,7 @@ def _find_position_of_minute(self, minute_dt): def load_raw_arrays(self, fields, start_dt, end_dt, sids): """ + Parameters ---------- fields : list of str @@ -1266,14 +1245,11 @@ def load_raw_arrays(self, fields, start_dt, end_dt, sids): class MinuteBarUpdateReader(ABC): - """ - Abstract base class for minute update readers. - """ + """Abstract base class for minute update readers.""" @abstractmethod def read(self, dts, sids): - """ - Read and return pricing update data. + """Read and return pricing update data. Parameters ---------- @@ -1291,8 +1267,7 @@ def read(self, dts, sids): class H5MinuteBarUpdateWriter: - """ - Writer for files containing minute bar updates for consumption by a writer + """Writer for files containing minute bar updates for consumption by a writer for a ``MinuteBarReader`` format. Parameters @@ -1316,8 +1291,7 @@ def __init__(self, path, complevel=None, complib=None): self._path = path def write(self, frames): - """ - Write the frames to the target HDF5 file with ``pd.MultiIndex`` + """Write the frames to the target HDF5 file with ``pd.MultiIndex`` Parameters ---------- @@ -1335,8 +1309,7 @@ def write(self, frames): class H5MinuteBarUpdateReader(MinuteBarUpdateReader): - """ - Reader for minute bar updates stored in HDF5 files. + """Reader for minute bar updates stored in HDF5 files. Parameters ---------- diff --git a/src/zipline/data/benchmarks.py b/src/zipline/data/benchmarks.py index dc99da80ab..7cd69ce855 100644 --- a/src/zipline/data/benchmarks.py +++ b/src/zipline/data/benchmarks.py @@ -20,8 +20,7 @@ def get_benchmark_returns_from_file(filelike): - """ - Get a Series of benchmark returns from a file + """Get a Series of benchmark returns from a file Parameters ---------- @@ -40,8 +39,8 @@ def get_benchmark_returns_from_file(filelike): index_col=["date"], parse_dates=["date"], ) - if not df.index.tz: - df = df.tz_localize("utc") + if df.index.tz is not None: + df = df.tz_localize(None) if "return" not in df.columns: raise ValueError( diff --git a/src/zipline/data/continuous_future_reader.py b/src/zipline/data/continuous_future_reader.py index 06d8a9b57d..94e72d33a7 100644 --- a/src/zipline/data/continuous_future_reader.py +++ b/src/zipline/data/continuous_future_reader.py @@ -10,6 +10,7 @@ def __init__(self, bar_reader, roll_finders): def load_raw_arrays(self, columns, start_date, end_date, assets): """ + Parameters ---------- fields : list of str @@ -95,6 +96,7 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): @property def last_available_dt(self): """ + Returns ------- dt : pd.Timestamp @@ -104,8 +106,7 @@ def last_available_dt(self): @property def trading_calendar(self): - """ - Returns the zipline.utils.calendar.trading_calendar used to read + """Returns the zipline.utils.calendar.trading_calendar used to read the data. Can be None (if the writer didn't specify it). """ return self._bar_reader.trading_calendar @@ -113,6 +114,7 @@ def trading_calendar(self): @property def first_trading_day(self): """ + Returns ------- dt : pd.Timestamp @@ -122,8 +124,7 @@ def first_trading_day(self): return self._bar_reader.first_trading_day def get_value(self, continuous_future, dt, field): - """ - Retrieve the value at the given coordinates. + """Retrieve the value at the given coordinates. Parameters ---------- @@ -153,8 +154,7 @@ def get_value(self, continuous_future, dt, field): return self._bar_reader.get_value(sid, dt, field) def get_last_traded_dt(self, asset, dt): - """ - Get the latest minute on or before ``dt`` in which ``asset`` traded. + """Get the latest minute on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. @@ -181,6 +181,7 @@ def get_last_traded_dt(self, asset, dt): @property def sessions(self): """ + Returns ------- sessions : DatetimeIndex @@ -218,8 +219,8 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): rolls_by_asset = {} tc = self.trading_calendar - start_session = tc.minute_to_session_label(start_date) - end_session = tc.minute_to_session_label(end_date) + start_session = tc.minute_to_session(start_date) + end_session = tc.minute_to_session(end_date) for asset in assets: rf = self._roll_finders[asset.roll_style] @@ -227,7 +228,10 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): asset.root_symbol, start_session, end_session, asset.offset ) - sessions = tc.sessions_in_range(start_date, end_date) + sessions = tc.sessions_in_range( + start_date.normalize().tz_localize(None), + end_date.normalize().tz_localize(None), + ) minutes = tc.minutes_in_range(start_date, end_date) num_minutes = len(minutes) @@ -246,15 +250,15 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): sid, roll_date = roll start_loc = minutes.searchsorted(start) if roll_date is not None: - _, end = tc.open_and_close_for_session(roll_date - sessions.freq) + end = tc.session_close(roll_date - sessions.freq) end_loc = minutes.searchsorted(end) else: end = end_date end_loc = len(minutes) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll[-1] is not None: - start, _ = tc.open_and_close_for_session( - tc.minute_to_session_label(minutes[end_loc + 1]) + start = tc.session_first_minute( + tc.minute_to_session(minutes[end_loc + 1]) ) for column in columns: diff --git a/src/zipline/data/data_portal.py b/src/zipline/data/data_portal.py index 89ac6cabdf..5ade6eed0a 100644 --- a/src/zipline/data/data_portal.py +++ b/src/zipline/data/data_portal.py @@ -54,7 +54,6 @@ from zipline.data.bar_reader import NoDataOnDate from zipline.utils.memoize import remember_last -from zipline.utils.pandas_utils import normalize_date from zipline.errors import HistoryWindowStartsBeforeData @@ -248,7 +247,7 @@ def __init__( } self._daily_aggregator = DailyHistoryAggregator( - self.trading_calendar.schedule.market_open, + self.trading_calendar.first_minutes, _dispatch_minute_reader, self.trading_calendar, ) @@ -272,15 +271,15 @@ def __init__( self._first_trading_day = first_trading_day # Get the first trading minute - self._first_trading_minute, _ = ( - self.trading_calendar.open_and_close_for_session(self._first_trading_day) + self._first_trading_minute = ( + self.trading_calendar.session_first_minute(self._first_trading_day) if self._first_trading_day is not None else (None, None) ) # Store the locs of the first day and first minute self._first_trading_day_loc = ( - self.trading_calendar.all_sessions.get_loc(self._first_trading_day) + self.trading_calendar.sessions.get_loc(self._first_trading_day) if self._first_trading_day is not None else None ) @@ -310,8 +309,7 @@ def _reindex_extra_source(self, df, source_date_index): return df.reindex(index=source_date_index, method="ffill") def handle_extra_source(self, source_df, sim_params): - """ - Extra sources always have a sid column. + """Extra sources always have a sid column. We expand the given data (by forward filling) to the full range of the simulation dates, so that lookup is fast during simulation. @@ -320,7 +318,7 @@ def handle_extra_source(self, source_df, sim_params): return # Normalize all the dates in the df - source_df.index = source_df.index.normalize() + source_df.index = source_df.index.normalize().tz_localize(None) # source_df's sid column can either consist of assets we know about # (such as sid(24)) or of assets we don't know about (such as @@ -384,8 +382,7 @@ def _get_pricing_reader(self, data_frequency): return self._pricing_readers[data_frequency] def get_last_traded_dt(self, asset, dt, data_frequency): - """ - Given an asset and dt, returns the last traded dt from the viewpoint + """Given an asset and dt, returns the last traded dt from the viewpoint of the given dt. If there is a trade on the dt, the answer is dt provided. @@ -394,8 +391,7 @@ def get_last_traded_dt(self, asset, dt, data_frequency): @staticmethod def _is_extra_source(asset, field, map): - """ - Internal method that determines if this asset/field combination + """Internal method that determines if this asset/field combination represents a fetcher value or a regular OHLCVP lookup. """ # If we have an extra source with a column called "price", only look @@ -407,7 +403,7 @@ def _is_extra_source(asset, field, map): ) def _get_fetcher_value(self, asset, field, dt): - day = normalize_date(dt) + day = dt.normalize() try: return self._augmented_sources_map[field][asset].loc[day, field] @@ -422,7 +418,7 @@ def _get_single_asset_value(self, session_label, asset, field, dt, data_frequenc raise KeyError("Invalid column: " + str(field)) if ( - dt < asset.start_date + dt < asset.start_date.tz_localize(dt.tzinfo) or (data_frequency == "daily" and session_label > asset.end_date) or (data_frequency == "minute" and session_label > asset.end_date) ): @@ -458,8 +454,7 @@ def _get_single_asset_value(self, session_label, asset, field, dt, data_frequenc return self._get_minute_spot_value(asset, field, dt) def get_spot_value(self, assets, field, dt, data_frequency): - """ - Public API method that returns a scalar value representing the value + """Public API method that returns a scalar value representing the value of the desired asset's field at either the given dt. Parameters @@ -497,7 +492,7 @@ def get_spot_value(self, assets, field, dt, data_frequency): "Unexpected 'assets' value of type {}.".format(type(assets)) ) from exc - session_label = self.trading_calendar.minute_to_session_label(dt) + session_label = self.trading_calendar.minute_to_session(dt) if assets_is_scalar: return self._get_single_asset_value( @@ -521,8 +516,7 @@ def get_spot_value(self, assets, field, dt, data_frequency): ] def get_scalar_asset_spot_value(self, asset, field, dt, data_frequency): - """ - Public API method that returns a scalar value representing the value + """Public API method that returns a scalar value representing the value of the desired asset's field at either the given dt. Parameters @@ -549,7 +543,7 @@ def get_scalar_asset_spot_value(self, asset, field, dt, data_frequency): 'last_traded' the value will be a Timestamp. """ return self._get_single_asset_value( - self.trading_calendar.minute_to_session_label(dt), + self.trading_calendar.minute_to_session(dt), asset, field, dt, @@ -557,8 +551,7 @@ def get_scalar_asset_spot_value(self, asset, field, dt, data_frequency): ) def get_adjustments(self, assets, field, dt, perspective_dt): - """ - Returns a list of adjustments between the dt and perspective_dt for the + """Returns a list of adjustments between the dt and perspective_dt for the given field and list of assets Parameters @@ -592,9 +585,9 @@ def split_adj_factor(x): asset, self._splits_dict, "SPLITS" ) for adj_dt, adj in split_adjustments: - if dt < adj_dt <= perspective_dt: + if dt < adj_dt.tz_localize(dt.tzinfo) <= perspective_dt: adjustments_for_asset.append(split_adj_factor(adj)) - elif adj_dt > perspective_dt: + elif adj_dt.tz_localize(dt.tzinfo) > perspective_dt: break if field != "volume": @@ -613,9 +606,9 @@ def split_adj_factor(x): "DIVIDENDS", ) for adj_dt, adj in dividend_adjustments: - if dt < adj_dt <= perspective_dt: + if dt < adj_dt.tz_localize(dt.tzinfo) <= perspective_dt: adjustments_for_asset.append(adj) - elif adj_dt > perspective_dt: + elif adj_dt.tz_localize(dt.tzinfo) > perspective_dt: break ratio = reduce(mul, adjustments_for_asset, 1.0) @@ -626,8 +619,7 @@ def split_adj_factor(x): def get_adjusted_value( self, asset, field, dt, perspective_dt, data_frequency, spot_value=None ): - """ - Returns a scalar value representing the value + """Returns a scalar value representing the value of the desired asset's field at the given dt with adjustments applied. Parameters @@ -751,7 +743,7 @@ def _get_daily_spot_value(self, asset, column, dt): @remember_last def _get_days_for_window(self, end_date, bar_count): - tds = self.trading_calendar.all_sessions + tds = self.trading_calendar.sessions end_loc = tds.get_loc(end_date) start_loc = end_loc - bar_count + 1 if start_loc < self._first_trading_day_loc: @@ -765,11 +757,10 @@ def _get_days_for_window(self, end_date, bar_count): def _get_history_daily_window( self, assets, end_dt, bar_count, field_to_use, data_frequency ): - """ - Internal method that returns a dataframe containing history bars + """Internal method that returns a dataframe containing history bars of daily frequency for the given sids. """ - session = self.trading_calendar.minute_to_session_label(end_dt) + session = self.trading_calendar.minute_to_session(end_dt) days_for_window = self._get_days_for_window(session, bar_count) if len(assets) == 0: @@ -821,13 +812,13 @@ def _handle_minute_history_out_of_bounds(self, bar_count): cal = self.trading_calendar first_trading_minute_loc = ( - cal.all_minutes.get_loc(self._first_trading_minute) + cal.minutes.get_loc(self._first_trading_minute) if self._first_trading_minute is not None else None ) - suggested_start_day = cal.minute_to_session_label( - cal.all_minutes[first_trading_minute_loc + bar_count] + cal.day + suggested_start_day = cal.minute_to_session( + cal.minutes[first_trading_minute_loc + bar_count] + cal.day ) raise HistoryWindowStartsBeforeData( @@ -837,8 +828,7 @@ def _handle_minute_history_out_of_bounds(self, bar_count): ) def _get_history_minute_window(self, assets, end_dt, bar_count, field_to_use): - """ - Internal method that returns a dataframe containing history bars + """Internal method that returns a dataframe containing history bars of minute frequency for the given sids. """ # get all the minutes for this window @@ -863,8 +853,7 @@ def _get_history_minute_window(self, assets, end_dt, bar_count, field_to_use): def get_history_window( self, assets, end_dt, bar_count, frequency, field, data_frequency, ffill=True ): - """ - Public API method that returns a dataframe containing the requested + """Public API method that returns a dataframe containing the requested history window. Data is fully adjusted. Parameters @@ -894,10 +883,10 @@ def get_history_window( A dataframe containing the requested data. """ if field not in OHLCVP_FIELDS and field != "sid": - raise ValueError("Invalid field: {0}".format(field)) + raise ValueError(f"Invalid field: {field}") if bar_count < 1: - raise ValueError("bar_count must be >= 1, but got {}".format(bar_count)) + raise ValueError(f"bar_count must be >= 1, but got {bar_count}") if frequency == "1d": if field == "price": @@ -914,7 +903,7 @@ def get_history_window( else: df = self._get_history_minute_window(assets, end_dt, bar_count, field) else: - raise ValueError("Invalid frequency: {0}".format(frequency)) + raise ValueError(f"Invalid frequency: {frequency}") # forward-fill price if field == "price": @@ -966,15 +955,17 @@ def get_history_window( # end_date. normed_index = df.index.normalize() for asset in df.columns: - if history_end >= asset.end_date: + if history_end >= asset.end_date.tz_localize(history_end.tzinfo): # if the window extends past the asset's end date, set # all post-end-date values to NaN in that asset's series - df.loc[normed_index > asset.end_date, asset] = nan + df.loc[ + normed_index > asset.end_date.tz_localize(normed_index.tz), + asset, + ] = nan return df def _get_minute_window_data(self, assets, field, minutes_for_window): - """ - Internal method that gets a window of adjusted minute data for an asset + """Internal method that gets a window of adjusted minute data for an asset and specified date range. Used to support the history API method for minute bars. @@ -1001,8 +992,7 @@ def _get_minute_window_data(self, assets, field, minutes_for_window): ) def _get_daily_window_data(self, assets, field, days_in_window, extra_slot=True): - """ - Internal method that gets a window of adjusted daily data for a sid + """Internal method that gets a window of adjusted daily data for a sid and specified date range. Used to support the history API method for daily bars. @@ -1056,8 +1046,7 @@ def _get_daily_window_data(self, assets, field, days_in_window, extra_slot=True) return return_array def _get_adjustment_list(self, asset, adjustments_dict, table_name): - """ - Internal method that returns a list of adjustments for the given sid. + """Internal method that returns a list of adjustments for the given sid. Parameters ---------- @@ -1091,8 +1080,7 @@ def _get_adjustment_list(self, asset, adjustments_dict, table_name): return adjustments def get_splits(self, assets, dt): - """ - Returns any splits for the given sids and the given dt. + """Returns any splits for the given sids and the given dt. Parameters ---------- @@ -1126,8 +1114,7 @@ def get_splits(self, assets, dt): return splits def get_stock_dividends(self, sid, trading_days): - """ - Returns all the stock dividends for a specific sid that occur + """Returns all the stock dividends for a specific sid that occur in the given trading range. Parameters @@ -1187,8 +1174,7 @@ def contains(self, asset, field): ) def get_fetcher_assets(self, dt): - """ - Returns a list of assets for the current date, as defined by the + """Returns a list of assets for the current date, as defined by the fetcher data. Returns @@ -1200,7 +1186,7 @@ def get_fetcher_assets(self, dt): if self._extra_source_df is None: return [] - day = normalize_date(dt) + day = dt.normalize() if day in self._extra_source_df.index: assets = self._extra_source_df.loc[day]["sid"] @@ -1213,8 +1199,7 @@ def get_fetcher_assets(self, dt): return [assets] if isinstance(assets, Asset) else [] def get_current_future_chain(self, continuous_future, dt): - """ - Retrieves the future chain for the contract at the given `dt` according + """Retrieves the future chain for the contract at the given `dt` according the `continuous_future` specification. Returns @@ -1226,7 +1211,7 @@ def get_current_future_chain(self, continuous_future, dt): is the next upcoming contract and so on. """ rf = self._roll_finders[continuous_future.roll_style] - session = self.trading_calendar.minute_to_session_label(dt) + session = self.trading_calendar.minute_to_session(dt) contract_center = rf.get_contract_center( continuous_future.root_symbol, session, continuous_future.offset ) diff --git a/src/zipline/data/fx/hdf5.py b/src/zipline/data/fx/hdf5.py index abbb2ac359..e221a9c785 100644 --- a/src/zipline/data/fx/hdf5.py +++ b/src/zipline/data/fx/hdf5.py @@ -141,8 +141,7 @@ def __init__(self, group, default_rate): @classmethod def from_path(cls, path, default_rate): - """ - Construct from a file path. + """Construct from a file path. Parameters ---------- @@ -190,6 +189,9 @@ def get_rates(self, rate, quote, bases, dts): check_dts(dts) + # TODO FIXME TZ MESS + if dts.tzinfo is None: + dts = dts.tz_localize(self.dts.tzinfo) col_ixs = self.dts.searchsorted(dts, side="right") - 1 row_ixs = self.currencies.get_indexer(bases) diff --git a/src/zipline/data/fx/in_memory.py b/src/zipline/data/fx/in_memory.py index 55d47ea070..1000c7a7cf 100644 --- a/src/zipline/data/fx/in_memory.py +++ b/src/zipline/data/fx/in_memory.py @@ -1,5 +1,4 @@ -"""Interface and definitions for foreign exchange rate readers. -""" +"""Interface and definitions for foreign exchange rate readers.""" from interface import implements import numpy as np @@ -8,8 +7,7 @@ class InMemoryFXRateReader(implements(FXRateReader)): - """ - A simple in-memory FXRateReader. + """A simple in-memory FXRateReader. This is primarily used for testing. @@ -51,7 +49,7 @@ def get_rates(self, rate, quote, bases, dts): # method a lot, so we implement our own indexing logic. values = df.values - row_ixs = df.index.searchsorted(dts, side="right") - 1 + row_ixs = df.index.searchsorted(dts.tz_localize(None), side="right") - 1 col_ixs = df.columns.get_indexer(bases) out = values[:, col_ixs][row_ixs] diff --git a/src/zipline/data/hdf5_daily_bars.py b/src/zipline/data/hdf5_daily_bars.py index 0d566d51c1..f2037bbd71 100644 --- a/src/zipline/data/hdf5_daily_bars.py +++ b/src/zipline/data/hdf5_daily_bars.py @@ -709,7 +709,7 @@ def last_available_dt(self): dt : pd.Timestamp The last session for which the reader can provide data. """ - return pd.Timestamp(self.dates[-1], tz="UTC") + return pd.Timestamp(self.dates[-1]) @property def trading_calendar(self): @@ -730,7 +730,7 @@ def first_trading_day(self): The first trading day (session) for which the reader can provide data. """ - return pd.Timestamp(self.dates[0], tz="UTC") + return pd.Timestamp(self.dates[0]) @lazyval def sessions(self): @@ -741,7 +741,7 @@ def sessions(self): All session labels (unioning the range for all assets) which the reader can provide. """ - return pd.to_datetime(self.dates, utc=True) + return pd.to_datetime(self.dates) def get_value(self, sid, dt, field): """ @@ -792,8 +792,7 @@ def get_value(self, sid, dt, field): return value def get_last_traded_dt(self, asset, dt): - """ - Get the latest day on or before ``dt`` in which ``asset`` traded. + """Get the latest day on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. @@ -822,11 +821,12 @@ def get_last_traded_dt(self, asset, dt): if len(nonzero_volume_ixs) == 0: return pd.NaT - return pd.Timestamp(self.dates[nonzero_volume_ixs][-1], tz="UTC") + return pd.Timestamp(self.dates[nonzero_volume_ixs][-1]) class MultiCountryDailyBarReader(CurrencyAwareSessionBarReader): """ + Parameters --------- readers : dict[str -> SessionBarReader] @@ -845,8 +845,7 @@ def __init__(self, readers): @classmethod def from_file(cls, h5_file): - """ - Construct from an h5py.File. + """Construct from an h5py.File. Parameters ---------- @@ -901,6 +900,7 @@ def _country_code_for_assets(self, assets): def load_raw_arrays(self, columns, start_date, end_date, assets): """ + Parameters ---------- columns : list of str @@ -931,6 +931,7 @@ def load_raw_arrays(self, columns, start_date, end_date, assets): @property def last_available_dt(self): """ + Returns ------- dt : pd.Timestamp @@ -951,6 +952,7 @@ def trading_calendar(self): @property def first_trading_day(self): """ + Returns ------- dt : pd.Timestamp @@ -962,6 +964,7 @@ def first_trading_day(self): @property def sessions(self): """ + Returns ------- sessions : DatetimeIndex @@ -973,12 +976,10 @@ def sessions(self): np.union1d, (reader.dates for reader in self._readers.values()), ), - utc=True, ) def get_value(self, sid, dt, field): - """ - Retrieve the value at the given coordinates. + """Retrieve the value at the given coordinates. Parameters ---------- @@ -1012,8 +1013,7 @@ def get_value(self, sid, dt, field): return self._readers[country_code].get_value(sid, dt, field) def get_last_traded_dt(self, asset, dt): - """ - Get the latest day on or before ``dt`` in which ``asset`` traded. + """Get the latest day on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. diff --git a/src/zipline/data/history_loader.py b/src/zipline/data/history_loader.py index 795e0c1f75..bc2636d087 100644 --- a/src/zipline/data/history_loader.py +++ b/src/zipline/data/history_loader.py @@ -17,7 +17,7 @@ abstractmethod, ) -from numpy import concatenate +import numpy as np from lru import LRU from pandas import isnull from toolz import sliding_window @@ -32,7 +32,7 @@ from zipline.utils.math_utils import number_of_decimal_places from zipline.utils.memoize import lazyval from zipline.utils.numpy_utils import float64_dtype -from zipline.utils.pandas_utils import find_in_sorted_index, normalize_date +from zipline.utils.pandas_utils import find_in_sorted_index # Default number of decimal places used for rounding asset prices. DEFAULT_ASSET_PRICE_DECIMALS = 3 @@ -44,6 +44,7 @@ def __init__(self, adjustment_reader): def load_pricing_adjustments(self, columns, dts, assets): """ + Returns ------- adjustments : list[dict[int -> Adjustment]] @@ -59,8 +60,7 @@ def load_pricing_adjustments(self, columns, dts, assets): return out def _get_adjustments_in_range(self, asset, dts, field): - """ - Get the Float64Multiply objects to pass to an AdjustedArrayWindow. + """Get the Float64Multiply objects to pass to an AdjustedArrayWindow. For the use of AdjustedArrayWindow in the loader, which looks back from current simulation time back to a window of data the dictionary is @@ -88,13 +88,13 @@ def _get_adjustments_in_range(self, asset, dts, field): The adjustments as a dict of loc -> Float64Multiply """ sid = int(asset) - start = normalize_date(dts[0]) - end = normalize_date(dts[-1]) + start = dts[0].normalize() + end = dts[-1].normalize() adjs = {} if field != "volume": mergers = self._adjustments_reader.get_adjustments_for_sid("mergers", sid) for m in mergers: - dt = m[0] + dt = m[0].tz_localize(dts.tzinfo) if start < dt <= end: end_loc = dts.searchsorted(dt) adj_loc = end_loc @@ -105,7 +105,7 @@ def _get_adjustments_in_range(self, asset, dts, field): adjs[adj_loc] = [mult] divs = self._adjustments_reader.get_adjustments_for_sid("dividends", sid) for d in divs: - dt = d[0] + dt = d[0].tz_localize(dts.tzinfo) if start < dt <= end: end_loc = dts.searchsorted(dt) adj_loc = end_loc @@ -116,7 +116,7 @@ def _get_adjustments_in_range(self, asset, dts, field): adjs[adj_loc] = [mult] splits = self._adjustments_reader.get_adjustments_for_sid("splits", sid) for s in splits: - dt = s[0] + dt = s[0].tz_localize(dts.tzinfo) if start < dt <= end: if field == "volume": ratio = 1.0 / s[1] @@ -133,8 +133,7 @@ def _get_adjustments_in_range(self, asset, dts, field): class ContinuousFutureAdjustmentReader: - """ - Calculates adjustments for continuous futures, based on the + """Calculates adjustments for continuous futures, based on the close and open of the contracts on the either side of each roll. """ @@ -154,6 +153,7 @@ def __init__( def load_pricing_adjustments(self, columns, dts, assets): """ + Returns ------- adjustments : list[dict[int -> Adjustment]] @@ -195,10 +195,10 @@ def _get_adjustments_in_range(self, cf, dts, field): for front, back in sliding_window(2, rolls): front_sid, roll_dt = front back_sid = back[0] - dt = tc.previous_session_label(roll_dt) + dt = tc.previous_session(roll_dt) if self._frequency == "minute": - dt = tc.open_and_close_for_session(dt)[1] - roll_dt = tc.open_and_close_for_session(roll_dt)[0] + dt = tc.session_close(dt) + roll_dt = tc.session_first_minute(roll_dt) partitions.append((front_sid, back_sid, dt, roll_dt)) for partition in partitions: front_sid, back_sid, dt, roll_dt = partition @@ -223,8 +223,7 @@ def _get_adjustments_in_range(self, cf, dts, field): class SlidingWindow: - """ - Wrapper around an AdjustedArrayWindow which supports monotonically + """Wrapper around an AdjustedArrayWindow which supports monotonically increasing (by datetime) requests for a sized window of data. Parameters @@ -261,8 +260,7 @@ def get(self, end_ix): class HistoryLoader(ABC): - """ - Loader for sliding history windows, with support for adjustments. + """Loader for sliding history windows, with support for adjustments. Parameters ---------- @@ -339,8 +337,7 @@ def _decimal_places_for_asset(self, asset, reference_date): return DEFAULT_ASSET_PRICE_DECIMALS def _ensure_sliding_windows(self, assets, dts, field, is_perspective_after): - """ - Ensure that there is a Float64Multiply window for each asset that can + """Ensure that there is a Float64Multiply window for each asset that can provide data for the given parameters. If the corresponding window for the (assets, len(dts), field) does not exist, then create a new one. @@ -448,8 +445,7 @@ def _ensure_sliding_windows(self, assets, dts, field, is_perspective_after): return [asset_windows[asset] for asset in assets] def history(self, assets, dts, field, is_perspective_after): - """ - A window of pricing data with adjustments applied assuming that the + """A window of pricing data with adjustments applied assuming that the end of the window is the day before the current simulation time. Parameters @@ -525,7 +521,7 @@ def history(self, assets, dts, field, is_perspective_after): block = self._ensure_sliding_windows(assets, dts, field, is_perspective_after) end_ix = self._calendar.searchsorted(dts[-1]) - return concatenate( + return np.concatenate( [window.get(end_ix) for window in block], axis=1, ) @@ -556,9 +552,14 @@ def _frequency(self): @lazyval def _calendar(self): - mm = self.trading_calendar.all_minutes - start = mm.searchsorted(self._reader.first_trading_day) - end = mm.searchsorted(self._reader.last_available_dt, side="right") + mm = self.trading_calendar.minutes + start = mm.searchsorted(self._reader.first_trading_day.tz_localize("UTC")) + if self._reader.last_available_dt.tzinfo is None: + end = mm.searchsorted( + self._reader.last_available_dt.tz_localize("UTC"), side="right" + ) + else: + end = mm.searchsorted(self._reader.last_available_dt, side="right") return mm[start:end] def _array(self, dts, assets, field): diff --git a/src/zipline/data/in_memory_daily_bars.py b/src/zipline/data/in_memory_daily_bars.py index e2484d4ee8..4d928cb1d2 100644 --- a/src/zipline/data/in_memory_daily_bars.py +++ b/src/zipline/data/in_memory_daily_bars.py @@ -133,8 +133,7 @@ def currency_codes(self, sids): def verify_frames_aligned(frames, calendar): - """ - Verify that DataFrames in ``frames`` have the same indexing scheme and are + """Verify that DataFrames in ``frames`` have the same indexing scheme and are aligned to ``calendar``. Parameters @@ -158,6 +157,6 @@ def verify_frames_aligned(frames, calendar): start, end = indexes[0][[0, -1]] cal_sessions = calendar.sessions_in_range(start, end) check_indexes_all_same( - [indexes[0], cal_sessions], - "DataFrame index doesn't match {} calendar:".format(calendar.name), + [indexes[0].tz_localize(None), cal_sessions], + f"DataFrame index doesn't match {calendar.name} calendar:", ) diff --git a/src/zipline/data/resample.py b/src/zipline/data/resample.py index f6b40bf394..881bf63aef 100644 --- a/src/zipline/data/resample.py +++ b/src/zipline/data/resample.py @@ -42,8 +42,7 @@ def minute_frame_to_session_frame(minute_frame, calendar): - """ - Resample a DataFrame with minute data into the frame expected by a + """Resample a DataFrame with minute data into the frame expected by a BcolzDailyBarWriter. Parameters @@ -64,13 +63,12 @@ def minute_frame_to_session_frame(minute_frame, calendar): how = OrderedDict( (c, _MINUTE_TO_SESSION_OHCLV_HOW[c]) for c in minute_frame.columns ) - labels = calendar.minute_index_to_session_labels(minute_frame.index) + labels = calendar.minutes_to_sessions(minute_frame.index) return minute_frame.groupby(labels).agg(how) def minute_to_session(column, close_locs, data, out): - """ - Resample an array with minute data into an array with session data. + """Resample an array with minute data into an array with session data. This function assumes that the minute data is the exact length of all minutes in the sessions in the output. @@ -103,8 +101,7 @@ def minute_to_session(column, close_locs, data, out): class DailyHistoryAggregator: - """ - Converts minute pricing data into a daily summary, to be used for the + """Converts minute pricing data into a daily summary, to be used for the last slot in a call to history with a frequency of `1d`. This summary is the same as a daily bar rollup of minute data, with the @@ -151,7 +148,7 @@ def __init__(self, market_opens, minute_reader, trading_calendar): self._one_min = pd.Timedelta("1 min").value def _prelude(self, dt, field): - session = self._trading_calendar.minute_to_session_label(dt) + session = self._trading_calendar.minute_to_session(dt) dt_value = dt.value cache = self._caches[field] if cache is None or cache[0] != session: @@ -159,7 +156,6 @@ def _prelude(self, dt, field): cache = self._caches[field] = (session, market_open, {}) _, market_open, entries = cache - market_open = market_open.tz_localize("UTC") if dt != market_open: prev_dt = dt_value - self._one_min else: @@ -167,8 +163,7 @@ def _prelude(self, dt, field): return market_open, prev_dt, dt_value, entries def opens(self, assets, dt): - """ - The open field's aggregation returns the first value that occurs + """The open field's aggregation returns the first value that occurs for the day, if there has been no data on or before the `dt` the open is `nan`. @@ -182,7 +177,7 @@ def opens(self, assets, dt): market_open, prev_dt, dt_value, entries = self._prelude(dt, "open") opens = [] - session_label = self._trading_calendar.minute_to_session_label(dt) + session_label = self._trading_calendar.minute_to_session(dt) for asset in assets: if not asset.is_alive_for_session(session_label): @@ -240,8 +235,7 @@ def opens(self, assets, dt): return np.array(opens) def highs(self, assets, dt): - """ - The high field's aggregation returns the largest high seen between + """The high field's aggregation returns the largest high seen between the market open and the current dt. If there has been no data on or before the `dt` the high is `nan`. @@ -252,7 +246,7 @@ def highs(self, assets, dt): market_open, prev_dt, dt_value, entries = self._prelude(dt, "high") highs = [] - session_label = self._trading_calendar.minute_to_session_label(dt) + session_label = self._trading_calendar.minute_to_session(dt) for asset in assets: if not asset.is_alive_for_session(session_label): @@ -309,8 +303,7 @@ def highs(self, assets, dt): return np.array(highs) def lows(self, assets, dt): - """ - The low field's aggregation returns the smallest low seen between + """The low field's aggregation returns the smallest low seen between the market open and the current dt. If there has been no data on or before the `dt` the low is `nan`. @@ -321,7 +314,7 @@ def lows(self, assets, dt): market_open, prev_dt, dt_value, entries = self._prelude(dt, "low") lows = [] - session_label = self._trading_calendar.minute_to_session_label(dt) + session_label = self._trading_calendar.minute_to_session(dt) for asset in assets: if not asset.is_alive_for_session(session_label): @@ -373,8 +366,7 @@ def lows(self, assets, dt): return np.array(lows) def closes(self, assets, dt): - """ - The close field's aggregation returns the latest close at the given + """The close field's aggregation returns the latest close at the given dt. If the close for the given dt is `nan`, the most recent non-nan `close` is used. @@ -387,7 +379,7 @@ def closes(self, assets, dt): market_open, prev_dt, dt_value, entries = self._prelude(dt, "close") closes = [] - session_label = self._trading_calendar.minute_to_session_label(dt) + session_label = self._trading_calendar.minute_to_session(dt) def _get_filled_close(asset): """ @@ -446,8 +438,7 @@ def _get_filled_close(asset): return np.array(closes) def volumes(self, assets, dt): - """ - The volume field's aggregation returns the sum of all volumes + """The volume field's aggregation returns the sum of all volumes between the market open and the `dt` If there has been no data on or before the `dt` the volume is 0. @@ -458,7 +449,7 @@ def volumes(self, assets, dt): market_open, prev_dt, dt_value, entries = self._prelude(dt, "volume") volumes = [] - session_label = self._trading_calendar.minute_to_session_label(dt) + session_label = self._trading_calendar.minute_to_session(dt) for asset in assets: if not asset.is_alive_for_session(session_label): @@ -516,7 +507,7 @@ def __init__(self, calendar, minute_bar_reader): self._minute_bar_reader = minute_bar_reader def _get_resampled(self, columns, start_session, end_session, assets): - range_open = self._calendar.session_open(start_session) + range_open = self._calendar.session_first_minute(start_session) range_close = self._calendar.session_close(end_session) minute_data = self._minute_bar_reader.load_raw_arrays( @@ -537,10 +528,7 @@ def _get_resampled(self, columns, start_session, end_session, assets): range_open, range_close, ) - session_closes = self._calendar.session_closes_in_range( - start_session, - end_session, - ) + session_closes = self._calendar.closes[start_session:end_session] close_ilocs = minutes.searchsorted(pd.DatetimeIndex(session_closes)) results = [] @@ -578,12 +566,12 @@ def get_value(self, sid, session, colname): def sessions(self): cal = self._calendar first = self._minute_bar_reader.first_trading_day - last = cal.minute_to_session_label(self._minute_bar_reader.last_available_dt) + last = cal.minute_to_session(self._minute_bar_reader.last_available_dt) return cal.sessions_in_range(first, last) @lazyval def last_available_dt(self): - return self.trading_calendar.minute_to_session_label( + return self.trading_calendar.minute_to_session( self._minute_bar_reader.last_available_dt ) @@ -595,13 +583,12 @@ def get_last_traded_dt(self, asset, dt): last_dt = self._minute_bar_reader.get_last_traded_dt(asset, dt) if pd.isnull(last_dt): # todo: this doesn't seem right - return self.trading_calendar.first_trading_session - return self.trading_calendar.minute_to_session_label(last_dt) + return self.trading_calendar.first_session + return self.trading_calendar.minute_to_session(last_dt) class ReindexBarReader(ABC): - """ - A base class for readers which reindexes results, filling in the additional + """A base class for readers which reindexes results, filling in the additional indices with empty data. Used to align the reading assets which trade on different calendars. @@ -712,9 +699,7 @@ def load_raw_arrays(self, fields, start_dt, end_dt, sids): class ReindexMinuteBarReader(ReindexBarReader, MinuteBarReader): - """ - See: ``ReindexBarReader`` - """ + """See: ``ReindexBarReader``""" def _outer_dts(self, start_dt, end_dt): return self._trading_calendar.minutes_in_range(start_dt, end_dt) @@ -724,9 +709,7 @@ def _inner_dts(self, start_dt, end_dt): class ReindexSessionBarReader(ReindexBarReader, SessionBarReader): - """ - See: ``ReindexBarReader`` - """ + """See: ``ReindexBarReader``""" def _outer_dts(self, start_dt, end_dt): return self.trading_calendar.sessions_in_range(start_dt, end_dt) diff --git a/src/zipline/data/session_bars.py b/src/zipline/data/session_bars.py index 00893a985d..7bd85698fd 100644 --- a/src/zipline/data/session_bars.py +++ b/src/zipline/data/session_bars.py @@ -17,9 +17,7 @@ class SessionBarReader(BarReader): - """ - Reader for OHCLV pricing data at a session frequency. - """ + """Reader for OHCLV pricing data at a session frequency.""" @property def data_frequency(self): @@ -29,6 +27,7 @@ def data_frequency(self): @abstractmethod def sessions(self): """ + Returns ------- sessions : DatetimeIndex @@ -40,8 +39,7 @@ def sessions(self): class CurrencyAwareSessionBarReader(SessionBarReader): @abstractmethod def currency_codes(self, sids): - """ - Get currencies in which prices are quoted for the requested sids. + """Get currencies in which prices are quoted for the requested sids. Assumes that a sid's prices are always quoted in a single currency. diff --git a/src/zipline/errors.py b/src/zipline/errors.py index 21a88ef99c..64ae59f913 100644 --- a/src/zipline/errors.py +++ b/src/zipline/errors.py @@ -62,8 +62,7 @@ class InvalidBenchmarkAsset(ZiplineError): class WrongDataForTransform(ZiplineError): - """ - Raised whenever a rolling transform is called on an event that + """Raised whenever a rolling transform is called on an event that does not have the necessary properties. """ @@ -71,8 +70,7 @@ class WrongDataForTransform(ZiplineError): class UnsupportedSlippageModel(ZiplineError): - """ - Raised if a user script calls the set_slippage magic + """Raised if a user script calls the set_slippage magic with a slipage object that isn't a VolumeShareSlippage or FixedSlipapge """ @@ -84,8 +82,7 @@ class UnsupportedSlippageModel(ZiplineError): class IncompatibleSlippageModel(ZiplineError): - """ - Raised if a user tries to set a futures slippage model for equities or vice + """Raised if a user tries to set a futures slippage model for equities or vice versa. """ @@ -132,8 +129,7 @@ class RegisterAccountControlPostInit(ZiplineError): class UnsupportedCommissionModel(ZiplineError): - """ - Raised if a user script calls the set_commission magic + """Raised if a user script calls the set_commission magic with a commission object that isn't a PerShare, PerTrade or PerDollar commission """ @@ -145,8 +141,7 @@ class UnsupportedCommissionModel(ZiplineError): class IncompatibleCommissionModel(ZiplineError): - """ - Raised if a user tries to set a futures commission model for equities or + """Raised if a user tries to set a futures commission model for equities or vice versa. """ @@ -157,8 +152,7 @@ class IncompatibleCommissionModel(ZiplineError): class UnsupportedCancelPolicy(ZiplineError): - """ - Raised if a user script calls set_cancel_policy with an object that isn't + """Raised if a user script calls set_cancel_policy with an object that isn't a CancelPolicy. """ @@ -169,8 +163,7 @@ class UnsupportedCancelPolicy(ZiplineError): class SetCommissionPostInit(ZiplineError): - """ - Raised if a users script calls set_commission magic + """Raised if a users script calls set_commission magic after the initialize method has returned. """ @@ -181,9 +174,7 @@ class SetCommissionPostInit(ZiplineError): class TransactionWithNoVolume(ZiplineError): - """ - Raised if a transact call returns a transaction with zero volume. - """ + """Raised if a transact call returns a transaction with zero volume.""" msg = """ Transaction {txn} has a volume of zero. @@ -191,8 +182,7 @@ class TransactionWithNoVolume(ZiplineError): class TransactionWithWrongDirection(ZiplineError): - """ - Raised if a transact call returns a transaction with a direction that + """Raised if a transact call returns a transaction with a direction that does not match the order. """ @@ -202,9 +192,7 @@ class TransactionWithWrongDirection(ZiplineError): class TransactionWithNoAmount(ZiplineError): - """ - Raised if a transact call returns a transaction with zero amount. - """ + """Raised if a transact call returns a transaction with zero amount.""" msg = """ Transaction {txn} has an amount of zero. @@ -212,8 +200,7 @@ class TransactionWithNoAmount(ZiplineError): class TransactionVolumeExceedsOrder(ZiplineError): - """ - Raised if a transact call returns a transaction with a volume greater than + """Raised if a transact call returns a transaction with a volume greater than the corresponding order. """ @@ -223,8 +210,7 @@ class TransactionVolumeExceedsOrder(ZiplineError): class UnsupportedOrderParameters(ZiplineError): - """ - Raised if a set of mutually exclusive parameters are passed to an order + """Raised if a set of mutually exclusive parameters are passed to an order call. """ @@ -232,16 +218,13 @@ class UnsupportedOrderParameters(ZiplineError): class CannotOrderDelistedAsset(ZiplineError): - """ - Raised if an order is for a delisted asset. - """ + """Raised if an order is for a delisted asset.""" msg = "{msg}" class BadOrderParameters(ZiplineError): - """ - Raised if any impossible parameters (nan, negative limit/stop) + """Raised if any impossible parameters (nan, negative limit/stop) are passed to an order call. """ @@ -249,33 +232,25 @@ class BadOrderParameters(ZiplineError): class OrderDuringInitialize(ZiplineError): - """ - Raised if order is called during initialize() - """ + """Raised if order is called during initialize()""" msg = "{msg}" class SetBenchmarkOutsideInitialize(ZiplineError): - """ - Raised if set_benchmark is called outside initialize() - """ + """Raised if set_benchmark is called outside initialize()""" msg = "'set_benchmark' can only be called within initialize function." class ZeroCapitalError(ZiplineError): - """ - Raised if initial capital is set at or below zero - """ + """Raised if initial capital is set at or below zero""" msg = "initial capital base must be greater than zero" class AccountControlViolation(ZiplineError): - """ - Raised if the account violates a constraint set by a AccountControl. - """ + """Raised if the account violates a constraint set by a AccountControl.""" msg = """ Account violates account constraint {constraint}. @@ -283,9 +258,7 @@ class AccountControlViolation(ZiplineError): class TradingControlViolation(ZiplineError): - """ - Raised if an order would violate a constraint set by a TradingControl. - """ + """Raised if an order would violate a constraint set by a TradingControl.""" msg = """ Order for {amount} shares of {asset} at {datetime} violates trading constraint @@ -294,8 +267,7 @@ class TradingControlViolation(ZiplineError): class IncompatibleHistoryFrequency(ZiplineError): - """ - Raised when a frequency is given to history which is not supported. + """Raised when a frequency is given to history which is not supported. At least, not yet. """ @@ -306,16 +278,13 @@ class IncompatibleHistoryFrequency(ZiplineError): class OrderInBeforeTradingStart(ZiplineError): - """ - Raised when an algorithm calls an order method in before_trading_start. - """ + """Raised when an algorithm calls an order method in before_trading_start.""" msg = "Cannot place orders inside before_trading_start." class MultipleSymbolsFound(ZiplineError): - """ - Raised when a symbol() call contains a symbol that changed over + """Raised when a symbol() call contains a symbol that changed over time and is thus not resolvable without additional information provided via as_of_date. """ @@ -330,8 +299,7 @@ class MultipleSymbolsFound(ZiplineError): class MultipleSymbolsFoundForFuzzySymbol(MultipleSymbolsFound): - """ - Raised when a fuzzy symbol lookup is not resolvable without additional + """Raised when a fuzzy symbol lookup is not resolvable without additional information. """ @@ -347,8 +315,7 @@ class MultipleSymbolsFoundForFuzzySymbol(MultipleSymbolsFound): class SameSymbolUsedAcrossCountries(MultipleSymbolsFound): - """ - Raised when a symbol() call contains a symbol that is used in more than + """Raised when a symbol() call contains a symbol that is used in more than one country and is thus not resolvable without a country_code. """ @@ -363,9 +330,7 @@ class SameSymbolUsedAcrossCountries(MultipleSymbolsFound): class SymbolNotFound(ZiplineError): - """ - Raised when a symbol() call contains a non-existant symbol. - """ + """Raised when a symbol() call contains a non-existant symbol.""" msg = """ Symbol '{symbol}' was not found. @@ -373,9 +338,7 @@ class SymbolNotFound(ZiplineError): class RootSymbolNotFound(ZiplineError): - """ - Raised when a lookup_future_chain() call contains a non-existant symbol. - """ + """Raised when a lookup_future_chain() call contains a non-existant symbol.""" msg = """ Root symbol '{root_symbol}' was not found. @@ -383,8 +346,7 @@ class RootSymbolNotFound(ZiplineError): class ValueNotFoundForField(ZiplineError): - """ - Raised when a lookup_by_supplementary_mapping() call contains a + """Raised when a lookup_by_supplementary_mapping() call contains a value does not exist for the specified mapping type. """ @@ -394,8 +356,7 @@ class ValueNotFoundForField(ZiplineError): class MultipleValuesFoundForField(ZiplineError): - """ - Raised when a lookup_by_supplementary_mapping() call contains a + """Raised when a lookup_by_supplementary_mapping() call contains a value that changed over time for the specified field and is thus not resolvable without additional information provided via as_of_date. @@ -411,8 +372,7 @@ class MultipleValuesFoundForField(ZiplineError): class NoValueForSid(ZiplineError): - """ - Raised when a get_supplementary_field() call contains a sid that + """Raised when a get_supplementary_field() call contains a sid that does not have a value for the specified mapping type. """ @@ -422,8 +382,7 @@ class NoValueForSid(ZiplineError): class MultipleValuesFoundForSid(ZiplineError): - """ - Raised when a get_supplementary_field() call contains a value that + """Raised when a get_supplementary_field() call contains a value that changed over time for the specified field and is thus not resolvable without additional information provided via as_of_date. """ @@ -437,8 +396,7 @@ class MultipleValuesFoundForSid(ZiplineError): class SidsNotFound(ZiplineError): - """ - Raised when a retrieve_asset() or retrieve_all() call contains a + """Raised when a retrieve_asset() or retrieve_all() call contains a non-existent sid. """ @@ -458,9 +416,7 @@ def msg(self): class EquitiesNotFound(SidsNotFound): - """ - Raised when a call to `retrieve_equities` fails to find an asset. - """ + """Raised when a call to `retrieve_equities` fails to find an asset.""" @lazyval def msg(self): @@ -470,9 +426,7 @@ def msg(self): class FutureContractsNotFound(SidsNotFound): - """ - Raised when a call to `retrieve_futures_contracts` fails to find an asset. - """ + """Raised when a call to `retrieve_futures_contracts` fails to find an asset.""" @lazyval def msg(self): @@ -482,9 +436,7 @@ def msg(self): class ConsumeAssetMetaDataError(ZiplineError): - """ - Raised when AssetFinder.consume() is called on an invalid object. - """ + """Raised when AssetFinder.consume() is called on an invalid object.""" msg = """ AssetFinder can not consume metadata of type {obj}. Metadata must be a dict, a @@ -494,8 +446,7 @@ class ConsumeAssetMetaDataError(ZiplineError): class SidAssignmentError(ZiplineError): - """ - Raised when an AssetFinder tries to build an Asset that does not have a sid + """Raised when an AssetFinder tries to build an Asset that does not have a sid and that AssetFinder is not permitted to assign sids. """ @@ -505,9 +456,7 @@ class SidAssignmentError(ZiplineError): class NoSourceError(ZiplineError): - """ - Raised when no source is given to the pipeline - """ + """Raised when no source is given to the pipeline""" msg = """ No data source given. @@ -515,9 +464,7 @@ class NoSourceError(ZiplineError): class PipelineDateError(ZiplineError): - """ - Raised when only one date is passed to the pipeline - """ + """Raised when only one date is passed to the pipeline""" msg = """ Only one simulation date given. Please specify both the 'start' and 'end' for @@ -527,8 +474,7 @@ class PipelineDateError(ZiplineError): class WindowLengthTooLong(ZiplineError): - """ - Raised when a trailing window is instantiated with a lookback greater than + """Raised when a trailing window is instantiated with a lookback greater than the length of the underlying array. """ @@ -539,8 +485,7 @@ class WindowLengthTooLong(ZiplineError): class WindowLengthNotPositive(ZiplineError): - """ - Raised when a trailing window would be instantiated with a length less than + """Raised when a trailing window would be instantiated with a length less than 1. """ @@ -548,8 +493,7 @@ class WindowLengthNotPositive(ZiplineError): class NonWindowSafeInput(ZiplineError): - """ - Raised when a Pipeline API term that is not deemed window safe is specified + """Raised when a Pipeline API term that is not deemed window safe is specified as an input to another windowed term. This is an error because it's generally not safe to compose windowed @@ -560,8 +504,7 @@ class NonWindowSafeInput(ZiplineError): class TermInputsNotSpecified(ZiplineError): - """ - Raised if a user attempts to construct a term without specifying inputs and + """Raised if a user attempts to construct a term without specifying inputs and that term does not have class-level default inputs. """ @@ -569,9 +512,7 @@ class TermInputsNotSpecified(ZiplineError): class NonPipelineInputs(ZiplineError): - """ - Raised when a non-pipeline object is passed as input to a ComputableTerm - """ + """Raised when a non-pipeline object is passed as input to a ComputableTerm""" def __init__(self, term, inputs): self.term = term @@ -590,17 +531,13 @@ def __str__(self): class TermOutputsEmpty(ZiplineError): - """ - Raised if a user attempts to construct a term with an empty outputs list. - """ + """Raised if a user attempts to construct a term with an empty outputs list.""" msg = "{termname} requires at least one output when passed an outputs " "argument." class InvalidOutputName(ZiplineError): - """ - Raised if a term's output names conflict with any of its attributes. - """ + """Raised if a term's output names conflict with any of its attributes.""" msg = ( "{output_name!r} cannot be used as an output name for {termname}. " @@ -610,8 +547,7 @@ class InvalidOutputName(ZiplineError): class WindowLengthNotSpecified(ZiplineError): - """ - Raised if a user attempts to construct a term without specifying window + """Raised if a user attempts to construct a term without specifying window length and that term does not have a class-level default window length. """ @@ -619,8 +555,7 @@ class WindowLengthNotSpecified(ZiplineError): class InvalidTermParams(ZiplineError): - """ - Raised if a user attempts to construct a Term using ParameterizedTermMixin + """Raised if a user attempts to construct a Term using ParameterizedTermMixin without specifying a `params` list in the class body. """ @@ -631,8 +566,7 @@ class InvalidTermParams(ZiplineError): class DTypeNotSpecified(ZiplineError): - """ - Raised if a user attempts to construct a term without specifying dtype and + """Raised if a user attempts to construct a term without specifying dtype and that term does not have class-level default dtype. """ @@ -640,8 +574,7 @@ class DTypeNotSpecified(ZiplineError): class NotDType(ZiplineError): - """ - Raised when a pipeline Term is constructed with a dtype that isn't a numpy + """Raised when a pipeline Term is constructed with a dtype that isn't a numpy dtype object. """ @@ -652,8 +585,7 @@ class NotDType(ZiplineError): class UnsupportedDType(ZiplineError): - """ - Raised when a pipeline Term is constructed with a dtype that's not + """Raised when a pipeline Term is constructed with a dtype that's not supported. """ @@ -664,8 +596,7 @@ class UnsupportedDType(ZiplineError): class BadPercentileBounds(ZiplineError): - """ - Raised by API functions accepting percentile bounds when the passed bounds + """Raised by API functions accepting percentile bounds when the passed bounds are invalid. """ @@ -677,8 +608,7 @@ class BadPercentileBounds(ZiplineError): class UnknownRankMethod(ZiplineError): - """ - Raised during construction of a Rank factor when supplied a bad Rank + """Raised during construction of a Rank factor when supplied a bad Rank method. """ @@ -686,9 +616,7 @@ class UnknownRankMethod(ZiplineError): class AttachPipelineAfterInitialize(ZiplineError): - """ - Raised when a user tries to call add_pipeline outside of initialize. - """ + """Raised when a user tries to call add_pipeline outside of initialize.""" msg = ( "Attempted to attach a pipeline after initialize(). " @@ -697,9 +625,7 @@ class AttachPipelineAfterInitialize(ZiplineError): class PipelineOutputDuringInitialize(ZiplineError): - """ - Raised when a user tries to call `pipeline_output` during initialize. - """ + """Raised when a user tries to call `pipeline_output` during initialize.""" msg = ( "Attempted to call pipeline_output() during initialize. " @@ -708,9 +634,7 @@ class PipelineOutputDuringInitialize(ZiplineError): class NoSuchPipeline(ZiplineError, KeyError): - """ - Raised when a user tries to access a non-existent pipeline by name. - """ + """Raised when a user tries to access a non-existent pipeline by name.""" msg = ( "No pipeline named '{name}' exists. Valid pipeline names are {valid}. " @@ -719,8 +643,7 @@ class NoSuchPipeline(ZiplineError, KeyError): class DuplicatePipelineName(ZiplineError): - """ - Raised when a user tries to attach a pipeline with a name that already + """Raised when a user tries to attach a pipeline with a name that already exists for another attached pipeline. """ @@ -746,8 +669,7 @@ def __init__(self, hint="", **kwargs): class NoFurtherDataError(ZiplineError): - """ - Raised by calendar operations that would ask for dates beyond the extent of + """Raised by calendar operations that would ask for dates beyond the extent of our known data. """ @@ -778,9 +700,7 @@ def from_lookback_window( class UnsupportedDatetimeFormat(ZiplineError): - """ - Raised when an unsupported datetime is passed to an API method. - """ + """Raised when an unsupported datetime is passed to an API method.""" msg = ( "The input '{input}' passed to '{method}' is not " @@ -823,9 +743,7 @@ class NonExistentAssetInTimeFrame(ZiplineError): class InvalidCalendarName(ZiplineError): - """ - Raised when a calendar with an invalid name is requested. - """ + """Raised when a calendar with an invalid name is requested.""" msg = "The requested TradingCalendar, {calendar_name}, does not exist." diff --git a/src/zipline/examples/__init__.py b/src/zipline/examples/__init__.py index cd45026d56..cb810ca304 100644 --- a/src/zipline/examples/__init__.py +++ b/src/zipline/examples/__init__.py @@ -64,9 +64,7 @@ def load_example_modules(): def run_example(example_modules, example_name, environ, benchmark_returns=None): - """ - Run an example module from zipline.examples. - """ + """Run an example module from zipline.examples.""" mod = example_modules[example_name] register_calendar("YAHOO", get_calendar("NYSE"), force=True) diff --git a/src/zipline/examples/buy_and_hold.py b/src/zipline/examples/buy_and_hold.py index 79b4722fa5..817d31b2fb 100644 --- a/src/zipline/examples/buy_and_hold.py +++ b/src/zipline/examples/buy_and_hold.py @@ -42,7 +42,4 @@ def _test_args(): """Extra arguments to use when zipline's automated tests run this example.""" import pandas as pd - return { - "start": pd.Timestamp("2008", tz="utc"), - "end": pd.Timestamp("2013", tz="utc"), - } + return {"start": pd.Timestamp("2008"), "end": pd.Timestamp("2013")} diff --git a/src/zipline/examples/buyapple.py b/src/zipline/examples/buyapple.py index 890d7b148b..9533e2313d 100644 --- a/src/zipline/examples/buyapple.py +++ b/src/zipline/examples/buyapple.py @@ -40,6 +40,7 @@ def analyze(context=None, results=None): import matplotlib.pyplot as plt # Plot the portfolio and asset data. + plt.clf() ax1 = plt.subplot(211) results.portfolio_value.plot(ax=ax1) ax1.set_ylabel("Portfolio value (USD)") @@ -56,7 +57,4 @@ def _test_args(): """Extra arguments to use when zipline's automated tests run this example.""" import pandas as pd - return { - "start": pd.Timestamp("2014-01-01", tz="utc"), - "end": pd.Timestamp("2014-11-01", tz="utc"), - } + return {"start": pd.Timestamp("2014-01-01"), "end": pd.Timestamp("2014-11-01")} diff --git a/src/zipline/examples/buyapple_ide.py b/src/zipline/examples/buyapple_ide.py index 7a80d1c300..9263ab845b 100644 --- a/src/zipline/examples/buyapple_ide.py +++ b/src/zipline/examples/buyapple_ide.py @@ -64,8 +64,8 @@ def analyze(context=None, results=None): print(benchmark_returns.head()) result = run_algorithm( - start=start.tz_localize("UTC"), - end=end.tz_localize("UTC"), + start=start, + end=end, initialize=initialize, handle_data=handle_data, capital_base=100000, diff --git a/src/zipline/examples/dual_ema_talib.py b/src/zipline/examples/dual_ema_talib.py index 334a583882..8234017613 100644 --- a/src/zipline/examples/dual_ema_talib.py +++ b/src/zipline/examples/dual_ema_talib.py @@ -140,7 +140,4 @@ def _test_args(): """Extra arguments to use when zipline's automated tests run this example.""" import pandas as pd - return { - "start": pd.Timestamp("2014-01-01", tz="utc"), - "end": pd.Timestamp("2014-11-01", tz="utc"), - } + return {"start": pd.Timestamp("2014-01-01"), "end": pd.Timestamp("2014-11-01")} diff --git a/src/zipline/examples/dual_moving_average.py b/src/zipline/examples/dual_moving_average.py index e4c4aea45d..01f6b803a3 100644 --- a/src/zipline/examples/dual_moving_average.py +++ b/src/zipline/examples/dual_moving_average.py @@ -127,7 +127,4 @@ def _test_args(): """Extra arguments to use when zipline's automated tests run this example.""" import pandas as pd - return { - "start": pd.Timestamp("2011", tz="utc"), - "end": pd.Timestamp("2013", tz="utc"), - } + return {"start": pd.Timestamp("2011"), "end": pd.Timestamp("2013")} diff --git a/src/zipline/examples/momentum_pipeline.py b/src/zipline/examples/momentum_pipeline.py index 4426f69e8d..b6ab34a7b8 100644 --- a/src/zipline/examples/momentum_pipeline.py +++ b/src/zipline/examples/momentum_pipeline.py @@ -93,7 +93,7 @@ def _test_args(): return { # We run through october of 2013 because DELL is in the test data and # it went private on 2013-10-29. - "start": pd.Timestamp("2013-10-07", tz="utc"), - "end": pd.Timestamp("2013-11-30", tz="utc"), + "start": pd.Timestamp("2013-10-07"), + "end": pd.Timestamp("2013-11-30"), "capital_base": 100000, } diff --git a/src/zipline/examples/olmar.py b/src/zipline/examples/olmar.py index 218cd5b45d..8c87fdddfb 100644 --- a/src/zipline/examples/olmar.py +++ b/src/zipline/examples/olmar.py @@ -161,7 +161,4 @@ def _test_args(): """Extra arguments to use when zipline's automated tests run this example.""" import pandas as pd - return { - "start": pd.Timestamp("2004", tz="utc"), - "end": pd.Timestamp("2008", tz="utc"), - } + return {"start": pd.Timestamp("2004"), "end": pd.Timestamp("2008")} diff --git a/src/zipline/finance/asset_restrictions.py b/src/zipline/finance/asset_restrictions.py index 865f80a4ef..3b18000ad6 100644 --- a/src/zipline/finance/asset_restrictions.py +++ b/src/zipline/finance/asset_restrictions.py @@ -23,15 +23,13 @@ class Restrictions(metaclass=abc.ABCMeta): - """ - Abstract restricted list interface, representing a set of assets that an + """Abstract restricted list interface, representing a set of assets that an algorithm is restricted from trading. """ @abc.abstractmethod def is_restricted(self, assets, dt): - """ - Is the asset restricted (RestrictionStates.FROZEN) on the given dt? + """Is the asset restricted (RestrictionStates.FROZEN) on the given dt? Parameters ---------- @@ -59,8 +57,7 @@ def __or__(self, other_restriction): class _UnionRestrictions(Restrictions): - """ - A union of a number of sub restrictions. + """A union of a number of sub restrictions. Parameters ---------- @@ -89,8 +86,7 @@ def __new__(cls, sub_restrictions): return new_instance def __or__(self, other_restriction): - """ - Overrides the base implementation for combining two restrictions, of + """Overrides the base implementation for combining two restrictions, of which the left side is a _UnionRestrictions. """ # Flatten the underlying sub restrictions of _UnionRestrictions @@ -114,9 +110,7 @@ def is_restricted(self, assets, dt): class NoRestrictions(Restrictions): - """ - A no-op restrictions that contains no restrictions. - """ + """A no-op restrictions that contains no restrictions.""" def is_restricted(self, assets, dt): if isinstance(assets, Asset): @@ -125,8 +119,7 @@ def is_restricted(self, assets, dt): class StaticRestrictions(Restrictions): - """ - Static restrictions stored in memory that are constant regardless of dt + """Static restrictions stored in memory that are constant regardless of dt for each asset. Parameters @@ -139,9 +132,7 @@ def __init__(self, restricted_list): self._restricted_set = frozenset(restricted_list) def is_restricted(self, assets, dt): - """ - An asset is restricted for all dts if it is in the static list. - """ + """An asset is restricted for all dts if it is in the static list.""" if isinstance(assets, Asset): return assets in self._restricted_set return pd.Series( @@ -151,8 +142,7 @@ def is_restricted(self, assets, dt): class HistoricalRestrictions(Restrictions): - """ - Historical restrictions stored in memory with effective dates for each + """Historical restrictions stored in memory with effective dates for each asset. Parameters @@ -172,8 +162,7 @@ def __init__(self, restrictions): } def is_restricted(self, assets, dt): - """ - Returns whether or not an asset or iterable of assets is restricted + """Returns whether or not an asset or iterable of assets is restricted on a dt. """ if isinstance(assets, Asset): @@ -188,15 +177,17 @@ def is_restricted(self, assets, dt): def _is_restricted_for_asset(self, asset, dt): state = RESTRICTION_STATES.ALLOWED for r in self._restrictions_by_asset.get(asset, ()): - if r.effective_date > dt: + r_effective_date = r.effective_date + if r_effective_date.tzinfo is None: + r_effective_date = r_effective_date.tz_localize(dt.tzinfo) + if r_effective_date > dt: break state = r.state return state == RESTRICTION_STATES.FROZEN class SecurityListRestrictions(Restrictions): - """ - Restrictions based on a security list. + """Restrictions based on a security list. Parameters ---------- diff --git a/src/zipline/finance/controls.py b/src/zipline/finance/controls.py index 03971efbba..148b4662bb 100644 --- a/src/zipline/finance/controls.py +++ b/src/zipline/finance/controls.py @@ -31,14 +31,12 @@ class TradingControl(metaclass=abc.ABCMeta): - """ - Abstract base class representing a fail-safe control on the behavior of any + """Abstract base class representing a fail-safe control on the behavior of any algorithm. """ def __init__(self, on_error, **kwargs): - """ - Track any arguments that should be printed in the error message + """Track any arguments that should be printed in the error message generated by self.fail. """ self.on_error = on_error @@ -46,8 +44,7 @@ def __init__(self, on_error, **kwargs): @abc.abstractmethod def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): - """ - Before any order is executed by TradingAlgorithm, this method should be + """Before any order is executed by TradingAlgorithm, this method should be called *exactly once* on each registered TradingControl object. If the specified asset and amount do not violate this TradingControl's @@ -68,8 +65,7 @@ def _constraint_msg(self, metadata): return constraint def handle_violation(self, asset, amount, datetime, metadata=None): - """ - Handle a TradingControlViolation, either by raising or logging and + """Handle a TradingControlViolation, either by raising or logging and error with information about the failure. If dynamic information should be displayed as well, pass it in via @@ -95,8 +91,7 @@ def __repr__(self): class MaxOrderCount(TradingControl): - """ - TradingControl representing a limit on the number of orders that can be + """TradingControl representing a limit on the number of orders that can be placed in a given trading day. """ @@ -108,9 +103,7 @@ def __init__(self, on_error, max_count): self.current_date = None def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): - """ - Fail if we've already placed self.max_count orders today. - """ + """Fail if we've already placed self.max_count orders today.""" algo_date = algo_datetime.date() # Reset order count if it's a new day. @@ -146,8 +139,7 @@ def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): class MaxOrderSize(TradingControl): - """ - TradingControl representing a limit on the magnitude of any single order + """TradingControl representing a limit on the magnitude of any single order placed with the given asset. Can be specified by share or by dollar value. """ @@ -170,8 +162,7 @@ def __init__(self, on_error, asset=None, max_shares=None, max_notional=None): raise ValueError("max_notional must be positive.") def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): - """ - Fail if the magnitude of the given order exceeds either self.max_shares + """Fail if the magnitude of the given order exceeds either self.max_shares or self.max_notional. """ @@ -193,8 +184,7 @@ def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): class MaxPositionSize(TradingControl): - """ - TradingControl representing a limit on the maximum position size that can + """TradingControl representing a limit on the maximum position size that can be held by an algo for a given asset. """ @@ -216,8 +206,7 @@ def __init__(self, on_error, asset=None, max_shares=None, max_notional=None): raise ValueError("max_notional must be positive.") def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): - """ - Fail if the given order would cause the magnitude of our position to be + """Fail if the given order would cause the magnitude of our position to be greater in shares than self.max_shares or greater in dollar value than self.max_notional. """ @@ -246,9 +235,7 @@ def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): class LongOnly(TradingControl): - """ - TradingControl representing a prohibition against holding short positions. - """ + """TradingControl representing a prohibition against holding short positions.""" def __init__(self, on_error): super(LongOnly, self).__init__(on_error) @@ -263,8 +250,7 @@ def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): class AssetDateBounds(TradingControl): - """ - TradingControl representing a prohibition against ordering an asset before + """TradingControl representing a prohibition against ordering an asset before its start_date, or after its end_date. """ @@ -272,47 +258,43 @@ def __init__(self, on_error): super(AssetDateBounds, self).__init__(on_error) def validate(self, asset, amount, portfolio, algo_datetime, algo_current_data): - """ - Fail if the algo has passed this Asset's end_date, or before the + """Fail if the algo has passed this Asset's end_date, or before the Asset's start date. """ # If the order is for 0 shares, then silently pass through. if amount == 0: return - normalized_algo_dt = pd.Timestamp(algo_datetime).normalize() + normalized_algo_dt = algo_datetime.normalize().tz_localize(None) # Fail if the algo is before this Asset's start_date if asset.start_date: - normalized_start = pd.Timestamp(asset.start_date).normalize() + normalized_start = asset.start_date.normalize() if normalized_algo_dt < normalized_start: metadata = {"asset_start_date": normalized_start} self.handle_violation(asset, amount, algo_datetime, metadata=metadata) # Fail if the algo has passed this Asset's end_date if asset.end_date: - normalized_end = pd.Timestamp(asset.end_date).normalize() + normalized_end = asset.end_date.normalize() if normalized_algo_dt > normalized_end: metadata = {"asset_end_date": normalized_end} self.handle_violation(asset, amount, algo_datetime, metadata=metadata) class AccountControl(metaclass=abc.ABCMeta): - """ - Abstract base class representing a fail-safe control on the behavior of any + """Abstract base class representing a fail-safe control on the behavior of any algorithm. """ def __init__(self, **kwargs): - """ - Track any arguments that should be printed in the error message + """Track any arguments that should be printed in the error message generated by self.fail. """ self.__fail_args = kwargs @abc.abstractmethod def validate(self, _portfolio, _account, _algo_datetime, _algo_current_data): - """ - On each call to handle data by TradingAlgorithm, this method should be + """On each call to handle data by TradingAlgorithm, this method should be called *exactly once* on each registered AccountControl object. If the check does not violate this AccountControl's restraint given @@ -325,9 +307,7 @@ def validate(self, _portfolio, _account, _algo_datetime, _algo_current_data): raise NotImplementedError def fail(self): - """ - Raise an AccountControlViolation with information about the failure. - """ + """Raise an AccountControlViolation with information about the failure.""" raise AccountControlViolation(constraint=repr(self)) def __repr__(self): @@ -337,14 +317,12 @@ def __repr__(self): class MaxLeverage(AccountControl): - """ - AccountControl representing a limit on the maximum leverage allowed + """AccountControl representing a limit on the maximum leverage allowed by the algorithm. """ def __init__(self, max_leverage): - """ - max_leverage is the gross leverage in decimal form. For example, + """max_leverage is the gross leverage in decimal form. For example, 2, limits an algorithm to trading at most double the account value. """ super(MaxLeverage, self).__init__(max_leverage=max_leverage) @@ -357,9 +335,7 @@ def __init__(self, max_leverage): raise ValueError("max_leverage must be positive") def validate(self, _portfolio, _account, _algo_datetime, _algo_current_data): - """ - Fail if the leverage is greater than the allowed leverage. - """ + """Fail if the leverage is greater than the allowed leverage.""" if _account.leverage > self.max_leverage: self.fail() @@ -389,9 +365,11 @@ def __init__(self, min_leverage, deadline): self.deadline = deadline def validate(self, _portfolio, account, algo_datetime, _algo_current_data): - """ - Make validation checks if we are after the deadline. + """Make validation checks if we are after the deadline. Fail if the leverage is less than the min leverage. """ - if algo_datetime > self.deadline and account.leverage < self.min_leverage: + if ( + algo_datetime > self.deadline.tz_localize(algo_datetime.tzinfo) + and account.leverage < self.min_leverage + ): self.fail() diff --git a/src/zipline/finance/metrics/metric.py b/src/zipline/finance/metrics/metric.py index 1e3e8e75b4..1bd5538909 100644 --- a/src/zipline/finance/metrics/metric.py +++ b/src/zipline/finance/metrics/metric.py @@ -466,11 +466,14 @@ def risk_metric_period( ] # Benchmark needs to be masked to the same dates as the algo returns + benchmark_ret_tzinfo = benchmark_returns.index.tzinfo benchmark_returns = benchmark_returns[ - (benchmark_returns.index >= start_session) - & (benchmark_returns.index <= algorithm_returns.index[-1]) + (benchmark_returns.index >= start_session.tz_localize(benchmark_ret_tzinfo)) + & ( + benchmark_returns.index + <= algorithm_returns.index[-1].tz_localize(benchmark_ret_tzinfo) + ) ] - benchmark_period_returns = ep.cum_returns(benchmark_returns).iloc[-1] algorithm_period_returns = ep.cum_returns(algorithm_returns).iloc[-1] @@ -536,7 +539,7 @@ def _periods_in_range( return tzinfo = end_date.tzinfo - end_date = end_date.tz_convert(None) + end_date = end_date for period_timestamp in months: period = period_timestamp.tz_localize(None).to_period( freq="%dM" % months_per @@ -569,7 +572,7 @@ def risk_report(cls, algorithm_returns, benchmark_returns, algorithm_leverages): periods_in_range = partial( cls._periods_in_range, months=months, - end_session=end_session.tz_convert(None), + end_session=end_session, end_date=end, algorithm_returns=algorithm_returns, benchmark_returns=benchmark_returns, diff --git a/src/zipline/finance/metrics/tracker.py b/src/zipline/finance/metrics/tracker.py index 38d07bd287..4f95175da4 100644 --- a/src/zipline/finance/metrics/tracker.py +++ b/src/zipline/finance/metrics/tracker.py @@ -55,9 +55,14 @@ class MetricsTracker: @staticmethod def _execution_open_and_close(calendar, session): - open_, close = calendar.open_and_close_for_session(session) - execution_open = calendar.execution_time_from_open(open_) - execution_close = calendar.execution_time_from_close(close) + if session.tzinfo is not None: + session = session.tz_localize(None) + + open_ = calendar.session_first_minute(session) + close = calendar.session_close(session) + + execution_open = open_ + execution_close = close return execution_open, execution_close @@ -203,8 +208,7 @@ def sync_last_sale_prices(self, dt, data_portal, handle_non_market_minutes=False ) def handle_minute_close(self, dt, data_portal): - """ - Handles the close of the given minute in minute emission. + """Handles the close of the given minute in minute emission. Parameters ---------- @@ -329,8 +333,7 @@ def handle_market_close(self, dt, data_portal): return packet def handle_simulation_end(self, data_portal): - """ - When the simulation is complete, run the full period risk report + """When the simulation is complete, run the full period risk report and send it out on the results socket. """ log.info( diff --git a/src/zipline/finance/slippage.py b/src/zipline/finance/slippage.py index 7fe7bc60d5..93aaf8c6a5 100644 --- a/src/zipline/finance/slippage.py +++ b/src/zipline/finance/slippage.py @@ -46,8 +46,7 @@ class LiquidityExceeded(Exception): def fill_price_worse_than_limit_price(fill_price, order): - """ - Checks whether the fill price is worse than the order's limit price. + """Checks whether the fill price is worse than the order's limit price. Parameters ---------- @@ -80,8 +79,7 @@ def fill_price_worse_than_limit_price(fill_price, order): class SlippageModel(metaclass=FinancialModelMeta): - """ - Abstract base class for slippage models. + """Abstract base class for slippage models. Slippage models are responsible for the rates and prices at which orders fill during a simulation. @@ -122,8 +120,7 @@ def volume_for_bar(self): @abstractmethod def process_order(self, data, order): - """ - Compute the number of shares and price to fill for ``order`` in the + """Compute the number of shares and price to fill for ``order`` in the current minute. Parameters @@ -230,24 +227,19 @@ def process_order(data, order): class EquitySlippageModel(SlippageModel, metaclass=AllowedAssetMarker): - """ - Base class for slippage models which only support equities. - """ + """Base class for slippage models which only support equities.""" allowed_asset_types = (Equity,) class FutureSlippageModel(SlippageModel, metaclass=AllowedAssetMarker): - """ - Base class for slippage models which only support futures. - """ + """Base class for slippage models which only support futures.""" allowed_asset_types = (Future,) class VolumeShareSlippage(SlippageModel): - """ - Model slippage as a quadratic function of percentage of historical volume. + """Model slippage as a quadratic function of percentage of historical volume. Orders to buy will be filled at:: @@ -343,8 +335,7 @@ def process_order(self, data, order): class FixedSlippage(SlippageModel): - """ - Simple model assuming a fixed-size spread for all assets. + """Simple model assuming a fixed-size spread for all assets. Parameters ---------- @@ -378,8 +369,7 @@ def process_order(self, data, order): class MarketImpactBase(SlippageModel): - """ - Base class for slippage models which compute a simulated price impact + """Base class for slippage models which compute a simulated price impact according to a history lookback. """ @@ -391,8 +381,7 @@ def __init__(self): @abstractmethod def get_txn_volume(self, data, order): - """ - Return the number of shares we would like to order in this minute. + """Return the number of shares we would like to order in this minute. Parameters ---------- @@ -415,8 +404,7 @@ def get_simulated_impact( mean_volume, volatility, ): - """ - Calculate simulated price impact. + """Calculate simulated price impact. Parameters ---------- @@ -476,8 +464,7 @@ def process_order(self, data, order): return impacted_price, math.copysign(txn_volume, order.direction) def _get_window_data(self, data, asset, window_length): - """ - Internal utility method to return the trailing mean volume over the + """Internal utility method to return the trailing mean volume over the past 'window_length' days, and volatility of close prices for a specific asset. @@ -534,8 +521,7 @@ def _get_window_data(self, data, asset, window_length): class VolatilityVolumeShare(MarketImpactBase): - """ - Model slippage for futures contracts according to the following formula: + """Model slippage for futures contracts according to the following formula: new_price = price + (price * MI / 10000), diff --git a/src/zipline/finance/trading.py b/src/zipline/finance/trading.py index 321d3302af..d29ceb2d99 100644 --- a/src/zipline/finance/trading.py +++ b/src/zipline/finance/trading.py @@ -17,7 +17,6 @@ import pandas as pd from zipline.utils.memoize import remember_last -from zipline.utils.pandas_utils import normalize_date log = logging.getLogger("Trading") @@ -43,17 +42,17 @@ def __init__( assert trading_calendar is not None, "Must pass in trading calendar!" assert start_session <= end_session, "Period start falls after period end." assert ( - start_session <= trading_calendar.last_trading_session + start_session.tz_localize(None) <= trading_calendar.last_session ), "Period start falls after the last known trading day." assert ( - end_session >= trading_calendar.first_trading_session + end_session.tz_localize(None) >= trading_calendar.first_session ), "Period end falls before the first known trading day." # chop off any minutes or hours on the given start and end dates, # as we only support session labels here (and we represent session # labels as midnight UTC). - self._start_session = normalize_date(start_session) - self._end_session = normalize_date(end_session) + self._start_session = start_session.normalize() + self._end_session = end_session.normalize() self._capital_base = capital_base self._emission_rate = emission_rate @@ -64,27 +63,27 @@ def __init__( self._trading_calendar = trading_calendar - if not trading_calendar.is_session(self._start_session): + if not trading_calendar.is_session(self._start_session.tz_localize(None)): # if the start date is not a valid session in this calendar, # push it forward to the first valid session - self._start_session = trading_calendar.minute_to_session_label( + self._start_session = trading_calendar.minute_to_session( self._start_session ) - if not trading_calendar.is_session(self._end_session): + if not trading_calendar.is_session(self._end_session.tz_localize(None)): # if the end date is not a valid session in this calendar, # pull it backward to the last valid session before the given # end date. - self._end_session = trading_calendar.minute_to_session_label( + self._end_session = trading_calendar.minute_to_session( self._end_session, direction="previous" ) - self._first_open = trading_calendar.open_and_close_for_session( - self._start_session - )[0] - self._last_close = trading_calendar.open_and_close_for_session( - self._end_session - )[1] + self._first_open = trading_calendar.session_first_minute( + self._start_session.tz_localize(None) + ) + self._last_close = trading_calendar.session_close( + self._end_session.tz_localize(None) + ) @property def capital_base(self): diff --git a/src/zipline/gens/tradesimulation.py b/src/zipline/gens/tradesimulation.py index cf95eabe60..ad8d6edd7b 100644 --- a/src/zipline/gens/tradesimulation.py +++ b/src/zipline/gens/tradesimulation.py @@ -272,6 +272,8 @@ def _cleanup_expired_assets(self, dt, position_assets): def past_auto_close_date(asset): acd = asset.auto_close_date + if acd is not None: + acd = acd.tz_localize(dt.tzinfo) return acd is not None and acd <= dt # Remove positions in any sids that have reached their auto_close date. diff --git a/src/zipline/pipeline/domain.py b/src/zipline/pipeline/domain.py index 825b5fb31f..35fb6b4ac9 100644 --- a/src/zipline/pipeline/domain.py +++ b/src/zipline/pipeline/domain.py @@ -34,9 +34,8 @@ class IDomain(Interface): """Domain interface.""" - def all_sessions(self): - """ - Get all trading sessions for the calendar of this domain. + def sessions(self): + """Get all trading sessions for the calendar of this domain. This determines the row labels of Pipeline outputs for pipelines run on this domain. @@ -75,8 +74,7 @@ def data_query_cutoff_for_sessions(self, sessions): @default def roll_forward(self, dt): - """ - Given a date, align it to the calendar of the pipeline's domain. + """Given a date, align it to the calendar of the pipeline's domain. Parameters ---------- @@ -86,26 +84,19 @@ def roll_forward(self, dt): ------- pd.Timestamp """ - try: - dt = pd.Timestamp(dt).tz_convert("UTC") - except TypeError: - dt = pd.Timestamp(dt).tz_localize("UTC") - - trading_days = self.all_sessions() + dt = pd.Timestamp(dt) + trading_days = self.sessions() try: return trading_days[trading_days.searchsorted(dt)] except IndexError as exc: raise ValueError( - "Date {} was past the last session for domain {}. " - "The last session for this domain is {}.".format( - dt.date(), self, trading_days[-1].date() - ) + f"Date {dt.date()} was past the last session for domain {self}. " + f"The last session for this domain is {trading_days[-1].date()}." ) from exc Domain = implements(IDomain) -Domain.__doc__ = """ -A domain represents a set of labels for the arrays computed by a Pipeline. +Domain.__doc__ = """A domain represents a set of labels for the arrays computed by a Pipeline. A domain defines two things: @@ -126,7 +117,7 @@ def roll_forward(self, dt): class GenericDomain(Domain): """Special singleton class used to represent generic DataSets and Columns.""" - def all_sessions(self): + def sessions(self): raise NotImplementedError("Can't get sessions for generic domain.") @property @@ -146,8 +137,7 @@ def __repr__(self): class EquityCalendarDomain(Domain): - """ - An equity domain whose sessions are defined by a named TradingCalendar. + """An equity domain whose sessions are defined by a named TradingCalendar. Parameters ---------- @@ -192,24 +182,20 @@ def country_code(self): def calendar(self): return get_calendar(self.calendar_name) - def all_sessions(self): - return self.calendar.all_sessions + def sessions(self): + return self.calendar.sessions def data_query_cutoff_for_sessions(self, sessions): - opens = self.calendar.opens.reindex(sessions).values + opens = self.calendar.first_minutes.reindex(sessions) missing_mask = pd.isnull(opens) if missing_mask.any(): missing_days = sessions[missing_mask] raise ValueError( "cannot resolve data query time for sessions that are not on" - " the %s calendar:\n%s" - % ( - self.calendar.name, - missing_days, - ), + f" the {self.calendar_name} calendar:\n{missing_days}" ) - return pd.DatetimeIndex(opens + self._data_query_offset, tz="UTC") + return pd.DatetimeIndex(opens) + self._data_query_offset def __repr__(self): return "EquityCalendarDomain({!r}, {!r})".format( @@ -312,8 +298,7 @@ def __repr__(self): def infer_domain(terms): - """ - Infer the domain from a collection of terms. + """Infer the domain from a collection of terms. The algorithm for inferring domains is as follows: @@ -357,9 +342,7 @@ def infer_domain(terms): # This would be better if we provided more context for which domains came from # which terms. class AmbiguousDomain(Exception): - """ - Raised when we attempt to infer a domain from a collection of mixed terms. - """ + """Raised when we attempt to infer a domain from a collection of mixed terms.""" _TEMPLATE = dedent( """\ @@ -404,7 +387,11 @@ class EquitySessionDomain(Domain): __funcname="EquitySessionDomain", ) def __init__( - self, sessions, country_code, data_query_time=None, data_query_date_offset=0 + self, + sessions, + country_code, + data_query_time=None, + data_query_date_offset=0, ): self._country_code = country_code self._sessions = sessions @@ -422,7 +409,7 @@ def __init__( def country_code(self): return self._country_code - def all_sessions(self): + def sessions(self): return self._sessions def data_query_cutoff_for_sessions(self, sessions): diff --git a/src/zipline/pipeline/engine.py b/src/zipline/pipeline/engine.py index da99e09d0e..8e4a3e9463 100644 --- a/src/zipline/pipeline/engine.py +++ b/src/zipline/pipeline/engine.py @@ -83,8 +83,7 @@ class PipelineEngine(ABC): @abstractmethod def run_pipeline(self, pipeline, start_date, end_date, hooks=None): - """ - Compute values for ``pipeline`` from ``start_date`` to ``end_date``. + """Compute values for ``pipeline`` from ``start_date`` to ``end_date``. Parameters ---------- @@ -117,8 +116,7 @@ def run_pipeline(self, pipeline, start_date, end_date, hooks=None): def run_chunked_pipeline( self, pipeline, start_date, end_date, chunksize, hooks=None ): - """ - Compute values for ``pipeline`` from ``start_date`` to ``end_date``, in + """Compute values for ``pipeline`` from ``start_date`` to ``end_date``, in date chunks of size ``chunksize``. Chunked execution reduces memory consumption, and may reduce @@ -159,16 +157,13 @@ def run_chunked_pipeline( class NoEngineRegistered(Exception): - """ - Raised if a user tries to call pipeline_output in an algorithm that hasn't + """Raised if a user tries to call pipeline_output in an algorithm that hasn't set up a pipeline engine. """ class ExplodingPipelineEngine(PipelineEngine): - """ - A PipelineEngine that doesn't do anything. - """ + """A PipelineEngine that doesn't do anything.""" def run_pipeline(self, pipeline, start_date, end_date, hooks=None): raise NoEngineRegistered( @@ -216,8 +211,7 @@ def default_populate_initial_workspace( class SimplePipelineEngine(PipelineEngine): - """ - PipelineEngine class that computes each term independently. + """PipelineEngine class that computes each term independently. Parameters ---------- @@ -281,8 +275,7 @@ def __init__( def run_chunked_pipeline( self, pipeline, start_date, end_date, chunksize, hooks=None ): - """ - Compute values for ``pipeline`` from ``start_date`` to ``end_date``, in + """Compute values for ``pipeline`` from ``start_date`` to ``end_date``, in date chunks of size ``chunksize``. Chunked execution reduces memory consumption, and may reduce @@ -321,7 +314,7 @@ def run_chunked_pipeline( """ domain = self.resolve_domain(pipeline) ranges = compute_date_range_chunks( - domain.all_sessions(), + domain.sessions(), start_date, end_date, chunksize, @@ -343,8 +336,7 @@ def run_chunked_pipeline( return categorical_df_concat(nonempty_chunks, inplace=True) def run_pipeline(self, pipeline, start_date, end_date, hooks=None): - """ - Compute values for ``pipeline`` from ``start_date`` to ``end_date``. + """Compute values for ``pipeline`` from ``start_date`` to ``end_date``. Parameters ---------- @@ -387,7 +379,7 @@ def _run_pipeline_impl(self, pipeline, start_date, end_date, hooks): if end_date < start_date: raise ValueError( "start_date must be before or equal to end_date \n" - "start_date=%s, end_date=%s" % (start_date, end_date) + f"start_date={start_date}, end_date={end_date}" ) domain = self.resolve_domain(pipeline) @@ -441,8 +433,7 @@ def _run_pipeline_impl(self, pipeline, start_date, end_date, hooks): ) def _compute_root_mask(self, domain, start_date, end_date, extra_rows): - """ - Compute a lifetimes matrix from our AssetFinder, then drop columns that + """Compute a lifetimes matrix from our AssetFinder, then drop columns that didn't exist at all during the query dates. Parameters @@ -467,18 +458,18 @@ def _compute_root_mask(self, domain, start_date, end_date, extra_rows): that existed for at least one day between `start_date` and `end_date`. """ - sessions = domain.all_sessions() + sessions = domain.sessions() if start_date not in sessions: raise ValueError( - "Pipeline start date ({}) is not a trading session for " - "domain {}.".format(start_date, domain) + f"Pipeline start date ({start_date}) is not a trading session for " + f"domain {domain}." ) elif end_date not in sessions: raise ValueError( - "Pipeline end date {} is not a trading session for " - "domain {}.".format(end_date, domain) + f"Pipeline end date {end_date} is not a trading session for " + f"domain {domain}." ) start_idx, end_idx = sessions.slice_locs(start_date, end_date) @@ -579,8 +570,7 @@ def _inputs_for_term(term, workspace, graph, domain, refcounts): def compute_chunk( self, graph, dates, sids, workspace, refcounts, execution_order, hooks ): - """ - Compute the Pipeline terms in the graph for the requested start and end + """Compute the Pipeline terms in the graph for the requested start and end dates. This is where we do the actual work of running a pipeline. diff --git a/src/zipline/pipeline/factors/statistical.py b/src/zipline/pipeline/factors/statistical.py index 93e03758db..91c085c57a 100644 --- a/src/zipline/pipeline/factors/statistical.py +++ b/src/zipline/pipeline/factors/statistical.py @@ -351,8 +351,7 @@ def __new__(cls, target, returns_length, correlation_length, mask=NotSpecified): class RollingLinearRegressionOfReturns(RollingLinearRegression): - """ - Perform an ordinary least-squares regression predicting the returns of all + """Perform an ordinary least-squares regression predicting the returns of all other assets on the given asset. Parameters @@ -467,8 +466,7 @@ def __new__(cls, target, returns_length, regression_length, mask=NotSpecified): class SimpleBeta(CustomFactor, StandardOutputs): - """ - Factor producing the slope of a regression line between each asset's daily + """Factor producing the slope of a regression line between each asset's daily returns to the daily returns of a single "target" asset. Parameters @@ -545,8 +543,7 @@ def __repr__(self): def vectorized_beta(dependents, independent, allowed_missing, out=None): - """ - Compute slopes of linear regressions between columns of ``dependents`` and + """Compute slopes of linear regressions between columns of ``dependents`` and ``independent``. Parameters @@ -644,8 +641,7 @@ def vectorized_beta(dependents, independent, allowed_missing, out=None): def vectorized_pearson_r(dependents, independents, allowed_missing, out=None): - """ - Compute Pearson's r between columns of ``dependents`` and ``independents``. + """Compute Pearson's r between columns of ``dependents`` and ``independents``. Parameters ---------- diff --git a/src/zipline/pipeline/graph.py b/src/zipline/pipeline/graph.py index 273b99f8c6..009f59fe12 100644 --- a/src/zipline/pipeline/graph.py +++ b/src/zipline/pipeline/graph.py @@ -285,7 +285,7 @@ def __init__(self, domain, terms, start_date, end_date, min_extra_rows=0): self.domain = domain - sessions = domain.all_sessions() + sessions = domain.sessions() for term in terms.values(): self.set_extra_rows( term, diff --git a/src/zipline/pipeline/loaders/earnings_estimates.py b/src/zipline/pipeline/loaders/earnings_estimates.py index 5c660bfe41..ce280bc0a8 100644 --- a/src/zipline/pipeline/loaders/earnings_estimates.py +++ b/src/zipline/pipeline/loaders/earnings_estimates.py @@ -64,8 +64,7 @@ def split_normalized_quarters(normalized_quarters): def required_estimates_fields(columns): - """ - Compute the set of resource columns required to serve + """Compute the set of resource columns required to serve `columns`. """ # We also expect any of the field names that our loadable columns @@ -74,8 +73,7 @@ def required_estimates_fields(columns): def validate_column_specs(events, columns): - """ - Verify that the columns of ``events`` can be used by a + """Verify that the columns of ``events`` can be used by a EarningsEstimatesLoader to serve the BoundColumns described by `columns`. """ @@ -102,8 +100,7 @@ def add_new_adjustments(adjustments_dict, adjustments, column_name, ts): class EarningsEstimatesLoader(implements(PipelineLoader)): - """ - An abstract pipeline loader for estimates data that can load data a + """An abstract pipeline loader for estimates data that can load data a variable number of quarters forwards/backwards from calendar dates depending on the `num_announcements` attribute of the columns' dataset. If split adjustments are to be applied, a loader, split-adjusted columns, @@ -195,8 +192,7 @@ def get_requested_quarter_data( num_announcements, dates, ): - """ - Selects the requested data for each date. + """Selects the requested data for each date. Parameters ---------- @@ -255,8 +251,7 @@ def get_requested_quarter_data( return requested_qtr_data.unstack(SID_FIELD_NAME).reindex(dates) def get_split_adjusted_asof_idx(self, dates): - """ - Compute the index in `dates` where the split-adjusted-asof-date + """Compute the index in `dates` where the split-adjusted-asof-date falls. This is the date up to which, and including which, we will need to unapply all adjustments for and then re-apply them as they come in. After this date, adjustments are applied as normal. @@ -271,10 +266,8 @@ def get_split_adjusted_asof_idx(self, dates): split_adjusted_asof_idx : int The index in `dates` at which the data should be split. """ - split_adjusted_asof_idx = dates.searchsorted( - pd.to_datetime(self._split_adjusted_asof, utc=True) - # make_utc_aware(pd.DatetimeIndex(self._split_adjusted_asof)) - ) + split_adjusted_asof_idx = dates.searchsorted(self._split_adjusted_asof) + # make_utc_aware(pd.DatetimeIndex(self._split_adjusted_asof)) # The split-asof date is after the date index. if split_adjusted_asof_idx == len(dates): split_adjusted_asof_idx = len(dates) - 1 @@ -282,7 +275,7 @@ def get_split_adjusted_asof_idx(self, dates): if self._split_adjusted_asof < dates[0]: split_adjusted_asof_idx = -1 else: - if self._split_adjusted_asof < dates[0].tz_localize(None): + if self._split_adjusted_asof < dates[0]: split_adjusted_asof_idx = -1 return split_adjusted_asof_idx @@ -297,8 +290,7 @@ def collect_overwrites_for_sid( all_adjustments_for_sid, sid, ): - """ - Given a sid, collect all overwrites that should be applied for this + """Given a sid, collect all overwrites that should be applied for this sid at each quarter boundary. Parameters @@ -332,8 +324,7 @@ def collect_overwrites_for_sid( return next_qtr_start_indices = dates.searchsorted( - # pd.to_datetime(group[EVENT_DATE_FIELD_NAME], utc=True), - make_utc_aware(pd.DatetimeIndex(group[EVENT_DATE_FIELD_NAME])), + pd.DatetimeIndex(group[EVENT_DATE_FIELD_NAME]), side=self.searchsorted_side, ) @@ -419,8 +410,7 @@ def get_adjustments_for_sid( def merge_into_adjustments_for_all_sids( self, all_adjustments_for_sid, col_to_all_adjustments ): - """ - Merge adjustments for a particular sid into a dictionary containing + """Merge adjustments for a particular sid into a dictionary containing adjustments for all sids. Parameters @@ -448,8 +438,7 @@ def get_adjustments( columns, **kwargs, ): - """ - Creates an AdjustedArray from the given estimates data for the given + """Creates an AdjustedArray from the given estimates data for the given dates. Parameters @@ -512,8 +501,7 @@ def create_overwrites_for_quarter( sid_idx, columns, ): - """ - Add entries to the dictionary of columns to adjustments for the given + """Add entries to the dictionary of columns to adjustments for the given sid and the given quarter. Parameters @@ -677,8 +665,7 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): def get_last_data_per_qtr( self, assets_with_data, columns, dates, data_query_cutoff_times ): - """ - Determine the last piece of information we know for each column on each + """Determine the last piece of information we know for each column on each date in the index for each sid and quarter. Parameters @@ -725,7 +712,7 @@ def get_last_data_per_qtr( inplace=True, ) stacked_last_per_qtr[EVENT_DATE_FIELD_NAME] = pd.to_datetime( - stacked_last_per_qtr[EVENT_DATE_FIELD_NAME], utc=True + stacked_last_per_qtr[EVENT_DATE_FIELD_NAME] ) stacked_last_per_qtr = stacked_last_per_qtr.sort_values(EVENT_DATE_FIELD_NAME) return last_per_qtr, stacked_last_per_qtr @@ -764,8 +751,7 @@ def get_shifted_qtrs(self, zero_qtrs, num_announcements): return zero_qtrs + (num_announcements - 1) def get_zeroth_quarter_idx(self, stacked_last_per_qtr): - """ - Filters for releases that are on or after each simulation date and + """Filters for releases that are on or after each simulation date and determines the next quarter by picking out the upcoming release for each date in the index. @@ -826,8 +812,7 @@ def get_shifted_qtrs(self, zero_qtrs, num_announcements): return zero_qtrs - (num_announcements - 1) def get_zeroth_quarter_idx(self, stacked_last_per_qtr): - """ - Filters for releases that are on or after each simulation date and + """Filters for releases that are on or after each simulation date and determines the previous quarter by picking out the most recent release relative to each date in the index. @@ -878,8 +863,7 @@ def validate_split_adjusted_column_specs(name_map, columns): class SplitAdjustedEstimatesLoader(EarningsEstimatesLoader): - """ - Estimates loader that loads data that needs to be split-adjusted. + """Estimates loader that loads data that needs to be split-adjusted. Parameters ---------- @@ -939,8 +923,7 @@ def get_adjustments_for_sid( split_adjusted_asof_idx=None, split_adjusted_cols_for_group=None, ): - """ - Collects both overwrites and adjustments for a particular sid. + """Collects both overwrites and adjustments for a particular sid. Parameters ---------- @@ -1001,9 +984,7 @@ def get_adjustments( columns, **kwargs, ): - """ - Calculates both split adjustments and overwrites for all sids. - """ + """Calculates both split adjustments and overwrites for all sids.""" split_adjusted_cols_for_group = [ self.name_map[col.name] for col in columns @@ -1025,8 +1006,7 @@ def get_adjustments( def determine_end_idx_for_adjustment( self, adjustment_ts, dates, upper_bound, requested_quarter, sid_estimates ): - """ - Determines the date until which the adjustment at the given date + """Determines the date until which the adjustment at the given date index should be applied for the given quarter. Parameters @@ -1058,13 +1038,11 @@ def determine_end_idx_for_adjustment( # the date of this adjustment newest_kd_for_qtr = sid_estimates[ (sid_estimates[NORMALIZED_QUARTERS] == requested_quarter) - & (pd.to_datetime(sid_estimates[TS_FIELD_NAME], utc=True) >= adjustment_ts) + & (sid_estimates[TS_FIELD_NAME] >= adjustment_ts) ][TS_FIELD_NAME].min() if pd.notnull(newest_kd_for_qtr): - newest_kd_idx = dates.searchsorted( - pd.to_datetime(newest_kd_for_qtr, utc=True) - # make_utc_aware(pd.DatetimeIndex(newest_kd_for_qtr)) - ) + newest_kd_idx = dates.searchsorted(newest_kd_for_qtr) + # make_utc_aware(pd.DatetimeIndex(newest_kd_for_qtr)) # We have fresh information that comes in # before the end of the overwrite and # presumably is already split-adjusted to the @@ -1082,8 +1060,7 @@ def collect_pre_split_asof_date_adjustments( pre_adjustments, requested_split_adjusted_columns, ): - """ - Collect split adjustments that occur before the + """Collect split adjustments that occur before the split-adjusted-asof-date. All those adjustments must first be UN-applied at the first date index and then re-applied on the appropriate dates in order to match point in time share pricing data. @@ -1145,8 +1122,7 @@ def collect_post_asof_split_adjustments( sid_estimates, requested_split_adjusted_columns, ): - """ - Collect split adjustments that occur after the + """Collect split adjustments that occur after the split-adjusted-asof-date. Each adjustment needs to be applied to all dates on which knowledge for the requested quarter was older than the date of the adjustment. @@ -1238,6 +1214,7 @@ def retrieve_split_adjustment_data_for_sid( self, dates, sid, split_adjusted_asof_idx ): """ + dates : pd.DatetimeIndex The calendar dates. sid : int @@ -1310,8 +1287,7 @@ def _collect_adjustments( def merge_split_adjustments_with_overwrites( self, pre, post, overwrites, requested_split_adjusted_columns ): - """ - Merge split adjustments with the dict containing overwrites. + """Merge split adjustments with the dict containing overwrites. Parameters ---------- @@ -1358,8 +1334,7 @@ def collect_split_adjustments( post_adjustments, requested_split_adjusted_columns, ): - """ - Collect split adjustments for previous quarters and apply them to the + """Collect split adjustments for previous quarters and apply them to the given dictionary of splits for the given sid. Since overwrites just replace all estimates before the new quarter with NaN, we don't need to worry about re-applying split adjustments. @@ -1424,8 +1399,7 @@ def collect_split_adjustments( post_adjustments, requested_split_adjusted_columns, ): - """ - Collect split adjustments for future quarters. Re-apply adjustments + """Collect split adjustments for future quarters. Re-apply adjustments that would be overwritten by overwrites. Merge split adjustments with overwrites into the given dictionary of splits for the given sid. diff --git a/src/zipline/pipeline/loaders/equity_pricing_loader.py b/src/zipline/pipeline/loaders/equity_pricing_loader.py index 1bb9be3f63..669961da60 100644 --- a/src/zipline/pipeline/loaders/equity_pricing_loader.py +++ b/src/zipline/pipeline/loaders/equity_pricing_loader.py @@ -77,7 +77,7 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): # be known at the **start** of each date. We assume that the latest # data known on day N is the data from day (N - 1), so we shift all # query dates back by a trading session. - sessions = domain.all_sessions() + sessions = domain.sessions() shifted_dates = shift_dates(sessions, dates[0], dates[-1], shift=1) ohlcv_cols, currency_cols = self._split_column_types(columns) diff --git a/src/zipline/pipeline/loaders/frame.py b/src/zipline/pipeline/loaders/frame.py index 2518d3cabb..1086e476cb 100644 --- a/src/zipline/pipeline/loaders/frame.py +++ b/src/zipline/pipeline/loaders/frame.py @@ -4,10 +4,7 @@ from functools import partial from interface import implements -from numpy import ( - ix_, - zeros, -) +import numpy as np import pandas as pd from zipline.lib.adjusted_array import AdjustedArray @@ -28,8 +25,7 @@ class DataFrameLoader(implements(PipelineLoader)): - """ - A PipelineLoader that reads its input from DataFrames. + """A PipelineLoader that reads its input from DataFrames. Mostly useful for testing, but can also be used for real work if your data fits in memory. @@ -79,8 +75,7 @@ def __init__(self, column, baseline, adjustments=None): self.adjustment_sids = pd.Index(adjustments.sid, dtype="int64") def format_adjustments(self, dates, assets): - """ - Build a dict of Adjustment objects in the format expected by + """Build a dict of Adjustment objects in the format expected by AdjustedArray. Returns a dict of the form: @@ -108,7 +103,7 @@ def format_adjustments(self, dates, assets): min_date, max_date, ) - dates_filter = zeros(len(self.adjustments), dtype="bool") + dates_filter = np.zeros(len(self.adjustments), dtype="bool") dates_filter[date_bounds] = True # Ignore adjustments whose apply_date is in range, but whose end_date # is out of range. @@ -145,9 +140,8 @@ def format_adjustments(self, dates, assets): return out def load_adjusted_array(self, domain, columns, dates, sids, mask): - """ - Load data from our stored baseline. - """ + """Load data from our stored baseline.""" + if len(columns) != 1: raise ValueError("Can't load multiple columns with DataFrameLoader") @@ -161,7 +155,7 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): good_dates = date_indexer != -1 good_assets = assets_indexer != -1 - data = self.baseline[ix_(date_indexer, assets_indexer)] + data = self.baseline[np.ix_(date_indexer, assets_indexer)] mask = (good_assets & as_column(good_dates)) & mask # Mask out requested columns/rows that didn't match. @@ -178,5 +172,6 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): def _validate_input_column(self, column): """Make sure a passed column is our column.""" + if column != self.column and column.unspecialize() != self.column: - raise ValueError("Can't load unknown column %s" % column) + raise ValueError(f"Can't load unknown column {column}") diff --git a/src/zipline/pipeline/loaders/synthetic.py b/src/zipline/pipeline/loaders/synthetic.py index 02a775cc9e..46386b61fd 100644 --- a/src/zipline/pipeline/loaders/synthetic.py +++ b/src/zipline/pipeline/loaders/synthetic.py @@ -1,17 +1,8 @@ -""" -Synthetic data loaders for testing. -""" +"""Synthetic data loaders for testing.""" + from interface import implements -from numpy import ( - arange, - array, - eye, - float64, - full, - iinfo, - nan, - uint32, -) +import numpy as np + from numpy.random import RandomState from pandas import DataFrame, Timestamp from sqlite3 import connect as sqlite3_connect @@ -33,7 +24,7 @@ ) -UINT_32_MAX = iinfo(uint32).max +UINT_32_MAX = np.iinfo(np.uint32).max def nanos_to_seconds(nanos): @@ -41,8 +32,7 @@ def nanos_to_seconds(nanos): class PrecomputedLoader(implements(PipelineLoader)): - """ - Synthetic PipelineLoader that uses a pre-computed array for each column. + """Synthetic PipelineLoader that uses a pre-computed array for each column. Parameters ---------- @@ -80,9 +70,7 @@ def __init__(self, constants, dates, sids): self._loaders = loaders def load_adjusted_array(self, domain, columns, dates, sids, mask): - """ - Load by delegating to sub-loaders. - """ + """Load by delegating to sub-loaders.""" out = {} for col in columns: try: @@ -96,8 +84,7 @@ def load_adjusted_array(self, domain, columns, dates, sids, mask): class EyeLoader(PrecomputedLoader): - """ - A PrecomputedLoader that emits arrays containing 1s on the diagonal and 0s + """A PrecomputedLoader that emits arrays containing 1s on the diagonal and 0s elsewhere. Parameters @@ -113,15 +100,14 @@ class EyeLoader(PrecomputedLoader): def __init__(self, columns, dates, sids): shape = (len(dates), len(sids)) super(EyeLoader, self).__init__( - {column: eye(shape, dtype=column.dtype) for column in columns}, + {column: np.eye(shape, dtype=column.dtype) for column in columns}, dates, sids, ) class SeededRandomLoader(PrecomputedLoader): - """ - A PrecomputedLoader that emits arrays randomly-generated with a given seed. + """A PrecomputedLoader that emits arrays randomly-generated with a given seed. Parameters ---------- @@ -144,9 +130,7 @@ def __init__(self, seed, columns, dates, sids): ) def values(self, dtype, dates, sids): - """ - Make a random array of shape (len(dates), len(sids)) with ``dtype``. - """ + """Make a random array of shape (len(dates), len(sids)) with ``dtype``.""" shape = (len(dates), len(sids)) return { datetime64ns_dtype: self._datetime_values, @@ -158,8 +142,7 @@ def values(self, dtype, dates, sids): @property def state(self): - """ - Make a new RandomState from our seed. + """Make a new RandomState from our seed. This ensures that every call to _*_values produces the same output every time for a given SeededRandomLoader instance. @@ -167,9 +150,7 @@ def state(self): return RandomState(self._seed) def _float_values(self, shape): - """ - Return uniformly-distributed floats between -0.0 and 100.0. - """ + """Return uniformly-distributed floats between -0.0 and 100.0.""" return self.state.uniform(low=0.0, high=100.0, size=shape) def _int_values(self, shape): @@ -181,9 +162,7 @@ def _int_values(self, shape): ) # default is system int def _datetime_values(self, shape): - """ - Return uniformly-distributed dates in 2014. - """ + """Return uniformly-distributed dates in 2014.""" start = Timestamp("2014", tz="UTC").asm8 offsets = self.state.randint( low=0, @@ -193,9 +172,7 @@ def _datetime_values(self, shape): return start + offsets def _bool_values(self, shape): - """ - Return uniformly-distributed True/False values. - """ + """Return uniformly-distributed True/False values.""" return self.state.randn(*shape) < 0 def _object_values(self, shape): @@ -205,29 +182,31 @@ def _object_values(self, shape): OHLCV = ("open", "high", "low", "close", "volume") OHLC = ("open", "high", "low", "close") -PSEUDO_EPOCH = Timestamp("2000-01-01", tz="UTC") + +PSEUDO_EPOCH_UTC = Timestamp("2000-01-01", tz="UTC") +PSEUDO_EPOCH_NAIVE = Timestamp("2000-01-01") + +# TODO FIX TZ MESS def asset_start(asset_info, asset): ret = asset_info.loc[asset]["start_date"] - if ret.tz is None: - ret = ret.tz_localize("UTC") - assert ret.tzname() == "UTC", "Unexpected non-UTC timestamp" + # if ret.tz is None: + # ret = ret.tz_localize("UTC") + # assert ret.tzname() == "UTC", "Unexpected non-UTC timestamp" return ret def asset_end(asset_info, asset): ret = asset_info.loc[asset]["end_date"] - if ret.tz is None: - ret = ret.tz_localize("UTC") - assert ret.tzname() == "UTC", "Unexpected non-UTC timestamp" + # if ret.tz is None: + # ret = ret.tz_localize("UTC") + # assert ret.tzname() == "UTC", "Unexpected non-UTC timestamp" return ret def make_bar_data(asset_info, calendar, holes=None): - """ - - For a given asset/date/column combination, we generate a corresponding raw + """For a given asset/date/column combination, we generate a corresponding raw value using the following formula for OHLCV columns: data(asset, date, column) = (100,000 * asset_id) @@ -262,7 +241,7 @@ def make_bar_data(asset_info, calendar, holes=None): """ assert ( # Using .value here to avoid having to care about UTC-aware dates. - PSEUDO_EPOCH.value + PSEUDO_EPOCH_UTC.value < calendar.normalize().min().value <= asset_info["start_date"].min().value ), "calendar.min(): %s\nasset_info['start_date'].min(): %s" % ( @@ -273,8 +252,7 @@ def make_bar_data(asset_info, calendar, holes=None): assert (asset_info["start_date"] < asset_info["end_date"]).all() def _raw_data_for_asset(asset_id): - """ - Generate 'raw' data that encodes information about the asset. + """Generate 'raw' data that encodes information about the asset. See docstring for a description of the data format. """ @@ -287,17 +265,26 @@ def _raw_data_for_asset(asset_id): ) ] - data = full( + data = np.full( (len(datetimes), len(US_EQUITY_PRICING_BCOLZ_COLUMNS)), asset_id * 100 * 1000, - dtype=uint32, + dtype=np.uint32, ) # Add 10,000 * column-index to OHLCV columns - data[:, :5] += arange(5, dtype=uint32) * 1000 + data[:, :5] += np.arange(5, dtype=np.uint32) * 1000 # Add days since Jan 1 2001 for OHLCV columns. - data[:, :5] += array((datetimes - PSEUDO_EPOCH).days)[:, None].astype(uint32) + # TODO FIXME TZ MESS + + if datetimes.tzinfo is None: + data[:, :5] += np.array( + (datetimes.tz_localize("UTC") - PSEUDO_EPOCH_UTC).days + )[:, None].astype(np.uint32) + else: + data[:, :5] += np.array((datetimes - PSEUDO_EPOCH_UTC).days)[ + :, None + ].astype(np.uint32) frame = DataFrame( data, @@ -307,7 +294,7 @@ def _raw_data_for_asset(asset_id): if holes is not None and asset_id in holes: for dt in holes[asset_id]: - frame.loc[dt, OHLC] = nan + frame.loc[dt, OHLC] = np.nan frame.loc[dt, ["volume"]] = 0 frame["day"] = nanos_to_seconds(datetimes.asi8) @@ -319,15 +306,14 @@ def _raw_data_for_asset(asset_id): def expected_bar_value(asset_id, date, colname): - """ - Check that the raw value for an asset/date/column triple is as + """Check that the raw value for an asset/date/column triple is as expected. Used by tests to verify data written by a writer. """ from_asset = asset_id * 100000 from_colname = OHLCV.index(colname) * 1000 - from_date = (date - PSEUDO_EPOCH).days + from_date = (date - PSEUDO_EPOCH_NAIVE.tz_localize(date.tzinfo)).days return from_asset + from_colname + from_date @@ -340,8 +326,7 @@ def expected_bar_value_with_holes(asset_id, date, colname, holes, missing_value) def expected_bar_values_2d(dates, assets, asset_info, colname, holes=None): - """ - Return an 2D array containing cls.expected_value(asset_id, date, + """Return an 2D array containing cls.expected_value(asset_id, date, colname) for each date/asset pair in the inputs. Missing locs are filled with 0 for volume and NaN for price columns: @@ -351,13 +336,13 @@ def expected_bar_values_2d(dates, assets, asset_info, colname, holes=None): - Locs defined in `holes`. """ if colname == "volume": - dtype = uint32 + dtype = np.uint32 missing = 0 else: - dtype = float64 + dtype = np.float64 missing = float("nan") - data = full((len(dates), len(assets)), missing, dtype=dtype) + data = np.full((len(dates), len(assets)), missing, dtype=dtype) for j, asset in enumerate(assets): # Use missing values when asset_id is not contained in asset_info. if asset not in asset_info.index: @@ -368,7 +353,10 @@ def expected_bar_values_2d(dates, assets, asset_info, colname, holes=None): for i, date in enumerate(dates): # No value expected for dates outside the asset's start/end # date. - if not (start <= date <= end): + # TODO FIXME TZ MESS + if not ( + start.tz_localize(date.tzinfo) <= date <= end.tz_localize(date.tzinfo) + ): continue if holes is not None: @@ -387,8 +375,7 @@ def expected_bar_values_2d(dates, assets, asset_info, colname, holes=None): class NullAdjustmentReader(SQLiteAdjustmentReader): - """ - A SQLiteAdjustmentReader that stores no adjustments and uses in-memory + """A SQLiteAdjustmentReader that stores no adjustments and uses in-memory SQLite. """ @@ -397,19 +384,19 @@ def __init__(self): writer = SQLiteAdjustmentWriter(conn, None, None) empty = DataFrame( { - "sid": array([], dtype=uint32), - "effective_date": array([], dtype=uint32), - "ratio": array([], dtype=float), + "sid": np.array([], dtype=np.uint32), + "effective_date": np.array([], dtype=np.uint32), + "ratio": np.array([], dtype=float), } ) empty_dividends = DataFrame( { - "sid": array([], dtype=uint32), - "amount": array([], dtype=float64), - "record_date": array([], dtype="datetime64[ns]"), - "ex_date": array([], dtype="datetime64[ns]"), - "declared_date": array([], dtype="datetime64[ns]"), - "pay_date": array([], dtype="datetime64[ns]"), + "sid": np.array([], dtype=np.uint32), + "amount": np.array([], dtype=np.float64), + "record_date": np.array([], dtype="datetime64[ns]"), + "ex_date": np.array([], dtype="datetime64[ns]"), + "declared_date": np.array([], dtype="datetime64[ns]"), + "pay_date": np.array([], dtype="datetime64[ns]"), } ) writer.write(splits=empty, mergers=empty, dividends=empty_dividends) diff --git a/src/zipline/pipeline/loaders/utils.py b/src/zipline/pipeline/loaders/utils.py index 1e44b6ea21..d9776c62b9 100644 --- a/src/zipline/pipeline/loaders/utils.py +++ b/src/zipline/pipeline/loaders/utils.py @@ -60,11 +60,7 @@ def next_event_indexer( sid_ixs = all_sids.searchsorted(event_sids) # side='right' here ensures that we include the event date itself # if it's in all_dates. - dt_ixs = all_dates.searchsorted( - # pd.to_datetime(event_dates, utc=True), side="right") - make_utc_aware(pd.DatetimeIndex(event_dates)), - side="right", - ) + dt_ixs = all_dates.searchsorted(pd.DatetimeIndex(event_dates), side="right") ts_ixs = data_query_cutoff.searchsorted( # pd.to_datetime(event_timestamps, utc=True), side="right" make_utc_aware(pd.DatetimeIndex(event_timestamps)), @@ -278,8 +274,7 @@ def ffill_across_cols(df, columns, name_map): def shift_dates(dates, start_date, end_date, shift): - """ - Shift dates of a pipeline query back by ``shift`` days. + """Shift dates of a pipeline query back by ``shift`` days. Parameters ---------- diff --git a/src/zipline/sources/benchmark_source.py b/src/zipline/sources/benchmark_source.py index 5c0f512987..d73c1d6dfd 100644 --- a/src/zipline/sources/benchmark_source.py +++ b/src/zipline/sources/benchmark_source.py @@ -55,12 +55,11 @@ def __init__( if self.emission_rate == "minute": # we need to take the env's benchmark returns, which are daily, # and resample them to minute - minutes = trading_calendar.minutes_for_sessions_in_range( - sessions[0], sessions[-1] + minutes = trading_calendar.sessions_minutes(sessions[0], sessions[-1]) + minute_series = daily_series.tz_localize(minutes.tzinfo).reindex( + index=minutes, method="ffill" ) - minute_series = daily_series.reindex(index=minutes, method="ffill") - self._precalculated_series = minute_series else: self._precalculated_series = daily_series @@ -177,13 +176,10 @@ def _compute_daily_returns(g): @classmethod def downsample_minute_return_series(cls, trading_calendar, minutely_returns): - sessions = trading_calendar.minute_index_to_session_labels( + sessions = trading_calendar.minutes_to_sessions( minutely_returns.index, ) - closes = trading_calendar.session_closes_in_range( - sessions[0], - sessions[-1], - ) + closes = trading_calendar.closes[sessions[0] : sessions[-1]] daily_returns = minutely_returns[closes].pct_change() daily_returns.index = closes.index return daily_returns.iloc[1:] @@ -227,7 +223,7 @@ def _initialize_precalculated_series( the partial daily returns for each minute """ if self.emission_rate == "minute": - minutes = trading_calendar.minutes_for_sessions_in_range( + minutes = trading_calendar.sessions_minutes( self.sessions[0], self.sessions[-1] ) benchmark_series = data_portal.get_history_window( diff --git a/src/zipline/sources/test_source.py b/src/zipline/sources/test_source.py index d4dd25162a..504155ec20 100644 --- a/src/zipline/sources/test_source.py +++ b/src/zipline/sources/test_source.py @@ -13,9 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" -A source to be used in testing. -""" +"""A source to be used in testing.""" from datetime import timedelta import itertools @@ -40,9 +38,8 @@ def create_trade(sid, price, amount, datetime, source_id="test_factory"): def date_gen(start, end, trading_calendar, delta=timedelta(minutes=1), repeats=None): - """ - Utility to generate a stream of dates. - """ + """Utility to generate a stream of dates.""" + daily_delta = not (delta.total_seconds() % timedelta(days=1).total_seconds()) cur = start if daily_delta: @@ -51,23 +48,22 @@ def date_gen(start, end, trading_calendar, delta=timedelta(minutes=1), repeats=N cur = cur.replace(hour=0, minute=0, second=0, microsecond=0) def advance_current(cur): - """ - Advances the current dt skipping non market days and minutes. - """ + """Advances the current dt skipping non market days and minutes.""" + cur = cur + delta - currently_executing = ( - daily_delta and (cur in trading_calendar.all_sessions) - ) or (trading_calendar.is_open_on_minute(cur)) + currently_executing = (daily_delta and (cur in trading_calendar.sessions)) or ( + trading_calendar.is_open_on_minute(cur) + ) if currently_executing: return cur else: if daily_delta: - return trading_calendar.minute_to_session_label(cur) + return trading_calendar.minute_to_session(cur).tz_localize(cur.tzinfo) else: - return trading_calendar.open_and_close_for_session( - trading_calendar.minute_to_session_label(cur) + return trading_calendar.session_open_close( + trading_calendar.minute_to_session(cur) )[0] # yield count trade events, all on trading days, and @@ -83,8 +79,7 @@ def advance_current(cur): class SpecificEquityTrades: - """ - Yields all events in event_list that match the given sid_filter. + """Yields all events in event_list that match the given sid_filter. If no event_list is specified, generates an internal stream of events to filter. Returns all events if filter is None. diff --git a/src/zipline/testing/core.py b/src/zipline/testing/core.py index 0c24f09928..f02a67f89d 100644 --- a/src/zipline/testing/core.py +++ b/src/zipline/testing/core.py @@ -13,7 +13,7 @@ import numpy as np import pandas as pd -from mock import patch +import mock from numpy.testing import assert_allclose, assert_array_equal from sqlalchemy import create_engine from testfixtures import TempDirectory @@ -48,7 +48,7 @@ def seconds_to_timestamp(seconds): - return pd.Timestamp(seconds, unit="s", tz="UTC") + return pd.Timestamp(seconds, unit="s") def to_utc(time_str): @@ -57,8 +57,7 @@ def to_utc(time_str): def str_to_seconds(s): - """ - Convert a pandas-intelligible string to (integer) seconds since UTC. + """Convert a pandas-intelligible string to (integer) seconds since UTC. >>> from pandas import Timestamp >>> (Timestamp('2014-01-01') - Timestamp(0)).total_seconds() @@ -159,9 +158,9 @@ def security_list_copy(): shutil.copytree( os.path.join(old_dir, subdir), os.path.join(new_dir, subdir) ) - with patch.object( + with mock.patch.object( security_list, "SECURITY_LISTS_DIR", new_dir - ), patch.object(security_list, "using_copy", True, create=True): + ), mock.patch.object(security_list, "using_copy", True, create=True): yield finally: shutil.rmtree(new_dir, True) @@ -190,8 +189,7 @@ def add_security_data(adds, deletes): def all_pairs_matching_predicate(values, pred): - """ - Return an iterator of all pairs, (v0, v1) from values such that + """Return an iterator of all pairs, (v0, v1) from values such that `pred(v0, v1) == True` @@ -218,8 +216,7 @@ def all_pairs_matching_predicate(values, pred): def product_upper_triangle(values, include_diagonal=False): - """ - Return an iterator over pairs, (v0, v1), drawn from values. + """Return an iterator over pairs, (v0, v1), drawn from values. If `include_diagonal` is True, returns all pairs such that v0 <= v1. If `include_diagonal` is False, returns all pairs such that v0 < v1. @@ -231,9 +228,7 @@ def product_upper_triangle(values, include_diagonal=False): def all_subindices(index): - """ - Return all valid sub-indices of a pandas Index. - """ + """Return all valid sub-indices of a pandas Index.""" return ( index[start:stop] for start, stop in product_upper_triangle(range(len(index) + 1)) @@ -250,8 +245,7 @@ def make_trade_data_for_asset_info( volume_step_by_date, volume_step_by_sid, ): - """ - Convert the asset info dataframe into a dataframe of trade data for each + """Convert the asset info dataframe into a dataframe of trade data for each sid, and write to the writer if provided. Write NaNs for locations where assets did not exist. Return a dict of the dataframes, keyed by sid. """ @@ -270,7 +264,8 @@ def make_trade_data_for_asset_info( start_date, end_date = asset_info.loc[sid, ["start_date", "end_date"]] # Normalize here so the we still generate non-NaN values on the minutes # for an asset's last trading day. - for i, date in enumerate(dates.normalize()): + # TODO FIXME TZ MESS + for i, date in enumerate(dates.normalize().tz_localize(None)): if not (start_date <= date <= end_date): prices[i, j] = 0 volumes[i, j] = 0 @@ -291,8 +286,7 @@ def make_trade_data_for_asset_info( def check_allclose(actual, desired, rtol=1e-07, atol=0, err_msg="", verbose=True): - """ - Wrapper around np.testing.assert_allclose that also verifies that inputs + """Wrapper around np.testing.assert_allclose that also verifies that inputs are ndarrays. See Also @@ -312,8 +306,7 @@ def check_allclose(actual, desired, rtol=1e-07, atol=0, err_msg="", verbose=True def check_arrays(x, y, err_msg="", verbose=True, check_dtypes=True): - """ - Wrapper around np.testing.assert_array_equal that also verifies that inputs + """Wrapper around np.testing.assert_array_equal that also verifies that inputs are ndarrays. See Also @@ -355,8 +348,7 @@ class UnexpectedAttributeAccess(Exception): class ExplodingObject: - """ - Object that will raise an exception on any attribute access. + """Object that will raise an exception on any attribute access. Useful for verifying that an object is never touched during a function/method call. @@ -367,12 +359,8 @@ def __getattribute__(self, name): def write_minute_data(trading_calendar, tempdir, minutes, sids): - first_session = trading_calendar.minute_to_session_label( - minutes[0], direction="none" - ) - last_session = trading_calendar.minute_to_session_label( - minutes[-1], direction="none" - ) + first_session = trading_calendar.minute_to_session(minutes[0], direction="none") + last_session = trading_calendar.minute_to_session(minutes[-1], direction="none") sessions = trading_calendar.sessions_in_range(first_session, last_session) @@ -479,7 +467,7 @@ def create_minute_df_for_asset( start_val=1, minute_blacklist=None, ): - asset_minutes = trading_calendar.minutes_for_sessions_in_range(start_dt, end_dt) + asset_minutes = trading_calendar.sessions_minutes(start_dt, end_dt) minutes_count = len(asset_minutes) if interval > 1: @@ -676,10 +664,8 @@ def get_history_window( data_frequency, ffill=True, ): - end_idx = self.trading_calendar.all_sessions.searchsorted(end_dt) - days = self.trading_calendar.all_sessions[ - (end_idx - bar_count + 1) : (end_idx + 1) - ] + end_idx = self.trading_calendar.sessions.searchsorted(end_dt) + days = self.trading_calendar.sessions[(end_idx - bar_count + 1) : (end_idx + 1)] df = pd.DataFrame( np.full((bar_count, len(assets)), 100.0), index=days, columns=assets @@ -687,7 +673,7 @@ def get_history_window( if frequency == "1m" and not df.empty: df = df.reindex( - self.trading_calendar.minutes_for_sessions_in_range( + self.trading_calendar.sessions_minutes( df.index[0], df.index[-1], ), @@ -698,8 +684,7 @@ def get_history_window( class FetcherDataPortal(DataPortal): - """ - Mock dataportal that returns fake data for history and non-fetcher + """Mock dataportal that returns fake data for history and non-fetcher spot value. """ @@ -847,8 +832,7 @@ def __str__(self): # @nottest def subtest(iterator, *_names): - """ - Construct a subtest in a unittest. + """Construct a subtest in a unittest. Consider using ``zipline.testing.parameter_space`` when subtests are constructed over a single input or over the cross-product of multiple @@ -966,8 +950,7 @@ def create_mock_adjustment_data(splits=None, dividends=None, mergers=None): def assert_timestamp_equal(left, right, compare_nat_equal=True, msg=""): - """ - Assert that two pandas Timestamp objects are the same. + """Assert that two pandas Timestamp objects are the same. Parameters ---------- @@ -984,15 +967,12 @@ def assert_timestamp_equal(left, right, compare_nat_equal=True, msg=""): def powerset(values): - """ - Return the power set (i.e., the set of all subsets) of entries in `values`. - """ + """Return the power set (i.e., the set of all subsets) of entries in `values`.""" return concat(combinations(values, i) for i in range(len(values) + 1)) def to_series(knowledge_dates, earning_dates): - """ - Helper for converting a dict of strings to a Series of datetimes. + """Helper for converting a dict of strings to a Series of datetimes. This is just for making the test cases more readable. """ @@ -1003,10 +983,8 @@ def to_series(knowledge_dates, earning_dates): def gen_calendars(start, stop, critical_dates): - """ - Generate calendars to use as inputs. - """ - all_dates = pd.date_range(start, stop, tz="utc") + """Generate calendars to use as inputs.""" + all_dates = pd.date_range(start, stop) for to_drop in map(list, powerset(critical_dates)): # Have to yield tuples. yield (all_dates.drop(to_drop),) @@ -1018,8 +996,7 @@ def gen_calendars(start, stop, critical_dates): @contextmanager def temp_pipeline_engine(calendar, sids, random_seed, symbols=None): - """ - A contextManager that yields a SimplePipelineEngine holding a reference to + """A contextManager that yields a SimplePipelineEngine holding a reference to an AssetFinder generated via tmp_asset_finder. Parameters @@ -1050,8 +1027,7 @@ def get_loader(column): def bool_from_envvar(name, default=False, env=None): - """ - Get a boolean value from the environment, making a reasonable attempt to + """Get a boolean value from the environment, making a reasonable attempt to convert "truthy" values to True and "falsey" values to False. Strings are coerced to bools using ``json.loads(s.lower())``. @@ -1095,8 +1071,7 @@ def bool_from_envvar(name, default=False, env=None): def parameter_space(__fail_fast=_FAIL_FAST_DEFAULT, **params): - """ - Wrapper around subtest that allows passing keywords mapping names to + """Wrapper around subtest that allows passing keywords mapping names to iterables of values. The decorated test function will be called with the cross-product of all @@ -1206,8 +1181,7 @@ def create_empty_splits_mergers_frame(): def make_alternating_boolean_array(shape, first_value=True): - """ - Create a 2D numpy array with the given shape containing alternating values + """Create a 2D numpy array with the given shape containing alternating values of False, True, False, True,... along each row and each column. Examples @@ -1236,8 +1210,7 @@ def make_alternating_boolean_array(shape, first_value=True): def make_cascading_boolean_array(shape, first_value=True): - """ - Create a numpy array with the given shape containing cascading boolean + """Create a numpy array with the given shape containing cascading boolean values, with `first_value` being the top-left value. Examples @@ -1273,8 +1246,7 @@ def make_cascading_boolean_array(shape, first_value=True): @expect_dimensions(array=2) def permute_rows(seed, array): - """ - Shuffle each row in ``array`` based on permutations generated by ``seed``. + """Shuffle each row in ``array`` based on permutations generated by ``seed``. Parameters ---------- @@ -1288,17 +1260,13 @@ def permute_rows(seed, array): def write_compressed(path, content): - """ - Write a compressed (gzipped) file to `path`. - """ + """Write a compressed (gzipped) file to `path`.""" with gzip.open(path, "wb") as f: f.write(content) def read_compressed(path): - """ - Write a compressed (gzipped) file from `path`. - """ + """Write a compressed (gzipped) file from `path`.""" with gzip.open(path, "rb") as f: return f.read() @@ -1315,9 +1283,7 @@ def test_resource_path(*path_parts): @contextmanager def patch_os_environment(remove=None, **values): - """ - Context manager for patching the operating system environment. - """ + """Context manager for patching the operating system environment.""" old_values = {} remove = remove or [] for key in remove: @@ -1474,7 +1440,7 @@ def patched_read_csv(filepath_or_buffer, *args, **kwargs): % filepath_or_buffer, ) - with patch.object(module, "read_csv", patched_read_csv): + with mock.patch.object(module, "read_csv", patched_read_csv): yield @@ -1515,8 +1481,7 @@ def batch_order(self, *args, **kwargs): class AssetID(CustomFactor): - """ - CustomFactor that returns the AssetID of each asset. + """CustomFactor that returns the AssetID of each asset. Useful for providing a Factor that produces a different value for each asset. @@ -1576,8 +1541,7 @@ def prices_generating_returns(returns, starting_price): def random_tick_prices( starting_price, count, tick_size=0.01, tick_range=(-5, 7), seed=42 ): - """ - Construct a time series of prices that ticks by a random multiple of + """Construct a time series of prices that ticks by a random multiple of ``tick_size`` every period. Parameters @@ -1729,8 +1693,7 @@ def write_hdf5_daily_bars( def exchange_info_for_domains(domains): - """ - Build an exchange_info suitable for passing to an AssetFinder from a list + """Build an exchange_info suitable for passing to an AssetFinder from a list of EquityCalendarDomain. """ return pd.DataFrame.from_records( diff --git a/src/zipline/testing/fixtures.py b/src/zipline/testing/fixtures.py index 610ea1469f..d3fce9187d 100644 --- a/src/zipline/testing/fixtures.py +++ b/src/zipline/testing/fixtures.py @@ -99,8 +99,7 @@ def __new__(cls, name, bases, clsdict): class ZiplineTestCase(TestCase, metaclass=DebugMROMeta): - """ - Shared extensions to core unittest.TestCase. + """Shared extensions to core unittest.TestCase. Overrides the default unittest setUp/tearDown functions with versions that use ExitStack to correctly clean up resources, even in the face of @@ -119,7 +118,7 @@ class ZiplineTestCase(TestCase, metaclass=DebugMROMeta): @final @classmethod - def setUpClass(cls): + def setup_class(cls): # Hold a set of all the "static" attributes on the class. These are # things that are not populated after the class was created like # methods or other class level attributes. @@ -134,7 +133,7 @@ def setUpClass(cls): " without calling super()." ) except BaseException: # Clean up even on KeyboardInterrupt - cls.tearDownClass() + cls.teardown_class() raise @classmethod @@ -156,7 +155,7 @@ def init_class_fixtures(cls): @final @classmethod - def tearDownClass(cls): + def teardown_class(cls): # We need to get this before it's deleted by the loop. stack = cls._class_teardown_stack for name in set(vars(cls)) - cls._static_class_attributes: @@ -170,9 +169,8 @@ def tearDownClass(cls): @final @classmethod def enter_class_context(cls, context_manager): - """ - Enter a context manager to be exited during the tearDownClass - """ + """Enter a context manager to be exited during the tearDownClass""" + if cls._in_setup: raise ValueError( "Attempted to enter a class context in init_instance_fixtures." @@ -183,8 +181,7 @@ def enter_class_context(cls, context_manager): @final @classmethod def add_class_callback(cls, callback, *args, **kwargs): - """ - Register a callback to be executed during tearDownClass. + """Register a callback to be executed during tearDownClass. Parameters ---------- @@ -231,15 +228,12 @@ def tearDown(self): @final def enter_instance_context(self, context_manager): - """ - Enter a context manager that should be exited during tearDown. - """ + """Enter a context manager that should be exited during tearDown.""" return self._instance_teardown_stack.enter_context(context_manager) @final def add_instance_callback(self, callback): - """ - Register a callback to be executed during tearDown. + """Register a callback to be executed during tearDown. Parameters ---------- @@ -287,8 +281,7 @@ def alias(attr_name): class WithDefaultDateBounds(object, metaclass=DebugMROMeta): - """ - ZiplineTestCase mixin which makes it possible to synchronize date bounds + """ZiplineTestCase mixin which makes it possible to synchronize date bounds across fixtures. This fixture should always be the last fixture in bases of any fixture or @@ -302,8 +295,8 @@ class WithDefaultDateBounds(object, metaclass=DebugMROMeta): dates. """ - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-12-29", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-12-29") class WithAssetFinder(WithDefaultDateBounds): @@ -582,8 +575,7 @@ def BENCHMARK_RETURNS(cls): class WithSimParams(WithDefaultDateBounds): - """ - ZiplineTestCase mixin providing cls.sim_params as a class level fixture. + """ZiplineTestCase mixin providing cls.sim_params as a class level fixture. Attributes ---------- @@ -636,8 +628,7 @@ def init_class_fixtures(cls): class WithTradingSessions(WithDefaultDateBounds, WithTradingCalendars): - """ - ZiplineTestCase mixin providing cls.trading_days, cls.all_trading_sessions + """ZiplineTestCase mixin providing cls.trading_days, cls.all_trading_sessions as a class-level fixture. After init_class_fixtures has been called, `cls.all_trading_sessions` @@ -676,17 +667,22 @@ def init_class_fixtures(cls): for cal_str in cls.TRADING_CALENDAR_STRS: trading_calendar = cls.trading_calendars[cal_str] - sessions = trading_calendar.sessions_in_range( - make_utc_aware(cls.DATA_MIN_DAY), make_utc_aware(cls.DATA_MAX_DAY) - ) + DATA_MIN_DAY = cls.DATA_MIN_DAY + DATA_MAX_DAY = cls.DATA_MAX_DAY + + if DATA_MIN_DAY.tzinfo is not None: + DATA_MIN_DAY = DATA_MIN_DAY.tz_localize(None) + if DATA_MAX_DAY.tzinfo is not None: + DATA_MAX_DAY = DATA_MAX_DAY.tz_localize(None) + + sessions = trading_calendar.sessions_in_range(DATA_MIN_DAY, DATA_MAX_DAY) # Set name for aliasing. setattr(cls, "{0}_sessions".format(cal_str.lower()), sessions) cls.trading_sessions[cal_str] = sessions class WithTmpDir: - """ - ZiplineTestCase mixing providing cls.tmpdir as a class-level fixture. + """ZiplineTestCase mixing providing cls.tmpdir as a class-level fixture. After init_class_fixtures has been called, `cls.tmpdir` is populated with a `testfixtures.TempDirectory` object whose path is `cls.TMP_DIR_PATH`. @@ -709,8 +705,7 @@ def init_class_fixtures(cls): class WithInstanceTmpDir: - """ - ZiplineTestCase mixing providing self.tmpdir as an instance-level fixture. + """ZiplineTestCase mixing providing self.tmpdir as an instance-level fixture. After init_instance_fixtures has been called, `self.tmpdir` is populated with a `testfixtures.TempDirectory` object whose path is @@ -733,8 +728,7 @@ def init_instance_fixtures(self): class WithEquityDailyBarData(WithAssetFinder, WithTradingCalendars): - """ - ZiplineTestCase mixin providing cls.make_equity_daily_bar_data. + """ZiplineTestCase mixin providing cls.make_equity_daily_bar_data. Attributes ---------- @@ -846,29 +840,38 @@ def init_class_fixtures(cls): super(WithEquityDailyBarData, cls).init_class_fixtures() trading_calendar = cls.trading_calendars[Equity] - if trading_calendar.is_session(cls.EQUITY_DAILY_BAR_START_DATE): + if trading_calendar.is_session( + cls.EQUITY_DAILY_BAR_START_DATE.normalize().tz_localize(None) + ): first_session = cls.EQUITY_DAILY_BAR_START_DATE else: - first_session = trading_calendar.minute_to_session_label( + first_session = trading_calendar.minute_to_session( pd.Timestamp(cls.EQUITY_DAILY_BAR_START_DATE) ) if cls.EQUITY_DAILY_BAR_LOOKBACK_DAYS > 0: first_session = trading_calendar.sessions_window( - first_session, -1 * cls.EQUITY_DAILY_BAR_LOOKBACK_DAYS + first_session, -1 * (cls.EQUITY_DAILY_BAR_LOOKBACK_DAYS + 1) )[0] + # TODO FIXME TZ MESS + if first_session.tzinfo is not None: + first_session = first_session.tz_localize(None) + + EQUITY_DAILY_BAR_END_DATE = cls.EQUITY_DAILY_BAR_END_DATE + if EQUITY_DAILY_BAR_END_DATE.tzinfo is not None: + EQUITY_DAILY_BAR_END_DATE = cls.EQUITY_DAILY_BAR_END_DATE.tz_localize(None) + days = trading_calendar.sessions_in_range( - first_session, - cls.EQUITY_DAILY_BAR_END_DATE, + first_session.normalize(), + EQUITY_DAILY_BAR_END_DATE.normalize(), ) cls.equity_daily_bar_days = days class WithFutureDailyBarData(WithAssetFinder, WithTradingCalendars): - """ - ZiplineTestCase mixin providing cls.make_future_daily_bar_data. + """ZiplineTestCase mixin providing cls.make_future_daily_bar_data. Attributes ---------- @@ -941,18 +944,18 @@ def init_class_fixtures(cls): super(WithFutureDailyBarData, cls).init_class_fixtures() trading_calendar = cls.trading_calendars[Future] if cls.FUTURE_DAILY_BAR_USE_FULL_CALENDAR: - days = trading_calendar.all_sessions + days = trading_calendar.sessions else: if trading_calendar.is_session(cls.FUTURE_DAILY_BAR_START_DATE): first_session = cls.FUTURE_DAILY_BAR_START_DATE else: - first_session = trading_calendar.minute_to_session_label( + first_session = trading_calendar.minute_to_session( pd.Timestamp(cls.FUTURE_DAILY_BAR_START_DATE) ) if cls.FUTURE_DAILY_BAR_LOOKBACK_DAYS > 0: first_session = trading_calendar.sessions_window( - first_session, -1 * cls.FUTURE_DAILY_BAR_LOOKBACK_DAYS + first_session, -1 * (cls.FUTURE_DAILY_BAR_LOOKBACK_DAYS + 1) )[0] days = trading_calendar.sessions_in_range( @@ -964,8 +967,7 @@ def init_class_fixtures(cls): class WithBcolzEquityDailyBarReader(WithEquityDailyBarData, WithTmpDir): - """ - ZiplineTestCase mixin providing cls.bcolz_daily_bar_path, + """ZiplineTestCase mixin providing cls.bcolz_daily_bar_path, cls.bcolz_daily_bar_ctable, and cls.bcolz_equity_daily_bar_reader class level fixtures. @@ -1064,8 +1066,7 @@ def init_class_fixtures(cls): class WithBcolzFutureDailyBarReader(WithFutureDailyBarData, WithTmpDir): - """ - ZiplineTestCase mixin providing cls.bcolz_daily_bar_path, + """ZiplineTestCase mixin providing cls.bcolz_daily_bar_path, cls.bcolz_daily_bar_ctable, and cls.bcolz_future_daily_bar_reader class level fixtures. @@ -1163,19 +1164,22 @@ class WithBcolzEquityDailyBarReaderFromCSVs(WithBcolzEquityDailyBarReader): def _trading_days_for_minute_bars(calendar, start_date, end_date, lookback_days): - first_session = calendar.minute_to_session_label(start_date) + first_session = calendar.minute_to_session(start_date) if lookback_days > 0: - first_session = calendar.sessions_window(first_session, -1 * lookback_days)[0] + first_session = calendar.sessions_window( + first_session, -1 * (lookback_days + 1) + )[0] - return calendar.sessions_in_range(first_session, end_date) + return calendar.sessions_in_range( + first_session, end_date.normalize().tz_localize(None) + ) # TODO_SS: This currently doesn't define any relationship between country_code # and calendar, which would be useful downstream. class WithWriteHDF5DailyBars(WithEquityDailyBarData, WithTmpDir): - """ - Fixture class defining the capability of writing HDF5 daily bars to disk. + """Fixture class defining the capability of writing HDF5 daily bars to disk. Uses cls.make_equity_daily_bar_data (inherited from WithEquityDailyBarData) to determine the data to write. @@ -1226,8 +1230,7 @@ def write_hdf5_daily_bars(cls, path, country_codes): class WithHDF5EquityMultiCountryDailyBarReader(WithWriteHDF5DailyBars): - """ - Fixture providing cls.hdf5_daily_bar_path and + """Fixture providing cls.hdf5_daily_bar_path and cls.hdf5_equity_daily_bar_reader class level fixtures. After init_class_fixtures has been called: @@ -1289,8 +1292,7 @@ def init_class_fixtures(cls): class WithEquityMinuteBarData(WithAssetFinder, WithTradingCalendars): - """ - ZiplineTestCase mixin providing cls.equity_minute_bar_days. + """ZiplineTestCase mixin providing cls.equity_minute_bar_days. After init_class_fixtures has been called: - `cls.equity_minute_bar_days` has the range over which data has been @@ -1329,7 +1331,7 @@ class WithEquityMinuteBarData(WithAssetFinder, WithTradingCalendars): def make_equity_minute_bar_data(cls): trading_calendar = cls.trading_calendars[Equity] return create_minute_bar_data( - trading_calendar.minutes_for_sessions_in_range( + trading_calendar.sessions_minutes( cls.equity_minute_bar_days[0], cls.equity_minute_bar_days[-1], ), @@ -1342,15 +1344,14 @@ def init_class_fixtures(cls): trading_calendar = cls.trading_calendars[Equity] cls.equity_minute_bar_days = _trading_days_for_minute_bars( trading_calendar, - pd.Timestamp(cls.EQUITY_MINUTE_BAR_START_DATE), - pd.Timestamp(cls.EQUITY_MINUTE_BAR_END_DATE), + cls.EQUITY_MINUTE_BAR_START_DATE, + cls.EQUITY_MINUTE_BAR_END_DATE, cls.EQUITY_MINUTE_BAR_LOOKBACK_DAYS, ) class WithFutureMinuteBarData(WithAssetFinder, WithTradingCalendars): - """ - ZiplineTestCase mixin providing cls.future_minute_bar_days. + """ZiplineTestCase mixin providing cls.future_minute_bar_days. After init_class_fixtures has been called: - `cls.future_minute_bar_days` has the range over which data has been @@ -1390,7 +1391,7 @@ class which writes the minute bar data for use by a reader. def make_future_minute_bar_data(cls): trading_calendar = get_calendar("us_futures") return create_minute_bar_data( - trading_calendar.minutes_for_sessions_in_range( + trading_calendar.sessions_minutes( cls.future_minute_bar_days[0], cls.future_minute_bar_days[-1], ), @@ -1403,8 +1404,8 @@ def init_class_fixtures(cls): trading_calendar = get_calendar("us_futures") cls.future_minute_bar_days = _trading_days_for_minute_bars( trading_calendar, - pd.Timestamp(cls.FUTURE_MINUTE_BAR_START_DATE), - pd.Timestamp(cls.FUTURE_MINUTE_BAR_END_DATE), + cls.FUTURE_MINUTE_BAR_START_DATE, + cls.FUTURE_MINUTE_BAR_END_DATE, cls.FUTURE_MINUTE_BAR_LOOKBACK_DAYS, ) @@ -1544,7 +1545,7 @@ def make_equity_minute_bar_data(cls): trading_calendar = cls.trading_calendars[Equity] sids = cls.asset_finder.equities_sids - minutes = trading_calendar.minutes_for_sessions_in_range( + minutes = trading_calendar.sessions_minutes( cls.equity_minute_bar_days[0], cls.equity_minute_bar_days[-1], ) @@ -1574,7 +1575,7 @@ def make_future_minute_bar_data(cls): trading_calendar = cls.trading_calendars[Future] sids = cls.asset_finder.futures_sids - minutes = trading_calendar.minutes_for_sessions_in_range( + minutes = trading_calendar.sessions_minutes( cls.future_minute_bar_days[0], cls.future_minute_bar_days[-1], ) @@ -1835,8 +1836,7 @@ class WithDataPortal( WithBcolzEquityMinuteBarReader, WithBcolzFutureMinuteBarReader, ): - """ - ZiplineTestCase mixin providing self.data_portal as an instance level + """ZiplineTestCase mixin providing self.data_portal as an instance level fixture. After init_instance_fixtures has been called, `self.data_portal` will be @@ -1926,8 +1926,7 @@ def init_instance_fixtures(self): class WithResponses: - """ - ZiplineTestCase mixin that provides self.responses as an instance + """ZiplineTestCase mixin that provides self.responses as an instance fixture. After init_instance_fixtures has been called, `self.responses` will be @@ -1956,12 +1955,10 @@ def create_bardata(self, simulation_dt_func, restrictions=None): class WithMakeAlgo(WithBenchmarkReturns, WithSimParams, WithDataPortal): - """ - ZiplineTestCase mixin that provides a ``make_algo`` method. - """ + """ZiplineTestCase mixin that provides a ``make_algo`` method.""" - START_DATE = pd.Timestamp("2014-12-29", tz="UTC") - END_DATE = pd.Timestamp("2015-1-05", tz="UTC") + START_DATE = pd.Timestamp("2014-12-29") + END_DATE = pd.Timestamp("2015-1-05") SIM_PARAMS_DATA_FREQUENCY = "minute" DEFAULT_ALGORITHM_CLASS = TradingAlgorithm @@ -1976,8 +1973,7 @@ def BENCHMARK_SID(cls): def merge_with_inherited_algo_kwargs( self, overriding_type, suite_overrides, method_overrides ): - """ - Helper for subclasses overriding ``make_algo_kwargs``. + """Helper for subclasses overriding ``make_algo_kwargs``. A common pattern for tests using `WithMakeAlgoKwargs` is that a particular test suite has a set of default keywords it wants to use @@ -2030,9 +2026,7 @@ def make_algo(self, algo_class=None, **overrides): return algo_class(**self.make_algo_kwargs(**overrides)) def run_algorithm(self, **overrides): - """ - Create and run an TradingAlgorithm in memory. - """ + """Create and run an TradingAlgorithm in memory.""" return self.make_algo(**overrides).run() @@ -2080,8 +2074,8 @@ def init_class_fixtures(cls): cal = get_calendar(cls.FX_RATES_CALENDAR) cls.fx_rates_sessions = cal.sessions_in_range( - cls.FX_RATES_START_DATE, - cls.FX_RATES_END_DATE, + cls.FX_RATES_START_DATE.tz_localize(None), + cls.FX_RATES_END_DATE.tz_localize(None), ) cls.fx_rates = cls.make_fx_rates( @@ -2097,8 +2091,7 @@ def init_class_fixtures(cls): @classmethod def make_fx_rates_from_reference(cls, reference): - """ - Helper method for implementing make_fx_rates. + """Helper method for implementing make_fx_rates. Takes a (dates x currencies) DataFrame of "reference" values, which are assumed to be the "true" value of each currency in some unknown @@ -2214,8 +2207,7 @@ def get_expected_fx_rates_columnar(cls, rate, quote, bases, dts): def fast_get_loc_ffilled(dts, dt): - """ - Equivalent to dts.get_loc(dt, method='ffill'), but with reasonable + """Equivalent to dts.get_loc(dt, method='ffill'), but with reasonable microperformance. """ ix = dts.searchsorted(dt, side="right") - 1 diff --git a/src/zipline/testing/predicates.py b/src/zipline/testing/predicates.py index 79e606ecf5..24b3e98225 100644 --- a/src/zipline/testing/predicates.py +++ b/src/zipline/testing/predicates.py @@ -1,5 +1,6 @@ from collections import OrderedDict -from contextlib import contextmanager + +# from contextlib import contextmanager import datetime from functools import partial diff --git a/src/zipline/utils/cache.py b/src/zipline/utils/cache.py index 974bc50ae8..de89ad6aa8 100644 --- a/src/zipline/utils/cache.py +++ b/src/zipline/utils/cache.py @@ -1,6 +1,4 @@ -""" -Caching utilities for zipline -""" +"""Caching utilities for zipline""" from collections.abc import MutableMapping import errno from functools import partial @@ -26,8 +24,7 @@ class Expired(Exception): class CachedObject: - """ - A simple struct for maintaining a cached object with an expiration date. + """A simple struct for maintaining a cached object with an expiration date. Parameters ---------- @@ -87,8 +84,7 @@ def _unsafe_get_value(self): class ExpiringCache: - """ - A cache of multiple CachedObjects, which returns the wrapped the value + """A cache of multiple CachedObjects, which returns the wrapped the value or raises and deletes the CachedObject if the value has expired. Parameters diff --git a/src/zipline/utils/calendar_utils.py b/src/zipline/utils/calendar_utils.py index 9a845a6889..1da959d93f 100644 --- a/src/zipline/utils/calendar_utils.py +++ b/src/zipline/utils/calendar_utils.py @@ -1,86 +1,39 @@ -from pytz import UTC +import inspect +from functools import partial + import pandas as pd +from exchange_calendars import ExchangeCalendar as TradingCalendar +from exchange_calendars import clear_calendars +from exchange_calendars import get_calendar as ec_get_calendar # get_calendar, +from exchange_calendars import ( + get_calendar_names, + register_calendar, + register_calendar_alias, +) +from exchange_calendars.calendar_utils import global_calendar_dispatcher -PANDAS_VERSION = pd.__version__ +# from exchange_calendars.errors import InvalidCalendarName +from exchange_calendars.utils.pandas_utils import days_at_time # noqa: reexport -# NOTE: -# trading-calendars is no longer maintained and does not support pandas > 1.2.5. -# exchange-calendars is a fork that retained the same functionalities, -# but dropped support for zipline 1 minute delay in open and changed some default settings in calendars. -# -# We resort here to monkey patching the `_fabricate` function of the ExchangeCalendarDispatcher -# and importing `ExchangeCalendar as TradingCalendar` to get as close as possible to the -# behavior expected by zipline, while also maintaining the possibility to revert back -# to pandas==1.2.5 and trading-calendars in case something breaks heavily. -# -# In order to avoid problems, especially when using the exchange-calendars, -# all imports should be done via `calendar_utils`, e.g: -# `from zipline.utils.calendar_utils import get_calendar, register_calendar, ...` -# -# Some calendars like for instance the Korean exchange have been extensively updated and might no longer -# work as expected -try: - from exchange_calendars import ExchangeCalendar as TradingCalendar - from exchange_calendars.calendar_utils import ( - ExchangeCalendarDispatcher, - _default_calendar_factories, - _default_calendar_aliases, - ) - from exchange_calendars.errors import InvalidCalendarName - from exchange_calendars.utils.pandas_utils import days_at_time # noqa: reexport +# https://stackoverflow.com/questions/56753846/python-wrapping-function-with-signature +def wrap_with_signature(signature): + def wrapper(func): + func.__signature__ = signature + return func - def _fabricate(self, name: str, **kwargs): - """Fabricate calendar with `name` and `**kwargs`.""" - try: - factory = self._calendar_factories[name] - except KeyError as e: - raise InvalidCalendarName(calendar_name=name) from e - if name in ["us_futures", "CMES", "XNYS"]: - # exchange_calendars has a different default start data - # that we need to overwrite in order to pass the legacy tests - setattr(factory, "default_start", pd.Timestamp("1990-01-01", tz=UTC)) - # kwargs["start"] = pd.Timestamp("1990-01-01", tz="UTC") - if name not in ["us_futures", "24/7", "24/5", "CMES"]: - # Zipline had default open time of t+1min - factory.open_times = [ - (d, t.replace(minute=t.minute + 1)) for d, t in factory.open_times - ] - calendar = factory(**kwargs) - self._factory_output_cache[name] = (calendar, kwargs) - return calendar + return wrapper - # Yay! Monkey patching - ExchangeCalendarDispatcher._fabricate = _fabricate - global_calendar_dispatcher = ExchangeCalendarDispatcher( - calendars={}, - calendar_factories=_default_calendar_factories, - aliases=_default_calendar_aliases, - ) - get_calendar = global_calendar_dispatcher.get_calendar +@wrap_with_signature(inspect.signature(ec_get_calendar)) +def get_calendar(*args, **kwargs): + if args[0] in ["us_futures", "CMES", "XNYS", "NYSE"]: + return ec_get_calendar(*args, side="right", start=pd.Timestamp("1990-01-01")) + return ec_get_calendar(*args, side="right") - get_calendar_names = global_calendar_dispatcher.get_calendar_names - clear_calendars = global_calendar_dispatcher.clear_calendars - deregister_calendar = global_calendar_dispatcher.deregister_calendar - register_calendar = global_calendar_dispatcher.register_calendar - register_calendar_type = global_calendar_dispatcher.register_calendar_type - register_calendar_alias = global_calendar_dispatcher.register_calendar_alias - resolve_alias = global_calendar_dispatcher.resolve_alias - aliases_to_names = global_calendar_dispatcher.aliases_to_names - names_to_aliases = global_calendar_dispatcher.names_to_aliases -except ImportError as exc: - if PANDAS_VERSION > "1.2.5": - raise ImportError( - "For pandas >= 1.3 YOU MUST INSTALL exchange-calendars" - ) from exc - else: - from trading_calendars import ( - register_calendar, - TradingCalendar, - get_calendar, - register_calendar_alias, - ) - from trading_calendars.calendar_utils import global_calendar_dispatcher - from trading_calendars.utils.pandas_utils import days_at_time # noqa: reexport +# get_calendar = compose(partial(get_calendar, side="right"), "XNYS") +# NOTE Sessions are now timezone-naive (previously UTC). +# Schedule columns now have timezone set as UTC +# (whilst the times have always been defined in terms of UTC, +# previously the dtype was timezone-naive). diff --git a/src/zipline/utils/date_utils.py b/src/zipline/utils/date_utils.py index e1425cff2c..503cf1aef2 100644 --- a/src/zipline/utils/date_utils.py +++ b/src/zipline/utils/date_utils.py @@ -43,9 +43,7 @@ def compute_date_range_chunks(sessions, start_date, end_date, chunksize): def make_utc_aware(dti): - """ - Normalizes a pd.DateTimeIndex. Assumes UTC if tz-naive. - """ + """Normalizes a pd.DateTimeIndex. Assumes UTC if tz-naive.""" try: # ensure tz-aware Timestamp has tz UTC return dti.tz_convert(tz="UTC") diff --git a/src/zipline/utils/events.py b/src/zipline/utils/events.py index d95c9e4a47..9472f96d3a 100644 --- a/src/zipline/utils/events.py +++ b/src/zipline/utils/events.py @@ -58,9 +58,7 @@ def ensure_utc(time, tz="UTC"): - """ - Normalize a time. If the time is tz-naive, assume it is UTC. - """ + """Normalize a time. If the time is tz-naive, assume it is UTC.""" if not time.tzinfo: time = time.replace(tzinfo=pytz.timezone(tz)) return time.replace(tzinfo=pytz.utc) @@ -130,10 +128,9 @@ def _build_date(date, kwargs): # TODO: only used in tests +# TODO FIX TZ def _build_time(time, kwargs): - """ - Builds the time argument for event rules. - """ + """Builds the time argument for event rules.""" tz = kwargs.pop("tz", "UTC") if time: if kwargs: @@ -368,18 +365,20 @@ def __init__(self, offset=None, **kwargs): self._one_minute = datetime.timedelta(minutes=1) def calculate_dates(self, dt): - """ - Given a date, find that day's open and period end (open + offset). - """ - period_start, period_close = self.cal.open_and_close_for_session( - self.cal.minute_to_session_label(dt), - ) + """Given a date, find that day's open and period end (open + offset).""" + + period_start = self.cal.session_first_minute(self.cal.minute_to_session(dt)) + period_close = self.cal.session_close(self.cal.minute_to_session(dt)) # Align the market open and close times here with the execution times # used by the simulation clock. This ensures that scheduled functions # trigger at the correct times. - self._period_start = self.cal.execution_time_from_open(period_start) - self._period_close = self.cal.execution_time_from_close(period_close) + if self.cal.name == "us_futures": + self._period_start = self.cal.execution_time_from_open(period_start) + self._period_close = self.cal.execution_time_from_close(period_close) + else: + self._period_start = period_start + self._period_close = period_close self._period_end = self._period_start + self.offset - self._one_minute @@ -400,8 +399,7 @@ def should_trigger(self, dt): class BeforeClose(StatelessRule): - """ - A rule that triggers for some offset time before the market closes. + """A rule that triggers for some offset time before the market closes. Example that triggers for the last 30 minutes every day: >>> BeforeClose(minutes=30) # doctest: +ELLIPSIS @@ -425,14 +423,15 @@ def calculate_dates(self, dt): """ Given a dt, find that day's close and period start (close - offset). """ - period_end = self.cal.open_and_close_for_session( - self.cal.minute_to_session_label(dt), - )[1] + period_end = self.cal.session_close(self.cal.minute_to_session(dt)) # Align the market close time here with the execution time used by the # simulation clock. This ensures that scheduled functions trigger at # the correct times. - self._period_end = self.cal.execution_time_from_close(period_end) + if self.cal == "us_futures": + self._period_end = self.cal.execution_time_from_close(period_end) + else: + self._period_end = period_end self._period_start = self._period_end - self.offset self._period_close = self._period_end @@ -459,7 +458,7 @@ class NotHalfDay(StatelessRule): """ def should_trigger(self, dt): - return self.cal.minute_to_session_label(dt) not in self.cal.early_closes + return self.cal.minute_to_session(dt) not in self.cal.early_closes class TradingDayOfWeekRule(StatelessRule, metaclass=ABCMeta): @@ -472,13 +471,13 @@ def __init__(self, n, invert): def should_trigger(self, dt): # is this market minute's period in the list of execution periods? - val = self.cal.minute_to_session_label(dt, direction="none").value + val = self.cal.minute_to_session(dt, direction="none").value return val in self.execution_period_values @lazyval def execution_period_values(self): # calculate the list of periods that match the given criteria - sessions = self.cal.all_sessions + sessions = self.cal.sessions return set( pd.Series(data=sessions) # Group by ISO year (0) and week (1) @@ -519,13 +518,13 @@ def __init__(self, n, invert): def should_trigger(self, dt): # is this market minute's period in the list of execution periods? - value = self.cal.minute_to_session_label(dt, direction="none").value + value = self.cal.minute_to_session(dt, direction="none").value return value in self.execution_period_values @lazyval def execution_period_values(self): # calculate the list of periods that match the given criteria - sessions = self.cal.all_sessions + sessions = self.cal.sessions return set( pd.Series(data=sessions) .groupby([sessions.year, sessions.month]) @@ -605,8 +604,7 @@ def should_trigger(self, dt): class date_rules: - """ - Factories for date-based :func:`~zipline.api.schedule_function` rules. + """Factories for date-based :func:`~zipline.api.schedule_function` rules. See Also -------- diff --git a/src/zipline/utils/factory.py b/src/zipline/utils/factory.py index d617292874..5f42a12698 100644 --- a/src/zipline/utils/factory.py +++ b/src/zipline/utils/factory.py @@ -31,7 +31,7 @@ def create_simulation_parameters( year=2006, start=None, end=None, - capital_base=float("1.0e5"), + capital_base=1.0e5, num_days=None, data_frequency="daily", emission_rate="daily", @@ -48,8 +48,8 @@ def create_simulation_parameters( if end is None: if num_days: - start_index = trading_calendar.all_sessions.searchsorted(start) - end = trading_calendar.all_sessions[start_index + num_days - 1] + start_index = trading_calendar.sessions.searchsorted(start) + end = trading_calendar.sessions[start_index + num_days - 1] else: end = pd.Timestamp("{0}-12-31".format(year), tz="UTC") elif type(end) == datetime: diff --git a/src/zipline/utils/final.py b/src/zipline/utils/final.py index 2b31b5c26a..a8b867411d 100644 --- a/src/zipline/utils/final.py +++ b/src/zipline/utils/final.py @@ -116,9 +116,7 @@ def __get__(self, instance, owner): class finaldescriptor(final): - """ - A final wrapper around a descriptor. - """ + """A final wrapper around a descriptor.""" def __get__(self, instance, owner): return self._attr.__get__(instance, owner) diff --git a/src/zipline/utils/pandas_utils.py b/src/zipline/utils/pandas_utils.py index cf1126b07b..944e88dc81 100644 --- a/src/zipline/utils/pandas_utils.py +++ b/src/zipline/utils/pandas_utils.py @@ -21,25 +21,12 @@ skip_pipeline_blaze = "Blaze doesn't play nicely with Pandas >=1.0" -def normalize_date(dt): - """ - Normalize datetime.datetime value to midnight. Returns datetime.date as - a datetime.datetime at midnight - - Returns - ------- - normalized : datetime.datetime or Timestamp - """ - return dt.normalize() - - def july_5th_holiday_observance(datetime_index): return datetime_index[datetime_index.year != 2013] def explode(df): - """ - Take a DataFrame and return a triple of + """Take a DataFrame and return a triple of (df.index, df.columns, df.values) """ @@ -115,8 +102,7 @@ def mask_between_time(dts, start, end, include_start=True, include_end=True): def find_in_sorted_index(dts, dt): - """ - Find the index of ``dt`` in ``dts``. + """Find the index of ``dt`` in ``dts``. This function should be used instead of `dts.get_loc(dt)` if the index is large enough that we don't want to initialize a hash table in ``dts``. In @@ -146,8 +132,7 @@ def find_in_sorted_index(dts, dt): def nearest_unequal_elements(dts, dt): - """ - Find values in ``dts`` closest but not equal to ``dt``. + """Find values in ``dts`` closest but not equal to ``dt``. Returns a pair of (last_before, first_after). @@ -196,16 +181,12 @@ def nearest_unequal_elements(dts, dt): def timedelta_to_integral_seconds(delta): - """ - Convert a pd.Timedelta to a number of seconds as an int. - """ + """Convert a pd.Timedelta to a number of seconds as an int.""" return int(delta.total_seconds()) def timedelta_to_integral_minutes(delta): - """ - Convert a pd.Timedelta to a number of minutes as an int. - """ + """Convert a pd.Timedelta to a number of minutes as an int.""" return timedelta_to_integral_seconds(delta) // 60 @@ -223,8 +204,7 @@ def ignore_pandas_nan_categorical_warning(): def categorical_df_concat(df_list, inplace=False): - """ - Prepare list of pandas DataFrames to be used as input to pd.concat. + """Prepare list of pandas DataFrames to be used as input to pd.concat. Ensure any columns of type 'category' have the same categories across each dataframe. diff --git a/src/zipline/utils/run_algo.py b/src/zipline/utils/run_algo.py index f375beef3e..8ef95fab38 100644 --- a/src/zipline/utils/run_algo.py +++ b/src/zipline/utils/run_algo.py @@ -100,7 +100,7 @@ def _run( trading_calendar = get_calendar("XNYS") # date parameter validation - if trading_calendar.session_distance(start, end) < 1: + if trading_calendar.sessions_distance(start, end) < 1: raise _RunAlgoError( "There are no trading days between %s and %s" % ( diff --git a/src/zipline/utils/security_list.py b/src/zipline/utils/security_list.py index 82ec6c42ab..6fa3dfb904 100644 --- a/src/zipline/utils/security_list.py +++ b/src/zipline/utils/security_list.py @@ -4,7 +4,8 @@ import os.path import pandas as pd -import pytz + +# import pytz import zipline from zipline.errors import SymbolNotFound @@ -58,7 +59,7 @@ def __contains__(self, item): def current_securities(self, dt): for kd in self._knowledge_dates: - if dt < kd: + if dt < kd.tz_localize(dt.tzinfo): break if kd in self._cache: self._current_set = self._cache[kd] @@ -114,8 +115,7 @@ def restrict_leveraged_etfs(self): def load_from_directory(list_name): - """ - To resolve the symbol in the LEVERAGED_ETF list, + """To resolve the symbol in the LEVERAGED_ETF list, the date on which the symbol was in effect is needed. Furthermore, to maintain a point in time record of our own maintenance @@ -134,11 +134,11 @@ def load_from_directory(list_name): data = {} dir_path = os.path.join(SECURITY_LISTS_DIR, list_name) for kd_name in listdir(dir_path): - kd = datetime.strptime(kd_name, DATE_FORMAT).replace(tzinfo=pytz.utc) + kd = datetime.strptime(kd_name, DATE_FORMAT) data[kd] = {} kd_path = os.path.join(dir_path, kd_name) for ld_name in listdir(kd_path): - ld = datetime.strptime(ld_name, DATE_FORMAT).replace(tzinfo=pytz.utc) + ld = datetime.strptime(ld_name, DATE_FORMAT) data[kd][ld] = {} ld_path = os.path.join(kd_path, ld_name) for fname in listdir(ld_path): diff --git a/tests/conftest.py b/tests/conftest.py index 185dd385b3..9d6d404587 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,8 +13,8 @@ DEFAULT_DATE_BOUNDS = { - "START_DATE": pd.Timestamp("2006-01-03", tz="utc"), - "END_DATE": pd.Timestamp("2006-12-29", tz="utc"), + "START_DATE": pd.Timestamp("2006-01-03"), + "END_DATE": pd.Timestamp("2006-12-29"), } @@ -45,8 +45,7 @@ def empty_assets_db(sql_db, request): @pytest.fixture(scope="class") def with_trading_calendars(request): - """ - fixture providing cls.trading_calendar, + """fixture providing cls.trading_calendar, cls.all_trading_calendars, cls.trading_calendar_for_asset_type as a class-level fixture. diff --git a/tests/data/bundles/test_core.py b/tests/data/bundles/test_core.py index a7b25cbd06..f931580312 100644 --- a/tests/data/bundles/test_core.py +++ b/tests/data/bundles/test_core.py @@ -48,8 +48,8 @@ class BundleCoreTestCase(WithInstanceTmpDir, WithDefaultDateBounds, ZiplineTestCase): - START_DATE = pd.Timestamp("2014-01-06", tz="utc") - END_DATE = pd.Timestamp("2014-01-10", tz="utc") + START_DATE = pd.Timestamp("2014-01-06") + END_DATE = pd.Timestamp("2014-01-10") def init_instance_fixtures(self): super(BundleCoreTestCase, self).init_instance_fixtures() @@ -129,10 +129,7 @@ def bundle_ingest( def test_ingest(self): calendar = get_calendar("XNYS") sessions = calendar.sessions_in_range(self.START_DATE, self.END_DATE) - minutes = calendar.minutes_for_sessions_in_range( - self.START_DATE, - self.END_DATE, - ) + minutes = calendar.sessions_minutes(self.START_DATE, self.END_DATE) sids = tuple(range(3)) equities = make_simple_equity_info( @@ -382,8 +379,8 @@ def _empty_ingest(self, _wrote_to=[]): @self.register( "bundle", calendar_name="NYSE", - start_session=pd.Timestamp("2014", tz="UTC"), - end_session=pd.Timestamp("2014", tz="UTC"), + start_session=pd.Timestamp("2014"), + end_session=pd.Timestamp("2014"), ) def _( environ, diff --git a/tests/data/bundles/test_csvdir.py b/tests/data/bundles/test_csvdir.py index 3815fa6894..7381ed6189 100644 --- a/tests/data/bundles/test_csvdir.py +++ b/tests/data/bundles/test_csvdir.py @@ -20,8 +20,8 @@ class TestCSVDIRBundle: symbols = "AAPL", "IBM", "KO", "MSFT" - asset_start = pd.Timestamp("2012-01-03", tz="utc") - asset_end = pd.Timestamp("2014-12-31", tz="utc") + asset_start = pd.Timestamp("2012-01-03") + asset_end = pd.Timestamp("2014-12-31") bundle = bundles["csvdir"] calendar = get_calendar(bundle.calendar_name) start_date = calendar.first_session @@ -288,7 +288,7 @@ def test_bundle(self): assert equity.start_date == self.asset_start, equity assert equity.end_date == self.asset_end, equity - sessions = self.calendar.all_sessions + sessions = self.calendar.sessions actual = bundle.equity_daily_bar_reader.load_raw_arrays( self.columns, sessions[sessions.get_indexer([self.asset_start], "bfill")[0]], diff --git a/tests/data/bundles/test_quandl.py b/tests/data/bundles/test_quandl.py index 86dff09427..1e8f115895 100644 --- a/tests/data/bundles/test_quandl.py +++ b/tests/data/bundles/test_quandl.py @@ -30,8 +30,8 @@ class QuandlBundleTestCase(WithResponses, ZiplineTestCase): symbols = "AAPL", "BRK_A", "MSFT", "ZEN" - start_date = pd.Timestamp("2014-01", tz="utc") - end_date = pd.Timestamp("2015-01", tz="utc") + start_date = pd.Timestamp("2014-01") + end_date = pd.Timestamp("2015-01") bundle = bundles["quandl"] calendar = get_calendar(bundle.calendar_name) api_key = "IamNotaQuandlAPIkey" @@ -70,7 +70,7 @@ def pricing(): # the first index our written data will appear in the files on disk start_idx = ( - self.calendar.all_sessions.get_indexer([self.start_date], "ffill")[0] + 1 + self.calendar.sessions.get_indexer([self.start_date], "ffill")[0] + 1 ) # convert an index into the raw dataframe into an index into the @@ -219,7 +219,7 @@ def test_bundle(self): sids = 0, 1, 2, 3 assert set(bundle.asset_finder.sids) == set(sids) - sessions = self.calendar.all_sessions + sessions = self.calendar.sessions actual = bundle.equity_daily_bar_reader.load_raw_arrays( self.columns, sessions[sessions.get_indexer([self.start_date], "bfill")[0]], diff --git a/tests/data/test_adjustments.py b/tests/data/test_adjustments.py index 13b6d25ddd..8e6bd0e5fb 100644 --- a/tests/data/test_adjustments.py +++ b/tests/data/test_adjustments.py @@ -19,8 +19,6 @@ ZiplineTestCase, ) -nat = pd.Timestamp("nat") - class TestSQLiteAdjustmentsWriter( WithTradingCalendars, WithInstanceTmpDir, ZiplineTestCase @@ -83,22 +81,20 @@ def writer_from_close(self, close): def assert_all_empty(self, dfs): for k, v in dfs.items(): - assert len(v) == 0, "%s dataframe should be empty" % k + assert len(v) == 0, f"{k} dataframe should be empty" def test_calculate_dividend_ratio(self): first_date_ix = 200 - dates = self.trading_calendar.all_sessions[first_date_ix : first_date_ix + 3] + dates = self.trading_calendar.sessions[first_date_ix : first_date_ix + 3] - before_pricing_data = (dates[0] - self.trading_calendar.day).tz_convert("UTC") - one_day_past_pricing_data = (dates[-1] + self.trading_calendar.day).tz_convert( - "UTC" - ) - ten_days_past_pricing_data = ( - dates[-1] + self.trading_calendar.day * 10 - ).tz_convert("UTC") + before_pricing_data = dates[0] - self.trading_calendar.day + one_day_past_pricing_data = dates[-1] + self.trading_calendar.day + + ten_days_past_pricing_data = dates[-1] + self.trading_calendar.day * 10 def T(n): - return dates[n].tz_convert("UTC") + # return dates[n].tz_localize("UTC") + return dates[n] close = pd.DataFrame( [ @@ -144,7 +140,7 @@ def T(n): # they appear unchanged in the dividends payouts ix = first_date_ix for col in "declared_date", "record_date", "pay_date": - extra_dates = self.trading_calendar.all_sessions[ix : ix + len(dividends)] + extra_dates = self.trading_calendar.sessions[ix : ix + len(dividends)] ix += len(dividends) dividends[col] = extra_dates @@ -166,7 +162,10 @@ def T(n): assert_frame_equal(dividend_payouts, expected_dividend_payouts) expected_dividend_ratios = pd.DataFrame( - [[T(1), 0.95, 0], [T(2), 0.90, 1]], + [ + [T(1), 0.95, 0], + [T(2), 0.90, 1], + ], columns=["effective_date", "ratio", "sid"], ) dividend_ratios.sort_values( @@ -205,7 +204,7 @@ def _test_identity(self, name): sids = np.arange(5) # tx_convert makes tz-naive - dates = self.trading_calendar.all_sessions.tz_convert("UTC") + dates = self.trading_calendar.sessions def T(n): return dates[n] @@ -238,7 +237,7 @@ def test_mergers(self): def test_stock_dividends(self): sids = np.arange(5) - dates = self.trading_calendar.all_sessions.tz_convert("UTC") + dates = self.trading_calendar.sessions def T(n): return dates[n] @@ -275,8 +274,9 @@ def T(n): @parameter_space(convert_dates=[True, False]) def test_empty_frame_dtypes(self, convert_dates): """Test that dataframe dtypes are preserved for empty tables.""" + sids = np.arange(5) - dates = self.trading_calendar.all_sessions.tz_convert("UTC") + dates = self.trading_calendar.sessions if convert_dates: date_dtype = np.dtype("M8[ns]") diff --git a/tests/data/test_daily_bars.py b/tests/data/test_daily_bars.py index 12c6ae83fd..7c1110f0e0 100644 --- a/tests/data/test_daily_bars.py +++ b/tests/data/test_daily_bars.py @@ -120,16 +120,16 @@ TEST_QUERY_ASSETS = EQUITY_INFO.index -TEST_CALENDAR_START = pd.Timestamp("2015-06-01", tz="UTC") -TEST_CALENDAR_STOP = pd.Timestamp("2015-06-30", tz="UTC") +TEST_CALENDAR_START = pd.Timestamp("2015-06-01") +TEST_CALENDAR_STOP = pd.Timestamp("2015-06-30") -TEST_QUERY_START = pd.Timestamp("2015-06-10", tz="UTC") -TEST_QUERY_STOP = pd.Timestamp("2015-06-19", tz="UTC") +TEST_QUERY_START = pd.Timestamp("2015-06-10") +TEST_QUERY_STOP = pd.Timestamp("2015-06-19") HOLES = { - "US": {5: (pd.Timestamp("2015-06-17", tz="UTC"),)}, - "CA": {17: (pd.Timestamp("2015-06-17", tz="UTC"),)}, + "US": {5: (pd.Timestamp("2015-06-17"),)}, + "CA": {17: (pd.Timestamp("2015-06-17"),)}, } @@ -152,8 +152,8 @@ def init_class_fixtures(cls): super(_DailyBarsTestCase, cls).init_class_fixtures() cls.sessions = cls.trading_calendar.sessions_in_range( - cls.trading_calendar.minute_to_session_label(TEST_CALENDAR_START), - cls.trading_calendar.minute_to_session_label(TEST_CALENDAR_STOP), + cls.trading_calendar.minute_to_session(TEST_CALENDAR_START), + cls.trading_calendar.minute_to_session(TEST_CALENDAR_STOP), ) @classmethod @@ -302,8 +302,8 @@ def test_write_attrs(self): assert result.attrs["last_row"] == expected_last_row assert result.attrs["calendar_offset"] == expected_calendar_offset cal = get_calendar(result.attrs["calendar_name"]) - first_session = pd.Timestamp(result.attrs["start_session_ns"], tz="UTC") - end_session = pd.Timestamp(result.attrs["end_session_ns"], tz="UTC") + first_session = pd.Timestamp(result.attrs["start_session_ns"]) + end_session = pd.Timestamp(result.attrs["end_session_ns"]) sessions = cal.sessions_in_range(first_session, end_session) assert_equal(self.sessions, sessions) @@ -343,8 +343,7 @@ def test_read(self, columns): ) def test_start_on_asset_start(self): - """ - Test loading with queries that starts on the first day of each asset's + """Test loading with queries that starts on the first day of each asset's lifetime. """ columns = ["high", "volume"] @@ -357,8 +356,7 @@ def test_start_on_asset_start(self): ) def test_start_on_asset_end(self): - """ - Test loading with queries that start on the last day of each asset's + """Test loading with queries that start on the last day of each asset's lifetime. """ columns = ["close", "volume"] @@ -371,8 +369,7 @@ def test_start_on_asset_end(self): ) def test_end_on_asset_start(self): - """ - Test loading with queries that end on the first day of each asset's + """Test loading with queries that end on the first day of each asset's lifetime. """ columns = ["close", "volume"] @@ -385,8 +382,7 @@ def test_end_on_asset_start(self): ) def test_end_on_asset_end(self): - """ - Test loading with queries that end on the last day of each asset's + """Test loading with queries that end on the last day of each asset's lifetime. """ columns = [CLOSE, VOLUME] @@ -399,9 +395,7 @@ def test_end_on_asset_end(self): ) def test_read_known_and_unknown_sids(self): - """ - Test a query with some known sids mixed in with unknown sids. - """ + """Test a query with some known sids mixed in with unknown sids.""" # Construct a list of alternating valid and invalid query sids, # bookended by invalid sids. @@ -515,10 +509,10 @@ def test_unadjusted_get_value_no_data(self): reader = self.daily_bar_reader for asset in self.assets: - before_start = self.trading_calendar.previous_session_label( + before_start = self.trading_calendar.previous_session( self.asset_start(asset) ) - after_end = self.trading_calendar.next_session_label(self.asset_end(asset)) + after_end = self.trading_calendar.next_session(self.asset_end(asset)) # Attempting to get data for an asset before its start date # should raise NoDataBeforeDate. @@ -568,7 +562,7 @@ def test_get_last_traded_dt(self): # is either the end date for the asset, or ``mid_date`` if # the asset is *still* alive at that point. Otherwise, it # is pd.NaT. - mid_date = pd.Timestamp("2015-06-15", tz="UTC") + mid_date = pd.Timestamp("2015-06-15") if self.asset_start(sid) <= mid_date: expected = min(self.asset_end(sid), mid_date) else: @@ -587,7 +581,7 @@ def test_get_last_traded_dt(self): assert_equal( self.daily_bar_reader.get_last_traded_dt( self.asset_finder.retrieve_asset(sid), - pd.Timestamp(0, tz="UTC"), + pd.Timestamp(0), ), pd.NaT, ) @@ -662,7 +656,7 @@ class BcolzDailyBarWriterMissingDataTestCase( # Sid 5 is active from 2015-06-02 to 2015-06-30. MISSING_DATA_SID = 5 # Leave out data for a day in the middle of the query range. - MISSING_DATA_DAY = pd.Timestamp("2015-06-15", tz="UTC") + MISSING_DATA_DAY = pd.Timestamp("2015-06-15") @classmethod def make_equity_info(cls): @@ -690,7 +684,7 @@ def test_missing_values_assertion(self): "Got 20 rows for daily bars table with first day=2015-06-02, last " "day=2015-06-30, expected 21 rows.\n" "Missing sessions: " - "[Timestamp('2015-06-15 00:00:00+0000', tz='UTC')]\n" + "[Timestamp('2015-06-15 00:00:00')]\n" "Extra sessions: []" ) with pytest.raises(AssertionError, match=expected_msg): @@ -745,11 +739,11 @@ def test_sessions(self): def test_invalid_date(self): INVALID_DATES = ( # Before the start of the daily bars. - self.trading_calendar.previous_session_label(TEST_CALENDAR_START), + self.trading_calendar.previous_session(TEST_CALENDAR_START), # A Sunday. pd.Timestamp("2015-06-07", tz="UTC"), # After the end of the daily bars. - self.trading_calendar.next_session_label(TEST_CALENDAR_STOP), + self.trading_calendar.next_session(TEST_CALENDAR_STOP), ) for invalid_date in INVALID_DATES: @@ -969,10 +963,10 @@ def test_unadjusted_get_value_no_data(self): reader = self.daily_bar_reader for asset in self.assets: - before_start = self.trading_calendar.previous_session_label( + before_start = self.trading_calendar.previous_session( self.asset_start(asset) ) - after_end = self.trading_calendar.next_session_label(self.asset_end(asset)) + after_end = self.trading_calendar.next_session(self.asset_end(asset)) # Attempting to get data for an asset before its start date # should raise NoDataBeforeDate. @@ -1022,7 +1016,7 @@ def test_get_last_traded_dt(self): # is either the end date for the asset, or ``mid_date`` if # the asset is *still* alive at that point. Otherwise, it # is pd.NaT. - mid_date = pd.Timestamp("2015-06-15", tz="UTC") + mid_date = pd.Timestamp("2015-06-15") if self.asset_start(sid) <= mid_date: expected = min(self.asset_end(sid), mid_date) else: diff --git a/tests/data/test_dispatch_bar_reader.py b/tests/data/test_dispatch_bar_reader.py index 9afea13fa1..68b12c86bd 100644 --- a/tests/data/test_dispatch_bar_reader.py +++ b/tests/data/test_dispatch_bar_reader.py @@ -48,13 +48,13 @@ class AssetDispatchSessionBarTestCase( ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 - START_DATE = Timestamp("2016-08-22", tz="UTC") - END_DATE = Timestamp("2016-08-24", tz="UTC") + START_DATE = Timestamp("2016-08-22") + END_DATE = Timestamp("2016-08-24") @classmethod def make_future_minute_bar_data(cls): m_opens = [ - cls.trading_calendar.open_and_close_for_session(session)[0] + cls.trading_calendar.session_first_minute(session) for session in cls.trading_sessions["us_futures"] ] yield 10001, DataFrame( @@ -191,7 +191,7 @@ def test_load_raw_arrays(self): ), ) - for i, (sid, expected, msg) in enumerate(expected_per_sid): + for i, (_sid, expected, msg) in enumerate(expected_per_sid): for j, result in enumerate(results): assert_almost_equal(result[:, i], expected[j], err_msg=msg) @@ -204,12 +204,12 @@ class AssetDispatchMinuteBarTestCase( ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 - START_DATE = Timestamp("2016-08-24", tz="UTC") - END_DATE = Timestamp("2016-08-24", tz="UTC") + START_DATE = Timestamp("2016-08-24") + END_DATE = Timestamp("2016-08-24") @classmethod def make_equity_minute_bar_data(cls): - minutes = cls.trading_calendars[Equity].minutes_for_session(cls.START_DATE) + minutes = cls.trading_calendars[Equity].session_minutes(cls.START_DATE) yield 1, DataFrame( { "open": [100.5, 101.5], @@ -243,8 +243,8 @@ def make_equity_minute_bar_data(cls): @classmethod def make_future_minute_bar_data(cls): - e_m = cls.trading_calendars[Equity].minutes_for_session(cls.START_DATE) - f_m = cls.trading_calendar.minutes_for_session(cls.START_DATE) + e_m = cls.trading_calendars[Equity].session_minutes(cls.START_DATE) + f_m = cls.trading_calendar.session_minutes(cls.START_DATE) # Equity market open occurs at loc 930 in Future minutes. minutes = [f_m[0], e_m[0], e_m[1]] yield 10001, DataFrame( @@ -315,7 +315,7 @@ def init_class_fixtures(cls): ) def test_load_raw_arrays_at_future_session_open(self): - f_minutes = self.trading_calendar.minutes_for_session(self.START_DATE) + f_minutes = self.trading_calendar.session_minutes(self.START_DATE) results = self.dispatch_reader.load_raw_arrays( ["open", "close"], f_minutes[0], f_minutes[2], [2, 10003, 1, 10001] @@ -354,7 +354,7 @@ def test_load_raw_arrays_at_future_session_open(self): ) def test_load_raw_arrays_at_equity_session_open(self): - e_minutes = self.trading_calendars[Equity].minutes_for_session(self.START_DATE) + e_minutes = self.trading_calendars[Equity].session_minutes(self.START_DATE) results = self.dispatch_reader.load_raw_arrays( ["open", "high"], e_minutes[0], e_minutes[2], [10002, 1, 3, 10001] diff --git a/tests/data/test_fx.py b/tests/data/test_fx.py index e28e1bac5e..4e256a1d01 100644 --- a/tests/data/test_fx.py +++ b/tests/data/test_fx.py @@ -9,8 +9,7 @@ class _FXReaderTestCase(zp_fixtures.WithFXRates, zp_fixtures.ZiplineTestCase): - """ - Base class for testing FXRateReader implementations. + """Base class for testing FXRateReader implementations. To test a new FXRateReader implementation, subclass from this base class and implement the ``reader`` property, returning an FXRateReader that uses @@ -19,8 +18,8 @@ class _FXReaderTestCase(zp_fixtures.WithFXRates, zp_fixtures.ZiplineTestCase): __test__ = False - FX_RATES_START_DATE = pd.Timestamp("2014-01-01", tz="UTC") - FX_RATES_END_DATE = pd.Timestamp("2014-01-31", tz="UTC") + FX_RATES_START_DATE = pd.Timestamp("2014-01-01") + FX_RATES_END_DATE = pd.Timestamp("2014-01-31") # Calendar to which exchange rates data is aligned. FX_RATES_CALENDAR = "24/5" @@ -285,10 +284,12 @@ def test_fast_get_loc_ffilled(self): for dt in pd.date_range("2014-01-02", "2014-01-08"): result = zp_fixtures.fast_get_loc_ffilled(dts.values, dt.asm8) - expected = dts.get_loc(dt, method="ffill") + expected = dts.get_indexer([dt], method="ffill")[0] assert_equal(result, expected) with pytest.raises(KeyError): + # TODO FIXME get_loc is deprecated but get_indexer doesn't raise keyerror + # THIS IS worrying as -1 is returned instead dts.get_loc(pd.Timestamp("2014-01-01"), method="ffill") with pytest.raises(KeyError): diff --git a/tests/data/test_hdf5_daily_bars.py b/tests/data/test_hdf5_daily_bars.py index 80e5d9dbe3..eca848a15c 100644 --- a/tests/data/test_hdf5_daily_bars.py +++ b/tests/data/test_hdf5_daily_bars.py @@ -12,8 +12,7 @@ class H5WriterTestCase(zp_fixtures.WithTmpDir, zp_fixtures.ZiplineTestCase): def test_write_empty_country(self): - """ - Test that we can write an empty country to an HDF5 daily bar writer. + """Test that we can write an empty country to an HDF5 daily bar writer. This is useful functionality for some tests, but it requires a bunch of special cased logic in the writer. @@ -70,7 +69,6 @@ def ohlcv(frame): "2014-01-04", "2014-01-06", "2014-01-07", - ], - utc=True, + ] ), ) diff --git a/tests/data/test_minute_bars.py b/tests/data/test_minute_bars.py index c033c0d9e7..c7abe3539b 100644 --- a/tests/data/test_minute_bars.py +++ b/tests/data/test_minute_bars.py @@ -15,7 +15,6 @@ from datetime import timedelta import os import numpy as np -from numpy import nan import pandas as pd from numpy.testing import assert_almost_equal, assert_array_equal from unittest import skip @@ -43,8 +42,8 @@ # Calendar is set to cover several half days, to check a case where half # days would be read out of order in cases of windows which spanned over # multiple half days. -TEST_CALENDAR_START = pd.Timestamp("2014-06-02", tz="UTC") -TEST_CALENDAR_STOP = pd.Timestamp("2015-12-31", tz="UTC") +TEST_CALENDAR_START = pd.Timestamp("2014-06-02") +TEST_CALENDAR_STOP = pd.Timestamp("2015-12-31") class BcolzMinuteBarTestCase( @@ -58,8 +57,10 @@ def init_class_fixtures(cls): cal = cls.trading_calendar.schedule.loc[TEST_CALENDAR_START:TEST_CALENDAR_STOP] - cls.market_opens = cal.market_open.dt.tz_localize("UTC") - cls.market_closes = cal.market_close.dt.tz_localize("UTC") + cls.market_opens = cls.trading_calendar.first_minutes[ + TEST_CALENDAR_START:TEST_CALENDAR_STOP + ] + cls.market_closes = cal.close cls.test_calendar_start = cls.market_opens.index[0] cls.test_calendar_stop = cls.market_opens.index[-1] @@ -208,43 +209,33 @@ def test_write_two_bars(self): self.writer.write_sid(sid, data) open_price = self.reader.get_value(sid, minute_0, "open") - assert 10.0 == open_price high_price = self.reader.get_value(sid, minute_0, "high") - assert 20.0 == high_price low_price = self.reader.get_value(sid, minute_0, "low") - assert 30.0 == low_price close_price = self.reader.get_value(sid, minute_0, "close") - assert 40.0 == close_price volume_price = self.reader.get_value(sid, minute_0, "volume") - assert 50.0 == volume_price open_price = self.reader.get_value(sid, minute_1, "open") - assert 11.0 == open_price high_price = self.reader.get_value(sid, minute_1, "high") - assert 21.0 == high_price low_price = self.reader.get_value(sid, minute_1, "low") - assert 31.0 == low_price close_price = self.reader.get_value(sid, minute_1, "close") - assert 41.0 == close_price volume_price = self.reader.get_value(sid, minute_1, "volume") - assert 51.0 == volume_price def test_write_on_second_day(self): @@ -264,23 +255,18 @@ def test_write_on_second_day(self): self.writer.write_sid(sid, data) open_price = self.reader.get_value(sid, minute, "open") - assert 10.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 20.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 30.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 40.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 50.0 == volume_price def test_write_empty(self): @@ -293,23 +279,18 @@ def test_write_empty(self): self.writer.write_sid(sid, data) open_price = self.reader.get_value(sid, minute, "open") - - assert_almost_equal(nan, open_price) + assert_almost_equal(np.nan, open_price) high_price = self.reader.get_value(sid, minute, "high") - - assert_almost_equal(nan, high_price) + assert_almost_equal(np.nan, high_price) low_price = self.reader.get_value(sid, minute, "low") - - assert_almost_equal(nan, low_price) + assert_almost_equal(np.nan, low_price) close_price = self.reader.get_value(sid, minute, "close") - - assert_almost_equal(nan, close_price) + assert_almost_equal(np.nan, close_price) volume_price = self.reader.get_value(sid, minute, "volume") - assert_almost_equal(0, volume_price) def test_write_on_multiple_days(self): @@ -343,45 +324,34 @@ def test_write_on_multiple_days(self): minute = minutes[0] open_price = self.reader.get_value(sid, minute, "open") - assert 10.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 20.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 30.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 40.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 50.0 == volume_price minute = minutes[1] - open_price = self.reader.get_value(sid, minute, "open") - assert 11.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 21.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 31.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 41.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 51.0 == volume_price def test_no_overwrite(self): @@ -477,7 +447,7 @@ def test_append_on_new_day(self): # The second minute should have been padded with zeros for col in ("open", "high", "low", "close"): - assert_almost_equal(nan, reader.get_value(sid, second_minute, col)) + assert_almost_equal(np.nan, reader.get_value(sid, second_minute, col)) assert 0 == reader.get_value(sid, second_minute, "volume") # The next day minute should have data. @@ -485,8 +455,7 @@ def test_append_on_new_day(self): assert_almost_equal(ohlcv[col], reader.get_value(sid, next_day_minute, col)) def test_write_multiple_sids(self): - """ - Test writing multiple sids. + """Test writing multiple sids. Tests both that the data is written to the correct sid, as well as ensuring that the logic for creating the subdirectory path to each sid @@ -533,51 +502,38 @@ def test_write_multiple_sids(self): sid = sids[0] open_price = self.reader.get_value(sid, minute, "open") - assert 15.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 17.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 11.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 15.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 100.0 == volume_price sid = sids[1] - open_price = self.reader.get_value(sid, minute, "open") - assert 25.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 27.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 21.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 25.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 200.0 == volume_price def test_pad_data(self): - """ - Test writing empty data. - """ + """Test writing empty data.""" sid = 1 last_date = self.writer.last_date_in_output_for_sid(sid) assert last_date is pd.NaT @@ -604,35 +560,27 @@ def test_pad_data(self): self.writer.write_sid(sid, data) open_price = self.reader.get_value(sid, minute, "open") - assert 15.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 17.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 11.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 15.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 100.0 == volume_price # Check that if we then pad the rest of this day, we end up with # 2 days worth of minutes. self.writer.pad(sid, day) - assert len(self.writer._ensure_ctable(sid)) == self.writer._minutes_per_day * 2 def test_nans(self): - """ - Test writing empty data. - """ + """Test writing empty data.""" sid = 1 last_date = self.writer.last_date_in_output_for_sid(sid) assert last_date is pd.NaT @@ -647,10 +595,10 @@ def test_nans(self): minutes = pd.date_range(minute, periods=9, freq="min") data = pd.DataFrame( data={ - "open": np.full(9, nan), - "high": np.full(9, nan), - "low": np.full(9, nan), - "close": np.full(9, nan), + "open": np.full(9, np.nan), + "high": np.full(9, np.nan), + "low": np.full(9, np.nan), + "close": np.full(9, np.nan), "volume": np.full(9, 0.0), }, index=minutes, @@ -673,14 +621,12 @@ def test_nans(self): for i, field in enumerate(fields): if field != "volume": - assert_array_equal(np.full(9, nan), ohlcv_window[i][0]) + assert_array_equal(np.full(9, np.nan), ohlcv_window[i][0]) else: assert_array_equal(np.zeros(9), ohlcv_window[i][0]) def test_differing_nans(self): - """ - Also test nans of differing values/construction. - """ + """Also test nans of differing values/construction.""" sid = 1 last_date = self.writer.last_date_in_output_for_sid(sid) assert last_date is pd.NaT @@ -729,7 +675,7 @@ def test_differing_nans(self): for i, field in enumerate(fields): if field != "volume": - assert_array_equal(np.full(9, nan), ohlcv_window[i][0]) + assert_array_equal(np.full(9, np.nan), ohlcv_window[i][0]) else: assert_array_equal(np.zeros(9), ohlcv_window[i][0]) @@ -748,43 +694,33 @@ def test_write_cols(self): self.writer.write_cols(sid, dts, cols) open_price = self.reader.get_value(sid, minute_0, "open") - assert 10.0 == open_price high_price = self.reader.get_value(sid, minute_0, "high") - assert 20.0 == high_price low_price = self.reader.get_value(sid, minute_0, "low") - assert 30.0 == low_price close_price = self.reader.get_value(sid, minute_0, "close") - assert 40.0 == close_price volume_price = self.reader.get_value(sid, minute_0, "volume") - assert 50.0 == volume_price open_price = self.reader.get_value(sid, minute_1, "open") - assert 11.0 == open_price high_price = self.reader.get_value(sid, minute_1, "high") - assert 21.0 == high_price low_price = self.reader.get_value(sid, minute_1, "low") - assert 31.0 == low_price close_price = self.reader.get_value(sid, minute_1, "close") - assert 41.0 == close_price volume_price = self.reader.get_value(sid, minute_1, "volume") - assert 51.0 == volume_price def test_write_cols_mismatch_length(self): @@ -805,9 +741,7 @@ def test_write_cols_mismatch_length(self): self.writer.write_cols(sid, dts, cols) def test_unadjusted_minutes(self): - """ - Test unadjusted minutes. - """ + """Test unadjusted minutes.""" start_minute = self.market_opens[TEST_CALENDAR_START] minutes = [ start_minute, @@ -817,10 +751,10 @@ def test_unadjusted_minutes(self): sids = [1, 2] data_1 = pd.DataFrame( data={ - "open": [15.0, nan, 15.1], - "high": [17.0, nan, 17.1], - "low": [11.0, nan, 11.1], - "close": [14.0, nan, 14.1], + "open": [15.0, np.nan, 15.1], + "high": [17.0, np.nan, 17.1], + "low": [11.0, np.nan, 11.1], + "close": [14.0, np.nan, 14.1], "volume": [1000, 0, 1001], }, index=minutes, @@ -829,10 +763,10 @@ def test_unadjusted_minutes(self): data_2 = pd.DataFrame( data={ - "open": [25.0, nan, 25.1], - "high": [27.0, nan, 27.1], - "low": [21.0, nan, 21.1], - "close": [24.0, nan, 24.1], + "open": [25.0, np.nan, 25.1], + "high": [27.0, np.nan, 27.1], + "low": [21.0, np.nan, 21.1], + "close": [24.0, np.nan, 24.1], "volume": [2000, 0, 2001], }, index=minutes, @@ -862,13 +796,12 @@ def test_unadjusted_minutes(self): assert_almost_equal(data[sid][col], arrays[i][j]) def test_unadjusted_minutes_early_close(self): - """ - Test unadjusted minute window, ensuring that early closes are filtered + """Test unadjusted minute window, ensuring that early closes are filtered out. """ - day_before_thanksgiving = pd.Timestamp("2015-11-25", tz="UTC") - xmas_eve = pd.Timestamp("2015-12-24", tz="UTC") - market_day_after_xmas = pd.Timestamp("2015-12-28", tz="UTC") + day_before_thanksgiving = pd.Timestamp("2015-11-25") + xmas_eve = pd.Timestamp("2015-12-24") + market_day_after_xmas = pd.Timestamp("2015-12-28") minutes = [ self.market_closes[day_before_thanksgiving] - pd.Timedelta("2 min"), @@ -918,9 +851,9 @@ def test_unadjusted_minutes_early_close(self): data = {sids[0]: data_1, sids[1]: data_2} - start_minute_loc = self.trading_calendar.all_minutes.get_loc(minutes[0]) + start_minute_loc = self.trading_calendar.minutes.get_loc(minutes[0]) minute_locs = [ - self.trading_calendar.all_minutes.get_loc(minute) - start_minute_loc + self.trading_calendar.minutes.get_loc(minute) - start_minute_loc for minute in minutes ] @@ -931,8 +864,8 @@ def test_unadjusted_minutes_early_close(self): ) def test_adjust_non_trading_minutes(self): - start_day = pd.Timestamp("2015-06-01", tz="UTC") - end_day = pd.Timestamp("2015-06-02", tz="UTC") + start_day = pd.Timestamp("2015-06-01") + end_day = pd.Timestamp("2015-06-02") sid = 1 cols = { @@ -942,12 +875,7 @@ def test_adjust_non_trading_minutes(self): "close": np.arange(1, 781), "volume": np.arange(1, 781), } - dts = np.array( - self.trading_calendar.minutes_for_sessions_in_range( - self.trading_calendar.minute_to_session_label(start_day), - self.trading_calendar.minute_to_session_label(end_day), - ) - ) + dts = np.array(self.trading_calendar.sessions_minutes(start_day, end_day)) self.writer.write_cols(sid, dts, cols) @@ -974,8 +902,8 @@ def test_adjust_non_trading_minutes(self): def test_adjust_non_trading_minutes_half_days(self): # half day - start_day = pd.Timestamp("2015-11-27", tz="UTC") - end_day = pd.Timestamp("2015-11-30", tz="UTC") + start_day = pd.Timestamp("2015-11-27") + end_day = pd.Timestamp("2015-11-30") sid = 1 cols = { @@ -985,12 +913,7 @@ def test_adjust_non_trading_minutes_half_days(self): "close": np.arange(1, 601), "volume": np.arange(1, 601), } - dts = np.array( - self.trading_calendar.minutes_for_sessions_in_range( - self.trading_calendar.minute_to_session_label(start_day), - self.trading_calendar.minute_to_session_label(end_day), - ) - ) + dts = np.array(self.trading_calendar.sessions_minutes(start_day, end_day)) self.writer.write_cols(sid, dts, cols) @@ -1026,8 +949,8 @@ def test_set_sid_attrs(self): """Confirm that we can set the attributes of a sid's file correctly.""" sid = 1 - start_day = pd.Timestamp("2015-11-27", tz="UTC") - end_day = pd.Timestamp("2015-06-02", tz="UTC") + start_day = pd.Timestamp("2015-11-27") + end_day = pd.Timestamp("2015-06-02") attrs = { "start_day": start_day.value / int(1e9), "end_day": end_day.value / int(1e9), @@ -1077,33 +1000,27 @@ def test_truncate_between_data_points(self): # Refresh the reader since truncate update the metadata. self.reader = BcolzMinuteBarReader(self.dest) - assert self.writer.last_date_in_output_for_sid(sid) == days[0] cal = self.trading_calendar - _, last_close = cal.open_and_close_for_session(days[0]) + last_close = cal.session_close(days[0]) assert self.reader.last_available_dt == last_close minute = minutes[0] open_price = self.reader.get_value(sid, minute, "open") - assert 10.0 == open_price high_price = self.reader.get_value(sid, minute, "high") - assert 20.0 == high_price low_price = self.reader.get_value(sid, minute, "low") - assert 30.0 == low_price close_price = self.reader.get_value(sid, minute, "close") - assert 40.0 == close_price volume_price = self.reader.get_value(sid, minute, "volume") - assert 50.0 == volume_price def test_truncate_all_data_points(self): @@ -1144,19 +1061,19 @@ def test_truncate_all_data_points(self): assert self.writer.last_date_in_output_for_sid(sid) == self.test_calendar_start cal = self.trading_calendar - _, last_close = cal.open_and_close_for_session(self.test_calendar_start) + last_close = cal.session_close(self.test_calendar_start) assert self.reader.last_available_dt == last_close def test_early_market_close(self): # Date to test is 2015-11-30 9:31 # Early close is 2015-11-27 18:00 - friday_after_tday = pd.Timestamp("2015-11-27", tz="UTC") + friday_after_tday = pd.Timestamp("2015-11-27") friday_after_tday_close = self.market_closes[friday_after_tday] before_early_close = friday_after_tday_close - timedelta(minutes=8) after_early_close = friday_after_tday_close + timedelta(minutes=8) - monday_after_tday = pd.Timestamp("2015-11-30", tz="UTC") + monday_after_tday = pd.Timestamp("2015-11-30") minute = self.market_opens[monday_after_tday] # Test condition where there is data written after the market @@ -1167,10 +1084,10 @@ def test_early_market_close(self): sid = 1 data = pd.DataFrame( data={ - "open": [10.0, 11.0, nan], - "high": [20.0, 21.0, nan], - "low": [30.0, 31.0, nan], - "close": [40.0, 41.0, nan], + "open": [10.0, 11.0, np.nan], + "high": [20.0, 21.0, np.nan], + "low": [30.0, 31.0, np.nan], + "close": [40.0, 41.0, np.nan], "volume": [50, 51, 0], }, index=minutes, @@ -1178,23 +1095,18 @@ def test_early_market_close(self): self.writer.write_sid(sid, data) open_price = self.reader.get_value(sid, minute, "open") - - assert_almost_equal(nan, open_price) + assert_almost_equal(np.nan, open_price) high_price = self.reader.get_value(sid, minute, "high") - - assert_almost_equal(nan, high_price) + assert_almost_equal(np.nan, high_price) low_price = self.reader.get_value(sid, minute, "low") - - assert_almost_equal(nan, low_price) + assert_almost_equal(np.nan, low_price) close_price = self.reader.get_value(sid, minute, "close") - - assert_almost_equal(nan, close_price) + assert_almost_equal(np.nan, close_price) volume = self.reader.get_value(sid, minute, "volume") - assert 0 == volume asset = self.asset_finder.retrieve_asset(sid) @@ -1208,9 +1120,7 @@ def test_early_market_close(self): @skip("not requiring tables for now") def test_minute_updates(self): - """ - Test minute updates. - """ + """Test minute updates.""" start_minute = self.market_opens[TEST_CALENDAR_START] minutes = [ start_minute, @@ -1220,10 +1130,10 @@ def test_minute_updates(self): sids = [1, 2] data_1 = pd.DataFrame( data={ - "open": [15.0, nan, 15.1], - "high": [17.0, nan, 17.1], - "low": [11.0, nan, 11.1], - "close": [14.0, nan, 14.1], + "open": [15.0, np.nan, 15.1], + "high": [17.0, np.nan, 17.1], + "low": [11.0, np.nan, 11.1], + "close": [14.0, np.nan, 14.1], "volume": [1000, 0, 1001], }, index=minutes, @@ -1231,10 +1141,10 @@ def test_minute_updates(self): data_2 = pd.DataFrame( data={ - "open": [25.0, nan, 25.1], - "high": [27.0, nan, 27.1], - "low": [21.0, nan, 21.1], - "close": [24.0, nan, 24.1], + "open": [25.0, np.nan, 25.1], + "high": [27.0, np.nan, 27.1], + "low": [21.0, np.nan, 21.1], + "close": [24.0, np.nan, 24.1], "volume": [2000, 0, 2001], }, index=minutes, diff --git a/tests/data/test_resample.py b/tests/data/test_resample.py index e06d94a2c6..d647f62ac0 100644 --- a/tests/data/test_resample.py +++ b/tests/data/test_resample.py @@ -18,7 +18,6 @@ from numpy.testing import assert_almost_equal from numpy import nan, array, full, isnan import pandas as pd -from pandas import DataFrame from zipline.data.resample import ( minute_frame_to_session_frame, @@ -170,7 +169,7 @@ for sid, combos in _EQUITY_CASES: frames = [ - DataFrame(SCENARIOS[s], columns=OHLCV).set_index(NYSE_MINUTES[m]) + pd.DataFrame(SCENARIOS[s], columns=OHLCV).set_index(NYSE_MINUTES[m]) for s, m in combos ] EQUITY_CASES[sid] = pd.concat(frames) @@ -186,13 +185,13 @@ for sid, combos in _FUTURE_CASES: frames = [ - DataFrame(SCENARIOS[s], columns=OHLCV).set_index(FUT_MINUTES[m]) + pd.DataFrame(SCENARIOS[s], columns=OHLCV).set_index(FUT_MINUTES[m]) for s, m in combos ] FUTURE_CASES[sid] = pd.concat(frames) EXPECTED_AGGREGATION = { - 1: DataFrame( + 1: pd.DataFrame( { "open": [101.5, 101.5, 101.5, 101.5, 101.5, 101.5], "high": [101.9, 103.9, 103.9, 107.9, 108.9, 108.9], @@ -202,7 +201,7 @@ }, columns=OHLCV, ), - 2: DataFrame( + 2: pd.DataFrame( { "open": [nan, 103.5, 103.5, 103.5, 103.5, 103.5], "high": [nan, 103.9, 103.9, 103.9, 103.9, 103.9], @@ -213,7 +212,7 @@ columns=OHLCV, ), # Equity 3 straddles two days. - 3: DataFrame( + 3: pd.DataFrame( { "open": [107.5, 107.5, 107.5, nan, 103.5, 103.5], "high": [107.9, 108.9, 108.9, nan, 103.9, 103.9], @@ -224,7 +223,7 @@ columns=OHLCV, ), # Equity 4 straddles two days and is not active the first day. - 4: DataFrame( + 4: pd.DataFrame( { "open": [nan, nan, nan, 101.5, 101.5, 101.5], "high": [nan, nan, nan, 101.9, 103.9, 103.9], @@ -235,7 +234,7 @@ columns=OHLCV, ), # Equity 5 straddles two days and does not have data the first day. - 5: DataFrame( + 5: pd.DataFrame( { "open": [nan, nan, nan, 101.5, 101.5, 101.5], "high": [nan, nan, nan, 101.9, 103.9, 103.9], @@ -245,7 +244,7 @@ }, columns=OHLCV, ), - 1001: DataFrame( + 1001: pd.DataFrame( { "open": [101.5, 101.5, 101.5, 101.5, 101.5, 101.5], "high": [101.9, 103.9, 103.9, 103.9, 103.9, 103.9], @@ -255,7 +254,7 @@ }, columns=OHLCV, ), - 1002: DataFrame( + 1002: pd.DataFrame( { "open": [nan, 103.5, 103.5, 103.5, 103.5, 103.5], "high": [nan, 103.9, 103.9, 103.9, 103.9, 103.9], @@ -265,7 +264,7 @@ }, columns=OHLCV, ), - 1003: DataFrame( + 1003: pd.DataFrame( { "open": [107.5, 107.5, 107.5, nan, 103.5, 103.5], "high": [107.9, 108.9, 108.9, nan, 103.9, 103.9], @@ -275,7 +274,7 @@ }, columns=OHLCV, ), - 1004: DataFrame( + 1004: pd.DataFrame( { "open": [nan, nan, nan, 101.5, 101.5, 101.5], "high": [nan, nan, nan, 101.9, 103.9, 103.9], @@ -288,40 +287,40 @@ } EXPECTED_SESSIONS = { - 1: DataFrame( + 1: pd.DataFrame( [EXPECTED_AGGREGATION[1].iloc[-1].values], columns=OHLCV, - index=pd.to_datetime(["2016-03-15"], utc=True), + index=pd.to_datetime(["2016-03-15"]), ), - 2: DataFrame( + 2: pd.DataFrame( [EXPECTED_AGGREGATION[2].iloc[-1].values], columns=OHLCV, - index=pd.to_datetime(["2016-03-15"], utc=True), + index=pd.to_datetime(["2016-03-15"]), ), - 3: DataFrame( + 3: pd.DataFrame( EXPECTED_AGGREGATION[3].iloc[[2, 5]].values, columns=OHLCV, - index=pd.to_datetime(["2016-03-15", "2016-03-16"], utc=True), + index=pd.to_datetime(["2016-03-15", "2016-03-16"]), ), - 1001: DataFrame( + 1001: pd.DataFrame( [EXPECTED_AGGREGATION[1001].iloc[-1].values], columns=OHLCV, - index=pd.to_datetime(["2016-03-16"], utc=True), + index=pd.to_datetime(["2016-03-16"]), ), - 1002: DataFrame( + 1002: pd.DataFrame( [EXPECTED_AGGREGATION[1002].iloc[-1].values], columns=OHLCV, - index=pd.to_datetime(["2016-03-16"], utc=True), + index=pd.to_datetime(["2016-03-16"]), ), - 1003: DataFrame( + 1003: pd.DataFrame( EXPECTED_AGGREGATION[1003].iloc[[2, 5]].values, columns=OHLCV, - index=pd.to_datetime(["2016-03-16", "2016-03-17"], utc=True), + index=pd.to_datetime(["2016-03-16", "2016-03-17"]), ), - 1004: DataFrame( + 1004: pd.DataFrame( EXPECTED_AGGREGATION[1004].iloc[[2, 5]].values, columns=OHLCV, - index=pd.to_datetime(["2016-03-16", "2016-03-17"], utc=True), + index=pd.to_datetime(["2016-03-16", "2016-03-17"]), ), } @@ -337,14 +336,8 @@ class MinuteToDailyAggregationTestCase( # 20 21 22 23 24 25 26 # 27 28 29 30 31 - TRADING_ENV_MIN_DATE = START_DATE = pd.Timestamp( - "2016-03-01", - tz="UTC", - ) - TRADING_ENV_MAX_DATE = END_DATE = pd.Timestamp( - "2016-03-31", - tz="UTC", - ) + TRADING_ENV_MIN_DATE = START_DATE = pd.Timestamp("2016-03-01") + TRADING_ENV_MAX_DATE = END_DATE = pd.Timestamp("2016-03-31") TRADING_CALENDAR_STRS = ("NYSE", "us_futures") @@ -356,7 +349,7 @@ def make_equity_info(cls): frame = super(MinuteToDailyAggregationTestCase, cls).make_equity_info() # Make equity 4 start a day behind the data start to exercise assets # which not alive for the session. - frame.loc[[4], "start_date"] = pd.Timestamp("2016-03-16", tz="UTC") + frame.loc[[4], "start_date"] = pd.Timestamp("2016-03-16") return frame @classmethod @@ -389,13 +382,13 @@ def init_instance_fixtures(self): # Set up a fresh data portal for each test, since order of calling # needs to be tested. self.equity_daily_aggregator = DailyHistoryAggregator( - self.nyse_calendar.schedule.market_open, + self.nyse_calendar.first_minutes, self.bcolz_equity_minute_bar_reader, self.nyse_calendar, ) self.future_daily_aggregator = DailyHistoryAggregator( - self.us_futures_calendar.schedule.market_open, + self.us_futures_calendar.first_minutes, self.bcolz_future_minute_bar_reader, self.us_futures_calendar, ) @@ -616,14 +609,8 @@ class TestMinuteToSession(WithEquityMinuteBarData, ZiplineTestCase): # 20 21 22 23 24 25 26 # 27 28 29 30 31 - START_DATE = pd.Timestamp( - "2016-03-15", - tz="UTC", - ) - END_DATE = pd.Timestamp( - "2016-03-15", - tz="UTC", - ) + START_DATE = pd.Timestamp("2016-03-15") + END_DATE = pd.Timestamp("2016-03-15") ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 @classmethod @@ -654,8 +641,8 @@ class TestResampleSessionBars(WithBcolzFutureMinuteBarReader, ZiplineTestCase): ASSET_FINDER_FUTURE_SIDS = 1001, 1002, 1003, 1004 - START_DATE = pd.Timestamp("2016-03-16", tz="UTC") - END_DATE = pd.Timestamp("2016-03-17", tz="UTC") + START_DATE = pd.Timestamp("2016-03-16") + END_DATE = pd.Timestamp("2016-03-17") NUM_SESSIONS = 2 @classmethod @@ -687,14 +674,14 @@ def test_resample(self): calendar = self.trading_calendar for sid in self.ASSET_FINDER_FUTURE_SIDS: case_frame = FUTURE_CASES[sid] - first = calendar.minute_to_session_label(case_frame.index[0]) - last = calendar.minute_to_session_label(case_frame.index[-1]) + first = calendar.minute_to_session(case_frame.index[0]) + last = calendar.minute_to_session(case_frame.index[-1]) result = self.session_bar_reader.load_raw_arrays(OHLCV, first, last, [sid]) for i, field in enumerate(OHLCV): assert_almost_equal( EXPECTED_SESSIONS[sid][[field]], result[i], - err_msg="sid={0} field={1}".format(sid, field), + err_msg=f"sid={sid} field={field}", ) def test_sessions(self): @@ -719,18 +706,11 @@ def test_get_value(self): ) for sid in self.ASSET_FINDER_FUTURE_SIDS: expected = EXPECTED_SESSIONS[sid] - for dt_str, values in expected.iterrows(): - try: - dt = pd.Timestamp(dt_str, tz="UTC") - except ValueError: - dt = dt_str.tz_convert(tz="UTC") - + for dt, values in expected.iterrows(): for col in OHLCV: result = session_bar_reader.get_value(sid, dt, col) assert_almost_equal( - result, - values[col], - err_msg="sid={0} col={1} dt={2}".format(sid, col, dt), + result, values[col], err_msg=f"sid={sid} col={col} dt={dt}" ) def test_first_trading_day(self): @@ -739,7 +719,7 @@ def test_first_trading_day(self): def test_get_last_traded_dt(self): future = self.asset_finder.retrieve_asset(self.ASSET_FINDER_FUTURE_SIDS[0]) - assert self.trading_calendar.previous_session_label( + assert self.trading_calendar.previous_session( self.END_DATE ) == self.session_bar_reader.get_last_traded_dt(future, self.END_DATE) @@ -750,8 +730,8 @@ class TestReindexMinuteBars(WithBcolzEquityMinuteBarReader, ZiplineTestCase): ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 - START_DATE = pd.Timestamp("2015-12-01", tz="UTC") - END_DATE = pd.Timestamp("2015-12-31", tz="UTC") + START_DATE = pd.Timestamp("2015-12-01") + END_DATE = pd.Timestamp("2015-12-31") def test_load_raw_arrays(self): reindex_reader = ReindexMinuteBarReader( @@ -760,13 +740,13 @@ def test_load_raw_arrays(self): self.START_DATE, self.END_DATE, ) - m_open, m_close = self.trading_calendar.open_and_close_for_session( - self.START_DATE - ) + m_open = self.trading_calendar.session_first_minute(self.START_DATE) + m_close = self.trading_calendar.session_close(self.START_DATE) + outer_minutes = self.trading_calendar.minutes_in_range(m_open, m_close) result = reindex_reader.load_raw_arrays(OHLCV, m_open, m_close, [1, 2]) - opens = DataFrame(data=result[0], index=outer_minutes, columns=[1, 2]) + opens = pd.DataFrame(data=result[0], index=outer_minutes, columns=[1, 2]) opens_with_price = opens.dropna() assert 1440 == len(opens), ( @@ -820,8 +800,8 @@ class TestReindexSessionBars(WithBcolzEquityDailyBarReader, ZiplineTestCase): # Dates are chosen to span Thanksgiving, which is not a Holiday on # us_futures. - START_DATE = pd.Timestamp("2015-11-02", tz="UTC") - END_DATE = pd.Timestamp("2015-11-30", tz="UTC") + START_DATE = pd.Timestamp("2015-11-02") + END_DATE = pd.Timestamp("2015-11-30") # November 2015 # Su Mo Tu We Th Fr Sa @@ -850,7 +830,7 @@ def test_load_raw_arrays(self): OHLCV, self.START_DATE, self.END_DATE, [1, 2] ) - opens = DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) + opens = pd.DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) opens_with_price = opens.dropna() assert 21 == len(opens), ( @@ -862,7 +842,7 @@ def test_load_raw_arrays(self): "because Thanksgiving is a NYSE holiday." ) - tday = pd.Timestamp("2015-11-26", tz="UTC") + tday = pd.Timestamp("2015-11-26") # Thanksgiving, 2015-11-26. # Is a holiday in NYSE, but not in us_futures. @@ -877,7 +857,7 @@ def test_load_raw_arrays(self): # Thanksgiving, 2015-11-26. # Is a holiday in NYSE, but not in us_futures. - tday_loc = outer_sessions.get_loc(pd.Timestamp("2015-11-26", tz="UTC")) + tday_loc = outer_sessions.get_loc(pd.Timestamp("2015-11-26")) assert_almost_equal( nan, @@ -887,12 +867,12 @@ def test_load_raw_arrays(self): ) def test_load_raw_arrays_holiday_start(self): - tday = pd.Timestamp("2015-11-26", tz="UTC") + tday = pd.Timestamp("2015-11-26") outer_sessions = self.trading_calendar.sessions_in_range(tday, self.END_DATE) result = self.reader.load_raw_arrays(OHLCV, tday, self.END_DATE, [1, 2]) - opens = DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) + opens = pd.DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) opens_with_price = opens.dropna() assert 3 == len(opens), ( @@ -905,12 +885,12 @@ def test_load_raw_arrays_holiday_start(self): ) def test_load_raw_arrays_holiday_end(self): - tday = pd.Timestamp("2015-11-26", tz="UTC") + tday = pd.Timestamp("2015-11-26") outer_sessions = self.trading_calendar.sessions_in_range(self.START_DATE, tday) result = self.reader.load_raw_arrays(OHLCV, self.START_DATE, tday, [1, 2]) - opens = DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) + opens = pd.DataFrame(data=result[0], index=outer_sessions, columns=[1, 2]) opens_with_price = opens.dropna() assert 19 == len(opens), ( @@ -935,7 +915,7 @@ def test_get_value(self): assert self.reader.get_value(1, tday, "volume") == 0 - def test_last_availabe_dt(self): + def test_last_available_dt(self): assert self.reader.last_available_dt == self.END_DATE def test_get_last_traded_dt(self): @@ -945,8 +925,8 @@ def test_get_last_traded_dt(self): def test_sessions(self): sessions = self.reader.sessions assert 21 == len(sessions), "There should be 21 sessions in 2015-11." - assert pd.Timestamp("2015-11-02", tz="UTC") == sessions[0] - assert pd.Timestamp("2015-11-30", tz="UTC") == sessions[-1] + assert pd.Timestamp("2015-11-02") == sessions[0] + assert pd.Timestamp("2015-11-30") == sessions[-1] def test_first_trading_day(self): assert self.reader.first_trading_day == self.START_DATE diff --git a/tests/events/test_events.py b/tests/events/test_events.py index f08181d904..24ccb3abf9 100644 --- a/tests/events/test_events.py +++ b/tests/events/test_events.py @@ -192,7 +192,7 @@ def minutes_for_days(cal, ordered_days=False): # optimization in AfterOpen and BeforeClose, we rely on the fact that # the clock only ever moves forward in a simulation. For those cases, # we guarantee that the list of trading days we test is ordered. - ordered_session_list = random.sample(list(cal.all_sessions), 500) + ordered_session_list = random.sample(list(cal.sessions), 500) ordered_session_list.sort() def session_picker(day): @@ -202,9 +202,9 @@ def session_picker(day): # Other than AfterOpen and BeforeClose, we don't rely on the the nature # of the clock, so we don't care. def session_picker(day): - return random.choice(cal.all_sessions[:-1]) + return random.choice(cal.sessions[:-1]) - return [cal.minutes_for_session(session_picker(cnt)) for cnt in range(500)] + return [cal.session_minutes(session_picker(cnt)) for cnt in range(500)] # THE CLASS BELOW ARE GOING TO BE IMPORTED BY test_events_cme and nyse @@ -212,7 +212,7 @@ class RuleTestCase: CALENDAR_STRING = "foo" @classmethod - def setUpClass(cls): + def setup_class(cls): # On the AfterOpen and BeforeClose tests, we want ensure that the # functions are pure, and that running them with the same input will # provide the same output, regardless of whether the function is run 1 @@ -256,25 +256,25 @@ def test_completeness(self): class StatelessRulesTests(RuleTestCase): @classmethod - def setUpClass(cls): - super(StatelessRulesTests, cls).setUpClass() + def setup_class(cls): + super(StatelessRulesTests, cls).setup_class() cls.class_ = StatelessRule cls.cal = get_calendar(cls.CALENDAR_STRING) # First day of 09/2014 is closed whereas that for 10/2014 is open cls.sept_sessions = cls.cal.sessions_in_range( - pd.Timestamp("2014-09-01", tz="UTC"), - pd.Timestamp("2014-09-30", tz="UTC"), + pd.Timestamp("2014-09-01"), + pd.Timestamp("2014-09-30"), ) cls.oct_sessions = cls.cal.sessions_in_range( - pd.Timestamp("2014-10-01", tz="UTC"), - pd.Timestamp("2014-10-31", tz="UTC"), + pd.Timestamp("2014-10-01"), + pd.Timestamp("2014-10-31"), ) - cls.sept_week = cls.cal.minutes_for_sessions_in_range( - pd.Timestamp("2014-09-22", tz="UTC"), - pd.Timestamp("2014-09-26", tz="UTC"), + cls.sept_week = cls.cal.sessions_minutes( + pd.Timestamp("2014-09-22"), + pd.Timestamp("2014-09-26"), ) cls.HALF_SESSION = None @@ -330,21 +330,20 @@ def test_NotHalfDay(self): rule.cal = self.cal if self.HALF_SESSION: - for minute in self.cal.minutes_for_session(self.HALF_SESSION): + for minute in self.cal.session_minutes(self.HALF_SESSION): assert not rule.should_trigger(minute) if self.FULL_SESSION: - for minute in self.cal.minutes_for_session(self.FULL_SESSION): + for minute in self.cal.session_minutes(self.FULL_SESSION): assert rule.should_trigger(minute) def test_NthTradingDayOfWeek_day_zero(self): - """ - Test that we don't blow up when trying to call week_start's + """Test that we don't blow up when trying to call week_start's should_trigger on the first day of a trading environment. """ rule = NthTradingDayOfWeek(0) rule.cal = self.cal - first_open = self.cal.open_and_close_for_session(self.cal.all_sessions[0]) + first_open = self.cal.session_open_close(self.cal.sessions[0]) assert first_open def test_NthTradingDayOfWeek(self): @@ -352,10 +351,10 @@ def test_NthTradingDayOfWeek(self): rule = NthTradingDayOfWeek(n) rule.cal = self.cal should_trigger = rule.should_trigger - prev_period = self.cal.minute_to_session_label(self.sept_week[0]) + prev_period = self.cal.minute_to_session(self.sept_week[0]) n_tdays = 0 for minute in self.sept_week: - period = self.cal.minute_to_session_label(minute) + period = self.cal.minute_to_session(minute) if prev_period < period: n_tdays += 1 @@ -374,11 +373,11 @@ def test_NDaysBeforeLastTradingDayOfWeek(self): for minute in self.sept_week: if should_trigger(minute): n_tdays = 0 - session = self.cal.minute_to_session_label(minute, direction="none") - next_session = self.cal.next_session_label(session) + session = self.cal.minute_to_session(minute, direction="none") + next_session = self.cal.next_session(session) while next_session.dayofweek > session.dayofweek: session = next_session - next_session = self.cal.next_session_label(session) + next_session = self.cal.next_session(session) n_tdays += 1 assert n_tdays == n @@ -391,7 +390,7 @@ def test_NthTradingDayOfMonth(self): for sessions_list in (self.sept_sessions, self.oct_sessions): for n_tdays, session in enumerate(sessions_list): # just check the first 10 minutes of each session - for m in self.cal.minutes_for_session(session)[0:10]: + for m in self.cal.session_minutes(session)[0:10]: if should_trigger(m): assert n_tdays == n else: @@ -404,7 +403,7 @@ def test_NDaysBeforeLastTradingDayOfMonth(self): should_trigger = rule.should_trigger sessions = reversed(self.oct_sessions) for n_days_before, session in enumerate(sessions): - for m in self.cal.minutes_for_session(session)[0:10]: + for m in self.cal.session_minutes(session)[0:10]: if should_trigger(m): assert n_days_before == n else: @@ -462,8 +461,8 @@ class StatefulRulesTests(RuleTestCase): CALENDAR_STRING = "NYSE" @classmethod - def setUpClass(cls): - super(StatefulRulesTests, cls).setUpClass() + def setup_class(cls): + super(StatefulRulesTests, cls).setup_class() cls.class_ = StatefulRule cls.cal = get_calendar(cls.CALENDAR_STRING) diff --git a/tests/events/test_events_nyse.py b/tests/events/test_events_nyse.py index c5ce931a17..99e7beb6b5 100644 --- a/tests/events/test_events_nyse.py +++ b/tests/events/test_events_nyse.py @@ -23,14 +23,12 @@ from .test_events import StatelessRulesTests, StatefulRulesTests, minutes_for_days -T = partial(pd.Timestamp, tz="UTC") - class TestStatelessRulesNYSE(StatelessRulesTests, TestCase): CALENDAR_STRING = "NYSE" - HALF_SESSION = pd.Timestamp("2014-07-03", tz="UTC") - FULL_SESSION = pd.Timestamp("2014-09-24", tz="UTC") + HALF_SESSION = pd.Timestamp("2014-07-03") + FULL_SESSION = pd.Timestamp("2014-09-24") def test_edge_cases_for_TradingDayOfWeek(self): """ @@ -84,7 +82,8 @@ def test_edge_cases_for_TradingDayOfWeek(self): } results = { - x: rule.should_trigger(self.cal.next_open(T(x))) for x in expected.keys() + x: rule.should_trigger(self.cal.session_first_minute(x)) + for x in expected.keys() } assert expected == results @@ -109,7 +108,8 @@ def test_edge_cases_for_TradingDayOfWeek(self): } results = { - x: rule.should_trigger(self.cal.next_open(T(x))) for x in expected.keys() + x: rule.should_trigger(self.cal.session_first_minute(x)) + for x in expected.keys() } assert expected == results @@ -133,7 +133,8 @@ def test_edge_cases_for_TradingDayOfWeek(self): } results = { - x: rule.should_trigger(self.cal.next_open(T(x))) for x in expected.keys() + x: rule.should_trigger(self.cal.session_first_minute(x)) + for x in expected.keys() } assert expected == results @@ -154,8 +155,9 @@ def test_week_and_time_composed_rule(self, rule_type): should_trigger = composed_rule.should_trigger - week_minutes = self.cal.minutes_for_sessions_in_range( - pd.Timestamp("2014-01-06", tz="UTC"), pd.Timestamp("2014-01-10", tz="UTC") + week_minutes = self.cal.sessions_minutes( + pd.Timestamp("2014-01-06"), + pd.Timestamp("2014-01-10"), ) dt = pd.Timestamp("2014-01-06 14:30:00", tz="UTC") diff --git a/tests/finance/test_commissions.py b/tests/finance/test_commissions.py index 5aa3e33e2d..fc0f28aee8 100644 --- a/tests/finance/test_commissions.py +++ b/tests/finance/test_commissions.py @@ -26,8 +26,8 @@ def set_test_commission_unit(request, with_asset_finder): ASSET_FINDER_COUNTRY_CODE = "??" - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-12-29", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-12-29") equities = pd.DataFrame.from_dict( { diff --git a/tests/finance/test_risk.py b/tests/finance/test_risk.py index 5fd480f9a3..af9fcfa4d5 100644 --- a/tests/finance/test_risk.py +++ b/tests/finance/test_risk.py @@ -41,9 +41,9 @@ @pytest.fixture(scope="class") def set_test_risk(request, set_trading_calendar): request.cls.trading_calendar = set_trading_calendar - request.cls.start_session = pd.Timestamp("2006-01-01", tz="UTC") - request.cls.end_session = request.cls.trading_calendar.minute_to_session_label( - pd.Timestamp("2006-12-31", tz="UTC"), direction="previous" + request.cls.start_session = pd.Timestamp("2006-01-01") + request.cls.end_session = request.cls.trading_calendar.minute_to_session( + pd.Timestamp("2006-12-31"), direction="previous" ) request.cls.sim_params = SimulationParameters( start_session=request.cls.start_session, @@ -68,14 +68,14 @@ class TestRisk: def test_factory(self): returns = [0.1] * 100 r_objects = factory.create_returns_from_list(returns, self.sim_params) - assert r_objects.index[-1] <= pd.Timestamp("2006-12-31", tz="UTC") + assert r_objects.index[-1] <= pd.Timestamp("2006-12-31") def test_drawdown(self): for period in PERIODS: assert all(x["max_drawdown"] == 0 for x in self.metrics[period]) def test_benchmark_returns_06(self): - for period, period_len in zip(PERIODS, [1, 3, 6, 12]): + for period, _period_len in zip(PERIODS, [1, 3, 6, 12]): np.testing.assert_almost_equal( [x["benchmark_period_return"] for x in self.metrics[period]], [ @@ -185,12 +185,10 @@ def test_treasury_returns(self): ] * len(metrics[period]) def test_benchmarkrange(self): - start_session = self.trading_calendar.minute_to_session_label( - pd.Timestamp("2008-01-01", tz="UTC") - ) + start_session = pd.Timestamp("2008-01-01") - end_session = self.trading_calendar.minute_to_session_label( - pd.Timestamp("2010-01-01", tz="UTC"), direction="previous" + end_session = self.trading_calendar.minute_to_session( + pd.Timestamp("2010-01-01"), direction="previous" ) sim_params = SimulationParameters( @@ -211,8 +209,8 @@ def test_benchmarkrange(self): def test_partial_month(self): - start_session = self.trading_calendar.minute_to_session_label( - pd.Timestamp("1993-02-01", tz="UTC") + start_session = self.trading_calendar.minute_to_session( + pd.Timestamp("1993-02-01") ) # 1992 and 1996 were leap years diff --git a/tests/finance/test_slippage.py b/tests/finance/test_slippage.py index 5de7c97bef..8e54fd5a48 100644 --- a/tests/finance/test_slippage.py +++ b/tests/finance/test_slippage.py @@ -13,9 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" -Unit tests for finance.slippage -""" +"""Unit tests for finance.slippage""" + from collections import namedtuple import datetime from math import sqrt @@ -52,7 +51,6 @@ ZiplineTestCase, ) from zipline.utils.classproperty import classproperty -from zipline.utils.pandas_utils import normalize_date import pytest import re @@ -69,8 +67,8 @@ class SlippageTestCase( SIM_PARAMS_EMISSION_RATE = "daily" ASSET_FINDER_EQUITY_SIDS = (133,) - ASSET_FINDER_EQUITY_START_DATE = pd.Timestamp("2006-01-05", tz="utc") - ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2006-01-07", tz="utc") + ASSET_FINDER_EQUITY_START_DATE = pd.Timestamp("2006-01-05") + ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2006-01-07") minutes = pd.date_range( start=START_DATE, end=END_DATE - pd.Timedelta("1 minute"), freq="1min" ) @@ -639,8 +637,8 @@ class VolumeShareSlippageTestCase( SIM_PARAMS_EMISSION_RATE = "daily" ASSET_FINDER_EQUITY_SIDS = (133,) - ASSET_FINDER_EQUITY_START_DATE = pd.Timestamp("2006-01-05", tz="utc") - ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2006-01-07", tz="utc") + ASSET_FINDER_EQUITY_START_DATE = pd.Timestamp("2006-01-05") + ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2006-01-07") minutes = pd.date_range( start=START_DATE, end=END_DATE - pd.Timedelta("1 minute"), freq="1min" ) @@ -807,7 +805,7 @@ def test_volume_share_slippage_with_future(self): class VolatilityVolumeShareTestCase( WithCreateBarData, WithSimParams, WithDataPortal, ZiplineTestCase ): - ASSET_START_DATE = pd.Timestamp("2006-02-10", tz="utc") + ASSET_START_DATE = pd.Timestamp("2006-02-10") TRADING_CALENDAR_STRS = ("NYSE", "us_futures") TRADING_CALENDAR_PRIMARY_CAL = "us_futures" @@ -841,7 +839,8 @@ def make_future_minute_bar_data(cls): ) # Make the first month's worth of data NaN to simulate cases where a # futures contract does not exist yet. - data[0][1].loc[: cls.ASSET_START_DATE] = np.NaN + asset_start_date = cls.ASSET_START_DATE.tz_localize(data[0][1].index.tzinfo) + data[0][1].loc[:asset_start_date] = np.NaN return data def test_calculate_impact_buy(self): @@ -964,7 +963,7 @@ class MarketImpactTestCase(WithCreateBarData, ZiplineTestCase): def make_equity_minute_bar_data(cls): trading_calendar = cls.trading_calendars[Equity] return create_minute_bar_data( - trading_calendar.minutes_for_sessions_in_range( + trading_calendar.sessions_minutes( cls.equity_minute_bar_days[0], cls.equity_minute_bar_days[-1], ), @@ -973,7 +972,7 @@ def make_equity_minute_bar_data(cls): def test_window_data(self): session = pd.Timestamp("2006-03-01") - minute = self.trading_calendar.minutes_for_session(session)[1] + minute = self.trading_calendar.session_minutes(session)[1] data = self.create_bardata(simulation_dt_func=lambda: minute) asset = self.asset_finder.retrieve_asset(1) @@ -1016,8 +1015,8 @@ def test_window_data(self): class OrdersStopTestCase( WithSimParams, WithAssetFinder, WithTradingCalendars, ZiplineTestCase ): - START_DATE = pd.Timestamp("2006-01-05 14:31", tz="utc") - END_DATE = pd.Timestamp("2006-01-05 14:36", tz="utc") + START_DATE = pd.Timestamp("2006-01-05 14:31") + END_DATE = pd.Timestamp("2006-01-05 14:36") SIM_PARAMS_CAPITAL_BASE = 1.0e5 SIM_PARAMS_DATA_FREQUENCY = "minute" SIM_PARAMS_EMISSION_RATE = "daily" @@ -1173,7 +1172,7 @@ def test_orders_stop(self, name, order_data, event_data, expected): ), ) days = pd.date_range( - start=normalize_date(self.minutes[0]), end=normalize_date(self.minutes[-1]) + start=self.minutes[0].normalize(), end=self.minutes[-1].normalize() ) with tmp_bcolz_equity_minute_bar_reader( self.trading_calendar, days, assets @@ -1217,8 +1216,8 @@ def test_orders_stop(self, name, order_data, event_data, expected): class FixedBasisPointsSlippageTestCase(WithCreateBarData, ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-05", tz="utc") - END_DATE = pd.Timestamp("2006-01-05", tz="utc") + START_DATE = pd.Timestamp("2006-01-05") + END_DATE = pd.Timestamp("2006-01-05") ASSET_FINDER_EQUITY_SIDS = (133,) diff --git a/tests/history/generate_csvs.py b/tests/history/generate_csvs.py index a06195d6d0..df7ac73eff 100644 --- a/tests/history/generate_csvs.py +++ b/tests/history/generate_csvs.py @@ -95,7 +95,7 @@ def generate_minute_test_data( minutes_count = len(full_minutes) cal = get_calendar("XNYS") - minutes = cal.minutes_for_sessions_in_range(first_day, last_day) + minutes = cal.sessions_minutes(first_day, last_day) o = np.zeros(minutes_count, dtype=np.uint32) h = np.zeros(minutes_count, dtype=np.uint32) diff --git a/tests/metrics/test_metrics.py b/tests/metrics/test_metrics.py index d0ddba2401..f945874006 100644 --- a/tests/metrics/test_metrics.py +++ b/tests/metrics/test_metrics.py @@ -21,8 +21,8 @@ import pytest -def T(cs): - return pd.Timestamp(cs, tz="utc") +def ts_utc(cs): + return pd.Timestamp(cs, tz="UTC") def portfolio_snapshot(p): @@ -64,8 +64,8 @@ class TestConstantPrice( FUTURE_MINUTE_CONSTANT_HIGH = 1.0 FUTURE_MINUTE_CONSTANT_VOLUME = 100.0 - START_DATE = T("2014-01-06") - END_DATE = T("2014-01-10") + START_DATE = pd.Timestamp("2014-01-06") + END_DATE = pd.Timestamp("2014-01-10") # note: class attributes after this do not configure fixtures, they are # just used in this test suite @@ -75,9 +75,7 @@ class TestConstantPrice( future_contract_multiplier = 2 # this is the expected exposure for a position of one contract - future_constant_exposure = ( - FUTURE_MINUTE_CONSTANT_CLOSE * future_contract_multiplier - ) + future_constant_exposure = FUTURE_MINUTE_CONSTANT_CLOSE * future_contract_multiplier @classmethod def make_futures_info(cls): @@ -100,16 +98,13 @@ def init_class_fixtures(cls): ) cls.trading_minutes = pd.Index( - cls.trading_calendar.minutes_for_sessions_in_range( + cls.trading_calendar.sessions_minutes( cls.START_DATE, cls.END_DATE, ), ) cls.closes = pd.Index( - cls.trading_calendar.session_closes_in_range( - cls.START_DATE, - cls.END_DATE, - ), + cls.trading_calendar.closes[cls.START_DATE : cls.END_DATE] ) cls.closes.name = None @@ -178,9 +173,7 @@ def test_nop(self): # produces the expected values when queried mid-simulation check_portfolio_during_simulation=[True, False], ) - def test_equity_slippage( - self, direction, check_portfolio_during_simulation - ): + def test_equity_slippage(self, direction, check_portfolio_during_simulation): if direction not in ("long", "short"): raise ValueError( "direction must be either long or short, got: %r" % direction, @@ -291,8 +284,7 @@ def handle_data(context, data): ) first_day_capital_used = -( - shares * self.EQUITY_MINUTE_CONSTANT_CLOSE - + abs(per_fill_slippage.sum()) + shares * self.EQUITY_MINUTE_CONSTANT_CLOSE + abs(per_fill_slippage.sum()) ) expected_capital_used = pd.Series(0.0, index=self.closes) expected_capital_used.iloc[0] = first_day_capital_used @@ -357,9 +349,7 @@ def handle_data(context, data): # produces the expected values when queried mid-simulation check_portfolio_during_simulation=[True, False], ) - def test_equity_commissions( - self, direction, check_portfolio_during_simulation - ): + def test_equity_commissions(self, direction, check_portfolio_during_simulation): if direction not in ("long", "short"): raise ValueError( "direction must be either long or short, got: %r" % direction, @@ -478,8 +468,7 @@ def handle_data(context, data): ) first_day_capital_used = -( - shares * self.EQUITY_MINUTE_CONSTANT_CLOSE - + per_fill_commission.sum() + shares * self.EQUITY_MINUTE_CONSTANT_CLOSE + per_fill_commission.sum() ) expected_capital_used = pd.Series(0.0, index=self.closes) expected_capital_used.iloc[0] = first_day_capital_used @@ -546,12 +535,10 @@ def handle_data(context, data): # produces the expected values when queried mid-simulation check_portfolio_during_simulation=[True, False], ) - def test_equity_single_position( - self, direction, check_portfolio_during_simulation - ): + def test_equity_single_position(self, direction, check_portfolio_during_simulation): if direction not in ("long", "short"): raise ValueError( - "direction must be either long or short, got: %r" % direction, + f"direction must be either long or short, got: {direction!r}" ) shares = 1 if direction == "long" else -1 @@ -816,8 +803,8 @@ def handle_data(context, data): expected_single_order = { "amount": shares, "commission": 0.0, - "created": T("2014-01-06 14:31"), - "dt": T("2014-01-06 14:32"), + "created": ts_utc("2014-01-06 14:31"), + "dt": ts_utc("2014-01-06 14:32"), "filled": shares, "id": wildcard, "limit": None, @@ -830,9 +817,7 @@ def handle_data(context, data): } # we only order on the first day - expected_orders = [[expected_single_order]] + [[]] * ( - len(self.closes) - 1 - ) + expected_orders = [[expected_single_order]] + [[]] * (len(self.closes) - 1) assert_equal( orders.tolist(), @@ -850,7 +835,7 @@ def handle_data(context, data): expected_single_transaction = { "amount": shares, "commission": None, - "dt": T("2014-01-06 14:32"), + "dt": ts_utc("2014-01-06 14:32"), "order_id": wildcard, "price": 1.0, "sid": self.equity, @@ -962,9 +947,7 @@ def handle_data(context, data): # produces the expected values when queried mid-simulation check_portfolio_during_simulation=[True, False], ) - def test_future_single_position( - self, direction, check_portfolio_during_simulation - ): + def test_future_single_position(self, direction, check_portfolio_during_simulation): if direction not in ("long", "short"): raise ValueError( "direction must be either long or short, got: %r" % direction, @@ -1117,9 +1100,7 @@ def handle_data(context, data): # gross market exposure is # sum(long_exposure) + sum(abs(short_exposure)) # current notional capital is the current portfolio value - expected_max_leverage = ( - self.future_constant_exposure / capital_base_series - ) + expected_max_leverage = self.future_constant_exposure / capital_base_series assert_equal( perf["max_leverage"], expected_max_leverage, @@ -1203,8 +1184,8 @@ def handle_data(context, data): { "amount": contracts, "commission": 0.0, - "created": T("2014-01-06 14:31"), - "dt": T("2014-01-06 14:32"), + "created": ts_utc("2014-01-06 14:31"), + "dt": ts_utc("2014-01-06 14:32"), "filled": contracts, "id": wildcard, "limit": None, @@ -1238,7 +1219,7 @@ def handle_data(context, data): { "amount": contracts, "commission": None, - "dt": T("2014-01-06 14:32"), + "dt": ts_utc("2014-01-06 14:32"), "order_id": wildcard, "price": 1.0, "sid": self.future, @@ -1326,8 +1307,8 @@ class TestFixedReturns(WithMakeAlgo, ZiplineTestCase): EQUITY_DAILY_BAR_SOURCE_FROM_MINUTE = True FUTURE_DAILY_BAR_SOURCE_FROM_MINUTE = True - START_DATE = T("2014-01-06") - END_DATE = T("2014-01-10") + START_DATE = pd.Timestamp("2014-01-06") + END_DATE = pd.Timestamp("2014-01-10") # note: class attributes after this do not configure fixtures, they are # just used in this test suite @@ -1363,44 +1344,39 @@ def init_class_fixtures(cls): ) cls.equity_minutes = pd.Index( - cls.trading_calendars[Equity].minutes_for_sessions_in_range( + cls.trading_calendars[Equity].sessions_minutes( cls.START_DATE, cls.END_DATE, ), ) cls.equity_closes = pd.Index( - cls.trading_calendars[Equity].session_closes_in_range( - cls.START_DATE, - cls.END_DATE, - ), + cls.trading_calendars[Equity].closes[cls.START_DATE : cls.END_DATE] ) cls.equity_closes.name = None futures_cal = cls.trading_calendars[Future] cls.future_minutes = pd.Index( futures_cal.execution_minutes_for_sessions_in_range( - cls.START_DATE, - cls.END_DATE, - ), + cls.START_DATE, cls.END_DATE + ) ) cls.future_closes = pd.Index( futures_cal.execution_time_from_close( - futures_cal.session_closes_in_range( - cls.START_DATE, - cls.END_DATE, - ), + futures_cal.closes[cls.START_DATE : cls.END_DATE] ), ) cls.future_closes.name = None - cls.future_opens = pd.Index( - futures_cal.execution_time_from_open( - futures_cal.session_opens_in_range( - cls.START_DATE, - cls.END_DATE, - ), - ), - ) + if futures_cal.name == "us_futures": + cls.future_opens = pd.Index( + futures_cal.execution_time_from_open( + futures_cal.first_minutes[cls.START_DATE : cls.END_DATE] + ) + ) + else: + cls.future_opens = pd.Index( + futures_cal.first_minutes[cls.START_DATE, cls.END_DATE] + ) cls.future_opens.name = None def init_instance_fixtures(self): @@ -1431,9 +1407,7 @@ def init_instance_fixtures(self): else None ), adjustment_reader=( - self.adjustment_reader - if self.DATA_PORTAL_USE_ADJUSTMENTS - else None + self.adjustment_reader if self.DATA_PORTAL_USE_ADJUSTMENTS else None ), future_minute_reader=( self.bcolz_future_minute_bar_reader @@ -1450,12 +1424,8 @@ def init_instance_fixtures(self): ), last_available_session=self.DATA_PORTAL_LAST_AVAILABLE_SESSION, last_available_minute=self.DATA_PORTAL_LAST_AVAILABLE_MINUTE, - minute_history_prefetch_length=( - self.DATA_PORTAL_MINUTE_HISTORY_PREFETCH - ), - daily_history_prefetch_length=( - self.DATA_PORTAL_DAILY_HISTORY_PREFETCH - ), + minute_history_prefetch_length=(self.DATA_PORTAL_MINUTE_HISTORY_PREFETCH), + daily_history_prefetch_length=(self.DATA_PORTAL_DAILY_HISTORY_PREFETCH), ) @classmethod @@ -1483,7 +1453,7 @@ def _make_minute_bar_data(cls, calendar, sids): l, c, cls.asset_daily_volume, - trading_minutes=len(calendar.minutes_for_session(session)), + trading_minutes=len(calendar.session_minutes(session)), random_state=random_state, ) for o, h, l, c, session in zip( @@ -1496,7 +1466,7 @@ def _make_minute_bar_data(cls, calendar, sids): ], ignore_index=True, ) - data.index = calendar.minutes_for_sessions_in_range( + data.index = calendar.sessions_minutes( cls.START_DATE, cls.END_DATE, ) @@ -1526,12 +1496,10 @@ def make_future_minute_bar_data(cls): # produces the expected values when queried mid-simulation check_portfolio_during_simulation=[True, False], ) - def test_equity_single_position( - self, direction, check_portfolio_during_simulation - ): + def test_equity_single_position(self, direction, check_portfolio_during_simulation): if direction not in ("long", "short"): raise ValueError( - "direction must be either long or short, got: %r" % direction, + f"direction must be either long or short, got: {direction!r}" ) shares = 1 if direction == "long" else -1 @@ -1765,8 +1733,8 @@ def handle_data(context, data): expected_single_order = { "amount": shares, "commission": 0.0, - "created": T("2014-01-06 14:31"), - "dt": T("2014-01-06 14:32"), + "created": ts_utc("2014-01-06 14:31"), + "dt": ts_utc("2014-01-06 14:32"), "filled": shares, "id": wildcard, "limit": None, @@ -1799,12 +1767,12 @@ def handle_data(context, data): expected_single_transaction = { "amount": shares, "commission": None, - "dt": T("2014-01-06 14:32"), + "dt": ts_utc("2014-01-06 14:32"), "order_id": wildcard, "price": self.data_portal.get_scalar_asset_spot_value( self.equity, "close", - T("2014-01-06 14:32"), + ts_utc("2014-01-06 14:32"), "minute", ), "sid": self.equity, @@ -1887,8 +1855,7 @@ def handle_data(context, data): ) expected_returns = ( - portfolio_snapshots["portfolio_value"] - / self.SIM_PARAMS_CAPITAL_BASE + portfolio_snapshots["portfolio_value"] / self.SIM_PARAMS_CAPITAL_BASE ) - 1 assert_equal( portfolio_snapshots["returns"], @@ -1919,9 +1886,7 @@ def handle_data(context, data): # produces the expected values when queried mid-simulation check_portfolio_during_simulation=[True, False], ) - def test_future_single_position( - self, direction, check_portfolio_during_simulation - ): + def test_future_single_position(self, direction, check_portfolio_during_simulation): if direction not in ("long", "short"): raise ValueError( "direction must be either long or short, got: %r" % direction, @@ -1953,10 +1918,7 @@ def test_future_single_position( future_execution_open_prices = pd.Series( [ self.futures_data_portal.get_scalar_asset_spot_value( - self.future, - "close", - execution_open_minute, - "minute", + self.future, "close", execution_open_minute, "minute" ) for execution_open_minute in self.future_opens ], @@ -1965,7 +1927,6 @@ def test_future_single_position( def initialize(context): api.set_benchmark(self.equity) - api.set_slippage(us_futures=api.slippage.NoSlippage()) api.set_commission(us_futures=api.commission.NoCommission()) @@ -2080,10 +2041,7 @@ def handle_data(context, data): delta = -future_execution_close_prices + expected_fill_price expected_portfolio_value = pd.Series( - ( - self.SIM_PARAMS_CAPITAL_BASE - + self.future_contract_multiplier * delta - ), + (self.SIM_PARAMS_CAPITAL_BASE + self.future_contract_multiplier * delta), index=self.future_closes, ) @@ -2286,9 +2244,7 @@ def handle_data(context, data): check_names=False, ) - all_minutes = self.trading_calendars[ - Future - ].minutes_for_sessions_in_range( + all_minutes = self.trading_calendars[Future].sessions_minutes( self.START_DATE, self.END_DATE, ) @@ -2330,8 +2286,7 @@ def handle_data(context, data): ) expected_returns = ( - portfolio_snapshots["portfolio_value"] - / self.SIM_PARAMS_CAPITAL_BASE + portfolio_snapshots["portfolio_value"] / self.SIM_PARAMS_CAPITAL_BASE ) - 1 assert_equal( portfolio_snapshots["returns"], diff --git a/tests/pipeline/base.py b/tests/pipeline/base.py index 935b6d6c26..8f890d396c 100644 --- a/tests/pipeline/base.py +++ b/tests/pipeline/base.py @@ -54,8 +54,8 @@ def method(self, *args, **kwargs): class BaseUSEquityPipelineTestCase( WithTradingSessions, WithAssetFinder, ZiplineTestCase ): - START_DATE = pd.Timestamp("2014", tz="UTC") - END_DATE = pd.Timestamp("2014-12-31", tz="UTC") + START_DATE = pd.Timestamp("2014") + END_DATE = pd.Timestamp("2014-12-31") ASSET_FINDER_EQUITY_SIDS = list(range(20)) @classmethod diff --git a/tests/pipeline/test_domain.py b/tests/pipeline/test_domain.py index ae278e0844..60c52d9606 100644 --- a/tests/pipeline/test_domain.py +++ b/tests/pipeline/test_domain.py @@ -137,8 +137,8 @@ def create(cls, column, window_length): class MixedGenericsTestCase(zf.WithSeededRandomPipelineEngine, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2014-01-02", tz="utc") - END_DATE = pd.Timestamp("2014-01-31", tz="utc") + START_DATE = pd.Timestamp("2014-01-02") + END_DATE = pd.Timestamp("2014-01-31") ASSET_FINDER_EQUITY_SIDS = (1, 2, 3, 4, 5) ASSET_FINDER_COUNTRY_CODE = "US" @@ -396,7 +396,7 @@ def test_generic(self): def _test_equity_calendar_domain( self, domain, expected_cutoff_time, expected_cutoff_date_offset=0 ): - sessions = pd.DatetimeIndex(domain.calendar.all_sessions[:50]) + sessions = domain.calendar.sessions[:50] expected = days_at_time( sessions, @@ -462,48 +462,49 @@ def test_equity_calendar_domain(self): @pytest.mark.parametrize("domain", BUILT_IN_DOMAINS) def test_equity_calendar_not_aligned(self, domain): - valid_sessions = domain.all_sessions()[:50] + valid_sessions = domain.sessions()[:50] sessions = pd.date_range(valid_sessions[0], valid_sessions[-1]) invalid_sessions = sessions[~sessions.isin(valid_sessions)] assert len(invalid_sessions) > 1, "There must be at least one invalid session." expected_msg = ( "cannot resolve data query time for sessions that are not on the" - " %s calendar:\n%s" - ) % (domain.calendar.name, invalid_sessions) + f" {domain.calendar.name} calendar:\n{invalid_sessions}" + ) + with pytest.raises(ValueError, match=re.escape(expected_msg)): domain.data_query_cutoff_for_sessions(sessions) - Case = namedtuple("Case", "time date_offset expected_timedelta") + CASE = namedtuple("Case", "time date_offset expected_timedelta") @pytest.mark.parametrize( "parameters", ( - Case( + CASE( time=datetime.time(8, 45, tzinfo=pytz.utc), date_offset=0, expected_timedelta=datetime.timedelta(hours=8, minutes=45), ), - Case( + CASE( time=datetime.time(5, 0, tzinfo=pytz.utc), date_offset=0, expected_timedelta=datetime.timedelta(hours=5), ), - Case( + CASE( time=datetime.time(8, 45, tzinfo=pytz.timezone("Asia/Tokyo")), date_offset=0, # We should get 11:45 UTC, which is 8:45 in Tokyo time, # because Tokyo is 9 hours ahead of UTC. expected_timedelta=-datetime.timedelta(minutes=15), ), - Case( + CASE( time=datetime.time(23, 30, tzinfo=pytz.utc), date_offset=-1, # 23:30 on the previous day should be equivalent to rolling back by # 30 minutes. expected_timedelta=-datetime.timedelta(minutes=30), ), - Case( + CASE( time=datetime.time(23, 30, tzinfo=pytz.timezone("US/Eastern")), date_offset=-1, # 23:30 on the previous day in US/Eastern is equivalent to rolling @@ -521,10 +522,9 @@ def test_equity_calendar_not_aligned(self, domain): def test_equity_session_domain(self, parameters): time, date_offset, expected_timedelta = parameters naive_sessions = pd.date_range("2000-01-01", "2000-06-01") - utc_sessions = naive_sessions.tz_localize("UTC") domain = EquitySessionDomain( - utc_sessions, + naive_sessions, CountryCode.UNITED_STATES, data_query_time=time, data_query_date_offset=date_offset, @@ -534,7 +534,7 @@ def test_equity_session_domain(self, parameters): # crashes when adding a tz-aware DatetimeIndex and a # TimedeltaIndex. :sadpanda:. expected = (naive_sessions + expected_timedelta).tz_localize("utc") - actual = domain.data_query_cutoff_for_sessions(utc_sessions) + actual = domain.data_query_cutoff_for_sessions(naive_sessions) assert_equal(expected, actual) @@ -547,19 +547,13 @@ def test_roll_forward(self): # the first three days of the year are holidays on the Tokyo exchange, # so the first trading day should be the fourth - assert JP_EQUITIES.roll_forward("2017-01-01") == pd.Timestamp( - "2017-01-04", tz="UTC" - ) + assert JP_EQUITIES.roll_forward("2017-01-01") == pd.Timestamp("2017-01-04") # in US exchanges, the first trading day after 1/1 is the 3rd - assert US_EQUITIES.roll_forward("2017-01-01") == pd.Timestamp( - "2017-01-03", tz="UTC" - ) + assert US_EQUITIES.roll_forward("2017-01-01") == pd.Timestamp("2017-01-03") # passing a valid trading day to roll_forward should return that day - assert JP_EQUITIES.roll_forward("2017-01-04") == pd.Timestamp( - "2017-01-04", tz="UTC" - ) + assert JP_EQUITIES.roll_forward("2017-01-04") == pd.Timestamp("2017-01-04") # passing a date before the first session should return the # first session @@ -586,18 +580,13 @@ def test_roll_forward(self): # test that a roll_forward works with an EquitySessionDomain, # not just calendar domains sessions = pd.DatetimeIndex( - ["2000-01-01", "2000-02-01", "2000-04-01", "2000-06-01"], tz="UTC" + ["2000-01-01", "2000-02-01", "2000-04-01", "2000-06-01"] ) session_domain = EquitySessionDomain(sessions, CountryCode.UNITED_STATES) - assert session_domain.roll_forward("2000-02-01") == pd.Timestamp( - "2000-02-01", tz="UTC" - ) - - assert session_domain.roll_forward("2000-02-02") == pd.Timestamp( - "2000-04-01", tz="UTC" - ) + assert session_domain.roll_forward("2000-02-01") == pd.Timestamp("2000-02-01") + assert session_domain.roll_forward("2000-02-02") == pd.Timestamp("2000-04-01") class TestRepr: diff --git a/tests/pipeline/test_downsampling.py b/tests/pipeline/test_downsampling.py index 619b3aa1fd..abee184102 100644 --- a/tests/pipeline/test_downsampling.py +++ b/tests/pipeline/test_downsampling.py @@ -60,8 +60,8 @@ def compute(self, today, assets, out, cats): class ComputeExtraRowsTestCase(WithTradingSessions, ZiplineTestCase): - DATA_MIN_DAY = pd.Timestamp("2012-06", tz="UTC") - DATA_MAX_DAY = pd.Timestamp("2015", tz="UTC") + DATA_MIN_DAY = pd.Timestamp("2012-06") + DATA_MAX_DAY = pd.Timestamp("2015") TRADING_CALENDAR_STRS = ("NYSE", "LSE", "TSX") # Test with different window_lengths to ensure that window length is not @@ -582,10 +582,10 @@ def check_extra_row_calculations( class DownsampledPipelineTestCase(WithSeededRandomPipelineEngine, ZiplineTestCase): # Extend into the last few days of 2013 to test year/quarter boundaries. - START_DATE = pd.Timestamp("2013-12-15", tz="UTC") + START_DATE = pd.Timestamp("2013-12-15") # Extend into the first few days of 2015 to test year/quarter boundaries. - END_DATE = pd.Timestamp("2015-01-06", tz="UTC") + END_DATE = pd.Timestamp("2015-01-06") ASSET_FINDER_EQUITY_SIDS = tuple(range(10)) DOMAIN = US_EQUITIES @@ -600,7 +600,7 @@ def SEEDED_RANDOM_PIPELINE_DEFAULT_DOMAIN(cls): @classproperty def all_sessions(cls): - return cls.DOMAIN.all_sessions() + return cls.DOMAIN.sessions() def check_downsampled_term(self, term): @@ -631,8 +631,8 @@ def check_downsampled_term(self, term): # target period. raw_term_results = self.run_pipeline( Pipeline({"term": term}), - start_date=pd.Timestamp("2014-01-02", tz="UTC"), - end_date=pd.Timestamp("2015-01-06", tz="UTC"), + start_date=pd.Timestamp("2014-01-02"), + end_date=pd.Timestamp("2015-01-06"), )["term"].unstack() expected_results = { @@ -736,10 +736,9 @@ class DownsampledCAPipelineTestCase(DownsampledPipelineTestCase): class TestDownsampledRowwiseOperation(WithAssetFinder, ZiplineTestCase): - T = partial(pd.Timestamp, tz="utc") - START_DATE = T("2014-01-01") - END_DATE = T("2014-02-01") - HALF_WAY_POINT = T("2014-01-15") + START_DATE = pd.Timestamp("2014-01-01") + END_DATE = pd.Timestamp("2014-02-01") + HALF_WAY_POINT = pd.Timestamp("2014-01-15") dates = pd.date_range(START_DATE, END_DATE) diff --git a/tests/pipeline/test_engine.py b/tests/pipeline/test_engine.py index e433e9d559..6327e6ac3c 100644 --- a/tests/pipeline/test_engine.py +++ b/tests/pipeline/test_engine.py @@ -162,8 +162,8 @@ def compute(self, today, assets, out, *inputs): class WithConstantInputs(zf.WithAssetFinder): asset_ids = ASSET_FINDER_EQUITY_SIDS = 1, 2, 3, 4 - START_DATE = pd.Timestamp("2014-01-01", tz="utc") - END_DATE = pd.Timestamp("2014-03-01", tz="utc") + START_DATE = pd.Timestamp("2014-01-01") + END_DATE = pd.Timestamp("2014-03-01") ASSET_FINDER_COUNTRY_CODE = "US" @classmethod @@ -187,7 +187,6 @@ def init_class_fixtures(cls): cls.START_DATE, cls.END_DATE, freq="D", - tz="UTC", ) cls.loader = PrecomputedLoader( constants=cls.constants, @@ -786,8 +785,8 @@ class FrameInputTestCase( zf.WithAssetFinder, zf.WithTradingCalendars, zf.ZiplineTestCase ): asset_ids = ASSET_FINDER_EQUITY_SIDS = range(HUGE_SID, HUGE_SID + 3) - start = START_DATE = pd.Timestamp("2015-01-01", tz="utc") - end = END_DATE = pd.Timestamp("2015-01-31", tz="utc") + start = START_DATE = pd.Timestamp("2015-01-01") + end = END_DATE = pd.Timestamp("2015-01-31") ASSET_FINDER_COUNTRY_CODE = "US" @classmethod @@ -797,7 +796,6 @@ def init_class_fixtures(cls): cls.start, cls.end, freq=cls.trading_calendar.day, - tz="UTC", ) cls.assets = cls.asset_finder.retrieve_all(cls.asset_ids) cls.domain = US_EQUITIES @@ -896,9 +894,9 @@ def apply_date(idx, offset=0): class SyntheticBcolzTestCase( zf.WithAdjustmentReader, zf.WithAssetFinder, zf.ZiplineTestCase ): - first_asset_start = pd.Timestamp("2015-04-01", tz="UTC") - START_DATE = pd.Timestamp("2015-01-01", tz="utc") - END_DATE = pd.Timestamp("2015-08-01", tz="utc") + first_asset_start = pd.Timestamp("2015-04-01") + START_DATE = pd.Timestamp("2015-01-01") + END_DATE = pd.Timestamp("2015-08-01") @classmethod def make_equity_info(cls): @@ -1060,8 +1058,8 @@ class ParameterizedFactorTestCase( zf.WithAssetFinder, zf.WithTradingCalendars, zf.ZiplineTestCase ): sids = ASSET_FINDER_EQUITY_SIDS = pd.Index([1, 2, 3], dtype="int64") - START_DATE = pd.Timestamp("2015-01-31", tz="UTC") - END_DATE = pd.Timestamp("2015-03-01", tz="UTC") + START_DATE = pd.Timestamp("2015-01-31") + END_DATE = pd.Timestamp("2015-03-01") ASSET_FINDER_COUNTRY_CODE = "??" @classmethod @@ -1481,8 +1479,8 @@ def dispatcher(c): class ChunkedPipelineTestCase(zf.WithSeededRandomPipelineEngine, zf.ZiplineTestCase): - PIPELINE_START_DATE = pd.Timestamp("2006-01-05", tz="UTC") - END_DATE = pd.Timestamp("2006-12-29", tz="UTC") + PIPELINE_START_DATE = pd.Timestamp("2006-01-05") + END_DATE = pd.Timestamp("2006-12-29") ASSET_FINDER_COUNTRY_CODE = "US" def test_run_chunked_pipeline(self): diff --git a/tests/pipeline/test_events.py b/tests/pipeline/test_events.py index 7edfb60035..b408e35c54 100644 --- a/tests/pipeline/test_events.py +++ b/tests/pipeline/test_events.py @@ -221,7 +221,7 @@ def test_next_event_indexer(self): event_dates = events["event_date"].to_numpy() event_timestamps = events["timestamp"].to_numpy() - all_dates = pd.date_range("2014", "2014-01-31", tz="UTC") + all_dates = pd.date_range("2014", "2014-01-31") all_sids = np.unique(event_sids) domain = EquitySessionDomain( @@ -253,8 +253,9 @@ def check_next_event_indexer(self, events, all_dates, sid, indexer): assert len(relevant_events) == 2 ix1, ix2 = relevant_events.index - e1, e2 = relevant_events["event_date"].dt.tz_localize("UTC") - t1, t2 = relevant_events["timestamp"].dt.tz_localize("UTC") + + e1, e2 = relevant_events["event_date"] + t1, t2 = relevant_events["timestamp"] for date, computed_index in zip(all_dates, indexer): # An event is eligible to be the next event if it's between the @@ -272,8 +273,8 @@ def check_next_event_indexer(self, events, all_dates, sid, indexer): class EventsLoaderEmptyTestCase(WithAssetFinder, WithTradingSessions, ZiplineTestCase): - START_DATE = pd.Timestamp("2014-01-01", tz="utc") - END_DATE = pd.Timestamp("2014-01-30", tz="utc") + START_DATE = pd.Timestamp("2014-01-01") + END_DATE = pd.Timestamp("2014-01-30") ASSET_FINDER_COUNTRY_CODE = "US" @classmethod @@ -355,8 +356,8 @@ def test_load_empty(self): class EventsLoaderTestCase(WithAssetFinder, WithTradingSessions, ZiplineTestCase): - START_DATE = pd.Timestamp("2014-01-01", tz="utc") - END_DATE = pd.Timestamp("2014-01-30", tz="utc") + START_DATE = pd.Timestamp("2014-01-01") + END_DATE = pd.Timestamp("2014-01-30") ASSET_FINDER_COUNTRY_CODE = "US" @classmethod diff --git a/tests/pipeline/test_frameload.py b/tests/pipeline/test_frameload.py index 976e804d39..d0867f5adc 100644 --- a/tests/pipeline/test_frameload.py +++ b/tests/pipeline/test_frameload.py @@ -1,7 +1,7 @@ """ Tests for zipline.pipeline.loaders.frame.DataFrameLoader. """ -from mock import patch +import mock import numpy as np import pandas as pd from numpy.testing import assert_array_equal @@ -227,7 +227,7 @@ def test_adjustments(self): assert formatted_adjustments == expected_formatted_adjustments mask = self.mask[dates_slice, sids_slice] - with patch("zipline.pipeline.loaders.frame.AdjustedArray") as m: + with mock.patch("zipline.pipeline.loaders.frame.AdjustedArray") as m: loader.load_adjusted_array( US_EQUITIES, columns=[USEquityPricing.close], diff --git a/tests/pipeline/test_hooks.py b/tests/pipeline/test_hooks.py index 0dc6391f36..c157485942 100644 --- a/tests/pipeline/test_hooks.py +++ b/tests/pipeline/test_hooks.py @@ -226,8 +226,8 @@ class ProgressHooksTestCase(WithSeededRandomPipelineEngine, ZiplineTestCase): ASSET_FINDER_COUNTRY_CODE = "US" - START_DATE = pd.Timestamp("2014-01-02", tz="UTC") - END_DATE = pd.Timestamp("2014-01-31", tz="UTC") + START_DATE = pd.Timestamp("2014-01-02") + END_DATE = pd.Timestamp("2014-01-31") # Don't populate PREPOPULATED_TERM for days after this cutoff. # This is used to test that we correctly compute progress when the number diff --git a/tests/pipeline/test_international_markets.py b/tests/pipeline/test_international_markets.py index 6b698c2fcd..54133e75d9 100644 --- a/tests/pipeline/test_international_markets.py +++ b/tests/pipeline/test_international_markets.py @@ -1,5 +1,5 @@ -"""Tests for pipelines on international markets. -""" +"""Tests for pipelines on international markets.""" + from itertools import cycle, islice from parameterized import parameterized @@ -33,8 +33,7 @@ def T(s): class WithInternationalDailyBarData(zf.WithAssetFinder): - """ - Fixture for generating international daily bars. + """Fixture for generating international daily bars. Eventually this should be moved into zipline.testing.fixtures and should replace most of the existing machinery @@ -192,8 +191,8 @@ def run_pipeline(self, pipeline, start_date, end_date): class InternationalEquityTestCase( WithInternationalPricingPipelineEngine, zf.ZiplineTestCase ): - START_DATE = T("2014-01-02") - END_DATE = T("2014-02-06") # Chosen to match the asset setup data below. + START_DATE = pd.Timestamp("2014-01-02") + END_DATE = pd.Timestamp("2014-02-06") # Chosen to match the asset setup data below. EXCHANGE_INFO = pd.DataFrame.from_records( [ @@ -476,8 +475,7 @@ def assert_identical_results(self, left, right, start_date, end_date): def alive_in_range(asset, start, end, include_asset_start_date=False): - """ - Check if an asset was alive in the range from start to end. + """Check if an asset was alive in the range from start to end. Parameters ---------- @@ -504,8 +502,7 @@ def alive_in_range(asset, start, end, include_asset_start_date=False): def intervals_overlap(a, b): - """ - Check whether a pair of datetime intervals overlap. + """Check whether a pair of datetime intervals overlap. Parameters ---------- diff --git a/tests/pipeline/test_multidimensional_dataset.py b/tests/pipeline/test_multidimensional_dataset.py index fceb5a3fbb..7b0f04fb5d 100644 --- a/tests/pipeline/test_multidimensional_dataset.py +++ b/tests/pipeline/test_multidimensional_dataset.py @@ -153,9 +153,7 @@ class MD(DataSetFamily): ("M8", np.dtype("M8[ns]"), Slice), ("boolean", np.dtype("?"), Slice), } - actual_columns = { - (c.name, c.dtype, c.dataset) for c in Slice.columns - } + actual_columns = {(c.name, c.dtype, c.dataset) for c in Slice.columns} assert actual_columns == expected_columns # del spec @@ -183,8 +181,7 @@ def expect_slice_fails(*args, **kwargs): expect_slice_fails( "a", expected_msg=( - "no coordinate provided to MD for the following dimension:" - " dim_1" + "no coordinate provided to MD for the following dimension:" " dim_1" ), ) @@ -248,31 +245,23 @@ def expect_slice_fails(*args, **kwargs): expect_slice_fails( "not-in-0", "c", - expected_msg=( - "'not-in-0' is not a value along the dim_0 dimension of MD" - ), + expected_msg=("'not-in-0' is not a value along the dim_0 dimension of MD"), ) expect_slice_fails( dim_0="not-in-0", dim_1="c", - expected_msg=( - "'not-in-0' is not a value along the dim_0 dimension of MD" - ), + expected_msg=("'not-in-0' is not a value along the dim_0 dimension of MD"), ) expect_slice_fails( "a", "not-in-1", - expected_msg=( - "'not-in-1' is not a value along the dim_1 dimension of MD" - ), + expected_msg=("'not-in-1' is not a value along the dim_1 dimension of MD"), ) expect_slice_fails( dim_0="a", dim_1="not-in-1", - expected_msg=( - "'not-in-1' is not a value along the dim_1 dimension of MD" - ), + expected_msg=("'not-in-1' is not a value along the dim_1 dimension of MD"), ) def test_inheritance(self): diff --git a/tests/pipeline/test_pipeline.py b/tests/pipeline/test_pipeline.py index 1ea2f8b296..bb53d37ee7 100644 --- a/tests/pipeline/test_pipeline.py +++ b/tests/pipeline/test_pipeline.py @@ -1,7 +1,6 @@ -""" -Tests for zipline.pipeline.Pipeline -""" -from mock import patch +"""Tests for zipline.pipeline.Pipeline""" + +import mock from zipline.pipeline import Factor, Filter, Pipeline from zipline.pipeline.data import Column, DataSet, USEquityPricing @@ -154,7 +153,7 @@ def mock_display_graph(g, format="svg", include_asset_exists=False): mock_display_graph ), "Mock signature doesn't match signature for display_graph." - patch_display_graph = patch( + patch_display_graph = mock.patch( "zipline.pipeline.graph.display_graph", mock_display_graph, ) diff --git a/tests/pipeline/test_pipeline_algo.py b/tests/pipeline/test_pipeline_algo.py index 1e7702c374..88e1f5024f 100644 --- a/tests/pipeline/test_pipeline_algo.py +++ b/tests/pipeline/test_pipeline_algo.py @@ -37,7 +37,6 @@ WithBcolzEquityDailyBarReaderFromCSVs, ZiplineTestCase, ) -from zipline.utils.pandas_utils import normalize_date import pytest @@ -59,9 +58,9 @@ def rolling_vwap(df, length): class ClosesAndVolumes(WithMakeAlgo, ZiplineTestCase): - START_DATE = pd.Timestamp("2014-01-01", tz="utc") - END_DATE = pd.Timestamp("2014-02-01", tz="utc") - dates = pd.date_range(START_DATE, END_DATE, freq=get_calendar("NYSE").day, tz="utc") + START_DATE = pd.Timestamp("2014-01-01") + END_DATE = pd.Timestamp("2014-02-01") + dates = pd.date_range(START_DATE, END_DATE, freq=get_calendar("NYSE").day) SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False @@ -206,9 +205,7 @@ def exists(self, date, asset): return asset.start_date <= date <= asset.end_date def test_attach_pipeline_after_initialize(self): - """ - Assert that calling attach_pipeline after initialize raises correctly. - """ + """Assert that calling attach_pipeline after initialize raises correctly.""" def initialize(context): pass @@ -238,9 +235,7 @@ def barf(context, data): algo.run() def test_pipeline_output_after_initialize(self): - """ - Assert that calling pipeline_output after initialize raises correctly. - """ + """Assert that calling pipeline_output after initialize raises correctly.""" def initialize(context): attach_pipeline(Pipeline(), "test") @@ -263,9 +258,7 @@ def before_trading_start(context, data): algo.run() def test_get_output_nonexistent_pipeline(self): - """ - Assert that calling add_pipeline after initialize raises appropriately. - """ + """Assert that calling add_pipeline after initialize raises appropriately.""" def initialize(context): attach_pipeline(Pipeline(), "test") @@ -297,8 +290,7 @@ def before_trading_start(context, data): ] ) def test_assets_appear_on_correct_days(self, test_name, chunks): - """ - Assert that assets appear at correct times during a backtest, with + """Assert that assets appear at correct times during a backtest, with correctly-adjusted close price values. """ @@ -324,7 +316,7 @@ def initialize(context): def handle_data(context, data): results = pipeline_output("test") - date = get_datetime().normalize() + date = self.trading_calendar.minute_to_session(get_datetime()) for asset in self.assets: # Assets should appear iff they exist today and yesterday. exists_today = self.exists(date, asset) @@ -347,8 +339,7 @@ def handle_data(context, data): algo.run() def test_multiple_pipelines(self): - """ - Test that we can attach multiple pipelines and access the correct + """Test that we can attach multiple pipelines and access the correct output based on the pipeline name. """ @@ -362,7 +353,7 @@ def initialize(context): def handle_data(context, data): closes = pipeline_output("test_close") volumes = pipeline_output("test_volume") - date = get_datetime().normalize() + date = self.trading_calendar.minute_to_session(get_datetime()) for asset in self.assets: # Assets should appear iff they exist today and yesterday. exists_today = self.exists(date, asset) @@ -392,8 +383,7 @@ def handle_data(context, data): algo.run() def test_duplicate_pipeline_names(self): - """ - Test that we raise an error when we try to attach a pipeline with a + """Test that we raise an error when we try to attach a pipeline with a name that already exists for another attached pipeline. """ @@ -407,9 +397,7 @@ def initialize(context): class MockDailyBarSpotReader: - """ - A BcolzDailyBarReader which returns a constant value for spot price. - """ + """A BcolzDailyBarReader which returns a constant value for spot price.""" def get_value(self, sid, day, column): return 100.0 @@ -426,8 +414,8 @@ class PipelineAlgorithmTestCase( BRK_A = 3 ASSET_FINDER_EQUITY_SIDS = AAPL, MSFT, BRK_A ASSET_FINDER_EQUITY_SYMBOLS = "AAPL", "MSFT", "BRK_A" - START_DATE = pd.Timestamp("2014", tz="UTC") - END_DATE = pd.Timestamp("2015", tz="UTC") + START_DATE = pd.Timestamp("2014") + END_DATE = pd.Timestamp("2015") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False @@ -495,8 +483,8 @@ def init_class_fixtures(cls): cls.bcolz_equity_daily_bar_reader, cls.adjustment_reader, ) - cls.dates = cls.raw_data[cls.AAPL].index.tz_localize("UTC") - cls.AAPL_split_date = pd.Timestamp("2014-06-09", tz="UTC") + cls.dates = cls.raw_data[cls.AAPL].index # .tz_localize("UTC") + cls.AAPL_split_date = pd.Timestamp("2014-06-09") cls.assets = cls.asset_finder.retrieve_all(cls.ASSET_FINDER_EQUITY_SIDS) def make_algo_kwargs(self, **overrides): @@ -581,12 +569,7 @@ def compute_expected_vwaps(self, window_lengths): return vwaps - @parameterized.expand( - [ - (True,), - (False,), - ] - ) + @parameterized.expand([(True,), (False,)]) def test_handle_adjustment(self, set_screen): AAPL, MSFT, BRK_A = assets = self.assets @@ -613,7 +596,7 @@ def initialize(context): attach_pipeline(pipeline, "test") def handle_data(context, data): - today = normalize_date(get_datetime()) + today = self.trading_calendar.minute_to_session(get_datetime()) results = pipeline_output("test") expect_over_300 = { AAPL: today < self.AAPL_split_date, @@ -689,15 +672,14 @@ def before_trading_start(context, data): assert count[0] > 0 def test_pipeline_beyond_daily_bars(self): - """ - Ensure that we can run an algo with pipeline beyond the max date + """Ensure that we can run an algo with pipeline beyond the max date of the daily bars. """ # For ensuring we call before_trading_start. count = [0] - current_day = self.trading_calendar.next_session_label( + current_day = self.trading_calendar.next_session( self.pipeline_loader.raw_price_reader.last_available_dt, ) @@ -737,8 +719,8 @@ def before_trading_start(context, data): class PipelineSequenceTestCase(WithMakeAlgo, ZiplineTestCase): # run algorithm for 3 days - START_DATE = pd.Timestamp("2014-12-29", tz="utc") - END_DATE = pd.Timestamp("2014-12-31", tz="utc") + START_DATE = pd.Timestamp("2014-12-29") + END_DATE = pd.Timestamp("2014-12-31") ASSET_FINDER_COUNTRY_CODE = "US" def get_pipeline_loader(self): diff --git a/tests/pipeline/test_quarters_estimates.py b/tests/pipeline/test_quarters_estimates.py index f9ddc188a0..18c5f169f3 100644 --- a/tests/pipeline/test_quarters_estimates.py +++ b/tests/pipeline/test_quarters_estimates.py @@ -77,8 +77,7 @@ class QtrEstimates(Estimates): def create_expected_df_for_factor_compute(start_date, sids, tuples, end_date): - """ - Given a list of tuples of new data we get for each sid on each critical + """Given a list of tuples of new data we get for each sid on each critical date (when information changes), create a DataFrame that fills that data through a date range ending at `end_date`. """ @@ -89,16 +88,15 @@ def create_expected_df_for_factor_compute(start_date, sids, tuples, end_date): df = df.reindex(pd.date_range(start_date, end_date)) # Index name is lost during reindex. df.index = df.index.rename("knowledge_date") - df["at_date"] = end_date.tz_localize("utc") - df = df.set_index(["at_date", df.index.tz_localize("utc")]).ffill() + df["at_date"] = end_date + df = df.set_index(["at_date", df.index]).ffill() new_sids = set(sids) - set(df.columns) df = df.reindex(columns=df.columns.union(new_sids)) return df class WithEstimates(WithTradingSessions, WithAdjustmentReader): - """ - ZiplineTestCase mixin providing cls.loader and cls.events as class + """ZiplineTestCase mixin providing cls.loader and cls.events as class level fixtures. @@ -118,8 +116,8 @@ class WithEstimates(WithTradingSessions, WithAdjustmentReader): """ # Short window defined in order for test to run faster. - START_DATE = pd.Timestamp("2014-12-28", tz="utc") - END_DATE = pd.Timestamp("2015-02-04", tz="utc") + START_DATE = pd.Timestamp("2014-12-28") + END_DATE = pd.Timestamp("2015-02-04") @classmethod def make_loader(cls, events, columns): @@ -236,8 +234,8 @@ def test_load_one_day(self): engine = self.make_engine() results = engine.run_pipeline( Pipeline({c.name: c.latest for c in dataset.columns}), - start_date=pd.Timestamp("2015-01-15", tz="utc"), - end_date=pd.Timestamp("2015-01-15", tz="utc"), + start_date=pd.Timestamp("2015-01-15"), + end_date=pd.Timestamp("2015-01-15"), ) assert_frame_equal( @@ -265,9 +263,7 @@ def make_expected_out(cls): FISCAL_QUARTER_FIELD_NAME: 1.0, FISCAL_YEAR_FIELD_NAME: 2015.0, }, - index=pd.MultiIndex.from_tuples( - ((pd.Timestamp("2015-01-15", tz="utc"), cls.sid0),) - ), + index=pd.MultiIndex.from_tuples(((pd.Timestamp("2015-01-15"), cls.sid0),)), ) @@ -291,9 +287,7 @@ def make_expected_out(cls): FISCAL_QUARTER_FIELD_NAME: 2.0, FISCAL_YEAR_FIELD_NAME: 2015.0, }, - index=pd.MultiIndex.from_tuples( - ((pd.Timestamp("2015-01-15", tz="utc"), cls.sid0),) - ), + index=pd.MultiIndex.from_tuples(((pd.Timestamp("2015-01-15"), cls.sid0),)), ) @@ -474,7 +468,7 @@ class WithEstimatesTimeZero(WithEstimates): """ # Shorter date range for performance - END_DATE = pd.Timestamp("2015-01-28", tz="utc") + END_DATE = pd.Timestamp("2015-01-28") q1_knowledge_dates = [ pd.Timestamp("2015-01-01"), @@ -791,15 +785,11 @@ def fill_expected_out(cls, expected): # Fill columns for 1 Q out for raw_name in cls.columns.values(): expected.loc[ - pd.Timestamp("2015-01-01", tz="UTC") : pd.Timestamp( - "2015-01-11", tz="UTC" - ), + pd.Timestamp("2015-01-01") : pd.Timestamp("2015-01-11"), raw_name + "1", ] = cls.events[raw_name].iloc[0] expected.loc[ - pd.Timestamp("2015-01-11", tz="UTC") : pd.Timestamp( - "2015-01-20", tz="UTC" - ), + pd.Timestamp("2015-01-11") : pd.Timestamp("2015-01-20"), raw_name + "1", ] = cls.events[raw_name].iloc[1] @@ -809,23 +799,21 @@ def fill_expected_out(cls, expected): # out. for col_name in ["estimate", "event_date"]: expected.loc[ - pd.Timestamp("2015-01-06", tz="UTC") : pd.Timestamp( - "2015-01-10", tz="UTC" - ), + pd.Timestamp("2015-01-06") : pd.Timestamp("2015-01-10"), col_name + "2", ] = cls.events[col_name].iloc[1] # But we know what FQ and FY we'd need in both Q1 and Q2 # because we know which FQ is next and can calculate from there expected.loc[ - pd.Timestamp("2015-01-01", tz="UTC") : pd.Timestamp("2015-01-09", tz="UTC"), + pd.Timestamp("2015-01-01") : pd.Timestamp("2015-01-09"), FISCAL_QUARTER_FIELD_NAME + "2", ] = 2 expected.loc[ - pd.Timestamp("2015-01-12", tz="UTC") : pd.Timestamp("2015-01-20", tz="UTC"), + pd.Timestamp("2015-01-12") : pd.Timestamp("2015-01-20"), FISCAL_QUARTER_FIELD_NAME + "2", ] = 3 expected.loc[ - pd.Timestamp("2015-01-01", tz="UTC") : pd.Timestamp("2015-01-20", tz="UTC"), + pd.Timestamp("2015-01-01") : pd.Timestamp("2015-01-20"), FISCAL_YEAR_FIELD_NAME + "2", ] = 2015 @@ -842,37 +830,32 @@ def fill_expected_out(cls, expected): # Fill columns for 1 Q out for raw_name in cls.columns.values(): expected[raw_name + "1"].loc[ - pd.Timestamp("2015-01-12", tz="UTC") : pd.Timestamp( - "2015-01-19", tz="UTC" - ) + pd.Timestamp( + "2015-01-12", + ) : pd.Timestamp("2015-01-19") ] = cls.events[raw_name].iloc[0] - expected[raw_name + "1"].loc[ - pd.Timestamp("2015-01-20", tz="UTC") : - ] = cls.events[raw_name].iloc[1] + expected[raw_name + "1"].loc[pd.Timestamp("2015-01-20") :] = cls.events[ + raw_name + ].iloc[1] # Fill columns for 2 Q out for col_name in ["estimate", "event_date"]: - expected[col_name + "2"].loc[ - pd.Timestamp("2015-01-20", tz="UTC") : - ] = cls.events[col_name].iloc[0] + expected[col_name + "2"].loc[pd.Timestamp("2015-01-20") :] = cls.events[ + col_name + ].iloc[0] expected[FISCAL_QUARTER_FIELD_NAME + "2"].loc[ - pd.Timestamp("2015-01-12", tz="UTC") : pd.Timestamp("2015-01-20", tz="UTC") + pd.Timestamp("2015-01-12") : pd.Timestamp("2015-01-20") ] = 4 expected[FISCAL_YEAR_FIELD_NAME + "2"].loc[ - pd.Timestamp("2015-01-12", tz="UTC") : pd.Timestamp("2015-01-20", tz="UTC") + pd.Timestamp("2015-01-12") : pd.Timestamp("2015-01-20") ] = 2014 - expected[FISCAL_QUARTER_FIELD_NAME + "2"].loc[ - pd.Timestamp("2015-01-20", tz="UTC") : - ] = 1 - expected[FISCAL_YEAR_FIELD_NAME + "2"].loc[ - pd.Timestamp("2015-01-20", tz="UTC") : - ] = 2015 + expected[FISCAL_QUARTER_FIELD_NAME + "2"].loc[pd.Timestamp("2015-01-20") :] = 1 + expected[FISCAL_YEAR_FIELD_NAME + "2"].loc[pd.Timestamp("2015-01-20") :] = 2015 return expected class WithVaryingNumEstimates(WithEstimates): - """ - ZiplineTestCase mixin providing fixtures and a test to ensure that we + """ZiplineTestCase mixin providing fixtures and a test to ensure that we have the correct overwrites when the event date changes. We want to make sure that if we have a quarter with an event date that gets pushed back, we don't start overwriting for the next quarter early. Likewise, @@ -937,15 +920,15 @@ def compute(self, today, assets, out, estimate): engine = self.make_engine() engine.run_pipeline( Pipeline({"est": SomeFactor()}), - start_date=pd.Timestamp("2015-01-13", tz="utc"), + start_date=pd.Timestamp("2015-01-13"), # last event date we have - end_date=pd.Timestamp("2015-01-14", tz="utc"), + end_date=pd.Timestamp("2015-01-14"), ) class PreviousVaryingNumEstimates(WithVaryingNumEstimates, ZiplineTestCase): def assert_compute(self, estimate, today): - if today == pd.Timestamp("2015-01-13", tz="utc"): + if today == pd.Timestamp("2015-01-13"): assert_array_equal(estimate[:, 0], np.array([np.NaN, np.NaN, 12])) assert_array_equal(estimate[:, 1], np.array([np.NaN, 12, 12])) else: @@ -959,7 +942,7 @@ def make_loader(cls, events, columns): class NextVaryingNumEstimates(WithVaryingNumEstimates, ZiplineTestCase): def assert_compute(self, estimate, today): - if today == pd.Timestamp("2015-01-13", tz="utc"): + if today == pd.Timestamp("2015-01-13"): assert_array_equal(estimate[:, 0], np.array([11, 12, 12])) assert_array_equal(estimate[:, 1], np.array([np.NaN, np.NaN, 21])) else: @@ -972,8 +955,7 @@ def make_loader(cls, events, columns): class WithEstimateWindows(WithEstimates): - """ - ZiplineTestCase mixin providing fixures and a test to test running a + """ZiplineTestCase mixin providing fixures and a test to test running a Pipeline with an estimates loader over differently-sized windows. Attributes @@ -998,15 +980,15 @@ class WithEstimateWindows(WithEstimates): the correct dates when we have a factor that asks for a window of data. """ - END_DATE = pd.Timestamp("2015-02-10", tz="utc") + END_DATE = pd.Timestamp("2015-02-10") window_test_start_date = pd.Timestamp("2015-01-05") critical_dates = [ - pd.Timestamp("2015-01-09", tz="utc"), - pd.Timestamp("2015-01-15", tz="utc"), - pd.Timestamp("2015-01-20", tz="utc"), - pd.Timestamp("2015-01-26", tz="utc"), - pd.Timestamp("2015-02-05", tz="utc"), - pd.Timestamp("2015-02-10", tz="utc"), + pd.Timestamp("2015-01-09"), + pd.Timestamp("2015-01-15"), + pd.Timestamp("2015-01-20"), + pd.Timestamp("2015-01-26"), + pd.Timestamp("2015-02-05"), + pd.Timestamp("2015-02-10"), ] # Starting date, number of announcements out. window_test_cases = list(itertools.product(critical_dates, (1, 2))) @@ -1151,7 +1133,7 @@ def compute(self, today, assets, out, estimate): Pipeline({"est": SomeFactor()}), start_date=start_date, # last event date we have - end_date=pd.Timestamp("2015-02-10", tz="utc"), + end_date=pd.Timestamp("2015-02-10"), ) @@ -2133,9 +2115,9 @@ class WithSplitAdjustedMultipleEstimateColumns(WithEstimates): we still split-adjust correctly. """ - END_DATE = pd.Timestamp("2015-02-10", tz="utc") - test_start_date = pd.Timestamp("2015-01-06", tz="utc") - test_end_date = pd.Timestamp("2015-01-12", tz="utc") + END_DATE = pd.Timestamp("2015-02-10") + test_start_date = pd.Timestamp("2015-01-06") + test_end_date = pd.Timestamp("2015-01-12") split_adjusted_asof = pd.Timestamp("2015-01-08") @classmethod @@ -2294,19 +2276,19 @@ def make_loader(cls, events, columns): @classmethod def make_expected_timelines_1q_out(cls): return { - pd.Timestamp("2015-01-06", tz="utc"): { + pd.Timestamp("2015-01-06"): { "estimate1": np.array([[np.NaN, np.NaN]] * 3), "estimate2": np.array([[np.NaN, np.NaN]] * 3), }, - pd.Timestamp("2015-01-07", tz="utc"): { + pd.Timestamp("2015-01-07"): { "estimate1": np.array([[np.NaN, np.NaN]] * 3), "estimate2": np.array([[np.NaN, np.NaN]] * 3), }, - pd.Timestamp("2015-01-08", tz="utc"): { + pd.Timestamp("2015-01-08"): { "estimate1": np.array([[np.NaN, np.NaN]] * 2 + [[np.NaN, 1110.0]]), "estimate2": np.array([[np.NaN, np.NaN]] * 2 + [[np.NaN, 2110.0]]), }, - pd.Timestamp("2015-01-09", tz="utc"): { + pd.Timestamp("2015-01-09"): { "estimate1": np.array( [[np.NaN, np.NaN]] + [[np.NaN, 1110.0 * 4]] @@ -2318,7 +2300,7 @@ def make_expected_timelines_1q_out(cls): + [[2100 * 3.0, 2110.0 * 4]] ), }, - pd.Timestamp("2015-01-12", tz="utc"): { + pd.Timestamp("2015-01-12"): { "estimate1": np.array( [[np.NaN, np.NaN]] * 2 + [[1200 * 3.0, 1210.0 * 4]] ), @@ -2331,19 +2313,11 @@ def make_expected_timelines_1q_out(cls): @classmethod def make_expected_timelines_2q_out(cls): return { - pd.Timestamp("2015-01-06", tz="utc"): { - "estimate2": np.array([[np.NaN, np.NaN]] * 3) - }, - pd.Timestamp("2015-01-07", tz="utc"): { - "estimate2": np.array([[np.NaN, np.NaN]] * 3) - }, - pd.Timestamp("2015-01-08", tz="utc"): { - "estimate2": np.array([[np.NaN, np.NaN]] * 3) - }, - pd.Timestamp("2015-01-09", tz="utc"): { - "estimate2": np.array([[np.NaN, np.NaN]] * 3) - }, - pd.Timestamp("2015-01-12", tz="utc"): { + pd.Timestamp("2015-01-06"): {"estimate2": np.array([[np.NaN, np.NaN]] * 3)}, + pd.Timestamp("2015-01-07"): {"estimate2": np.array([[np.NaN, np.NaN]] * 3)}, + pd.Timestamp("2015-01-08"): {"estimate2": np.array([[np.NaN, np.NaN]] * 3)}, + pd.Timestamp("2015-01-09"): {"estimate2": np.array([[np.NaN, np.NaN]] * 3)}, + pd.Timestamp("2015-01-12"): { "estimate2": np.array( [[np.NaN, np.NaN]] * 2 + [[2100 * 3.0, 2110.0 * 4]] ) @@ -2367,7 +2341,7 @@ def make_loader(cls, events, columns): @classmethod def make_expected_timelines_1q_out(cls): return { - pd.Timestamp("2015-01-06", tz="utc"): { + pd.Timestamp("2015-01-06"): { "estimate1": np.array( [[np.NaN, np.NaN]] + [[1100.0 * 1 / 0.3, 1110.0 * 1 / 0.4]] * 2 ), @@ -2375,19 +2349,19 @@ def make_expected_timelines_1q_out(cls): [[np.NaN, np.NaN]] + [[2100.0 * 1 / 0.3, 2110.0 * 1 / 0.4]] * 2 ), }, - pd.Timestamp("2015-01-07", tz="utc"): { + pd.Timestamp("2015-01-07"): { "estimate1": np.array([[1100.0, 1110.0]] * 3), "estimate2": np.array([[2100.0, 2110.0]] * 3), }, - pd.Timestamp("2015-01-08", tz="utc"): { + pd.Timestamp("2015-01-08"): { "estimate1": np.array([[1100.0, 1110.0]] * 3), "estimate2": np.array([[2100.0, 2110.0]] * 3), }, - pd.Timestamp("2015-01-09", tz="utc"): { + pd.Timestamp("2015-01-09"): { "estimate1": np.array([[1100 * 3.0, 1210.0 * 4]] * 3), "estimate2": np.array([[2100 * 3.0, 2210.0 * 4]] * 3), }, - pd.Timestamp("2015-01-12", tz="utc"): { + pd.Timestamp("2015-01-12"): { "estimate1": np.array([[1200 * 3.0, np.NaN]] * 3), "estimate2": np.array([[2200 * 3.0, np.NaN]] * 3), }, @@ -2396,29 +2370,22 @@ def make_expected_timelines_1q_out(cls): @classmethod def make_expected_timelines_2q_out(cls): return { - pd.Timestamp("2015-01-06", tz="utc"): { + pd.Timestamp("2015-01-06"): { "estimate2": np.array( [[np.NaN, np.NaN]] + [[2200 * 1 / 0.3, 2210.0 * 1 / 0.4]] * 2 ) }, - pd.Timestamp("2015-01-07", tz="utc"): { - "estimate2": np.array([[2200.0, 2210.0]] * 3) - }, - pd.Timestamp("2015-01-08", tz="utc"): { - "estimate2": np.array([[2200, 2210.0]] * 3) - }, - pd.Timestamp("2015-01-09", tz="utc"): { + pd.Timestamp("2015-01-07"): {"estimate2": np.array([[2200.0, 2210.0]] * 3)}, + pd.Timestamp("2015-01-08"): {"estimate2": np.array([[2200, 2210.0]] * 3)}, + pd.Timestamp("2015-01-09"): { "estimate2": np.array([[2200 * 3.0, np.NaN]] * 3) }, - pd.Timestamp("2015-01-12", tz="utc"): { - "estimate2": np.array([[np.NaN, np.NaN]] * 3) - }, + pd.Timestamp("2015-01-12"): {"estimate2": np.array([[np.NaN, np.NaN]] * 3)}, } class WithAdjustmentBoundaries(WithEstimates): - """ - ZiplineTestCase mixin providing class-level attributes, methods, + """ZiplineTestCase mixin providing class-level attributes, methods, and a test to make sure that when the split-adjusted-asof-date is not strictly within the date index, we can still apply adjustments correctly. @@ -2439,12 +2406,12 @@ class WithAdjustmentBoundaries(WithEstimates): dates of interest. """ - START_DATE = pd.Timestamp("2015-01-04", tz="utc") + START_DATE = pd.Timestamp("2015-01-04") # We want to run the pipeline starting from `START_DATE`, but the # pipeline results will start from the next day, which is # `test_start_date`. - test_start_date = pd.Timestamp("2015-01-05", tz="UTC") - END_DATE = test_end_date = pd.Timestamp("2015-01-12", tz="utc") + test_start_date = pd.Timestamp("2015-01-05") + END_DATE = test_end_date = pd.Timestamp("2015-01-12") split_adjusted_before_start = test_start_date - timedelta(days=1) split_adjusted_after_end = test_end_date + timedelta(days=1) # Must parametrize over this because there can only be 1 such date for @@ -2641,8 +2608,7 @@ def make_expected_out(cls): }, index=pd.date_range( cls.test_start_date, - pd.Timestamp("2015-01-08", tz="UTC"), - tz="utc", + pd.Timestamp("2015-01-08"), ), ), pd.DataFrame( @@ -2651,9 +2617,8 @@ def make_expected_out(cls): "estimate": 10.0, }, index=pd.date_range( - pd.Timestamp("2015-01-09", tz="UTC"), + pd.Timestamp("2015-01-09"), cls.test_end_date, - tz="utc", ), ), pd.DataFrame( @@ -2661,36 +2626,28 @@ def make_expected_out(cls): SID_FIELD_NAME: cls.s1, "estimate": 11.0, }, - index=pd.date_range( - cls.test_start_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_start_date, cls.test_end_date), ), pd.DataFrame( {SID_FIELD_NAME: cls.s2, "estimate": np.NaN}, - index=pd.date_range( - cls.test_start_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_start_date, cls.test_end_date), ), pd.DataFrame( {SID_FIELD_NAME: cls.s3, "estimate": np.NaN}, index=pd.date_range( cls.test_start_date, cls.test_end_date - timedelta(1), - tz="utc", ), ), pd.DataFrame( {SID_FIELD_NAME: cls.s3, "estimate": 13.0 * 0.13}, - index=pd.date_range( - cls.test_end_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_end_date, cls.test_end_date), ), pd.DataFrame( {SID_FIELD_NAME: cls.s4, "estimate": np.NaN}, index=pd.date_range( cls.test_start_date, cls.test_end_date - timedelta(2), - tz="utc", ), ), pd.DataFrame( @@ -2698,7 +2655,6 @@ def make_expected_out(cls): index=pd.date_range( cls.test_end_date - timedelta(1), cls.test_end_date, - tz="utc", ), ), ] @@ -2719,8 +2675,7 @@ def make_expected_out(cls): }, index=pd.date_range( cls.test_start_date, - pd.Timestamp("2015-01-08", tz="UTC"), - tz="utc", + pd.Timestamp("2015-01-08"), ), ), pd.DataFrame( @@ -2729,9 +2684,8 @@ def make_expected_out(cls): "estimate": 10.0, }, index=pd.date_range( - pd.Timestamp("2015-01-09", tz="UTC"), + pd.Timestamp("2015-01-09"), cls.test_end_date, - tz="utc", ), ), pd.DataFrame( @@ -2739,36 +2693,28 @@ def make_expected_out(cls): SID_FIELD_NAME: cls.s1, "estimate": 11.0, }, - index=pd.date_range( - cls.test_start_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_start_date, cls.test_end_date), ), pd.DataFrame( {SID_FIELD_NAME: cls.s2, "estimate": np.NaN}, - index=pd.date_range( - cls.test_start_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_start_date, cls.test_end_date), ), pd.DataFrame( {SID_FIELD_NAME: cls.s3, "estimate": np.NaN}, index=pd.date_range( cls.test_start_date, cls.test_end_date - timedelta(1), - tz="utc", ), ), pd.DataFrame( {SID_FIELD_NAME: cls.s3, "estimate": 13.0}, - index=pd.date_range( - cls.test_end_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_end_date, cls.test_end_date), ), pd.DataFrame( {SID_FIELD_NAME: cls.s4, "estimate": np.NaN}, index=pd.date_range( cls.test_start_date, cls.test_end_date - timedelta(2), - tz="utc", ), ), pd.DataFrame( @@ -2776,7 +2722,6 @@ def make_expected_out(cls): index=pd.date_range( cls.test_end_date - timedelta(1), cls.test_end_date, - tz="utc", ), ), ] @@ -2821,8 +2766,7 @@ def make_expected_out(cls): }, index=pd.date_range( cls.test_start_date, - pd.Timestamp("2015-01-09", tz="UTC"), - tz="utc", + pd.Timestamp("2015-01-09"), ), ), pd.DataFrame( @@ -2830,18 +2774,14 @@ def make_expected_out(cls): SID_FIELD_NAME: cls.s1, "estimate": 11.0, }, - index=pd.date_range( - cls.test_start_date, cls.test_start_date, tz="utc" - ), + index=pd.date_range(cls.test_start_date, cls.test_start_date), ), pd.DataFrame( { SID_FIELD_NAME: cls.s2, "estimate": 12.0, }, - index=pd.date_range( - cls.test_end_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_end_date, cls.test_end_date), ), pd.DataFrame( { @@ -2851,7 +2791,6 @@ def make_expected_out(cls): index=pd.date_range( cls.test_end_date - timedelta(1), cls.test_end_date, - tz="utc", ), ), pd.DataFrame( @@ -2862,7 +2801,6 @@ def make_expected_out(cls): index=pd.date_range( cls.test_end_date - timedelta(1), cls.test_end_date - timedelta(1), - tz="utc", ), ), ] @@ -2883,8 +2821,7 @@ def make_expected_out(cls): }, index=pd.date_range( cls.test_start_date, - pd.Timestamp("2015-01-09", tz="UTC"), - tz="utc", + pd.Timestamp("2015-01-09"), ), ), pd.DataFrame( @@ -2892,18 +2829,14 @@ def make_expected_out(cls): SID_FIELD_NAME: cls.s1, "estimate": 11.0, }, - index=pd.date_range( - cls.test_start_date, cls.test_start_date, tz="utc" - ), + index=pd.date_range(cls.test_start_date, cls.test_start_date), ), pd.DataFrame( { SID_FIELD_NAME: cls.s2, "estimate": 12.0, }, - index=pd.date_range( - cls.test_end_date, cls.test_end_date, tz="utc" - ), + index=pd.date_range(cls.test_end_date, cls.test_end_date), ), pd.DataFrame( { @@ -2913,7 +2846,6 @@ def make_expected_out(cls): index=pd.date_range( cls.test_end_date - timedelta(1), cls.test_end_date, - tz="utc", ), ), pd.DataFrame( @@ -2924,7 +2856,6 @@ def make_expected_out(cls): index=pd.date_range( cls.test_end_date - timedelta(1), cls.test_end_date - timedelta(1), - tz="utc", ), ), ] @@ -2947,8 +2878,7 @@ def make_expected_out(cls): class TestQuarterShift: - """ - This tests, in isolation, quarter calculation logic for shifting quarters + """This tests, in isolation, quarter calculation logic for shifting quarters backwards/forwards from a starting point. """ diff --git a/tests/pipeline/test_slice.py b/tests/pipeline/test_slice.py index 834d8f8b12..bf748b3088 100644 --- a/tests/pipeline/test_slice.py +++ b/tests/pipeline/test_slice.py @@ -40,8 +40,8 @@ class SliceTestCase(WithSeededRandomPipelineEngine, ZiplineTestCase): sids = ASSET_FINDER_EQUITY_SIDS = pd.Index([1, 2, 3], dtype="int64") - START_DATE = pd.Timestamp("2015-01-31", tz="UTC") - END_DATE = pd.Timestamp("2015-03-01", tz="UTC") + START_DATE = pd.Timestamp("2015-01-31") + END_DATE = pd.Timestamp("2015-03-01") ASSET_FINDER_COUNTRY_CODE = "US" SEEDED_RANDOM_PIPELINE_DEFAULT_DOMAIN = US_EQUITIES @@ -62,8 +62,7 @@ def init_class_fixtures(cls): @parameter_space(my_asset_column=[0, 1, 2], window_length_=[1, 2, 3]) def test_slice(self, my_asset_column, window_length_): - """ - Test that slices can be created by indexing into a term, and that they + """Test that slices can be created by indexing into a term, and that they have the correct shape when used as inputs. """ sids = self.sids @@ -94,8 +93,7 @@ def compute(self, today, assets, out, returns, returns_slice): @parameter_space(unmasked_column=[0, 1, 2], slice_column=[0, 1, 2]) def test_slice_with_masking(self, unmasked_column, slice_column): - """ - Test that masking a factor that uses slices as inputs does not mask the + """Test that masking a factor that uses slices as inputs does not mask the slice data. """ sids = self.sids @@ -142,9 +140,7 @@ def compute(self, today, assets, out, returns, returns_slice): self.run_pipeline(Pipeline(columns=columns), start_date, end_date) def test_adding_slice_column(self): - """ - Test that slices cannot be added as a pipeline column. - """ + """Test that slices cannot be added as a pipeline column.""" my_asset = self.asset_finder.retrieve_asset(self.sids[0]) open_slice = OpenPrice()[my_asset] @@ -156,17 +152,14 @@ def test_adding_slice_column(self): pipe.add(open_slice, "open_slice") def test_loadable_term_slices(self): - """ - Test that slicing loadable terms raises the proper error. - """ + """Test that slicing loadable terms raises the proper error.""" my_asset = self.asset_finder.retrieve_asset(self.sids[0]) with pytest.raises(NonSliceableTerm): USEquityPricing.close[my_asset] def test_non_existent_asset(self): - """ - Test that indexing into a term with a non-existent asset raises the + """Test that indexing into a term with a non-existent asset raises the proper exception. """ my_asset = Asset( @@ -191,8 +184,7 @@ def compute(self, today, assets, out, returns_slice): ) def test_window_safety_of_slices(self): - """ - Test that slices correctly inherit the `window_safe` property of the + """Test that slices correctly inherit the `window_safe` property of the term from which they are derived. """ col = self.col @@ -268,9 +260,7 @@ def compute(self, today, assets, out, col): ) def test_single_column_output(self): - """ - Tests for custom factors that compute a 1D out. - """ + """Tests for custom factors that compute a 1D out.""" start_date = self.pipeline_start_date end_date = self.pipeline_end_date diff --git a/tests/pipeline/test_statistical.py b/tests/pipeline/test_statistical.py index 965deb2def..54dc992fe8 100644 --- a/tests/pipeline/test_statistical.py +++ b/tests/pipeline/test_statistical.py @@ -1,6 +1,5 @@ -""" -Tests for statistical pipeline terms. -""" +"""Tests for statistical pipeline terms.""" + import numpy as np import pandas as pd from pandas.testing import assert_frame_equal @@ -51,8 +50,8 @@ @pytest.fixture(scope="class") def set_test_statistical_built_ins(request, with_asset_finder, with_trading_calendars): sids = ASSET_FINDER_EQUITY_SIDS = pd.Index([1, 2, 3], dtype="int64") - START_DATE = pd.Timestamp("2015-01-31", tz="UTC") - END_DATE = pd.Timestamp("2015-03-01", tz="UTC") + START_DATE = pd.Timestamp("2015-01-31") + END_DATE = pd.Timestamp("2015-03-01") ASSET_FINDER_EQUITY_SYMBOLS = ("A", "B", "C") ASSET_FINDER_COUNTRY_CODE = "US" @@ -91,12 +90,7 @@ def set_test_statistical_built_ins(request, with_asset_finder, with_trading_cale **dict(equities=equities, exchanges=exchanges) ) day = request.cls.trading_calendar.day - request.cls.dates = dates = pd.date_range( - "2015-02-01", - "2015-02-28", - freq=day, - tz="UTC", - ) + request.cls.dates = dates = pd.date_range("2015-02-01", "2015-02-28", freq=day) # Using these start and end dates because they are a contigous span of # 5 days (Monday - Friday) and they allow for plenty of days to look @@ -156,10 +150,10 @@ class TestStatisticalBuiltIns: @pytest.mark.parametrize("returns_length", [2, 3]) @pytest.mark.parametrize("correlation_length", [3, 4]) def test_correlation_factors(self, returns_length, correlation_length): - """ - Tests for the built-in factors `RollingPearsonOfReturns` and + """Tests for the built-in factors `RollingPearsonOfReturns` and `RollingSpearmanOfReturns`. """ + assets = self.assets my_asset = self.my_asset my_asset_column = self.my_asset_column @@ -255,9 +249,8 @@ def test_correlation_factors(self, returns_length, correlation_length): @pytest.mark.parametrize("returns_length", [2, 3]) @pytest.mark.parametrize("regression_length", [3, 4]) def test_regression_of_returns_factor(self, returns_length, regression_length): - """ - Tests for the built-in factor `RollingLinearRegressionOfReturns`. - """ + """Tests for the built-in factor `RollingLinearRegressionOfReturns`.""" + assets = self.assets my_asset = self.my_asset my_asset_column = self.my_asset_column @@ -377,8 +370,7 @@ def test_simple_beta_allowed_missing_calculation(self): assert beta.params["allowed_missing_count"] == expected def test_correlation_and_regression_with_bad_asset(self): - """ - Test that `RollingPearsonOfReturns`, `RollingSpearmanOfReturns` and + """Test that `RollingPearsonOfReturns`, `RollingSpearmanOfReturns` and `RollingLinearRegressionOfReturns` raise the proper exception when given a nonexistent target asset. """ @@ -510,9 +502,7 @@ def test_simple_beta_repr(self): allowed_missing_percentage=0.5, ) result = repr(beta) - expected = "SimpleBeta({}, length=50, allowed_missing=25)".format( - self.my_asset, - ) + expected = f"SimpleBeta({self.my_asset}, length=50, allowed_missing=25)" assert result == expected def test_simple_beta_graph_repr(self): @@ -528,8 +518,8 @@ def test_simple_beta_graph_repr(self): class StatisticalMethodsTestCase(zf.WithSeededRandomPipelineEngine, zf.ZiplineTestCase): sids = ASSET_FINDER_EQUITY_SIDS = pd.Index([1, 2, 3], dtype="int64") - START_DATE = pd.Timestamp("2015-01-31", tz="UTC") - END_DATE = pd.Timestamp("2015-03-01", tz="UTC") + START_DATE = pd.Timestamp("2015-01-31") + END_DATE = pd.Timestamp("2015-03-01") ASSET_FINDER_COUNTRY_CODE = "US" SEEDED_RANDOM_PIPELINE_DEFAULT_DOMAIN = US_EQUITIES @@ -572,11 +562,11 @@ def init_class_fixtures(cls): @parameter_space(returns_length=[2, 3], correlation_length=[3, 4]) def test_factor_correlation_methods(self, returns_length, correlation_length): - """ - Ensure that `Factor.pearsonr` and `Factor.spearmanr` are consistent + """Ensure that `Factor.pearsonr` and `Factor.spearmanr` are consistent with the built-in factors `RollingPearsonOfReturns` and `RollingSpearmanOfReturns`. """ + my_asset = self.my_asset start_date = self.pipeline_start_date end_date = self.pipeline_end_date @@ -675,10 +665,10 @@ def compute(self, today, assets, out): @parameter_space(returns_length=[2, 3], regression_length=[3, 4]) def test_factor_regression_method(self, returns_length, regression_length): - """ - Ensure that `Factor.linear_regression` is consistent with the built-in + """Ensure that `Factor.linear_regression` is consistent with the built-in factor `RollingLinearRegressionOfReturns`. """ + my_asset = self.my_asset start_date = self.pipeline_start_date end_date = self.pipeline_end_date @@ -716,8 +706,7 @@ def test_factor_regression_method(self, returns_length, regression_length): assert_frame_equal(regression_results, expected_regression_results) def test_regression_method_bad_type(self): - """ - Make sure we cannot call the Factor linear regression method on factors + """Make sure we cannot call the Factor linear regression method on factors or slices that are not of float or int dtype. """ # These are arbitrary for the purpose of this test. @@ -752,10 +741,10 @@ def compute(self, today, assets, out): @parameter_space(correlation_length=[2, 3, 4]) def test_factor_correlation_methods_two_factors(self, correlation_length): - """ - Tests for `Factor.pearsonr` and `Factor.spearmanr` when passed another + """Tests for `Factor.pearsonr` and `Factor.spearmanr` when passed another 2D factor instead of a Slice. """ + assets = self.assets dates = self.dates start_date = self.pipeline_start_date @@ -859,10 +848,10 @@ def test_factor_correlation_methods_two_factors(self, correlation_length): @parameter_space(regression_length=[2, 3, 4]) def test_factor_regression_method_two_factors(self, regression_length): - """ - Tests for `Factor.linear_regression` when passed another 2D factor + """Tests for `Factor.linear_regression` when passed another 2D factor instead of a Slice. """ + assets = self.assets dates = self.dates start_date = self.pipeline_start_date diff --git a/tests/pipeline/test_technical.py b/tests/pipeline/test_technical.py index 61e5c3ee75..f9a706d2eb 100644 --- a/tests/pipeline/test_technical.py +++ b/tests/pipeline/test_technical.py @@ -143,9 +143,7 @@ class TestAroon: np.recarray( shape=(nassets,), dtype=dtype, - buf=np.array( - [100 * 3 / 9, 100 * 5 / 9] * nassets, dtype="f8" - ), + buf=np.array([100 * 3 / 9, 100 * 5 / 9] * nassets, dtype="f8"), ), ), ], @@ -367,7 +365,7 @@ class TestRateOfChangePercentage: ([2.0] * 10, 0.0, "constant"), ([2.0] + [1.0] * 9, -50.0, "step"), ([2.0 + x for x in range(10)], 450.0, "linear"), - ([2.0 + x ** 2 for x in range(10)], 4050.0, "quadratic"), + ([2.0 + x**2 for x in range(10)], 4050.0, "quadratic"), ], ) def test_rate_of_change_percentage(self, data, expected, test_name): @@ -471,19 +469,13 @@ def test_bad_inputs(self): "MACDSignal() expected a value greater than or equal to 1" " for argument %r, but got 0 instead." ) - with pytest.raises( - ValueError, match=re.escape(template % "fast_period") - ): + with pytest.raises(ValueError, match=re.escape(template % "fast_period")): MovingAverageConvergenceDivergenceSignal(fast_period=0) - with pytest.raises( - ValueError, match=re.escape(template % "slow_period") - ): + with pytest.raises(ValueError, match=re.escape(template % "slow_period")): MovingAverageConvergenceDivergenceSignal(slow_period=0) - with pytest.raises( - ValueError, match=re.escape(template % "signal_period") - ): + with pytest.raises(ValueError, match=re.escape(template % "signal_period")): MovingAverageConvergenceDivergenceSignal(signal_period=0) err_msg = ( @@ -665,9 +657,7 @@ def test_simple_volatility(self): ann_vol = AnnualizedVolatility() today = pd.Timestamp("2016", tz="utc") assets = np.arange(nassets, dtype=np.float64) - returns = np.full( - (ann_vol.window_length, nassets), 0.004, dtype=np.float64 - ) + returns = np.full((ann_vol.window_length, nassets), 0.004, dtype=np.float64) out = np.empty(shape=(nassets,), dtype=np.float64) ann_vol.compute(today, assets, out, returns, 252) diff --git a/tests/pipeline/test_term.py b/tests/pipeline/test_term.py index 60d5c2f59a..97c09563bf 100644 --- a/tests/pipeline/test_term.py +++ b/tests/pipeline/test_term.py @@ -155,8 +155,8 @@ def to_dict(a_list): class DependencyResolutionTestCase(WithTradingSessions, ZiplineTestCase): TRADING_CALENDAR_STRS = ("NYSE",) - START_DATE = pd.Timestamp("2014-01-02", tz="UTC") - END_DATE = pd.Timestamp("2014-12-31", tz="UTC") + START_DATE = pd.Timestamp("2014-01-02") + END_DATE = pd.Timestamp("2014-12-31") execution_plan_start = pd.Timestamp("2014-06-01", tz="UTC") execution_plan_end = pd.Timestamp("2014-06-30", tz="UTC") @@ -393,7 +393,7 @@ def test_instance_caching_binops(self): assert (lhs - rhs) is (lhs - rhs) assert (lhs * rhs) is (lhs * rhs) assert (lhs / rhs) is (lhs / rhs) - assert (lhs ** rhs) is (lhs ** rhs) + assert (lhs**rhs) is (lhs**rhs) assert (1 + rhs) is (1 + rhs) assert (rhs + 1) is (rhs + 1) @@ -407,8 +407,8 @@ def test_instance_caching_binops(self): assert (2 / rhs) is (2 / rhs) assert (rhs / 2) is (rhs / 2) - assert (2 ** rhs) is (2 ** rhs) - assert (rhs ** 2) is (rhs ** 2) + assert (2**rhs) is (2**rhs) + assert (rhs**2) is (rhs**2) assert (f + g) + (f + g) is (f + g) + (f + g) diff --git a/tests/pipeline/test_us_equity_pricing_loader.py b/tests/pipeline/test_us_equity_pricing_loader.py index 1a9d99d989..c8a4e10f53 100644 --- a/tests/pipeline/test_us_equity_pricing_loader.py +++ b/tests/pipeline/test_us_equity_pricing_loader.py @@ -58,11 +58,11 @@ # 15 16 17 18 19 20 21 # 22 23 24 25 26 27 28 # 29 30 -TEST_CALENDAR_START = pd.Timestamp("2015-06-01", tz="UTC") -TEST_CALENDAR_STOP = pd.Timestamp("2015-06-30", tz="UTC") +TEST_CALENDAR_START = pd.Timestamp("2015-06-01") +TEST_CALENDAR_STOP = pd.Timestamp("2015-06-30") -TEST_QUERY_START = pd.Timestamp("2015-06-10", tz="UTC") -TEST_QUERY_STOP = pd.Timestamp("2015-06-19", tz="UTC") +TEST_QUERY_START = pd.Timestamp("2015-06-10") +TEST_QUERY_STOP = pd.Timestamp("2015-06-19") # One asset for each of the cases enumerated in load_raw_arrays_from_bcolz. EQUITY_INFO = pd.DataFrame( @@ -374,7 +374,7 @@ def expected_adjustments(self, start_date, end_date, tables, adjustment_type): for table in tables: for eff_date_secs, ratio, sid in table.itertuples(index=False): - eff_date = pd.Timestamp(eff_date_secs, unit="s", tz="UTC") + eff_date = pd.Timestamp(eff_date_secs, unit="s") # Ignore adjustments outside the query bounds. if not (start_date <= eff_date <= end_date): @@ -487,11 +487,7 @@ def create_expected_table(df, name): if convert_dts: for colname in reader._datetime_int_cols[name]: - expected_df[colname] = ( - expected_df[colname] - .astype("datetime64[s]") - .dt.tz_localize("UTC") - ) + expected_df[colname] = expected_df[colname].astype("datetime64[s]") return expected_df @@ -509,11 +505,7 @@ def create_expected_div_table(df, name): .astype(int) ) else: - expected_df[colname] = ( - expected_df[colname] - .astype("datetime64[s]") - .dt.tz_localize("UTC") - ) + expected_df[colname] = expected_df[colname].astype("datetime64[s]") return expected_df diff --git a/tests/resources/yahoo_samples/rebuild_samples b/tests/resources/yahoo_samples/rebuild_samples index 4378201fb9..0de01999bc 100644 --- a/tests/resources/yahoo_samples/rebuild_samples +++ b/tests/resources/yahoo_samples/rebuild_samples @@ -27,7 +27,7 @@ def pricing_for_sid(sid): def column(name): return np.arange(252) + 1 + sid * 10000 + modifier[name] * 1000 - trading_days = get_calendar("XNYS").all_sessions + trading_days = get_calendar("XNYS").sessions return ( pd.DataFrame( diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index ed948b4ddb..013f673648 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -158,8 +158,8 @@ def handle_data(self, data): class TestMiscellaneousAPI(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="UTC") - END_DATE = pd.Timestamp("2006-01-04", tz="UTC") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-04") SIM_PARAMS_DATA_FREQUENCY = "minute" sids = 1, 2 @@ -198,33 +198,33 @@ def make_futures_info(cls): 5: { "symbol": "CLG06", "root_symbol": "CL", - "start_date": pd.Timestamp("2005-12-01", tz="UTC"), - "notice_date": pd.Timestamp("2005-12-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-01-20", tz="UTC"), + "start_date": pd.Timestamp("2005-12-01"), + "notice_date": pd.Timestamp("2005-12-20"), + "expiration_date": pd.Timestamp("2006-01-20"), "exchange": "TEST", }, 6: { "root_symbol": "CL", "symbol": "CLK06", - "start_date": pd.Timestamp("2005-12-01", tz="UTC"), - "notice_date": pd.Timestamp("2006-03-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-04-20", tz="UTC"), + "start_date": pd.Timestamp("2005-12-01"), + "notice_date": pd.Timestamp("2006-03-20"), + "expiration_date": pd.Timestamp("2006-04-20"), "exchange": "TEST", }, 7: { "symbol": "CLQ06", "root_symbol": "CL", - "start_date": pd.Timestamp("2005-12-01", tz="UTC"), - "notice_date": pd.Timestamp("2006-06-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-07-20", tz="UTC"), + "start_date": pd.Timestamp("2005-12-01"), + "notice_date": pd.Timestamp("2006-06-20"), + "expiration_date": pd.Timestamp("2006-07-20"), "exchange": "TEST", }, 8: { "symbol": "CLX06", "root_symbol": "CL", - "start_date": pd.Timestamp("2006-02-01", tz="UTC"), - "notice_date": pd.Timestamp("2006-09-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-10-20", tz="UTC"), + "start_date": pd.Timestamp("2006-02-01"), + "notice_date": pd.Timestamp("2006-09-20"), + "expiration_date": pd.Timestamp("2006-10-20"), "exchange": "TEST", }, }, @@ -232,29 +232,33 @@ def make_futures_info(cls): ) def test_cancel_policy_outside_init(self): - code = """ -from zipline.api import cancel_policy, set_cancel_policy + code = dedent( + """ + from zipline.api import cancel_policy, set_cancel_policy -def initialize(algo): - pass + def initialize(algo): + pass -def handle_data(algo, data): - set_cancel_policy(cancel_policy.NeverCancel()) -""" + def handle_data(algo, data): + set_cancel_policy(cancel_policy.NeverCancel()) + """ + ) algo = self.make_algo(script=code) with pytest.raises(SetCancelPolicyPostInit): algo.run() def test_cancel_policy_invalid_param(self): - code = """ -from zipline.api import set_cancel_policy + code = dedent( + """ + from zipline.api import set_cancel_policy -def initialize(algo): - set_cancel_policy("foo") + def initialize(algo): + set_cancel_policy("foo") -def handle_data(algo, data): - pass -""" + def handle_data(algo, data): + pass + """ + ) algo = self.make_algo(script=code) with pytest.raises(UnsupportedCancelPolicy): algo.run() @@ -280,61 +284,63 @@ def fake_method(*args, **kwargs): assert sentinel is getattr(zipline.api, name)() def test_sid_datetime(self): - algo_text = """ -from zipline.api import sid, get_datetime + algo_text = dedent( + """ + from zipline.api import sid, get_datetime -def initialize(context): - pass + def initialize(context): + pass -def handle_data(context, data): - aapl_dt = data.current(sid(1), "last_traded") - assert_equal(aapl_dt, get_datetime()) -""" + def handle_data(context, data): + aapl_dt = data.current(sid(1), "last_traded") + assert_equal(aapl_dt, get_datetime()) + """ + ) self.run_algorithm( script=algo_text, namespace={"assert_equal": self.assertEqual}, ) def test_datetime_bad_params(self): - algo_text = """ -from zipline.api import get_datetime -from pytz import timezone + algo_text = dedent( + """ + from zipline.api import get_datetime + from pytz import timezone -def initialize(context): - pass + def initialize(context): + pass -def handle_data(context, data): - get_datetime(timezone) -""" + def handle_data(context, data): + get_datetime(timezone) + """ + ) algo = self.make_algo(script=algo_text) with pytest.raises(TypeError): algo.run() - @parameterized.expand( - [ - (-1000, "invalid_base"), - (0, "invalid_base"), - ] - ) + @parameterized.expand([(-1000, "invalid_base"), (0, "invalid_base")]) def test_invalid_capital_base(self, cap_base, name): - """ - Test that the appropriate error is being raised and orders aren't + """Test that the appropriate error is being raised and orders aren't filled for algos with capital base <= 0 """ - algo_text = """ -def initialize(context): - pass -def handle_data(context, data): - order(sid(24), 1000) - """ + algo_text = dedent( + """ + def initialize(context): + pass + + def handle_data(context, data): + order(sid(24), 1000) + """ + ) sim_params = SimulationParameters( - start_session=pd.Timestamp("2006-01-03", tz="UTC"), - end_session=pd.Timestamp("2006-01-06", tz="UTC"), + start_session=pd.Timestamp("2006-01-03"), + end_session=pd.Timestamp("2006-01-06"), capital_base=cap_base, data_frequency="minute", trading_calendar=self.trading_calendar, ) + expected_msg = "initial capital base must be greater than zero" with pytest.raises(ZeroCapitalError, match=expected_msg): # make_algo will trace to TradingAlgorithm, @@ -410,35 +416,41 @@ def handle_data(algo, data): def test_schedule_function_custom_cal(self): # run a simulation on the CMES cal, and schedule a function # using the NYSE cal - algotext = """ -from zipline.api import ( - schedule_function, get_datetime, time_rules, date_rules, calendars, -) + algotext = dedent( + """ + from zipline.api import ( + schedule_function, + get_datetime, + time_rules, + date_rules, + calendars, + ) -def initialize(context): - schedule_function( - func=log_nyse_open, - date_rule=date_rules.every_day(), - time_rule=time_rules.market_open(), - calendar=calendars.US_EQUITIES, - ) + def initialize(context): + schedule_function( + func=log_nyse_open, + date_rule=date_rules.every_day(), + time_rule=time_rules.market_open(), + calendar=calendars.US_EQUITIES, + ) - schedule_function( - func=log_nyse_close, - date_rule=date_rules.every_day(), - time_rule=time_rules.market_close(), - calendar=calendars.US_EQUITIES, - ) + schedule_function( + func=log_nyse_close, + date_rule=date_rules.every_day(), + time_rule=time_rules.market_close(), + calendar=calendars.US_EQUITIES, + ) - context.nyse_opens = [] - context.nyse_closes = [] + context.nyse_opens = [] + context.nyse_closes = [] -def log_nyse_open(context, data): - context.nyse_opens.append(get_datetime()) + def log_nyse_open(context, data): + context.nyse_opens.append(get_datetime()) -def log_nyse_close(context, data): - context.nyse_closes.append(get_datetime()) - """ + def log_nyse_close(context, data): + context.nyse_closes.append(get_datetime()) + """ + ) algo = self.make_algo( script=algotext, @@ -452,14 +464,14 @@ def log_nyse_close(context, data): for minute in algo.nyse_opens: # each minute should be a nyse session open - session_label = nyse.minute_to_session_label(minute) - session_open = nyse.session_open(session_label) + session_label = nyse.minute_to_session(minute) + session_open = nyse.session_first_minute(session_label) assert session_open == minute for minute in algo.nyse_closes: # each minute should be a minute before a nyse session close - session_label = nyse.minute_to_session_label(minute) - session_close = nyse.session_close(session_label) + session_label = nyse.minute_to_session(minute) + session_close = nyse.session_last_minute(session_label) assert session_close - timedelta(minutes=1) == minute # Test that passing an invalid calendar parameter raises an error. @@ -627,11 +639,11 @@ def test_asset_lookup(self): algo = self.make_algo() # this date doesn't matter - start_session = pd.Timestamp("2000-01-01", tz="UTC") + start_session = pd.Timestamp("2000-01-01") # Test before either PLAY existed algo.sim_params = algo.sim_params.create_new( - start_session, pd.Timestamp("2001-12-01", tz="UTC") + start_session, pd.Timestamp("2001-12-01") ) with pytest.raises(SymbolNotFound): @@ -641,26 +653,26 @@ def test_asset_lookup(self): # Test when first PLAY exists algo.sim_params = algo.sim_params.create_new( - start_session, pd.Timestamp("2002-12-01", tz="UTC") + start_session, pd.Timestamp("2002-12-01") ) list_result = algo.symbols("PLAY") assert 3 == list_result[0] # Test after first PLAY ends algo.sim_params = algo.sim_params.create_new( - start_session, pd.Timestamp("2004-12-01", tz="UTC") + start_session, pd.Timestamp("2004-12-01") ) assert 3 == algo.symbol("PLAY") # Test after second PLAY begins algo.sim_params = algo.sim_params.create_new( - start_session, pd.Timestamp("2005-12-01", tz="UTC") + start_session, pd.Timestamp("2005-12-01") ) assert 4 == algo.symbol("PLAY") # Test after second PLAY ends algo.sim_params = algo.sim_params.create_new( - start_session, pd.Timestamp("2006-12-01", tz="UTC") + start_session, pd.Timestamp("2006-12-01") ) assert 4 == algo.symbol("PLAY") list_result = algo.symbols("PLAY") @@ -689,17 +701,18 @@ def test_asset_lookup(self): def test_future_symbol(self): """Tests the future_symbol API function.""" + algo = self.make_algo() - algo.datetime = pd.Timestamp("2006-12-01", tz="UTC") + algo.datetime = pd.Timestamp("2006-12-01") # Check that we get the correct fields for the CLG06 symbol cl = algo.future_symbol("CLG06") assert cl.sid == 5 assert cl.symbol == "CLG06" assert cl.root_symbol == "CL" - assert cl.start_date == pd.Timestamp("2005-12-01", tz="UTC") - assert cl.notice_date == pd.Timestamp("2005-12-20", tz="UTC") - assert cl.expiration_date == pd.Timestamp("2006-01-20", tz="UTC") + assert cl.start_date == pd.Timestamp("2005-12-01") + assert cl.notice_date == pd.Timestamp("2005-12-20") + assert cl.expiration_date == pd.Timestamp("2006-01-20") with pytest.raises(SymbolNotFound): algo.future_symbol("") @@ -736,9 +749,9 @@ class TestSetSymbolLookupDate(zf.WithMakeAlgo, zf.ZiplineTestCase): # 15 16 17 18 19 20 21 # 22 23 24 25 26 27 28 # 29 30 31 - START_DATE = pd.Timestamp("2006-01-03", tz="UTC") - END_DATE = pd.Timestamp("2006-01-06", tz="UTC") - SIM_PARAMS_START_DATE = pd.Timestamp("2006-01-04", tz="UTC") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-06") + SIM_PARAMS_START_DATE = pd.Timestamp("2006-01-04") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False BENCHMARK_SID = 3 @@ -780,28 +793,28 @@ def make_equity_info(cls): index=cls.sids, ) - def test_set_symbol_lookup_date(self): - """ - Test the set_symbol_lookup_date API method. - """ - set_symbol_lookup_date = zipline.api.set_symbol_lookup_date + # TODO FIXME IMPORTANT pytest crashes with internal error if test below is uncommented + # def test_set_symbol_lookup_date(self): + # """Test the set_symbol_lookup_date API method.""" - def initialize(context): - set_symbol_lookup_date(self.asset_ends[0]) - assert zipline.api.symbol("DUP").sid == self.sids[0] + # set_symbol_lookup_date = zipline.api.set_symbol_lookup_date + + # def initialize(context): + # set_symbol_lookup_date(self.asset_ends[0]) + # assert zipline.api.symbol("DUP").sid == self.sids[0] - set_symbol_lookup_date(self.asset_ends[1]) - assert zipline.api.symbol("DUP").sid == self.sids[1] + # set_symbol_lookup_date(self.asset_ends[1]) + # assert zipline.api.symbol("DUP").sid == self.sids[1] - with pytest.raises(UnsupportedDatetimeFormat): - set_symbol_lookup_date("foobar") + # with pytest.raises(UnsupportedDatetimeFormat): + # set_symbol_lookup_date("foobar") - self.run_algorithm(initialize=initialize) + # self.run_algorithm(initialize=initialize) class TestPositions(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-01-06", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-06") SIM_PARAMS_CAPITAL_BASE = 1000 ASSET_FINDER_EQUITY_SIDS = (1, 133) @@ -844,7 +857,7 @@ def make_future_minute_bar_data(cls): trading_calendar = cls.trading_calendars[Future] sids = cls.asset_finder.futures_sids - minutes = trading_calendar.minutes_for_sessions_in_range( + minutes = trading_calendar.sessions_minutes( cls.future_minute_bar_days[0], cls.future_minute_bar_days[-1], ) @@ -1030,20 +1043,17 @@ def handle_data(algo, data): class TestBeforeTradingStart(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2016-01-06", tz="utc") - END_DATE = pd.Timestamp("2016-01-07", tz="utc") + START_DATE = pd.Timestamp("2016-01-06") + END_DATE = pd.Timestamp("2016-01-07") SIM_PARAMS_CAPITAL_BASE = 10000 SIM_PARAMS_DATA_FREQUENCY = "minute" EQUITY_DAILY_BAR_LOOKBACK_DAYS = EQUITY_MINUTE_BAR_LOOKBACK_DAYS = 1 - DATA_PORTAL_FIRST_TRADING_DAY = pd.Timestamp("2016-01-05", tz="UTC") - EQUITY_MINUTE_BAR_START_DATE = pd.Timestamp("2016-01-05", tz="UTC") - FUTURE_MINUTE_BAR_START_DATE = pd.Timestamp("2016-01-05", tz="UTC") + DATA_PORTAL_FIRST_TRADING_DAY = pd.Timestamp("2016-01-05") + EQUITY_MINUTE_BAR_START_DATE = pd.Timestamp("2016-01-05") + FUTURE_MINUTE_BAR_START_DATE = pd.Timestamp("2016-01-05") - data_start = ASSET_FINDER_EQUITY_START_DATE = pd.Timestamp( - "2016-01-05", - tz="utc", - ) + data_start = ASSET_FINDER_EQUITY_START_DATE = pd.Timestamp("2016-01-05") SPLIT_ASSET_SID = 3 ASSET_FINDER_EQUITY_SIDS = 1, 2, SPLIT_ASSET_SID @@ -1106,26 +1116,26 @@ def make_equity_daily_bar_data(cls, country_code, sids): def test_data_in_bts_minute(self): algo_code = dedent( """ - from zipline.api import record, sid - def initialize(context): - context.history_values = [] - - def before_trading_start(context, data): - record(the_price1=data.current(sid(1), "price")) - record(the_high1=data.current(sid(1), "high")) - record(the_price2=data.current(sid(2), "price")) - record(the_high2=data.current(sid(2), "high")) + from zipline.api import record, sid + def initialize(context): + context.history_values = [] - context.history_values.append(data.history( - [sid(1), sid(2)], - ["price", "high"], - 60, - "1m" - )) + def before_trading_start(context, data): + record(the_price1=data.current(sid(1), "price")) + record(the_high1=data.current(sid(1), "high")) + record(the_price2=data.current(sid(2), "price")) + record(the_high2=data.current(sid(2), "high")) + + context.history_values.append(data.history( + [sid(1), sid(2)], + ["price", "high"], + 60, + "1m" + )) - def handle_data(context, data): - pass - """ + def handle_data(context, data): + pass + """ ) algo = self.make_algo(script=algo_code) @@ -1179,26 +1189,26 @@ def handle_data(context, data): def test_data_in_bts_daily(self): algo_code = dedent( """ - from zipline.api import record, sid - def initialize(context): - context.history_values = [] - - def before_trading_start(context, data): - record(the_price1=data.current(sid(1), "price")) - record(the_high1=data.current(sid(1), "high")) - record(the_price2=data.current(sid(2), "price")) - record(the_high2=data.current(sid(2), "high")) + from zipline.api import record, sid + def initialize(context): + context.history_values = [] - context.history_values.append(data.history( - [sid(1), sid(2)], - ["price", "high"], - 1, - "1d", - )) + def before_trading_start(context, data): + record(the_price1=data.current(sid(1), "price")) + record(the_high1=data.current(sid(1), "high")) + record(the_price2=data.current(sid(2), "price")) + record(the_high2=data.current(sid(2), "high")) + + context.history_values.append(data.history( + [sid(1), sid(2)], + ["price", "high"], + 1, + "1d", + )) - def handle_data(context, data): - pass - """ + def handle_data(context, data): + pass + """ ) algo = self.make_algo(script=algo_code) @@ -1221,26 +1231,26 @@ def handle_data(context, data): def test_portfolio_bts(self): algo_code = dedent( """ - from zipline.api import order, sid, record + from zipline.api import order, sid, record - def initialize(context): - context.ordered = False - context.hd_portfolio = context.portfolio + def initialize(context): + context.ordered = False + context.hd_portfolio = context.portfolio - def before_trading_start(context, data): - bts_portfolio = context.portfolio + def before_trading_start(context, data): + bts_portfolio = context.portfolio - # Assert that the portfolio in BTS is the same as the last - # portfolio in handle_data - assert (context.hd_portfolio == bts_portfolio) - record(pos_value=bts_portfolio.positions_value) + # Assert that the portfolio in BTS is the same as the last + # portfolio in handle_data + assert (context.hd_portfolio == bts_portfolio) + record(pos_value=bts_portfolio.positions_value) - def handle_data(context, data): - if not context.ordered: - order(sid(1), 1) - context.ordered = True - context.hd_portfolio = context.portfolio - """ + def handle_data(context, data): + if not context.ordered: + order(sid(1), 1) + context.ordered = True + context.hd_portfolio = context.portfolio + """ ) algo = self.make_algo(script=algo_code) @@ -1256,27 +1266,27 @@ def handle_data(context, data): def test_account_bts(self): algo_code = dedent( """ - from zipline.api import order, sid, record, set_slippage, slippage + from zipline.api import order, sid, record, set_slippage, slippage - def initialize(context): - context.ordered = False - context.hd_account = context.account - set_slippage(slippage.VolumeShareSlippage()) + def initialize(context): + context.ordered = False + context.hd_account = context.account + set_slippage(slippage.VolumeShareSlippage()) - def before_trading_start(context, data): - bts_account = context.account + def before_trading_start(context, data): + bts_account = context.account - # Assert that the account in BTS is the same as the last account - # in handle_data - assert (context.hd_account == bts_account) - record(port_value=context.account.equity_with_loan) + # Assert that the account in BTS is the same as the last account + # in handle_data + assert (context.hd_account == bts_account) + record(port_value=context.account.equity_with_loan) - def handle_data(context, data): - if not context.ordered: - order(sid(1), 1) - context.ordered = True - context.hd_account = context.account - """ + def handle_data(context, data): + if not context.ordered: + order(sid(1), 1) + context.ordered = True + context.hd_account = context.account + """ ) algo = self.make_algo(script=algo_code) @@ -1294,32 +1304,32 @@ def handle_data(context, data): def test_portfolio_bts_with_overnight_split(self): algo_code = dedent( """ - from zipline.api import order, sid, record + from zipline.api import order, sid, record - def initialize(context): - context.ordered = False - context.hd_portfolio = context.portfolio - - def before_trading_start(context, data): - bts_portfolio = context.portfolio - # Assert that the portfolio in BTS is the same as the last - # portfolio in handle_data, except for the positions - for k in bts_portfolio.__dict__: - if k != 'positions': - assert (context.hd_portfolio.__dict__[k] - == bts_portfolio.__dict__[k]) - record(pos_value=bts_portfolio.positions_value) - record(pos_amount=bts_portfolio.positions[sid(3)].amount) - record( - last_sale_price=bts_portfolio.positions[sid(3)].last_sale_price - ) + def initialize(context): + context.ordered = False + context.hd_portfolio = context.portfolio - def handle_data(context, data): - if not context.ordered: - order(sid(3), 1) - context.ordered = True - context.hd_portfolio = context.portfolio - """ + def before_trading_start(context, data): + bts_portfolio = context.portfolio + # Assert that the portfolio in BTS is the same as the last + # portfolio in handle_data, except for the positions + for k in bts_portfolio.__dict__: + if k != 'positions': + assert (context.hd_portfolio.__dict__[k] + == bts_portfolio.__dict__[k]) + record(pos_value=bts_portfolio.positions_value) + record(pos_amount=bts_portfolio.positions[sid(3)].amount) + record( + last_sale_price=bts_portfolio.positions[sid(3)].last_sale_price + ) + + def handle_data(context, data): + if not context.ordered: + order(sid(3), 1) + context.ordered = True + context.hd_portfolio = context.portfolio + """ ) results = self.run_algorithm(script=algo_code) @@ -1339,27 +1349,27 @@ def handle_data(context, data): def test_account_bts_with_overnight_split(self): algo_code = dedent( """ - from zipline.api import order, sid, record, set_slippage, slippage + from zipline.api import order, sid, record, set_slippage, slippage - def initialize(context): - context.ordered = False - context.hd_account = context.account - set_slippage(slippage.VolumeShareSlippage()) + def initialize(context): + context.ordered = False + context.hd_account = context.account + set_slippage(slippage.VolumeShareSlippage()) - def before_trading_start(context, data): - bts_account = context.account - # Assert that the account in BTS is the same as the last account - # in handle_data - assert (context.hd_account == bts_account) - record(port_value=bts_account.equity_with_loan) + def before_trading_start(context, data): + bts_account = context.account + # Assert that the account in BTS is the same as the last account + # in handle_data + assert (context.hd_account == bts_account) + record(port_value=bts_account.equity_with_loan) - def handle_data(context, data): - if not context.ordered: - order(sid(1), 1) - context.ordered = True - context.hd_account = context.account - """ + def handle_data(context, data): + if not context.ordered: + order(sid(1), 1) + context.ordered = True + context.hd_account = context.account + """ ) results = self.run_algorithm(script=algo_code) @@ -1372,8 +1382,8 @@ def handle_data(context, data): class TestAlgoScript(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-12-31", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-12-31") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False EQUITY_DAILY_BAR_LOOKBACK_DAYS = 5 # max history window length @@ -1507,28 +1517,32 @@ def test_fixed_slippage(self): # verify order -> transaction -> portfolio position. # -------------- test_algo = self.make_algo( - script=""" -from zipline.api import (slippage, - commission, - set_slippage, - set_commission, - order, - record, - sid) + script=dedent( + """ + from zipline.api import ( + slippage, + commission, + set_slippage, + set_commission, + order, + record, + sid) -def initialize(context): - model = slippage.FixedSlippage(spread=0.10) - set_slippage(model) - set_commission(commission.PerTrade(100.00)) - context.count = 1 - context.incr = 0 + def initialize(context): + model = slippage.FixedSlippage(spread=0.10) + set_slippage(model) + set_commission(commission.PerTrade(100.00)) + context.count = 1 + context.incr = 0 -def handle_data(context, data): - if context.incr < context.count: - order(sid(0), -1000) - record(price=data.current(sid(0), "price")) + def handle_data(context, data): + if context.incr < context.count: + order(sid(0), -1000) + record(price=data.current(sid(0), "price")) - context.incr += 1""", + context.incr += 1 + """ + ), ) results = test_algo.run() @@ -1554,18 +1568,9 @@ def handle_data(context, data): @parameterized.expand( [ - ( - "no_minimum_commission", - 0, - ), - ( - "default_minimum_commission", - 0, - ), - ( - "alternate_minimum_commission", - 2, - ), + ("no_minimum_commission", 0), + ("default_minimum_commission", 0), + ("alternate_minimum_commission", 2), ] ) def test_volshare_slippage(self, name, minimum_commission): @@ -1595,31 +1600,31 @@ def test_volshare_slippage(self, name, minimum_commission): ) test_algo = self.make_algo( data_portal=data_portal, - script=""" -from zipline.api import * - -def initialize(context): - model = slippage.VolumeShareSlippage( - volume_limit=.3, - price_impact=0.05 - ) - set_slippage(model) - {0} - - context.count = 2 - context.incr = 0 - -def handle_data(context, data): - if context.incr < context.count: - # order small lots to be sure the - # order will fill in a single transaction - order(sid(0), 5000) - record(price=data.current(sid(0), "price")) - record(volume=data.current(sid(0), "volume")) - record(incr=context.incr) - context.incr += 1 - """.format( - commission_line + script=dedent( + f""" + from zipline.api import * + + def initialize(context): + model = slippage.VolumeShareSlippage( + volume_limit=.3, + price_impact=0.05 + ) + set_slippage(model) + {commission_line} + + context.count = 2 + context.incr = 0 + + def handle_data(context, data): + if context.incr < context.count: + # order small lots to be sure the + # order will fill in a single transaction + order(sid(0), 5000) + record(price=data.current(sid(0), "price")) + record(volume=data.current(sid(0), "volume")) + record(incr=context.incr) + context.incr += 1 + """ ), ) results = test_algo.run() @@ -1698,7 +1703,7 @@ def test_batch_market_order_matches_multiple_manual_orders(self): multi_blotter = RecordBatchBlotter() multi_test_algo = self.make_algo( script=dedent( - """\ + """ from collections import OrderedDict from zipline.api import sid, order @@ -1715,7 +1720,7 @@ def handle_data(context, data): context.placed = True - """ + """ ).format(share_counts=list(share_counts)), blotter=multi_blotter, ) @@ -1725,7 +1730,7 @@ def handle_data(context, data): batch_blotter = RecordBatchBlotter() batch_test_algo = self.make_algo( script=dedent( - """\ + """ import pandas as pd from zipline.api import sid, batch_market_order @@ -1747,7 +1752,7 @@ def handle_data(context, data): context.placed = True - """ + """ ).format(share_counts=list(share_counts)), blotter=batch_blotter, ) @@ -1769,7 +1774,7 @@ def test_batch_market_order_filters_null_orders(self): batch_blotter = RecordBatchBlotter() batch_test_algo = self.make_algo( script=dedent( - """\ + """ import pandas as pd from zipline.api import sid, batch_market_order @@ -1790,7 +1795,7 @@ def handle_data(context, data): context.placed = True - """ + """ ).format(share_counts=share_counts), blotter=batch_blotter, ) @@ -1800,37 +1805,38 @@ def handle_data(context, data): def test_order_dead_asset(self): # after asset 0 is dead params = SimulationParameters( - start_session=pd.Timestamp("2007-01-03", tz="UTC"), - end_session=pd.Timestamp("2007-01-05", tz="UTC"), + start_session=pd.Timestamp("2007-01-03"), + end_session=pd.Timestamp("2007-01-05"), trading_calendar=self.trading_calendar, ) # order method shouldn't blow up self.run_algorithm( - script=""" -from zipline.api import order, sid + script=dedent( + """ + from zipline.api import order, sid -def initialize(context): - pass + def initialize(context): + pass -def handle_data(context, data): - order(sid(0), 10) - """, + def handle_data(context, data): + order(sid(0), 10) + """ + ) ) # order_value and order_percent should blow up for order_str in ["order_value", "order_percent"]: test_algo = self.make_algo( - script=""" -from zipline.api import order_percent, order_value, sid + script=dedent( + f""" + from zipline.api import order_percent, order_value, sid -def initialize(context): - pass + def initialize(context): + pass -def handle_data(context, data): - {0}(sid(0), 10) - """.format( - order_str + def handle_data(context, data): + {order_str}(sid(0), 10)""" ), sim_params=params, ) @@ -1839,37 +1845,31 @@ def handle_data(context, data): test_algo.run() def test_portfolio_in_init(self): - """ - Test that accessing portfolio in init doesn't break. - """ + """Test that accessing portfolio in init doesn't break.""" self.run_algorithm(script=access_portfolio_in_init) def test_account_in_init(self): - """ - Test that accessing account in init doesn't break. - """ + """Test that accessing account in init doesn't break.""" self.run_algorithm(script=access_account_in_init) def test_without_kwargs(self): - """ - Test that api methods on the data object can be called with positional + """Test that api methods on the data object can be called with positional arguments. """ params = SimulationParameters( - start_session=pd.Timestamp("2006-01-10", tz="UTC"), - end_session=pd.Timestamp("2006-01-11", tz="UTC"), + start_session=pd.Timestamp("2006-01-10"), + end_session=pd.Timestamp("2006-01-11"), trading_calendar=self.trading_calendar, ) self.run_algorithm(sim_params=params, script=call_without_kwargs) def test_good_kwargs(self): - """ - Test that api methods on the data object can be called with keyword + """Test that api methods on the data object can be called with keyword arguments. """ params = SimulationParameters( - start_session=pd.Timestamp("2006-01-10", tz="UTC"), - end_session=pd.Timestamp("2006-01-11", tz="UTC"), + start_session=pd.Timestamp("2006-01-10"), + end_session=pd.Timestamp("2006-01-11"), trading_calendar=self.trading_calendar, ) self.run_algorithm(script=call_with_kwargs, sim_params=params) @@ -1881,8 +1881,7 @@ def test_good_kwargs(self): ] ) def test_bad_kwargs(self, name, algo_text): - """ - Test that api methods on the data object called with bad kwargs return + """Test that api methods on the data object called with bad kwargs return a meaningful TypeError that we create, rather than an unhelpful cython error """ @@ -1914,8 +1913,8 @@ def test_arg_types(self, name, inputs): def test_empty_asset_list_to_history(self): params = SimulationParameters( - start_session=pd.Timestamp("2006-01-10", tz="UTC"), - end_session=pd.Timestamp("2006-01-11", tz="UTC"), + start_session=pd.Timestamp("2006-01-10"), + end_session=pd.Timestamp("2006-01-11"), trading_calendar=self.trading_calendar, ) @@ -1953,8 +1952,7 @@ def test_get_open_orders_kwargs(self, name, script): algo.run() def test_empty_positions(self): - """ - Test that when we try context.portfolio.positions[stock] on a stock + """Test that when we try context.portfolio.positions[stock] on a stock for which we have no positions, we return a Position with values 0 (but more importantly, we don't crash) and don't save this Position to the user-facing dictionary PositionTracker._positions_store @@ -1966,16 +1964,15 @@ def test_empty_positions(self): assert all(amounts == 0) def test_schedule_function_time_rule_positionally_misplaced(self): - """ - Test that when a user specifies a time rule for the date_rule argument, + """Test that when a user specifies a time rule for the date_rule argument, but no rule in the time_rule argument (e.g. schedule_function(func, )), we assume that means assign a time rule but no date rule """ sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2006-01-12", tz="UTC"), - end=pd.Timestamp("2006-01-13", tz="UTC"), + start=pd.Timestamp("2006-01-12"), + end=pd.Timestamp("2006-01-13"), data_frequency="minute", ) @@ -2002,9 +1999,7 @@ def handle_data(algo, data): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("ignore", PerformanceWarning) - warnings.simplefilter( - "ignore", RuntimeWarning - ) # TODO: CHECK WHY DO I HAVE TO DO THAT (empyrical) + warnings.simplefilter("ignore", RuntimeWarning) algo = self.make_algo(script=algocode, sim_params=sim_params) algo.run() @@ -2037,8 +2032,8 @@ def handle_data(algo, data): class TestCapitalChanges(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="UTC") - END_DATE = pd.Timestamp("2006-01-09", tz="UTC") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-09") # XXX: This suite only has daily data for sid 0 and only has minutely data # for sid 1. @@ -2104,18 +2099,27 @@ def test_capital_changes_daily_mode(self, change_type, value): pd.Timestamp("2006-01-06", tz="UTC"): {"type": change_type, "value": value} } - algocode = """ -from zipline.api import set_slippage, set_commission, slippage, commission, \ - schedule_function, time_rules, order, sid + algocode = dedent( + """ + from zipline.api import ( + set_slippage, + set_commission, + slippage, + commission, + schedule_function, + time_rules, + order, + sid) -def initialize(context): - set_slippage(slippage.FixedSlippage(spread=0)) - set_commission(commission.PerShare(0, 0)) - schedule_function(order_stuff, time_rule=time_rules.market_open()) + def initialize(context): + set_slippage(slippage.FixedSlippage(spread=0)) + set_commission(commission.PerShare(0, 0)) + schedule_function(order_stuff, time_rule=time_rules.market_open()) -def order_stuff(context, data): - order(sid(0), 1000) -""" + def order_stuff(context, data): + order(sid(0), 1000) + """ + ) algo = self.make_algo( script=algocode, capital_changes=capital_changes, @@ -2154,10 +2158,9 @@ def order_stuff(context, data): # orders execute at price = 13, place orders # 1/09: orders execute at price = 14, place orders - expected_daily = {} - expected_capital_changes = np.array([0.0, 0.0, 0.0, 50000.0, 0.0]) + expected_daily = {} # Day 1, no transaction. Day 2, we transact, but the price of our stock # does not change. Day 3, we start getting returns expected_daily["returns"] = np.array( @@ -2279,8 +2282,8 @@ def test_capital_changes_minute_mode_daily_emission(self, change, values): change_loc, change_type = change.split("_") sim_params = SimulationParameters( - start_session=pd.Timestamp("2006-01-03", tz="UTC"), - end_session=pd.Timestamp("2006-01-05", tz="UTC"), + start_session=pd.Timestamp("2006-01-03"), + end_session=pd.Timestamp("2006-01-05"), data_frequency="minute", capital_base=1000.0, trading_calendar=self.nyse_calendar, @@ -2291,18 +2294,28 @@ def test_capital_changes_minute_mode_daily_emission(self, change, values): for datestr, value in values } - algocode = """ -from zipline.api import set_slippage, set_commission, slippage, commission, \ - schedule_function, time_rules, order, sid + algocode = dedent( + """ + from zipline.api import ( + set_slippage, + set_commission, + slippage, + commission, + schedule_function, + time_rules, + order, + sid, + ) -def initialize(context): - set_slippage(slippage.FixedSlippage(spread=0)) - set_commission(commission.PerShare(0, 0)) - schedule_function(order_stuff, time_rule=time_rules.market_open()) + def initialize(context): + set_slippage(slippage.FixedSlippage(spread=0)) + set_commission(commission.PerShare(0, 0)) + schedule_function(order_stuff, time_rule=time_rules.market_open()) -def order_stuff(context, data): - order(sid(1), 1) -""" + def order_stuff(context, data): + order(sid(1), 1) + """ + ) algo = self.make_algo( script=algocode, sim_params=sim_params, capital_changes=capital_changes @@ -2456,8 +2469,8 @@ def test_capital_changes_minute_mode_minute_emission(self, change, values): change_loc, change_type = change.split("_") sim_params = SimulationParameters( - start_session=pd.Timestamp("2006-01-03", tz="UTC"), - end_session=pd.Timestamp("2006-01-05", tz="UTC"), + start_session=pd.Timestamp("2006-01-03"), + end_session=pd.Timestamp("2006-01-05"), data_frequency="minute", emission_rate="minute", capital_base=1000.0, @@ -2704,8 +2717,8 @@ def order_stuff(context, data): class TestGetDatetime(zf.WithMakeAlgo, zf.ZiplineTestCase): SIM_PARAMS_DATA_FREQUENCY = "minute" - START_DATE = to_utc("2014-01-02 9:31") - END_DATE = to_utc("2014-01-03 9:31") + START_DATE = pd.Timestamp("2014-01-02 9:31") + END_DATE = pd.Timestamp("2014-01-03 9:31") ASSET_FINDER_EQUITY_SIDS = 0, 1 @@ -2761,8 +2774,8 @@ def handle_data(context, data): class TestTradingControls(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-01-06", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-06") sid = 133 sids = ASSET_FINDER_EQUITY_SIDS = 133, 134 @@ -3094,7 +3107,7 @@ def initialize(algo, count): algo.set_max_order_count(count) def handle_data(algo, data): - for i in range(5): + for _ in range(5): algo.order(self.asset, 1) algo.order_count += 1 @@ -3120,7 +3133,7 @@ def initialize(algo, max_orders_per_day): # 9. The last order of the second batch should fail. def handle_data(algo, data): if algo.minute_count == 0 or algo.minute_count == 100: - for i in range(5): + for _ in range(5): algo.order(self.asset, 1) algo.order_count += 1 @@ -3143,7 +3156,7 @@ def handle_data(algo, data): # reset each day. def handle_data(algo, data): if (algo.minute_count % 390) == 0: - for i in range(5): + for _ in range(5): algo.order(self.asset, 1) algo.order_count += 1 @@ -3223,8 +3236,8 @@ def handle_data(algo, data): class TestAssetDateBounds(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2014-01-02", tz="UTC") - END_DATE = pd.Timestamp("2014-01-03", tz="UTC") + START_DATE = pd.Timestamp("2014-01-02") + END_DATE = pd.Timestamp("2014-01-03") SIM_PARAMS_START_DATE = END_DATE # Only run for one day. SIM_PARAMS_DATA_FREQUENCY = "daily" @@ -3234,7 +3247,7 @@ class TestAssetDateBounds(zf.WithMakeAlgo, zf.ZiplineTestCase): @classmethod def make_equity_info(cls): - T = partial(pd.Timestamp, tz="UTC") + T = partial(pd.Timestamp) return pd.DataFrame.from_records( [ { @@ -3286,8 +3299,8 @@ def handle_data(algo, data): class TestAccountControls(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-01-06", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-06") (sidint,) = ASSET_FINDER_EQUITY_SIDS = (133,) BENCHMARK_SID = None @@ -3396,9 +3409,9 @@ def make_algo(min_leverage, grace_period): class TestFuturesAlgo(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2016-01-06", tz="utc") - END_DATE = pd.Timestamp("2016-01-07", tz="utc") - FUTURE_MINUTE_BAR_START_DATE = pd.Timestamp("2016-01-05", tz="UTC") + START_DATE = pd.Timestamp("2016-01-06") + END_DATE = pd.Timestamp("2016-01-07") + FUTURE_MINUTE_BAR_START_DATE = pd.Timestamp("2016-01-05") SIM_PARAMS_DATA_FREQUENCY = "minute" @@ -3590,8 +3603,8 @@ def test_volume_contract_slippage(self): class TestAnalyzeAPIMethod(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2016-01-05", tz="utc") - END_DATE = pd.Timestamp("2016-01-05", tz="utc") + START_DATE = pd.Timestamp("2016-01-05") + END_DATE = pd.Timestamp("2016-01-05") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False @@ -3617,8 +3630,8 @@ def analyze(context, perf): class TestOrderCancelation(zf.WithMakeAlgo, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2016-01-05", tz="utc") - END_DATE = pd.Timestamp("2016-01-07", tz="utc") + START_DATE = pd.Timestamp("2016-01-05") + END_DATE = pd.Timestamp("2016-01-07") ASSET_FINDER_EQUITY_SIDS = (1,) ASSET_FINDER_EQUITY_SYMBOLS = ("ASSET1",) @@ -3658,7 +3671,7 @@ def inject_fixtures(self, caplog): @classmethod def make_equity_minute_bar_data(cls): - asset_minutes = cls.trading_calendar.minutes_for_sessions_in_range( + asset_minutes = cls.trading_calendar.sessions_minutes( cls.START_DATE, cls.END_DATE, ) @@ -3712,8 +3725,7 @@ def prep_algo( minute_emission=[True, False], ) def test_eod_order_cancel_minute(self, direction, minute_emission): - """ - Test that EOD order cancel works in minute mode for both shorts and + """Test that EOD order cancel works in minute mode for both shorts and longs, and both daily emission and minute emission """ # order 1000 shares of asset1. the volume is only 1 share per bar, @@ -3799,8 +3811,7 @@ def test_eod_order_cancel_daily(self): class TestDailyEquityAutoClose(zf.WithMakeAlgo, zf.ZiplineTestCase): - """ - Tests if delisted equities are properly removed from a portfolio holding + """Tests if delisted equities are properly removed from a portfolio holding positions in said equities. """ @@ -3811,8 +3822,8 @@ class TestDailyEquityAutoClose(zf.WithMakeAlgo, zf.ZiplineTestCase): # 11 12 13 14 15 16 17 # 18 19 20 21 22 23 24 # 25 26 27 28 29 30 31 - START_DATE = pd.Timestamp("2015-01-05", tz="UTC") - END_DATE = pd.Timestamp("2015-01-13", tz="UTC") + START_DATE = pd.Timestamp("2015-01-05") + END_DATE = pd.Timestamp("2015-01-13") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False @@ -3869,9 +3880,7 @@ def final_daily_price(self, asset): return self.daily_data[asset.sid].loc[asset.end_date].close def default_initialize(self): - """ - Initialize function shared between test algos. - """ + """Initialize function shared between test algos.""" def initialize(context): context.ordered = False @@ -3883,9 +3892,7 @@ def initialize(context): return initialize def default_handle_data(self, assets, order_size): - """ - Handle data function shared between test algos. - """ + """Handle data function shared between test algos.""" def handle_data(context, data): if not context.ordered: @@ -3904,8 +3911,7 @@ def handle_data(context, data): __fail_fast=True, ) def test_daily_delisted_equities(self, order_size, capital_base): - """ - Make sure that after an equity gets delisted, our portfolio holds the + """Make sure that after an equity gets delisted, our portfolio holds the correct number of equities and correct amount of cash. """ assets = self.assets @@ -4036,8 +4042,7 @@ def transactions_for_date(date): } def test_cancel_open_orders(self): - """ - Test that any open orders for an equity that gets delisted are + """Test that any open orders for an equity that gets delisted are canceled. Unless an equity is auto closed, any open orders for that equity will persist indefinitely. """ @@ -4052,10 +4057,10 @@ def handle_data(context, data): # The only order we place in this test should never be filled. assert context.portfolio.cash == context.portfolio.starting_cash - today_session = self.trading_calendar.minute_to_session_label( + today_session = self.trading_calendar.minute_to_session( context.get_datetime() ) - day_after_auto_close = self.trading_calendar.next_session_label( + day_after_auto_close = self.trading_calendar.next_session( first_asset_auto_close_date, ) @@ -4139,8 +4144,8 @@ class TestMinutelyEquityAutoClose(zf.WithMakeAlgo, zf.ZiplineTestCase): # 11 12 13 14 15 16 17 # 18 19 20 21 22 23 24 # 25 26 27 28 29 30 31 - START_DATE = pd.Timestamp("2015-01-05", tz="UTC") - END_DATE = pd.Timestamp("2015-01-13", tz="UTC") + START_DATE = pd.Timestamp("2015-01-05") + END_DATE = pd.Timestamp("2015-01-13") BENCHMARK_SID = None @@ -4155,7 +4160,7 @@ def make_equity_info(cls): cls.START_DATE, cls.END_DATE, ) - cls.test_minutes = cls.trading_calendar.minutes_for_sessions_in_range( + cls.test_minutes = cls.trading_calendar.sessions_minutes( cls.START_DATE, cls.END_DATE, ) @@ -4203,9 +4208,7 @@ def final_minute_price(self, asset): ) def default_initialize(self): - """ - Initialize function shared between test algos. - """ + """Initialize function shared between test algos.""" def initialize(context): context.ordered = False @@ -4217,9 +4220,7 @@ def initialize(context): return initialize def default_handle_data(self, assets, order_size): - """ - Handle data function shared between test algos. - """ + """Handle data function shared between test algos.""" def handle_data(context, data): if not context.ordered: @@ -4356,10 +4357,10 @@ def transactions_for_date(date): class TestOrderAfterDelist(zf.WithMakeAlgo, zf.ZiplineTestCase): - start = pd.Timestamp("2016-01-05", tz="utc") - day_1 = pd.Timestamp("2016-01-06", tz="utc") - day_4 = pd.Timestamp("2016-01-11", tz="utc") - end = pd.Timestamp("2016-01-15", tz="utc") + start = pd.Timestamp("2016-01-05") + day_1 = pd.Timestamp("2016-01-06") + day_4 = pd.Timestamp("2016-01-11") + end = pd.Timestamp("2016-01-15") # FIXME: Pass a benchmark source here. BENCHMARK_SID = None @@ -4436,8 +4437,8 @@ def handle_data(context, data): algo = self.make_algo( script=algo_code, sim_params=SimulationParameters( - start_session=pd.Timestamp("2016-01-06", tz="UTC"), - end_session=pd.Timestamp("2016-01-07", tz="UTC"), + start_session=pd.Timestamp("2016-01-06"), + end_session=pd.Timestamp("2016-01-07"), trading_calendar=self.trading_calendar, data_frequency="minute", ), diff --git a/tests/test_api_shim.py b/tests/test_api_shim.py index da1f44757a..7fb326c4d0 100644 --- a/tests/test_api_shim.py +++ b/tests/test_api_shim.py @@ -18,8 +18,8 @@ def handle_data(context, data): class TestAPIShim(WithCreateBarData, WithMakeAlgo, ZiplineTestCase): - START_DATE = pd.Timestamp("2016-01-05", tz="UTC") - END_DATE = pd.Timestamp("2016-01-28", tz="UTC") + START_DATE = pd.Timestamp("2016-01-05") + END_DATE = pd.Timestamp("2016-01-28") SIM_PARAMS_DATA_FREQUENCY = "minute" sids = ASSET_FINDER_EQUITY_SIDS = 1, 2, 3 diff --git a/tests/test_assets.py b/tests/test_assets.py index 1a39bb512d..58b3bf0c3e 100644 --- a/tests/test_assets.py +++ b/tests/test_assets.py @@ -83,17 +83,16 @@ def build_lookup_generic_cases(): - """ - Generate test cases for the type of asset finder specific by + """Generate test cases for the type of asset finder specific by asset_finder_type for test_lookup_generic. """ - unique_start = pd.Timestamp("2013-01-01", tz="UTC") - unique_end = pd.Timestamp("2014-01-01", tz="UTC") + unique_start = pd.Timestamp("2013-01-01") + unique_end = pd.Timestamp("2014-01-01") - dupe_old_start = pd.Timestamp("2013-01-01", tz="UTC") - dupe_old_end = pd.Timestamp("2013-01-02", tz="UTC") - dupe_new_start = pd.Timestamp("2013-01-03", tz="UTC") - dupe_new_end = pd.Timestamp("2013-01-03", tz="UTC") + dupe_old_start = pd.Timestamp("2013-01-01") + dupe_old_end = pd.Timestamp("2013-01-02") + dupe_new_start = pd.Timestamp("2013-01-03") + dupe_new_end = pd.Timestamp("2013-01-03") equities = pd.DataFrame.from_records( [ @@ -319,11 +318,7 @@ def set_test_asset(request): request.cls.asset4 = Asset(4, exchange_info=request.cls.test_exchange) request.cls.asset5 = Asset( 5, - exchange_info=ExchangeInfo( - "still testing", - "still testing", - "??", - ), + exchange_info=ExchangeInfo("still testing", "still testing", "??"), ) @@ -545,24 +540,23 @@ def test_root_symbol(self): assert "OM" == future_symbol.root_symbol def test_lookup_future_symbol(self): - """ - Test the lookup_future_symbol method. - """ + """Test the lookup_future_symbol method.""" + om = self.asset_finder.lookup_future_symbol("OMH15") assert om.sid == 2468 assert om.symbol == "OMH15" assert om.root_symbol == "OM" - assert om.notice_date == pd.Timestamp("2014-01-20", tz="UTC") - assert om.expiration_date == pd.Timestamp("2014-02-20", tz="UTC") - assert om.auto_close_date == pd.Timestamp("2014-01-18", tz="UTC") + assert om.notice_date == pd.Timestamp("2014-01-20") + assert om.expiration_date == pd.Timestamp("2014-02-20") + assert om.auto_close_date == pd.Timestamp("2014-01-18") cl = self.asset_finder.lookup_future_symbol("CLG06") assert cl.sid == 0 assert cl.symbol == "CLG06" assert cl.root_symbol == "CL" - assert cl.start_date == pd.Timestamp("2005-12-01", tz="UTC") - assert cl.notice_date == pd.Timestamp("2005-12-20", tz="UTC") - assert cl.expiration_date == pd.Timestamp("2006-01-20", tz="UTC") + assert cl.start_date == pd.Timestamp("2005-12-01") + assert cl.notice_date == pd.Timestamp("2005-12-20") + assert cl.expiration_date == pd.Timestamp("2006-01-20") with pytest.raises(SymbolNotFound): self.asset_finder.lookup_future_symbol("") @@ -582,7 +576,7 @@ class TestAssetFinder: def test_blocked_lookup_symbol_query(self, asset_finder): # we will try to query for more variables than sqlite supports # to make sure we are properly chunking on the client side - as_of = pd.Timestamp("2013-01-01", tz="UTC") + as_of = pd.Timestamp("2013-01-01") # we need more sids than we can query from sqlite nsids = SQLITE_MAX_VARIABLE_NUMBER + 10 sids = range(nsids) @@ -605,7 +599,7 @@ def test_blocked_lookup_symbol_query(self, asset_finder): assert assets.keys() == set(sids) def test_lookup_symbol_delimited(self, asset_finder): - as_of = pd.Timestamp("2013-01-01", tz="UTC") + as_of = pd.Timestamp("2013-01-01") frame = pd.DataFrame.from_records( [ { @@ -645,7 +639,7 @@ def test_lookup_symbol_fuzzy(self, asset_finder): ] ) finder = asset_finder(equities=metadata) - dt = pd.Timestamp("2013-01-01", tz="UTC") + dt = pd.Timestamp("2013-01-01") # Try combos of looking up PRTYHRD with and without a time or fuzzy # Both non-fuzzys get no result @@ -676,7 +670,7 @@ def test_lookup_symbol_fuzzy(self, asset_finder): assert 2 == finder.lookup_symbol("BRK_A", dt, fuzzy=True) def test_lookup_symbol_change_ticker(self, asset_finder): - T = partial(pd.Timestamp, tz="utc") + T = partial(pd.Timestamp) metadata = pd.DataFrame.from_records( [ # sid 0 @@ -727,7 +721,7 @@ def test_lookup_symbol_change_ticker(self, asset_finder): with pytest.raises(SymbolNotFound): finder.lookup_symbol("C", T("2013-12-31")) - for asof in pd.date_range("2014-01-01", "2014-01-05", tz="utc"): + for asof in pd.date_range("2014-01-01", "2014-01-05"): # from 01 through 05 sid 0 held 'A' A_result = finder.lookup_symbol("A", asof) assert A_result == finder.retrieve_asset(0), str(asof) @@ -750,7 +744,7 @@ def test_lookup_symbol_change_ticker(self, asset_finder): # so it still maps to sid 1 assert finder.lookup_symbol("C", T("2014-01-07")) == finder.retrieve_asset(1) - for asof in pd.date_range("2014-01-06", "2014-01-11", tz="utc"): + for asof in pd.date_range("2014-01-06", "2014-01-11"): # from 06 through 10 sid 0 held 'B' # we test through the 11th because sid 1 is the last to hold 'B' # so it should ffill @@ -772,7 +766,7 @@ def test_lookup_symbol(self, asset_finder): # Incrementing by two so that start and end dates for each # generated Asset don't overlap (each Asset's end_date is the # day after its start date.) - dates = pd.date_range("2013-01-01", freq="2D", periods=5, tz="UTC") + dates = pd.date_range("2013-01-01", freq="2D", periods=5) df = pd.DataFrame.from_records( [ { @@ -873,12 +867,11 @@ def test_lookup_none_raises(self, asset_finder): asset_finder.lookup_symbol(None, pd.Timestamp("2013-01-01")) def test_lookup_mult_are_one(self, asset_finder): - """ - Ensure that multiple symbols that return the same sid are collapsed to + """Ensure that multiple symbols that return the same sid are collapsed to a single returned asset. """ - date = pd.Timestamp("2013-01-01", tz="UTC") + date = pd.Timestamp("2013-01-01") df = pd.DataFrame.from_records( [ @@ -908,7 +901,7 @@ def test_endless_multiple_resolves(self, asset_finder): they should be able to correctly get 2. """ - date = pd.Timestamp("2013-01-01", tz="UTC") + date = pd.Timestamp("2013-01-01") df = pd.DataFrame.from_records( [ @@ -948,15 +941,15 @@ def test_lookup_generic_handle_missing(self, asset_finder): { "sid": 0, "symbol": "real", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, { "sid": 1, "symbol": "also_real", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, # Sid whose end date is before our query date. We should @@ -964,8 +957,8 @@ def test_lookup_generic_handle_missing(self, asset_finder): { "sid": 2, "symbol": "real_but_old", - "start_date": pd.Timestamp("2002-1-1", tz="UTC"), - "end_date": pd.Timestamp("2003-1-1", tz="UTC"), + "start_date": pd.Timestamp("2002-1-1"), + "end_date": pd.Timestamp("2003-1-1"), "exchange": "TEST", }, # Sid whose start_date is **after** our query date. We should @@ -973,8 +966,8 @@ def test_lookup_generic_handle_missing(self, asset_finder): { "sid": 3, "symbol": "real_but_in_the_future", - "start_date": pd.Timestamp("2014-1-1", tz="UTC"), - "end_date": pd.Timestamp("2020-1-1", tz="UTC"), + "start_date": pd.Timestamp("2014-1-1"), + "end_date": pd.Timestamp("2020-1-1"), "exchange": "THE FUTURE", }, ] @@ -982,7 +975,7 @@ def test_lookup_generic_handle_missing(self, asset_finder): finder = asset_finder(equities=data) results, missing = finder.lookup_generic( ["REAL", 1, "FAKE", "REAL_BUT_OLD", "REAL_BUT_IN_THE_FUTURE"], - pd.Timestamp("2013-02-01", tz="UTC"), + pd.Timestamp("2013-02-01"), country_code=None, ) @@ -1004,15 +997,15 @@ def test_lookup_generic_multiple_symbols_across_countries(self, asset_finder): { "sid": 0, "symbol": "real", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "US_EXCHANGE", }, { "sid": 1, "symbol": "real", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "CA_EXCHANGE", }, ] @@ -1030,7 +1023,7 @@ def test_lookup_generic_multiple_symbols_across_countries(self, asset_finder): with pytest.raises(SameSymbolUsedAcrossCountries): asset_finder.lookup_generic( "real", - as_of_date=pd.Timestamp("2014-1-1", tz="UTC"), + as_of_date=pd.Timestamp("2014-1-1"), country_code=None, ) @@ -1043,7 +1036,7 @@ def test_lookup_generic_multiple_symbols_across_countries(self, asset_finder): matches, missing = asset_finder.lookup_generic( "real", - as_of_date=pd.Timestamp("2014-1-1", tz="UTC"), + as_of_date=pd.Timestamp("2014-1-1"), country_code="US", ) assert [matches] == [asset_finder.retrieve_asset(0)] @@ -1051,7 +1044,7 @@ def test_lookup_generic_multiple_symbols_across_countries(self, asset_finder): matches, missing = asset_finder.lookup_generic( "real", - as_of_date=pd.Timestamp("2014-1-1", tz="UTC"), + as_of_date=pd.Timestamp("2014-1-1"), country_code="CA", ) assert [matches] == [asset_finder.retrieve_asset(1)] @@ -1060,7 +1053,7 @@ def test_lookup_generic_multiple_symbols_across_countries(self, asset_finder): def test_compute_lifetimes(self, asset_finder): assets_per_exchange = 4 trading_day = self.trading_calendar.day - first_start = pd.Timestamp("2015-04-01", tz="UTC") + first_start = pd.Timestamp("2015-04-01") equities = pd.concat( [ @@ -1198,22 +1191,22 @@ def test_lookup_by_supplementary_field(self, asset_finder): { "sid": 0, "symbol": "A", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, { "sid": 1, "symbol": "B", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, { "sid": 2, "symbol": "C", - "start_date": pd.Timestamp("2013-7-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-7-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, ] @@ -1225,29 +1218,29 @@ def test_lookup_by_supplementary_field(self, asset_finder): "sid": 0, "field": "ALT_ID", "value": "100000000", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2013-6-28", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2013-6-28"), }, { "sid": 1, "field": "ALT_ID", "value": "100000001", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), }, { "sid": 0, "field": "ALT_ID", "value": "100000002", - "start_date": pd.Timestamp("2013-7-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-7-1"), + "end_date": pd.Timestamp("2014-1-1"), }, { "sid": 2, "field": "ALT_ID", "value": "100000000", - "start_date": pd.Timestamp("2013-7-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-7-1"), + "end_date": pd.Timestamp("2014-1-1"), }, ] ) @@ -1258,7 +1251,7 @@ def test_lookup_by_supplementary_field(self, asset_finder): ) # Before sid 0 has changed ALT_ID. - dt = pd.Timestamp("2013-6-28", tz="UTC") + dt = pd.Timestamp("2013-6-28") asset_0 = af.lookup_by_supplementary_field("ALT_ID", "100000000", dt) assert asset_0.sid == 0 @@ -1277,7 +1270,7 @@ def test_lookup_by_supplementary_field(self, asset_finder): af.lookup_by_supplementary_field("ALT_ID", "100000002", dt) # After all assets have ended. - dt = pd.Timestamp("2014-01-02", tz="UTC") + dt = pd.Timestamp("2014-01-02") asset_2 = af.lookup_by_supplementary_field("ALT_ID", "100000000", dt) assert asset_2.sid == 2 @@ -1303,22 +1296,22 @@ def test_get_supplementary_field(self, asset_finder): { "sid": 0, "symbol": "A", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, { "sid": 1, "symbol": "B", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, { "sid": 2, "symbol": "C", - "start_date": pd.Timestamp("2013-7-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-7-1"), + "end_date": pd.Timestamp("2014-1-1"), "exchange": "TEST", }, ] @@ -1330,29 +1323,29 @@ def test_get_supplementary_field(self, asset_finder): "sid": 0, "field": "ALT_ID", "value": "100000000", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2013-6-28", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2013-6-28"), }, { "sid": 1, "field": "ALT_ID", "value": "100000001", - "start_date": pd.Timestamp("2013-1-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-1-1"), + "end_date": pd.Timestamp("2014-1-1"), }, { "sid": 0, "field": "ALT_ID", "value": "100000002", - "start_date": pd.Timestamp("2013-7-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-7-1"), + "end_date": pd.Timestamp("2014-1-1"), }, { "sid": 2, "field": "ALT_ID", "value": "100000000", - "start_date": pd.Timestamp("2013-7-1", tz="UTC"), - "end_date": pd.Timestamp("2014-1-1", tz="UTC"), + "start_date": pd.Timestamp("2013-7-1"), + "end_date": pd.Timestamp("2014-1-1"), }, ] ) @@ -1363,7 +1356,7 @@ def test_get_supplementary_field(self, asset_finder): ) # Before sid 0 has changed ALT_ID and sid 2 has started. - dt = pd.Timestamp("2013-6-28", tz="UTC") + dt = pd.Timestamp("2013-6-28") for sid, expected in [(0, "100000000"), (1, "100000001")]: assert finder.get_supplementary_field(sid, "ALT_ID", dt) == expected @@ -1376,7 +1369,7 @@ def test_get_supplementary_field(self, asset_finder): finder.get_supplementary_field(2, "ALT_ID", dt), # After all assets have ended. - dt = pd.Timestamp("2014-01-02", tz="UTC") + dt = pd.Timestamp("2014-01-02") for sid, expected in [ (0, "100000002"), @@ -1542,7 +1535,7 @@ def country_code(n): return "A" + chr(ord("A") + n) def test_lookup_symbol_delimited(self, asset_finder): - as_of = pd.Timestamp("2013-01-01", tz="UTC") + as_of = pd.Timestamp("2013-01-01") num_assets = 3 sids = list(range(num_assets)) frame = pd.DataFrame.from_records( @@ -1619,7 +1612,7 @@ def test_lookup_symbol_fuzzy(self, asset_finder): } ) finder = asset_finder(equities=metadata, exchanges=exchanges) - dt = pd.Timestamp("2013-01-01", tz="UTC") + dt = pd.Timestamp("2013-01-01") # Try combos of looking up PRTYHRD with and without a time or fuzzy # Both non-fuzzys get no result @@ -1681,7 +1674,7 @@ def check_sid(expected_sid, ticker, country_code): check_sid(n * 3 + 2, "BRK_A", self.country_code(n)) def test_lookup_symbol_change_ticker(self, asset_finder): - T = partial(pd.Timestamp, tz="utc") + T = partial(pd.Timestamp) num_countries = 3 metadata = pd.DataFrame.from_records( [ @@ -1773,7 +1766,7 @@ def assert_resolves_in_each_country( # no one held 'C' before 01 assert_doesnt_resolve("C", T("2013-12-31")) - for asof in pd.date_range("2014-01-01", "2014-01-05", tz="utc"): + for asof in pd.date_range("2014-01-01", "2014-01-05"): # from 01 through 05 the first sid on the exchange held 'A' assert_resolves_in_each_country( "A", @@ -1805,7 +1798,7 @@ def assert_resolves_in_each_country( expected_name="Asset A", ) - for asof in pd.date_range("2014-01-06", "2014-01-11", tz="utc"): + for asof in pd.date_range("2014-01-06", "2014-01-11"): # from 06 through 10 sid 0 held 'B' # we test through the 11th because sid 1 is the last to hold 'B' # so it should ffill @@ -1833,7 +1826,7 @@ def test_lookup_symbol(self, asset_finder): # Incrementing by two so that start and end dates for each # generated Asset don't overlap (each Asset's end_date is the # day after its start date.) - dates = pd.date_range("2013-01-01", freq="2D", periods=5, tz="UTC") + dates = pd.date_range("2013-01-01", freq="2D", periods=5) df = pd.DataFrame.from_records( [ { @@ -1976,7 +1969,7 @@ def test_endless_multiple_resolves(self, asset_finder): they should be able to correctly get 2. """ - date = pd.Timestamp("2013-01-01", tz="UTC") + date = pd.Timestamp("2013-01-01") num_countries = 3 df = pd.DataFrame.from_records( concat( @@ -2240,7 +2233,7 @@ class TestVectorizedSymbolLookup: @pytest.mark.parametrize( "as_of", pd.to_datetime( - ["2014-01-02", "2014-01-15", "2014-01-17", "2015-01-02"], utc=True + ["2014-01-02", "2014-01-15", "2014-01-17", "2015-01-02"] ).to_list(), ) @pytest.mark.parametrize( @@ -2267,17 +2260,13 @@ def test_fuzzy(self): # FUZZ.Y shouldn't resolve unless fuzzy=True. syms = ["A", "B", "FUZZ.Y"] - dt = pd.Timestamp("2014-01-15", tz="UTC") + dt = pd.Timestamp("2014-01-15") with pytest.raises(SymbolNotFound): - af.lookup_symbols(syms, pd.Timestamp("2014-01-15", tz="UTC")) + af.lookup_symbols(syms, dt) with pytest.raises(SymbolNotFound): - af.lookup_symbols( - syms, - pd.Timestamp("2014-01-15", tz="UTC"), - fuzzy=False, - ) + af.lookup_symbols(syms, dt, fuzzy=False) results = af.lookup_symbols(syms, dt, fuzzy=True) assert results == af.retrieve_all([1, 3, 7]) @@ -2456,8 +2445,8 @@ def test_write_direct(self): ExchangeInfo("NYSE", "NYSE", "US"), symbol="AYY", asset_name="Ayy Inc.", - start_date=pd.Timestamp(0, tz="UTC"), - end_date=pd.Timestamp.max.tz_localize("UTC"), + start_date=pd.Timestamp(0), + end_date=pd.Timestamp.max, first_traded=None, auto_close_date=None, tick_size=0.01, @@ -2468,8 +2457,8 @@ def test_write_direct(self): ExchangeInfo("TSE", "TSE", "JP"), symbol="LMAO", asset_name="Lmao LP", - start_date=pd.Timestamp(0, tz="UTC"), - end_date=pd.Timestamp.max.tz_localize("UTC"), + start_date=pd.Timestamp(0), + end_date=pd.Timestamp.max, first_traded=None, auto_close_date=None, tick_size=0.01, @@ -2489,16 +2478,16 @@ def test_write_direct(self): expected_supplementary_map = { ("QSIP", str(hash("AYY"))): ( OwnershipPeriod( - start=pd.Timestamp(0, tz="UTC"), - end=pd.Timestamp.max.tz_localize("UTC"), + start=pd.Timestamp(0), + end=pd.Timestamp.max, sid=0, value=str(hash("AYY")), ), ), ("QSIP", str(hash("LMAO"))): ( OwnershipPeriod( - start=pd.Timestamp(0, tz="UTC"), - end=pd.Timestamp.max.tz_localize("UTC"), + start=pd.Timestamp(0), + end=pd.Timestamp.max, sid=1, value=str(hash("LMAO")), ), diff --git a/tests/test_bar_data.py b/tests/test_bar_data.py index 72a044186f..766b37375e 100644 --- a/tests/test_bar_data.py +++ b/tests/test_bar_data.py @@ -107,11 +107,8 @@ def check_internal_consistency(self, bar_data): class TestMinuteBarData( WithCreateBarData, WithBarDataChecks, WithDataPortal, ZiplineTestCase ): - START_DATE = pd.Timestamp("2016-01-05", tz="UTC") - END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp( - "2016-01-07", - tz="UTC", - ) + START_DATE = pd.Timestamp("2016-01-05") + END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2016-01-07") ASSET_FINDER_EQUITY_SIDS = 1, 2, 3, 4, 5 @@ -154,17 +151,17 @@ def make_futures_info(cls): 6: { "symbol": "CLG06", "root_symbol": "CL", - "start_date": pd.Timestamp("2005-12-01", tz="UTC"), - "notice_date": pd.Timestamp("2005-12-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-01-20", tz="UTC"), + "start_date": pd.Timestamp("2005-12-01"), + "notice_date": pd.Timestamp("2005-12-20"), + "expiration_date": pd.Timestamp("2006-01-20"), "exchange": "ICEUS", }, 7: { "symbol": "CLK06", "root_symbol": "CL", - "start_date": pd.Timestamp("2005-12-01", tz="UTC"), - "notice_date": pd.Timestamp("2006-03-20", tz="UTC"), - "expiration_date": pd.Timestamp("2006-04-20", tz="UTC"), + "start_date": pd.Timestamp("2005-12-01"), + "notice_date": pd.Timestamp("2006-03-20"), + "expiration_date": pd.Timestamp("2006-04-20"), "exchange": "ICEUS", }, }, @@ -207,17 +204,23 @@ def init_class_fixtures(cls): cls.ASSETS = [cls.ASSET1, cls.ASSET2] def test_current_session(self): - regular_minutes = self.trading_calendar.minutes_for_sessions_in_range( + regular_minutes = self.trading_calendar.sessions_minutes( self.equity_minute_bar_days[0], self.equity_minute_bar_days[-1] ) bts_minutes = days_at_time( - self.equity_minute_bar_days, time(8, 45), "US/Eastern" + self.equity_minute_bar_days, + time(8, 45), + "US/Eastern", + day_offset=0, ) # some other non-market-minute three_oh_six_am_minutes = days_at_time( - self.equity_minute_bar_days, time(3, 6), "US/Eastern" + self.equity_minute_bar_days, + time(3, 6), + "US/Eastern", + day_offset=0, ) all_minutes = [regular_minutes, bts_minutes, three_oh_six_am_minutes] @@ -225,12 +228,12 @@ def test_current_session(self): bar_data = self.create_bardata(lambda: minute) assert ( - self.trading_calendar.minute_to_session_label(minute) + self.trading_calendar.minute_to_session(minute) == bar_data.current_session ) def test_current_session_minutes(self): - first_day_minutes = self.trading_calendar.minutes_for_session( + first_day_minutes = self.trading_calendar.session_minutes( self.equity_minute_bar_days[0] ) @@ -242,12 +245,12 @@ def test_current_session_minutes(self): def test_minute_before_assets_trading(self): # grab minutes that include the day before the asset start - minutes = self.trading_calendar.minutes_for_session( - self.trading_calendar.previous_session_label(self.equity_minute_bar_days[0]) + minutes = self.trading_calendar.session_minutes( + self.trading_calendar.previous_session(self.equity_minute_bar_days[0]) ) # this entire day is before either asset has started trading - for idx, minute in enumerate(minutes): + for _, minute in enumerate(minutes): bar_data = self.create_bardata( lambda: minute, ) @@ -271,9 +274,7 @@ def test_minute_before_assets_trading(self): assert asset_value is pd.NaT def test_regular_minute(self): - minutes = self.trading_calendar.minutes_for_session( - self.equity_minute_bar_days[0] - ) + minutes = self.trading_calendar.session_minutes(self.equity_minute_bar_days[0]) for idx, minute in enumerate(minutes): # day2 has prices @@ -361,12 +362,12 @@ def test_regular_minute(self): ) def test_minute_of_last_day(self): - minutes = self.trading_calendar.minutes_for_session( + minutes = self.trading_calendar.session_minutes( self.equity_daily_bar_days[-1], ) # this is the last day the assets exist - for idx, minute in enumerate(minutes): + for _, minute in enumerate(minutes): bar_data = self.create_bardata( lambda: minute, ) @@ -375,16 +376,16 @@ def test_minute_of_last_day(self): assert bar_data.can_trade(self.ASSET2) def test_minute_after_assets_stopped(self): - minutes = self.trading_calendar.minutes_for_session( - self.trading_calendar.next_session_label(self.equity_minute_bar_days[-1]) + minutes = self.trading_calendar.session_minutes( + self.trading_calendar.next_session(self.equity_minute_bar_days[-1]) ) - last_trading_minute = self.trading_calendar.minutes_for_session( + last_trading_minute = self.trading_calendar.session_minutes( self.equity_minute_bar_days[-1] )[-1] # this entire day is after both assets have stopped trading - for idx, minute in enumerate(minutes): + for _, minute in enumerate(minutes): bar_data = self.create_bardata( lambda: minute, ) @@ -416,10 +417,10 @@ def test_get_value_is_unadjusted(self): assert 1 == len(splits) split = splits[0] - assert split[0] == pd.Timestamp("2016-01-06", tz="UTC") + assert split[0] == pd.Timestamp("2016-01-06") # ... but that's it's not applied when using spot value - minutes = self.trading_calendar.minutes_for_sessions_in_range( + minutes = self.trading_calendar.sessions_minutes( self.equity_minute_bar_days[0], self.equity_minute_bar_days[1] ) @@ -432,14 +433,14 @@ def test_get_value_is_unadjusted(self): def test_get_value_is_adjusted_if_needed(self): # on cls.days[1], the first 9 minutes of ILLIQUID_SPLIT_ASSET are # missing. let's get them. - day0_minutes = self.trading_calendar.minutes_for_session( + day0_minutes = self.trading_calendar.session_minutes( self.equity_minute_bar_days[0] ) - day1_minutes = self.trading_calendar.minutes_for_session( + day1_minutes = self.trading_calendar.session_minutes( self.equity_minute_bar_days[1] ) - for idx, minute in enumerate(day0_minutes[-10:-1]): + for _, minute in enumerate(day0_minutes[-10:-1]): bar_data = self.create_bardata( lambda: minute, ) @@ -451,7 +452,7 @@ def test_get_value_is_adjusted_if_needed(self): assert 390 == bar_data.current(self.ILLIQUID_SPLIT_ASSET, "price") - for idx, minute in enumerate(day1_minutes[0:9]): + for _, minute in enumerate(day1_minutes[0:9]): bar_data = self.create_bardata( lambda: minute, ) @@ -483,9 +484,7 @@ def test_get_value_at_midnight(self): # make sure that if the asset didn't trade at the previous # close, we properly ffill (or not ffill) assert 350 == bd.current(self.HILARIOUSLY_ILLIQUID_ASSET, "price") - assert np.isnan(bd.current(self.HILARIOUSLY_ILLIQUID_ASSET, "high")) - assert 0 == bd.current(self.HILARIOUSLY_ILLIQUID_ASSET, "volume") def test_get_value_during_non_market_hours(self): @@ -509,10 +508,10 @@ def test_can_trade_equity_same_cal_outside_lifetime(self): # verify that can_trade returns False for the session before the # asset's first session - session_before_asset1_start = self.trading_calendar.previous_session_label( + session_before_asset1_start = self.trading_calendar.previous_session( self.ASSET1.start_date ) - minutes_for_session = self.trading_calendar.minutes_for_session( + minutes_for_session = self.trading_calendar.session_minutes( session_before_asset1_start ) @@ -529,17 +528,15 @@ def test_can_trade_equity_same_cal_outside_lifetime(self): assert not bar_data.can_trade(self.ASSET1) # after asset lifetime - session_after_asset1_end = self.trading_calendar.next_session_label( + session_after_asset1_end = self.trading_calendar.next_session( self.ASSET1.end_date ) - bts_after_asset1_end = ( - session_after_asset1_end.replace(hour=8, minute=45) - .tz_convert(None) - .tz_localize("US/Eastern") - ) + bts_after_asset1_end = session_after_asset1_end.replace( + hour=8, minute=45 + ).tz_localize("US/Eastern") minutes_to_check = chain( - self.trading_calendar.minutes_for_session(session_after_asset1_end), + self.trading_calendar.session_minutes(session_after_asset1_end), [bts_after_asset1_end], ) @@ -555,7 +552,7 @@ def test_can_trade_equity_same_cal_exchange_closed(self): # outside the asset's calendar (assuming the asset is alive and # there is a last price), because the asset is alive on the # next market minute. - minutes = self.trading_calendar.minutes_for_sessions_in_range( + minutes = self.trading_calendar.sessions_minutes( self.ASSET1.start_date, self.ASSET1.end_date ) @@ -572,7 +569,7 @@ def test_can_trade_equity_same_cal_no_last_price(self): # for all minutes in that session before the first trade, and true # for all minutes afterwards. - minutes_in_session = self.trading_calendar.minutes_for_session( + minutes_in_session = self.trading_calendar.session_minutes( self.ASSET1.start_date ) @@ -606,7 +603,7 @@ def test_overnight_adjustments(self): assert 1 == len(splits) split = splits[0] - assert split[0] == pd.Timestamp("2016-01-06", tz="UTC") + assert split[0] == pd.Timestamp("2016-01-06") # Current day is 1/06/16 day = self.equity_daily_bar_days[1] @@ -635,8 +632,7 @@ def test_overnight_adjustments(self): assert value == expected[field] def test_can_trade_restricted(self): - """ - Test that can_trade will return False for a sid if it is restricted + """Test that can_trade will return False for a sid if it is restricted on that dt """ @@ -673,11 +669,8 @@ class TestMinuteBarDataFuturesCalendar( WithCreateBarData, WithBarDataChecks, ZiplineTestCase ): - START_DATE = pd.Timestamp("2016-01-05", tz="UTC") - END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp( - "2016-01-07", - tz="UTC", - ) + START_DATE = pd.Timestamp("2016-01-05") + END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2016-01-07") ASSET_FINDER_EQUITY_SIDS = [1] @@ -697,18 +690,18 @@ def make_futures_info(cls): 6: { "symbol": "CLH16", "root_symbol": "CL", - "start_date": pd.Timestamp("2016-01-04", tz="UTC"), - "notice_date": pd.Timestamp("2016-01-19", tz="UTC"), - "expiration_date": pd.Timestamp("2016-02-19", tz="UTC"), + "start_date": pd.Timestamp("2016-01-04"), + "notice_date": pd.Timestamp("2016-01-19"), + "expiration_date": pd.Timestamp("2016-02-19"), "exchange": "ICEUS", }, 7: { "symbol": "FVH16", "root_symbol": "FV", - "start_date": pd.Timestamp("2016-01-04", tz="UTC"), - "notice_date": pd.Timestamp("2016-01-22", tz="UTC"), - "expiration_date": pd.Timestamp("2016-02-22", tz="UTC"), - "auto_close_date": pd.Timestamp("2016-01-20", tz="UTC"), + "start_date": pd.Timestamp("2016-01-04"), + "notice_date": pd.Timestamp("2016-01-22"), + "expiration_date": pd.Timestamp("2016-02-22"), + "auto_close_date": pd.Timestamp("2016-01-20"), "exchange": "CMES", }, }, @@ -798,11 +791,8 @@ def test_can_trade_delisted(self): class TestDailyBarData( WithCreateBarData, WithBarDataChecks, WithDataPortal, ZiplineTestCase ): - START_DATE = pd.Timestamp("2016-01-05", tz="UTC") - END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp( - "2016-01-11", - tz="UTC", - ) + START_DATE = pd.Timestamp("2016-01-05") + END_DATE = ASSET_FINDER_EQUITY_END_DATE = pd.Timestamp("2016-01-11") CREATE_BARDATA_DATA_FREQUENCY = "daily" ASSET_FINDER_EQUITY_SIDS = set(range(1, 9)) @@ -817,7 +807,7 @@ class TestDailyBarData( @classmethod def make_equity_info(cls): frame = super(TestDailyBarData, cls).make_equity_info() - frame.loc[[1, 2], "end_date"] = pd.Timestamp("2016-01-08", tz="UTC") + frame.loc[[1, 2], "end_date"] = pd.Timestamp("2016-01-08") return frame @classmethod @@ -860,22 +850,18 @@ def make_dividends_data(cls): [ { # only care about ex date, the other dates don't matter here - "ex_date": pd.Timestamp("2016-01-06", tz="UTC").to_datetime64(), - "record_date": pd.Timestamp("2016-01-06", tz="UTC").to_datetime64(), - "declared_date": pd.Timestamp( - "2016-01-06", tz="UTC" - ).to_datetime64(), - "pay_date": pd.Timestamp("2016-01-06", tz="UTC").to_datetime64(), + "ex_date": pd.Timestamp("2016-01-06").to_datetime64(), + "record_date": pd.Timestamp("2016-01-06").to_datetime64(), + "declared_date": pd.Timestamp("2016-01-06").to_datetime64(), + "pay_date": pd.Timestamp("2016-01-06").to_datetime64(), "amount": 2.0, "sid": cls.DIVIDEND_ASSET_SID, }, { - "ex_date": pd.Timestamp("2016-01-07", tz="UTC").to_datetime64(), - "record_date": pd.Timestamp("2016-01-07", tz="UTC").to_datetime64(), - "declared_date": pd.Timestamp( - "2016-01-07", tz="UTC" - ).to_datetime64(), - "pay_date": pd.Timestamp("2016-01-07", tz="UTC").to_datetime64(), + "ex_date": pd.Timestamp("2016-01-07").to_datetime64(), + "record_date": pd.Timestamp("2016-01-07").to_datetime64(), + "declared_date": pd.Timestamp("2016-01-07").to_datetime64(), + "pay_date": pd.Timestamp("2016-01-07").to_datetime64(), "amount": 4.0, "sid": cls.ILLIQUID_DIVIDEND_ASSET_SID, }, @@ -937,7 +923,7 @@ def init_class_fixtures(cls): cls.ASSETS = [cls.ASSET1, cls.ASSET2] def get_last_minute_of_session(self, session_label): - return self.trading_calendar.open_and_close_for_session(session_label)[1] + return self.trading_calendar.session_close(session_label) def test_current_session(self): for session in self.trading_calendar.sessions_in_range( @@ -952,7 +938,7 @@ def test_current_session(self): def test_day_before_assets_trading(self): # use the day before self.bcolz_daily_bar_days[0] minute = self.get_last_minute_of_session( - self.trading_calendar.previous_session_label(self.equity_daily_bar_days[0]) + self.trading_calendar.previous_session(self.equity_daily_bar_days[0]) ) bar_data = self.create_bardata( @@ -982,7 +968,7 @@ def test_semi_active_day(self): bar_data = self.create_bardata( simulation_dt_func=lambda: self.get_last_minute_of_session( self.equity_daily_bar_days[0] - ), + ).tz_convert(None), ) self.check_internal_consistency(bar_data) @@ -1110,7 +1096,7 @@ def test_get_value_adjustments( assert 1 == len(adjustments) adjustment = adjustments[0] - assert adjustment[0] == pd.Timestamp("2016-01-06", tz="UTC") + assert adjustment[0] == pd.Timestamp("2016-01-06") # ... but that's it's not applied when using spot value bar_data = self.create_bardata( @@ -1146,8 +1132,7 @@ def test_get_value_adjustments( ) def test_can_trade_restricted(self): - """ - Test that can_trade will return False for a sid if it is restricted + """Test that can_trade will return False for a sid if it is restricted on that dt """ diff --git a/tests/test_benchmark.py b/tests/test_benchmark.py index 703e9415f4..246d81e4a7 100644 --- a/tests/test_benchmark.py +++ b/tests/test_benchmark.py @@ -45,8 +45,8 @@ @pytest.fixture(scope="class") def set_test_benchmark_spec(request, with_asset_finder): ASSET_FINDER_COUNTRY_CODE = "??" - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-12-29", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-12-29") request.cls.START_DATE = START_DATE request.cls.END_DATE = END_DATE @@ -94,8 +94,8 @@ def set_test_benchmark_spec(request, with_asset_finder): class TestBenchmark( WithDataPortal, WithSimParams, WithTradingCalendars, ZiplineTestCase ): - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-12-29", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-12-29") @classmethod def make_equity_info(cls): @@ -115,8 +115,8 @@ def make_equity_info(cls): }, 3: { "symbol": "C", - "start_date": pd.Timestamp("2006-05-26", tz="utc"), - "end_date": pd.Timestamp("2006-08-09", tz="utc"), + "start_date": pd.Timestamp("2006-05-26"), + "end_date": pd.Timestamp("2006-08-09"), "exchange": "TEST", }, 4: { @@ -222,13 +222,13 @@ def test_asset_not_trading(self): def test_asset_IPOed_same_day(self): # gotta get some minute data up in here. # add sid 4 for a couple of days - minutes = self.trading_calendar.minutes_for_sessions_in_range( + minutes = self.trading_calendar.sessions_minutes( self.sim_params.sessions[0], self.sim_params.sessions[5] ) tmp_reader = tmp_bcolz_equity_minute_bar_reader( self.trading_calendar, - self.trading_calendar.all_sessions, + self.trading_calendar.sessions, create_minute_bar_data(minutes, [2]), ) with tmp_reader as reader: @@ -376,6 +376,7 @@ def test_benchmark_sid(self, input_sid, caplog): def test_benchmark_file(self, tmp_path, caplog): """Test running with a benchmark file.""" + csv_file_path = tmp_path / "b.csv" with open(csv_file_path, "w") as csv_file: csv_file.write( @@ -398,12 +399,15 @@ def test_benchmark_file(self, tmp_path, caplog): assert sid is None - expected_dates = pd.to_datetime( - ["2020-01-03", "2020-01-06", "2020-01-07", "2020-01-08", "2020-01-09"], - utc=True, + expected_returns = pd.Series( + { + pd.Timestamp("2020-01-03"): -0.1, + pd.Timestamp("2020-01-06"): 0.333, + pd.Timestamp("2020-01-07"): 0.167, + pd.Timestamp("2020-01-08"): 0.143, + pd.Timestamp("2020-01-09"): 6.375, + } ) - expected_values = [-0.1, 0.333, 0.167, 0.143, 6.375] - expected_returns = pd.Series(index=expected_dates, data=expected_values) assert_series_equal(returns, expected_returns, check_names=False) diff --git a/tests/test_blotter.py b/tests/test_blotter.py index 3e1e7cc1c6..c3be7609c3 100644 --- a/tests/test_blotter.py +++ b/tests/test_blotter.py @@ -45,8 +45,8 @@ class BlotterTestCase( WithCreateBarData, WithDataPortal, WithSimParams, ZiplineTestCase ): - START_DATE = pd.Timestamp("2006-01-05", tz="utc") - END_DATE = pd.Timestamp("2006-01-06", tz="utc") + START_DATE = pd.Timestamp("2006-01-05") + END_DATE = pd.Timestamp("2006-01-06") ASSET_FINDER_EQUITY_SIDS = 24, 25 @classmethod diff --git a/tests/test_clock.py b/tests/test_clock.py index 1ff58b1654..95871aac26 100644 --- a/tests/test_clock.py +++ b/tests/test_clock.py @@ -22,9 +22,10 @@ def set_session(request): pd.Timestamp("2016-07-15"), pd.Timestamp("2016-07-19") ) - trading_o_and_c = request.cls.nyse_calendar.schedule.loc[request.cls.sessions] - request.cls.opens = trading_o_and_c["market_open"] - request.cls.closes = trading_o_and_c["market_close"] + request.cls.opens = request.cls.nyse_calendar.first_minutes[request.cls.sessions] + request.cls.closes = request.cls.nyse_calendar.schedule.loc[ + request.cls.sessions, "close" + ] @pytest.mark.usefixtures("set_session") @@ -34,18 +35,23 @@ def test_bts_before_session(self): self.sessions, self.opens, self.closes, - days_at_time(self.sessions, time(6, 17), "US/Eastern"), + days_at_time( + self.sessions, + time(6, 17), + "US/Eastern", + day_offset=0, + ), False, ) all_events = list(clock) def _check_session_bts_first(session_label, events, bts_dt): - minutes = self.nyse_calendar.minutes_for_session(session_label) + minutes = self.nyse_calendar.session_minutes(session_label) assert 393 == len(events) - assert events[0] == (session_label, SESSION_START) + assert events[0] == (session_label.tz_localize("UTC"), SESSION_START) assert events[1] == (bts_dt, BEFORE_TRADING_START_BAR) for i in range(2, 392): assert events[i] == (minutes[i - 2], BAR) @@ -104,11 +110,11 @@ def test_bts_on_last_minute(self): def verify_bts_during_session(self, bts_time, bts_session_times, bts_idx): def _check_session_bts_during(session_label, events, bts_dt): - minutes = self.nyse_calendar.minutes_for_session(session_label) + minutes = self.nyse_calendar.session_minutes(session_label) assert 393 == len(events) - assert events[0] == (session_label, SESSION_START) + assert events[0] == (session_label.tz_localize("UTC"), SESSION_START) for i in range(1, bts_idx): assert events[i] == (minutes[i - 1], BAR) @@ -124,7 +130,7 @@ def _check_session_bts_during(session_label, events, bts_dt): self.sessions, self.opens, self.closes, - days_at_time(self.sessions, bts_time, "US/Eastern"), + days_at_time(self.sessions, bts_time, "US/Eastern", day_offset=0), False, ) @@ -147,7 +153,7 @@ def test_bts_after_session(self): self.sessions, self.opens, self.closes, - days_at_time(self.sessions, time(19, 5), "US/Eastern"), + days_at_time(self.sessions, time(19, 5), "US/Eastern", day_offset=0), False, ) @@ -158,10 +164,10 @@ def test_bts_after_session(self): # 390 BARs, and then SESSION_END def _check_session_bts_after(session_label, events): - minutes = self.nyse_calendar.minutes_for_session(session_label) + minutes = self.nyse_calendar.session_minutes(session_label) assert 392 == len(events) - assert events[0] == (session_label, SESSION_START) + assert events[0] == (session_label.tz_localize("UTC"), SESSION_START) for i in range(1, 391): assert events[i] == (minutes[i - 1], BAR) diff --git a/tests/test_continuous_futures.py b/tests/test_continuous_futures.py index b511efd807..4f65ba98ad 100644 --- a/tests/test_continuous_futures.py +++ b/tests/test_continuous_futures.py @@ -46,11 +46,11 @@ def set_test_ordered_futures_contracts(request, with_asset_finder): "asset_name": ["Foo"] * 4, "symbol": ["FOF16", "FOG16", "FOH16", "FOJ16"], "sid": range(1, 5), - "start_date": pd.date_range("2015-01-01", periods=4, tz="UTC"), - "end_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "notice_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "expiration_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), - "auto_close_date": pd.date_range("2016-01-01", periods=4, tz="UTC"), + "start_date": pd.date_range("2015-01-01", periods=4), + "end_date": pd.date_range("2016-01-01", periods=4), + "notice_date": pd.date_range("2016-01-01", periods=4), + "expiration_date": pd.date_range("2016-01-01", periods=4), + "auto_close_date": pd.date_range("2016-01-01", periods=4), "tick_size": [0.001] * 4, "multiplier": [1000.0] * 4, "exchange": ["CMES"] * 4, @@ -65,11 +65,11 @@ def set_test_ordered_futures_contracts(request, with_asset_finder): "asset_name": ["Bar"] * 3, "symbol": ["BAF16", "BAG16", "BAH16"], "sid": range(5, 8), - "start_date": pd.date_range("2015-01-01", periods=3, tz="UTC"), - "end_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "notice_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "expiration_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), - "auto_close_date": pd.date_range("2016-01-01", periods=3, tz="UTC"), + "start_date": pd.date_range("2015-01-01", periods=3), + "end_date": pd.date_range("2016-01-01", periods=3), + "notice_date": pd.date_range("2016-01-01", periods=3), + "expiration_date": pd.date_range("2016-01-01", periods=3), + "auto_close_date": pd.date_range("2016-01-01", periods=3), "tick_size": [0.001] * 3, "multiplier": [1000.0] * 3, "exchange": ["CMES"] * 3, @@ -86,34 +86,32 @@ def set_test_ordered_futures_contracts(request, with_asset_finder): "symbol": ["BZF15", "BZG15", "BZH15", "BZJ16"], "sid": range(8, 12), "start_date": [ - pd.Timestamp("2015-01-02", tz="UTC"), - pd.Timestamp("2015-01-03", tz="UTC"), - pd.Timestamp("2015-02-23", tz="UTC"), - pd.Timestamp("2015-02-24", tz="UTC"), + pd.Timestamp("2015-01-02"), + pd.Timestamp("2015-01-03"), + pd.Timestamp("2015-02-23"), + pd.Timestamp("2015-02-24"), ], "end_date": pd.date_range( "2015-02-01", periods=4, freq="MS", - tz="UTC", ), "notice_date": [ - pd.Timestamp("2014-12-31", tz="UTC"), - pd.Timestamp("2015-02-18", tz="UTC"), - pd.Timestamp("2015-03-18", tz="UTC"), - pd.Timestamp("2015-04-17", tz="UTC"), + pd.Timestamp("2014-12-31"), + pd.Timestamp("2015-02-18"), + pd.Timestamp("2015-03-18"), + pd.Timestamp("2015-04-17"), ], "expiration_date": pd.date_range( "2015-02-01", periods=4, freq="MS", - tz="UTC", ), "auto_close_date": [ - pd.Timestamp("2014-12-29", tz="UTC"), - pd.Timestamp("2015-02-16", tz="UTC"), - pd.Timestamp("2015-03-16", tz="UTC"), - pd.Timestamp("2015-04-15", tz="UTC"), + pd.Timestamp("2014-12-29"), + pd.Timestamp("2015-02-16"), + pd.Timestamp("2015-03-16"), + pd.Timestamp("2015-04-15"), ], "tick_size": [0.001] * 4, "multiplier": [1000.0] * 4, @@ -140,11 +138,11 @@ def set_test_ordered_futures_contracts(request, with_asset_finder): class ContinuousFuturesTestCase( zf.WithCreateBarData, zf.WithMakeAlgo, zf.ZiplineTestCase ): - START_DATE = pd.Timestamp("2015-01-05", tz="UTC") - END_DATE = pd.Timestamp("2016-10-19", tz="UTC") + START_DATE = pd.Timestamp("2015-01-05") + END_DATE = pd.Timestamp("2016-10-19") - SIM_PARAMS_START = pd.Timestamp("2016-01-26", tz="UTC") - SIM_PARAMS_END = pd.Timestamp("2016-01-28", tz="UTC") + SIM_PARAMS_START = pd.Timestamp("2016-01-26") + SIM_PARAMS_END = pd.Timestamp("2016-01-28") SIM_PARAMS_DATA_FREQUENCY = "minute" TRADING_CALENDAR_STRS = ("us_futures",) TRADING_CALENDAR_PRIMARY_CAL = "us_futures" @@ -180,55 +178,55 @@ def make_futures_info(cls): "root_symbol": ["FOOBAR"] * 7, "asset_name": ["Foo"] * 7, "start_date": [ - pd.Timestamp("2015-01-05", tz="UTC"), - pd.Timestamp("2015-02-05", tz="UTC"), - pd.Timestamp("2015-03-05", tz="UTC"), - pd.Timestamp("2015-04-05", tz="UTC"), - pd.Timestamp("2015-05-05", tz="UTC"), - pd.Timestamp("2021-01-05", tz="UTC"), - pd.Timestamp("2015-01-05", tz="UTC"), + pd.Timestamp("2015-01-05"), + pd.Timestamp("2015-02-05"), + pd.Timestamp("2015-03-05"), + pd.Timestamp("2015-04-05"), + pd.Timestamp("2015-05-05"), + pd.Timestamp("2021-01-05"), + pd.Timestamp("2015-01-05"), ], "end_date": [ - pd.Timestamp("2016-08-19", tz="UTC"), - pd.Timestamp("2016-09-19", tz="UTC"), - pd.Timestamp("2016-10-19", tz="UTC"), - pd.Timestamp("2016-11-19", tz="UTC"), - pd.Timestamp("2022-08-19", tz="UTC"), - pd.Timestamp("2022-09-19", tz="UTC"), + pd.Timestamp("2016-08-19"), + pd.Timestamp("2016-09-19"), + pd.Timestamp("2016-10-19"), + pd.Timestamp("2016-11-19"), + pd.Timestamp("2022-08-19"), + pd.Timestamp("2022-09-19"), # Set the last contract's end date (which is the last # date for which there is data to a value that is # within the range of the dates being tested. This # models real life scenarios where the end date of the # furthest out contract is not necessarily the # greatest end date all contracts in the chain. - pd.Timestamp("2015-02-05", tz="UTC"), + pd.Timestamp("2015-02-05"), ], "notice_date": [ - pd.Timestamp("2016-01-27", tz="UTC"), - pd.Timestamp("2016-02-26", tz="UTC"), - pd.Timestamp("2016-03-24", tz="UTC"), - pd.Timestamp("2016-04-26", tz="UTC"), - pd.Timestamp("2016-05-26", tz="UTC"), - pd.Timestamp("2022-01-26", tz="UTC"), - pd.Timestamp("2022-02-26", tz="UTC"), + pd.Timestamp("2016-01-27"), + pd.Timestamp("2016-02-26"), + pd.Timestamp("2016-03-24"), + pd.Timestamp("2016-04-26"), + pd.Timestamp("2016-05-26"), + pd.Timestamp("2022-01-26"), + pd.Timestamp("2022-02-26"), ], "expiration_date": [ - pd.Timestamp("2016-01-27", tz="UTC"), - pd.Timestamp("2016-02-26", tz="UTC"), - pd.Timestamp("2016-03-24", tz="UTC"), - pd.Timestamp("2016-04-26", tz="UTC"), - pd.Timestamp("2016-05-26", tz="UTC"), - pd.Timestamp("2022-01-26", tz="UTC"), - pd.Timestamp("2022-02-26", tz="UTC"), + pd.Timestamp("2016-01-27"), + pd.Timestamp("2016-02-26"), + pd.Timestamp("2016-03-24"), + pd.Timestamp("2016-04-26"), + pd.Timestamp("2016-05-26"), + pd.Timestamp("2022-01-26"), + pd.Timestamp("2022-02-26"), ], "auto_close_date": [ - pd.Timestamp("2016-01-27", tz="UTC"), - pd.Timestamp("2016-02-26", tz="UTC"), - pd.Timestamp("2016-03-24", tz="UTC"), - pd.Timestamp("2016-04-26", tz="UTC"), - pd.Timestamp("2016-05-26", tz="UTC"), - pd.Timestamp("2022-01-26", tz="UTC"), - pd.Timestamp("2022-02-26", tz="UTC"), + pd.Timestamp("2016-01-27"), + pd.Timestamp("2016-02-26"), + pd.Timestamp("2016-03-24"), + pd.Timestamp("2016-04-26"), + pd.Timestamp("2016-05-26"), + pd.Timestamp("2022-01-26"), + pd.Timestamp("2022-02-26"), ], "tick_size": [0.001] * 7, "multiplier": [1000.0] * 7, @@ -245,29 +243,29 @@ def make_futures_info(cls): "asset_name": ["Baz"] * 3, "sid": range(10, 13), "start_date": [ - pd.Timestamp("2005-01-01", tz="UTC"), - pd.Timestamp("2005-01-21", tz="UTC"), - pd.Timestamp("2005-01-21", tz="UTC"), + pd.Timestamp("2005-01-01"), + pd.Timestamp("2005-01-21"), + pd.Timestamp("2005-01-21"), ], "end_date": [ - pd.Timestamp("2016-08-19", tz="UTC"), - pd.Timestamp("2016-11-21", tz="UTC"), - pd.Timestamp("2016-10-19", tz="UTC"), + pd.Timestamp("2016-08-19"), + pd.Timestamp("2016-11-21"), + pd.Timestamp("2016-10-19"), ], "notice_date": [ - pd.Timestamp("2016-01-11", tz="UTC"), - pd.Timestamp("2016-02-08", tz="UTC"), - pd.Timestamp("2016-03-09", tz="UTC"), + pd.Timestamp("2016-01-11"), + pd.Timestamp("2016-02-08"), + pd.Timestamp("2016-03-09"), ], "expiration_date": [ - pd.Timestamp("2016-01-11", tz="UTC"), - pd.Timestamp("2016-02-08", tz="UTC"), - pd.Timestamp("2016-03-09", tz="UTC"), + pd.Timestamp("2016-01-11"), + pd.Timestamp("2016-02-08"), + pd.Timestamp("2016-03-09"), ], "auto_close_date": [ - pd.Timestamp("2016-01-11", tz="UTC"), - pd.Timestamp("2016-02-08", tz="UTC"), - pd.Timestamp("2016-03-09", tz="UTC"), + pd.Timestamp("2016-01-11"), + pd.Timestamp("2016-02-08"), + pd.Timestamp("2016-03-09"), ], "tick_size": [0.001] * 3, "multiplier": [1000.0] * 3, @@ -283,29 +281,29 @@ def make_futures_info(cls): "asset_name": ["Most Active"] * 3, "sid": range(14, 17), "start_date": [ - pd.Timestamp("2005-01-01", tz="UTC"), - pd.Timestamp("2005-01-21", tz="UTC"), - pd.Timestamp("2005-01-21", tz="UTC"), + pd.Timestamp("2005-01-01"), + pd.Timestamp("2005-01-21"), + pd.Timestamp("2005-01-21"), ], "end_date": [ - pd.Timestamp("2016-08-19", tz="UTC"), - pd.Timestamp("2016-11-21", tz="UTC"), - pd.Timestamp("2016-10-19", tz="UTC"), + pd.Timestamp("2016-08-19"), + pd.Timestamp("2016-11-21"), + pd.Timestamp("2016-10-19"), ], "notice_date": [ - pd.Timestamp("2016-02-17", tz="UTC"), - pd.Timestamp("2016-03-16", tz="UTC"), - pd.Timestamp("2016-04-13", tz="UTC"), + pd.Timestamp("2016-02-17"), + pd.Timestamp("2016-03-16"), + pd.Timestamp("2016-04-13"), ], "expiration_date": [ - pd.Timestamp("2016-02-17", tz="UTC"), - pd.Timestamp("2016-03-16", tz="UTC"), - pd.Timestamp("2016-04-13", tz="UTC"), + pd.Timestamp("2016-02-17"), + pd.Timestamp("2016-03-16"), + pd.Timestamp("2016-04-13"), ], "auto_close_date": [ - pd.Timestamp("2016-02-17", tz="UTC"), - pd.Timestamp("2016-03-16", tz="UTC"), - pd.Timestamp("2016-04-13", tz="UTC"), + pd.Timestamp("2016-02-17"), + pd.Timestamp("2016-03-16"), + pd.Timestamp("2016-04-13"), ], "tick_size": [0.001] * 3, "multiplier": [1000.0] * 3, @@ -323,29 +321,29 @@ def make_futures_info(cls): "asset_name": ["Double Flip"] * 3, "sid": range(17, 20), "start_date": [ - pd.Timestamp("2005-01-01", tz="UTC"), - pd.Timestamp("2005-02-01", tz="UTC"), - pd.Timestamp("2005-03-01", tz="UTC"), + pd.Timestamp("2005-01-01"), + pd.Timestamp("2005-02-01"), + pd.Timestamp("2005-03-01"), ], "end_date": [ - pd.Timestamp("2016-08-19", tz="UTC"), - pd.Timestamp("2016-09-19", tz="UTC"), - pd.Timestamp("2016-10-19", tz="UTC"), + pd.Timestamp("2016-08-19"), + pd.Timestamp("2016-09-19"), + pd.Timestamp("2016-10-19"), ], "notice_date": [ - pd.Timestamp("2016-02-19", tz="UTC"), - pd.Timestamp("2016-03-18", tz="UTC"), - pd.Timestamp("2016-04-22", tz="UTC"), + pd.Timestamp("2016-02-19"), + pd.Timestamp("2016-03-18"), + pd.Timestamp("2016-04-22"), ], "expiration_date": [ - pd.Timestamp("2016-02-19", tz="UTC"), - pd.Timestamp("2016-03-18", tz="UTC"), - pd.Timestamp("2016-04-22", tz="UTC"), + pd.Timestamp("2016-02-19"), + pd.Timestamp("2016-03-18"), + pd.Timestamp("2016-04-22"), ], "auto_close_date": [ - pd.Timestamp("2016-02-17", tz="UTC"), - pd.Timestamp("2016-03-16", tz="UTC"), - pd.Timestamp("2016-04-20", tz="UTC"), + pd.Timestamp("2016-02-17"), + pd.Timestamp("2016-03-16"), + pd.Timestamp("2016-04-20"), ], "tick_size": [0.001] * 3, "multiplier": [1000.0] * 3, @@ -358,9 +356,9 @@ def make_futures_info(cls): @classmethod def make_future_minute_bar_data(cls): tc = cls.trading_calendar - start = pd.Timestamp("2016-01-26", tz="UTC") - end = pd.Timestamp("2016-04-29", tz="UTC") - dts = tc.minutes_for_sessions_in_range(start, end) + start = pd.Timestamp("2016-01-26") + end = pd.Timestamp("2016-04-29") + dts = tc.sessions_minutes(start, end) sessions = tc.sessions_in_range(start, end) # Generate values in the XXY.YYY space, with XX representing the # session and Y.YYY representing the minute within the session. @@ -410,18 +408,18 @@ def make_future_minute_bar_data(cls): # so that it does not particpate in volume rolls. sid_to_vol_stop_session = { - 0: pd.Timestamp("2016-01-26", tz="UTC"), - 1: pd.Timestamp("2016-02-26", tz="UTC"), - 2: pd.Timestamp("2016-03-18", tz="UTC"), - 3: pd.Timestamp("2016-04-20", tz="UTC"), - 6: pd.Timestamp("2016-01-27", tz="UTC"), + 0: pd.Timestamp("2016-01-26"), + 1: pd.Timestamp("2016-02-26"), + 2: pd.Timestamp("2016-03-18"), + 3: pd.Timestamp("2016-04-20"), + 6: pd.Timestamp("2016-01-27"), } for i in range(20): df = base_df.copy() df += i * 10000 if i in sid_to_vol_stop_session: vol_stop_session = sid_to_vol_stop_session[i] - m_open = tc.open_and_close_for_session(vol_stop_session)[0] + m_open = tc.session_first_minute(vol_stop_session) loc = dts.searchsorted(m_open) # Add a little bit of noise to roll. So that predicates that # check for exactly 0 do not work, since there may be @@ -431,7 +429,7 @@ def make_future_minute_bar_data(cls): j = i - 1 if j in sid_to_vol_stop_session: non_primary_end = sid_to_vol_stop_session[j] - m_close = tc.open_and_close_for_session(non_primary_end)[1] + m_close = tc.session_close(non_primary_end) if m_close > dts[0]: loc = dts.get_loc(m_close) # Add some volume before a roll, since a contract may be @@ -474,8 +472,7 @@ def make_future_minute_bar_data(cls): yield i, df def test_double_volume_switch(self): - """ - Test that when a double volume switch occurs we treat the first switch + """Test that when a double volume switch occurs we treat the first switch as the roll, assuming it is within a certain distance of the next auto close date. See `VolumeRollFinder._active_contract` for a full explanation and example. @@ -487,10 +484,8 @@ def test_double_volume_switch(self): None, ) - sessions = self.trading_calendar.sessions_in_range( - "2016-02-09", - "2016-02-17", - ) + sessions = self.trading_calendar.sessions_in_range("2016-02-09", "2016-02-17") + for session in sessions: bar_data = self.create_bardata(lambda: session) contract = bar_data.current(cf, "contract") @@ -498,7 +493,7 @@ def test_double_volume_switch(self): # The 'G' contract surpasses the 'F' contract in volume on # 2016-02-10, which means that the 'G' contract should become the # front contract starting on 2016-02-11. - if session < pd.Timestamp("2016-02-11", tz="UTC"): + if session < pd.Timestamp("2016-02-11"): assert contract.symbol == "DFF16" else: assert contract.symbol == "DFG16" @@ -509,15 +504,13 @@ def test_double_volume_switch(self): # `VolumeRollFinder._active_contract`. Therefore we should not roll to # the back contract and the front contract should remain current until # its auto close date. - sessions = self.trading_calendar.sessions_in_range( - "2016-03-01", - "2016-03-21", - ) + sessions = self.trading_calendar.sessions_in_range("2016-03-01", "2016-03-21") + for session in sessions: bar_data = self.create_bardata(lambda: session) contract = bar_data.current(cf, "contract") - if session < pd.Timestamp("2016-03-17", tz="UTC"): + if session < pd.Timestamp("2016-03-17"): assert contract.symbol == "DFG16" else: assert contract.symbol == "DFH16" @@ -530,8 +523,8 @@ def test_create_continuous_future(self): assert cf_primary.root_symbol == "FOOBAR" assert cf_primary.offset == 0 assert cf_primary.roll_style == "calendar" - assert cf_primary.start_date == pd.Timestamp("2015-01-05", tz="UTC") - assert cf_primary.end_date == pd.Timestamp("2022-09-19", tz="UTC") + assert cf_primary.start_date == pd.Timestamp("2015-01-05") + assert cf_primary.end_date == pd.Timestamp("2022-09-19") retrieved_primary = self.asset_finder.retrieve_asset(cf_primary.sid) @@ -544,8 +537,8 @@ def test_create_continuous_future(self): assert cf_secondary.root_symbol == "FOOBAR" assert cf_secondary.offset == 1 assert cf_secondary.roll_style == "calendar" - assert cf_primary.start_date == pd.Timestamp("2015-01-05", tz="UTC") - assert cf_primary.end_date == pd.Timestamp("2022-09-19", tz="UTC") + assert cf_primary.start_date == pd.Timestamp("2015-01-05") + assert cf_primary.end_date == pd.Timestamp("2022-09-19") retrieved = self.asset_finder.retrieve_asset(cf_secondary.sid) @@ -562,12 +555,12 @@ def test_current_contract(self): cf_primary = self.asset_finder.create_continuous_future( "FOOBAR", 0, "calendar", None ) - bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-26", tz="UTC")) + bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-26")) contract = bar_data.current(cf_primary, "contract") assert contract.symbol == "FOOBARF16" - bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-27", tz="UTC")) + bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-27")) contract = bar_data.current(cf_primary, "contract") assert contract.symbol == "FOOBARG16", ( @@ -583,7 +576,7 @@ def test_get_value_contract_daily(self): contract = self.data_portal.get_spot_value( cf_primary, "contract", - pd.Timestamp("2016-01-26", tz="UTC"), + pd.Timestamp("2016-01-26"), "daily", ) @@ -592,7 +585,7 @@ def test_get_value_contract_daily(self): contract = self.data_portal.get_spot_value( cf_primary, "contract", - pd.Timestamp("2016-01-27", tz="UTC"), + pd.Timestamp("2016-01-27"), "daily", ) @@ -617,19 +610,13 @@ def test_get_value_close_daily(self): ) value = self.data_portal.get_spot_value( - cf_primary, - "close", - pd.Timestamp("2016-01-26", tz="UTC"), - "daily", + cf_primary, "close", pd.Timestamp("2016-01-26"), "daily" ) assert value == 105011.44 value = self.data_portal.get_spot_value( - cf_primary, - "close", - pd.Timestamp("2016-01-27", tz="UTC"), - "daily", + cf_primary, "close", pd.Timestamp("2016-01-27"), "daily" ) assert value == 115021.44, ( @@ -641,10 +628,7 @@ def test_get_value_close_daily(self): # contract, to prevent a regression where the end date of the last # contract was used instead of the max date of all contracts. value = self.data_portal.get_spot_value( - cf_primary, - "close", - pd.Timestamp("2016-03-26", tz="UTC"), - "daily", + cf_primary, "close", pd.Timestamp("2016-03-26"), "daily" ) assert value == 135441.44, ( @@ -656,12 +640,12 @@ def test_current_contract_volume_roll(self): cf_primary = self.asset_finder.create_continuous_future( "FOOBAR", 0, "volume", None ) - bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-26", tz="UTC")) + bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-26")) contract = bar_data.current(cf_primary, "contract") assert contract.symbol == "FOOBARF16" - bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-27", tz="UTC")) + bar_data = self.create_bardata(lambda: pd.Timestamp("2016-01-27")) contract = bar_data.current(cf_primary, "contract") assert contract.symbol == "FOOBARG16", ( @@ -669,7 +653,7 @@ def test_current_contract_volume_roll(self): "the current contract." ) - bar_data = self.create_bardata(lambda: pd.Timestamp("2016-02-29", tz="UTC")) + bar_data = self.create_bardata(lambda: pd.Timestamp("2016-02-29")) contract = bar_data.current(cf_primary, "contract") assert ( contract.symbol == "FOOBARH16" @@ -678,23 +662,23 @@ def test_current_contract_volume_roll(self): def test_current_contract_in_algo(self): code = dedent( """ -from zipline.api import ( - record, - continuous_future, - schedule_function, - get_datetime, -) - -def initialize(algo): - algo.primary_cl = continuous_future('FOOBAR', 0, 'calendar', None) - algo.secondary_cl = continuous_future('FOOBAR', 1, 'calendar', None) - schedule_function(record_current_contract) - -def record_current_contract(algo, data): - record(datetime=get_datetime()) - record(primary=data.current(algo.primary_cl, 'contract')) - record(secondary=data.current(algo.secondary_cl, 'contract')) -""" + from zipline.api import ( + record, + continuous_future, + schedule_function, + get_datetime, + ) + + def initialize(algo): + algo.primary_cl = continuous_future('FOOBAR', 0, 'calendar', None) + algo.secondary_cl = continuous_future('FOOBAR', 1, 'calendar', None) + schedule_function(record_current_contract) + + def record_current_contract(algo, data): + record(datetime=get_datetime()) + record(primary=data.current(algo.primary_cl, 'contract')) + record(secondary=data.current(algo.secondary_cl, 'contract')) + """ ) results = self.run_algorithm(script=code) result = results.iloc[0] @@ -729,29 +713,29 @@ def record_current_contract(algo, data): def test_current_chain_in_algo(self): code = dedent( """ -from zipline.api import ( - record, - continuous_future, - schedule_function, - get_datetime, -) - -def initialize(algo): - algo.primary_cl = continuous_future('FOOBAR', 0, 'calendar', None) - algo.secondary_cl = continuous_future('FOOBAR', 1, 'calendar', None) - schedule_function(record_current_contract) - -def record_current_contract(algo, data): - record(datetime=get_datetime()) - primary_chain = data.current_chain(algo.primary_cl) - secondary_chain = data.current_chain(algo.secondary_cl) - record(primary_len=len(primary_chain)) - record(primary_first=primary_chain[0].symbol) - record(primary_last=primary_chain[-1].symbol) - record(secondary_len=len(secondary_chain)) - record(secondary_first=secondary_chain[0].symbol) - record(secondary_last=secondary_chain[-1].symbol) -""" + from zipline.api import ( + record, + continuous_future, + schedule_function, + get_datetime, + ) + + def initialize(algo): + algo.primary_cl = continuous_future('FOOBAR', 0, 'calendar', None) + algo.secondary_cl = continuous_future('FOOBAR', 1, 'calendar', None) + schedule_function(record_current_contract) + + def record_current_contract(algo, data): + record(datetime=get_datetime()) + primary_chain = data.current_chain(algo.primary_cl) + secondary_chain = data.current_chain(algo.secondary_cl) + record(primary_len=len(primary_chain)) + record(primary_first=primary_chain[0].symbol) + record(primary_last=primary_chain[-1].symbol) + record(secondary_len=len(secondary_chain)) + record(secondary_first=secondary_chain[0].symbol) + record(secondary_last=secondary_chain[-1].symbol) + """ ) results = self.run_algorithm(script=code) result = results.iloc[0] @@ -1078,58 +1062,58 @@ def test_history_close_session(self): "FOOBAR", 0, "calendar", None ) window = self.data_portal.get_history_window( - [cf.sid], pd.Timestamp("2016-03-06", tz="UTC"), 30, "1d", "close", "daily" + [cf.sid], pd.Timestamp("2016-03-06"), 30, "1d", "close", "daily" ) assert_almost_equal( - window.loc[pd.Timestamp("2016-01-26", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-01-26"), cf.sid], 105011.440, err_msg="At beginning of window, should be FOOBARG16's first value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-26", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-26"), cf.sid], 125241.440, err_msg="On session with roll, should be FOOBARH16's 24th value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-29", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-29"), cf.sid], 125251.440, err_msg="After roll, Should be FOOBARH16's 25th value.", ) # Advance the window a month. window = self.data_portal.get_history_window( - [cf.sid], pd.Timestamp("2016-04-06", tz="UTC"), 30, "1d", "close", "daily" + [cf.sid], pd.Timestamp("2016-04-06"), 30, "1d", "close", "daily" ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-24", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-24"), cf.sid], 115221.440, err_msg="At beginning of window, should be FOOBARG16's 22nd value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-26", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-26"), cf.sid], 125241.440, err_msg="On session with roll, should be FOOBARH16's 24th value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-29", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-29"), cf.sid], 125251.440, err_msg="On session after roll, should be FOOBARH16's 25th value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-03-24", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-03-24"), cf.sid], 135431.440, err_msg="On session with roll, should be FOOBARJ16's 43rd value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-03-28", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-03-28"), cf.sid], 135441.440, err_msg="On session after roll, Should be FOOBARJ16's 44th value.", ) @@ -1139,46 +1123,46 @@ def test_history_close_session_skip_volume(self): "MA", 0, "volume", None ) window = self.data_portal.get_history_window( - [cf.sid], pd.Timestamp("2016-03-06", tz="UTC"), 30, "1d", "close", "daily" + [cf.sid], pd.Timestamp("2016-03-06"), 30, "1d", "close", "daily" ) assert_almost_equal( - window.loc[pd.Timestamp("2016-01-26", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-01-26"), cf.sid], 245011.440, err_msg="At beginning of window, should be MAG16's first value.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-26", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-26"), cf.sid], 265241.440, err_msg="Should have skipped MAH16 to MAJ16.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-29", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-29"), cf.sid], 265251.440, err_msg="Should have remained MAJ16.", ) # Advance the window a month. window = self.data_portal.get_history_window( - [cf.sid], pd.Timestamp("2016-04-06", tz="UTC"), 30, "1d", "close", "daily" + [cf.sid], pd.Timestamp("2016-04-06"), 30, "1d", "close", "daily" ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-24", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-24"), cf.sid], 265221.440, err_msg="Should be MAJ16, having skipped MAH16.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-02-29", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-02-29"), cf.sid], 265251.440, err_msg="Should be MAJ1 for rest of window.", ) assert_almost_equal( - window.loc[pd.Timestamp("2016-03-24", tz="UTC"), cf.sid], + window.loc[pd.Timestamp("2016-03-24"), cf.sid], 265431.440, err_msg="Should be MAJ16 for rest of window.", ) @@ -1195,7 +1179,7 @@ def test_history_close_session_adjusted(self): ) window = self.data_portal.get_history_window( [cf, cf_mul, cf_add], - pd.Timestamp("2016-03-06", tz="UTC"), + pd.Timestamp("2016-03-06"), 30, "1d", "close", @@ -1249,7 +1233,7 @@ def test_history_close_session_adjusted(self): # Advance the window a month. window = self.data_portal.get_history_window( [cf, cf_mul, cf_add], - pd.Timestamp("2016-04-06", tz="UTC"), + pd.Timestamp("2016-04-06"), 30, "1d", "close", @@ -1534,8 +1518,8 @@ def test_history_close_minute_adjusted_volume_roll(self): class RollFinderTestCase(zf.WithBcolzFutureDailyBarReader, zf.ZiplineTestCase): - START_DATE = pd.Timestamp("2017-01-03", tz="UTC") - END_DATE = pd.Timestamp("2017-05-23", tz="UTC") + START_DATE = pd.Timestamp("2017-01-03") + END_DATE = pd.Timestamp("2017-05-23") TRADING_CALENDAR_STRS = ("us_futures",) TRADING_CALENDAR_PRIMARY_CAL = "us_futures" @@ -1556,14 +1540,14 @@ def make_futures_info(cls): two_days = 2 * day end_buffer_days = ROLL_DAYS_FOR_CURRENT_CONTRACT * day - cls.first_end_date = pd.Timestamp("2017-01-20", tz="UTC") - cls.second_end_date = pd.Timestamp("2017-02-17", tz="UTC") - cls.third_end_date = pd.Timestamp("2017-03-17", tz="UTC") + cls.first_end_date = pd.Timestamp("2017-01-20") + cls.second_end_date = pd.Timestamp("2017-02-17") + cls.third_end_date = pd.Timestamp("2017-03-17") cls.third_auto_close_date = cls.third_end_date - two_days cls.fourth_start_date = cls.third_auto_close_date - two_days - cls.fourth_end_date = pd.Timestamp("2017-04-17", tz="UTC") + cls.fourth_end_date = pd.Timestamp("2017-04-17") cls.fourth_auto_close_date = cls.fourth_end_date + two_days - cls.fifth_start_date = pd.Timestamp("2017-03-15", tz="UTC") + cls.fifth_start_date = pd.Timestamp("2017-03-15") cls.fifth_end_date = cls.END_DATE cls.fifth_auto_close_date = cls.fifth_end_date - two_days cls.last_start_date = cls.fourth_end_date @@ -1733,7 +1717,7 @@ def create_contract_data(volume): yield 1001, second_contract_data.copy().loc[: cls.second_end_date] third_contract_data = create_contract_data(5) - volume_flip_date = pd.Timestamp("2017-02-10", tz="UTC") + volume_flip_date = pd.Timestamp("2017-02-10") third_contract_data.loc[volume_flip_date:, "volume"] = 5000 yield 1002, third_contract_data @@ -1762,9 +1746,7 @@ def create_contract_data(volume): yield 2001, create_contract_data(100) def test_volume_roll(self): - """ - Test normally behaving rolls. - """ + """Test normally behaving rolls.""" rolls = self.volume_roll_finder.get_rolls( root_symbol="CL", start=self.START_DATE + self.trading_calendar.day, @@ -1772,8 +1754,8 @@ def test_volume_roll(self): offset=0, ) assert rolls == [ - (1000, pd.Timestamp("2017-01-19", tz="UTC")), - (1001, pd.Timestamp("2017-02-13", tz="UTC")), + (1000, pd.Timestamp("2017-01-19")), + (1001, pd.Timestamp("2017-02-13")), (1002, None), ] @@ -1781,7 +1763,7 @@ def test_no_roll(self): # If we call 'get_rolls' with start and end dates that do not have any # rolls between them, we should still expect the last roll date to be # computed successfully. - date_not_near_roll = pd.Timestamp("2017-02-01", tz="UTC") + date_not_near_roll = pd.Timestamp("2017-02-01") rolls = self.volume_roll_finder.get_rolls( root_symbol="CL", start=date_not_near_roll, @@ -1791,8 +1773,7 @@ def test_no_roll(self): assert rolls == [(1001, None)] def test_roll_in_grace_period(self): - """ - The volume roll finder can look for data up to a week before the given + """The volume roll finder can look for data up to a week before the given date. This test asserts that we not only return the correct active contract during that previous week (grace period), but also that we do not go into exception if one of the contracts does not exist. @@ -1804,7 +1785,7 @@ def test_roll_in_grace_period(self): offset=0, ) assert rolls == [ - (1002, pd.Timestamp("2017-03-16", tz="UTC")), + (1002, pd.Timestamp("2017-03-16")), (1003, None), ] @@ -1818,14 +1799,13 @@ def test_end_before_auto_close(self): offset=0, ) assert rolls == [ - (1002, pd.Timestamp("2017-03-16", tz="UTC")), - (1003, pd.Timestamp("2017-04-18", tz="UTC")), + (1002, pd.Timestamp("2017-03-16")), + (1003, pd.Timestamp("2017-04-18")), (1004, None), ] def test_roll_window_ends_on_auto_close(self): - """ - Test that when skipping over a low volume contract (CLM17), we use the + """Test that when skipping over a low volume contract (CLM17), we use the correct roll date for the previous contract (CLK17) when that contract's auto close date falls on the end date of the roll window. """ @@ -1836,8 +1816,8 @@ def test_roll_window_ends_on_auto_close(self): offset=0, ) assert rolls == [ - (1003, pd.Timestamp("2017-04-18", tz="UTC")), - (1004, pd.Timestamp("2017-05-19", tz="UTC")), + (1003, pd.Timestamp("2017-04-18")), + (1004, pd.Timestamp("2017-05-19")), (1006, None), ] @@ -1850,10 +1830,10 @@ def test_get_contract_center(self): # Test that the current contract adheres to the rolls. assert get_contract_center( - "CL", dt=pd.Timestamp("2017-01-18", tz="UTC") + "CL", dt=pd.Timestamp("2017-01-18") ) == asset_finder.retrieve_asset(1000) assert get_contract_center( - "CL", dt=pd.Timestamp("2017-01-19", tz="UTC") + "CL", dt=pd.Timestamp("2017-01-19") ) == asset_finder.retrieve_asset(1001) # Test that we still get the correct current contract close to or at @@ -1875,7 +1855,7 @@ def test_get_contract_center(self): class TestOrderedContracts: def test_contract_at_offset(self): contract_sids = np.array([1, 2, 3, 4], dtype=np.int64) - start_dates = pd.date_range("2015-01-01", periods=4, tz="UTC") + start_dates = pd.date_range("2015-01-01", periods=4) contracts = deque(self.asset_finder.retrieve_all(contract_sids)) @@ -1903,33 +1883,33 @@ def test_active_chain(self): # Test sid 1 as days increment, as the sessions march forward # a contract should be added per day, until all defined contracts # are returned. - chain = oc.active_chain(1, pd.Timestamp("2014-12-31", tz="UTC").value) + chain = oc.active_chain(1, pd.Timestamp("2014-12-31").value) assert [] == list(chain), ( "On session before first start date, no contracts " "in chain should be active." ) - chain = oc.active_chain(1, pd.Timestamp("2015-01-01", tz="UTC").value) + chain = oc.active_chain(1, pd.Timestamp("2015-01-01").value) assert [1] == list(chain), ( "[1] should be the active chain on 01-01, since all " "other start dates occur after 01-01." ) - chain = oc.active_chain(1, pd.Timestamp("2015-01-02", tz="UTC").value) + chain = oc.active_chain(1, pd.Timestamp("2015-01-02").value) assert [1, 2] == list(chain), "[1, 2] should be the active contracts on 01-02." - chain = oc.active_chain(1, pd.Timestamp("2015-01-03", tz="UTC").value) + chain = oc.active_chain(1, pd.Timestamp("2015-01-03").value) assert [1, 2, 3] == list( chain ), "[1, 2, 3] should be the active contracts on 01-03." - chain = oc.active_chain(1, pd.Timestamp("2015-01-04", tz="UTC").value) + chain = oc.active_chain(1, pd.Timestamp("2015-01-04").value) assert 4 == len(chain), ( "[1, 2, 3, 4] should be the active contracts on " "01-04, this is all defined contracts in the test " "case." ) - chain = oc.active_chain(1, pd.Timestamp("2015-01-05", tz="UTC").value) + chain = oc.active_chain(1, pd.Timestamp("2015-01-05").value) assert 4 == len(chain), ( "[1, 2, 3, 4] should be the active contracts on " "01-05. This tests the case where all start dates " @@ -1937,22 +1917,22 @@ def test_active_chain(self): ) # Test querying each sid at a time when all should be alive. - chain = oc.active_chain(2, pd.Timestamp("2015-01-05", tz="UTC").value) + chain = oc.active_chain(2, pd.Timestamp("2015-01-05").value) assert [2, 3, 4] == list(chain) - chain = oc.active_chain(3, pd.Timestamp("2015-01-05", tz="UTC").value) + chain = oc.active_chain(3, pd.Timestamp("2015-01-05").value) assert [3, 4] == list(chain) - chain = oc.active_chain(4, pd.Timestamp("2015-01-05", tz="UTC").value) + chain = oc.active_chain(4, pd.Timestamp("2015-01-05").value) assert [4] == list(chain) # Test defined contract to check edge conditions. - chain = oc.active_chain(4, pd.Timestamp("2015-01-03", tz="UTC").value) + chain = oc.active_chain(4, pd.Timestamp("2015-01-03").value) assert [] == list(chain), ( "No contracts should be active, since 01-03 is " "before 4's start date." ) - chain = oc.active_chain(4, pd.Timestamp("2015-01-04", tz="UTC").value) + chain = oc.active_chain(4, pd.Timestamp("2015-01-04").value) assert [4] == list(chain), "[4] should be active beginning at its start date." def test_delivery_predicate(self): @@ -1968,7 +1948,7 @@ def test_delivery_predicate(self): # Test sid 1 as days increment, as the sessions march forward # a contract should be added per day, until all defined contracts # are returned. - chain = oc.active_chain(5, pd.Timestamp("2015-01-05", tz="UTC").value) + chain = oc.active_chain(5, pd.Timestamp("2015-01-05").value) assert [5, 7] == list(chain), ( "Contract BAG16 (sid=6) should be ommitted from chain, since " "it does not satisfy the roll predicate." diff --git a/tests/test_data_portal.py b/tests/test_data_portal.py index 8681352b93..540201ff13 100644 --- a/tests/test_data_portal.py +++ b/tests/test_data_portal.py @@ -39,8 +39,8 @@ class DataPortalTestBase(WithDataPortal, WithTradingSessions): ASSET_FINDER_EQUITY_SIDS = (1, 2, 3) DIVIDEND_ASSET_SID = 3 - START_DATE = pd.Timestamp("2016-08-01", tz="utc") - END_DATE = pd.Timestamp("2016-08-08", tz="utc") + START_DATE = pd.Timestamp("2016-08-01") + END_DATE = pd.Timestamp("2016-08-08") TRADING_CALENDAR_STRS = ("NYSE", "us_futures") @@ -85,7 +85,7 @@ def make_futures_info(cls): def make_equity_minute_bar_data(cls): trading_calendar = cls.trading_calendars[Equity] # No data on first day. - dts = trading_calendar.minutes_for_session(cls.trading_days[0]) + dts = trading_calendar.session_minutes(cls.trading_days[0]) dfs = [] dfs.append( pd.DataFrame( @@ -99,7 +99,7 @@ def make_equity_minute_bar_data(cls): index=dts, ) ) - dts = trading_calendar.minutes_for_session(cls.trading_days[1]) + dts = trading_calendar.session_minutes(cls.trading_days[1]) dfs.append( pd.DataFrame( { @@ -112,7 +112,7 @@ def make_equity_minute_bar_data(cls): index=dts, ) ) - dts = trading_calendar.minutes_for_session(cls.trading_days[2]) + dts = trading_calendar.session_minutes(cls.trading_days[2]) dfs.append( pd.DataFrame( { @@ -125,7 +125,7 @@ def make_equity_minute_bar_data(cls): index=dts[:6], ) ) - dts = trading_calendar.minutes_for_session(cls.trading_days[3]) + dts = trading_calendar.session_minutes(cls.trading_days[3]) dfs.append( pd.DataFrame( { @@ -162,7 +162,7 @@ def make_future_minute_bar_data(cls): # No data on first day, future asset intentionally not on the same # dates as equities, so that cross-wiring of results do not create a # false positive. - dts = trading_calendar.minutes_for_session(trading_sessions[1]) + dts = trading_calendar.session_minutes(trading_sessions[1]) dfs = [] dfs.append( pd.DataFrame( @@ -176,7 +176,7 @@ def make_future_minute_bar_data(cls): index=dts, ) ) - dts = trading_calendar.minutes_for_session(trading_sessions[2]) + dts = trading_calendar.session_minutes(trading_sessions[2]) dfs.append( pd.DataFrame( { @@ -189,7 +189,7 @@ def make_future_minute_bar_data(cls): index=dts, ) ) - dts = trading_calendar.minutes_for_session(trading_sessions[3]) + dts = trading_calendar.session_minutes(trading_sessions[3]) dfs.append( pd.DataFrame( { @@ -202,7 +202,7 @@ def make_future_minute_bar_data(cls): index=dts[:6], ) ) - dts = trading_calendar.minutes_for_session(trading_sessions[4]) + dts = trading_calendar.session_minutes(trading_sessions[4]) dfs.append( pd.DataFrame( { @@ -218,7 +218,7 @@ def make_future_minute_bar_data(cls): asset10000_df = pd.concat(dfs) yield 10000, asset10000_df - missing_dts = trading_calendar.minutes_for_session(trading_sessions[0]) + missing_dts = trading_calendar.session_minutes(trading_sessions[0]) asset10001_df = pd.DataFrame( { "open": 1.00549, @@ -259,12 +259,12 @@ def test_get_last_traded_equity_minute(self): trading_calendar = self.trading_calendars[Equity] # Case: Missing data at front of data set, and request dt is before # first value. - dts = trading_calendar.minutes_for_session(self.trading_days[0]) + dts = trading_calendar.session_minutes(self.trading_days[0]) asset = self.asset_finder.retrieve_asset(1) assert pd.isnull(self.data_portal.get_last_traded_dt(asset, dts[0], "minute")) # Case: Data on requested dt. - dts = trading_calendar.minutes_for_session(self.trading_days[2]) + dts = trading_calendar.session_minutes(self.trading_days[2]) assert dts[1] == self.data_portal.get_last_traded_dt(asset, dts[1], "minute") @@ -276,11 +276,11 @@ def test_get_last_traded_future_minute(self): trading_calendar = self.trading_calendars[Future] # Case: Missing data at front of data set, and request dt is before # first value. - dts = trading_calendar.minutes_for_session(self.trading_days[0]) + dts = trading_calendar.session_minutes(self.trading_days[0]) assert pd.isnull(self.data_portal.get_last_traded_dt(asset, dts[0], "minute")) # Case: Data on requested dt. - dts = trading_calendar.minutes_for_session(self.trading_days[3]) + dts = trading_calendar.session_minutes(self.trading_days[3]) assert dts[1] == self.data_portal.get_last_traded_dt(asset, dts[1], "minute") @@ -308,7 +308,7 @@ def test_get_last_traded_dt_equity_daily(self): def test_get_spot_value_equity_minute(self): trading_calendar = self.trading_calendars[Equity] asset = self.asset_finder.retrieve_asset(1) - dts = trading_calendar.minutes_for_session(self.trading_days[2]) + dts = trading_calendar.session_minutes(self.trading_days[2]) # Case: Get data on exact dt. dt = dts[1] @@ -349,7 +349,7 @@ def test_get_spot_value_equity_minute(self): def test_get_spot_value_future_minute(self): trading_calendar = self.trading_calendars[Future] asset = self.asset_finder.retrieve_asset(10000) - dts = trading_calendar.minutes_for_session(self.trading_days[3]) + dts = trading_calendar.session_minutes(self.trading_days[3]) # Case: Get data on exact dt. dt = dts[1] @@ -391,7 +391,7 @@ def test_get_spot_value_multiple_assets(self): equity = self.asset_finder.retrieve_asset(1) future = self.asset_finder.retrieve_asset(10000) trading_calendar = self.trading_calendars[Future] - dts = trading_calendar.minutes_for_session(self.trading_days[3]) + dts = trading_calendar.session_minutes(self.trading_days[3]) # We expect the outputs to be lists of spot values. expected = pd.DataFrame( @@ -453,7 +453,7 @@ def test_get_adjustments(self, data_frequency, field): ) def test_get_last_traded_dt_minute(self): - minutes = self.nyse_calendar.minutes_for_session(self.trading_days[2]) + minutes = self.nyse_calendar.session_minutes(self.trading_days[2]) equity = self.asset_finder.retrieve_asset(1) result = self.data_portal.get_last_traded_dt(equity, minutes[3], "minute") assert minutes[3] == result, ( @@ -469,7 +469,7 @@ def test_get_last_traded_dt_minute(self): future = self.asset_finder.retrieve_asset(10000) calendar = self.trading_calendars[Future] - minutes = calendar.minutes_for_session(self.trading_days[3]) + minutes = calendar.session_minutes(self.trading_days[3]) result = self.data_portal.get_last_traded_dt(future, minutes[3], "minute") assert minutes[3] == result, ( @@ -497,7 +497,7 @@ def test_price_rounding(self, frequency, field): "calendar", None, ) - minutes = self.nyse_calendar.minutes_for_session(self.trading_days[0]) + minutes = self.nyse_calendar.session_minutes(self.trading_days[0]) if frequency == "1m": minute = minutes[0] @@ -505,7 +505,7 @@ def test_price_rounding(self, frequency, field): expected_future_volume = 100 data_frequency = "minute" else: - minute = minutes[0].normalize() + minute = self.nyse_calendar.minute_to_session(minutes[0]) expected_equity_volume = 100 * US_EQUITIES_MINUTES_PER_DAY expected_future_volume = 100 * FUTURES_MINUTES_PER_DAY data_frequency = "daily" diff --git a/tests/test_fetcher.py b/tests/test_fetcher.py index c2b4c794c9..1828cb359e 100644 --- a/tests/test_fetcher.py +++ b/tests/test_fetcher.py @@ -16,7 +16,7 @@ import pandas as pd import numpy as np -from mock import patch +import mock from zipline.errors import UnsupportedOrderParameters from zipline.sources.requests_csv import mask_requests_args from zipline.utils import factory @@ -45,8 +45,8 @@ # XXX: The algorithms in this suite do way more work than they should have to. class FetcherTestCase(WithResponses, WithMakeAlgo, ZiplineTestCase): - START_DATE = pd.Timestamp("2006-01-03", tz="utc") - END_DATE = pd.Timestamp("2006-12-29", tz="utc") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-12-29") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False @@ -54,8 +54,8 @@ class FetcherTestCase(WithResponses, WithMakeAlgo, ZiplineTestCase): @classmethod def make_equity_info(cls): - start_date = pd.Timestamp("2006-01-01", tz="UTC") - end_date = pd.Timestamp("2007-01-01", tz="UTC") + start_date = pd.Timestamp("2006-01-01") + end_date = pd.Timestamp("2007-01-01") return pd.DataFrame.from_dict( { 24: { @@ -90,7 +90,7 @@ def make_equity_info(cls): }, 13: { "start_date": start_date, - "end_date": pd.Timestamp("2010-01-01", tz="UTC"), + "end_date": pd.Timestamp("2010-01-01"), "symbol": "NFLX", "exchange": "nasdaq", }, @@ -138,8 +138,8 @@ def test_minutely_fetcher(self): ) sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2006-01-03", tz="UTC"), - end=pd.Timestamp("2006-01-10", tz="UTC"), + start=pd.Timestamp("2006-01-03"), + end=pd.Timestamp("2006-01-10"), emission_rate="minute", data_frequency="minute", ) @@ -365,7 +365,7 @@ def capture_kwargs(zelf, url, **kwargs): # Patching fetch_url instead of using responses in this test so that we # can intercept the requests keyword arguments and confirm that they're # correct. - with patch( + with mock.patch( "zipline.sources.requests_csv.PandasRequestsCSV.fetch_url", new=capture_kwargs, ): @@ -419,19 +419,18 @@ def test_fetcher_universe(self, name, data, column_name): # easier given the parameterization, and (b) there are enough tests # using responses that the fetch_url code is getting a good workout so # we don't have to use it in every test. - with patch( + with mock.patch( "zipline.sources.requests_csv.PandasRequestsCSV.fetch_url", new=lambda *a, **k: data, ): sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2006-01-09", tz="UTC"), - end=pd.Timestamp("2006-01-11", tz="UTC"), + start=pd.Timestamp("2006-01-09"), + end=pd.Timestamp("2006-01-11"), ) algocode = """ from pandas import Timestamp from zipline.api import fetch_csv, record, sid, get_datetime -from zipline.utils.pandas_utils import normalize_date def initialize(context): fetch_csv( @@ -446,7 +445,7 @@ def initialize(context): context.bar_count = 0 def handle_data(context, data): - expected = context.expected_sids[normalize_date(get_datetime())] + expected = context.expected_sids[get_datetime().normalize()] actual = data.fetcher_assets for stk in expected: if stk not in actual: @@ -479,8 +478,8 @@ def test_fetcher_universe_non_security_return(self): ) sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2006-01-09", tz="UTC"), - end=pd.Timestamp("2006-01-10", tz="UTC"), + start=pd.Timestamp("2006-01-09"), + end=pd.Timestamp("2006-01-10"), ) self.run_algo( @@ -539,8 +538,8 @@ def test_fetcher_universe_minute(self): ) sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2006-01-09", tz="UTC"), - end=pd.Timestamp("2006-01-11", tz="UTC"), + start=pd.Timestamp("2006-01-09"), + end=pd.Timestamp("2006-01-11"), data_frequency="minute", ) @@ -586,8 +585,8 @@ def test_fetcher_in_before_trading_start(self): ) sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2013-06-13", tz="UTC"), - end=pd.Timestamp("2013-11-15", tz="UTC"), + start=pd.Timestamp("2013-06-13"), + end=pd.Timestamp("2013-11-15"), data_frequency="minute", ) @@ -624,8 +623,8 @@ def test_fetcher_bad_data(self): ) sim_params = factory.create_simulation_parameters( - start=pd.Timestamp("2013-06-12", tz="UTC"), - end=pd.Timestamp("2013-06-14", tz="UTC"), + start=pd.Timestamp("2013-06-12"), + end=pd.Timestamp("2013-06-14"), data_frequency="minute", ) diff --git a/tests/test_finance.py b/tests/test_finance.py index 5358de741d..0d17b1c552 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -48,13 +48,11 @@ def set_test_finance(request, with_asset_finder): ASSET_FINDER_COUNTRY_CODE = "??" - T = partial(pd.Timestamp, tz="UTC") - START_DATES = [ - T("2006-01-03", tz="UTC"), + pd.Timestamp("2006-01-03"), ] * 3 END_DATES = [ - T("2006-12-29", tz="UTC"), + pd.Timestamp("2006-12-29"), ] * 3 equities = pd.DataFrame( @@ -89,8 +87,8 @@ def set_test_finance(request, with_asset_finder): @pytest.mark.usefixtures("set_test_finance", "with_trading_calendars") class TestFinance: - start = pd.Timestamp("2006-01-01", tz="utc") - end = pd.Timestamp("2006-12-31", tz="utc") + start = pd.Timestamp("2006-01-01") + end = pd.Timestamp("2006-12-31") # TODO: write tests for short sales # TODO: write a test to do massive buying or shorting. @@ -212,9 +210,11 @@ def transaction_sim(self, **params): complete_fill = params.get("complete_fill") asset1 = self.asset_finder.retrieve_asset(1) + with TempDirectory() as tempdir: if trade_interval < timedelta(days=1): + sim_params = factory.create_simulation_parameters( start=self.start, end=self.end, data_frequency="minute" ) @@ -241,8 +241,8 @@ def transaction_sim(self, **params): write_bcolz_minute_data( self.trading_calendar, self.trading_calendar.sessions_in_range( - self.trading_calendar.minute_to_session_label(minutes[0]), - self.trading_calendar.minute_to_session_label(minutes[-1]), + self.trading_calendar.minute_to_session(minutes[0]), + self.trading_calendar.minute_to_session(minutes[-1]), ), tempdir.path, assets.items(), @@ -318,7 +318,7 @@ def transaction_sim(self, **params): if sim_params.data_frequency == "minute": ticks = minutes else: - ticks = days + ticks = days.tz_localize("UTC") transactions = [] @@ -432,8 +432,8 @@ class TestSimulationParameters: def test_simulation_parameters(self): sp = SimulationParameters( - start_session=pd.Timestamp("2008-01-01", tz="UTC"), - end_session=pd.Timestamp("2008-12-31", tz="UTC"), + start_session=pd.Timestamp("2008-01-01"), + end_session=pd.Timestamp("2008-12-31"), capital_base=100000, trading_calendar=self.trading_calendar, ) @@ -453,22 +453,22 @@ def test_sim_params_days_in_period(self): # 27 28 29 30 31 params = SimulationParameters( - start_session=pd.Timestamp("2007-12-31", tz="UTC"), - end_session=pd.Timestamp("2008-01-07", tz="UTC"), - capital_base=100000, + start_session=pd.Timestamp("2007-12-31"), + end_session=pd.Timestamp("2008-01-07"), + capital_base=100_000, trading_calendar=self.trading_calendar, ) expected_trading_days = ( - datetime(2007, 12, 31, tzinfo=pytz.utc), + datetime(2007, 12, 31), # Skip new years # holidays taken from: http://www.nyse.com/press/1191407641943.html - datetime(2008, 1, 2, tzinfo=pytz.utc), - datetime(2008, 1, 3, tzinfo=pytz.utc), - datetime(2008, 1, 4, tzinfo=pytz.utc), + datetime(2008, 1, 2), + datetime(2008, 1, 3), + datetime(2008, 1, 4), # Skip Saturday # Skip Sunday - datetime(2008, 1, 7, tzinfo=pytz.utc), + datetime(2008, 1, 7), ) num_expected_trading_days = 5 diff --git a/tests/test_history.py b/tests/test_history.py index 7b3ef3a039..2e2da798cc 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -14,24 +14,22 @@ # limitations under the License. from collections import OrderedDict from textwrap import dedent -from parameterized import parameterized + import numpy as np -from numpy import nan import pandas as pd +import pytest +from parameterized import parameterized -from zipline._protocol import handle_non_market_minutes, BarData +import zipline.testing.fixtures as zf +from zipline._protocol import BarData, handle_non_market_minutes from zipline.assets import Asset, Equity -from zipline.errors import ( - HistoryWindowStartsBeforeData, -) +from zipline.errors import HistoryWindowStartsBeforeData from zipline.finance.asset_restrictions import NoRestrictions from zipline.testing import ( + MockDailyBarReader, create_minute_df_for_asset, str_to_seconds, - MockDailyBarReader, ) -import zipline.testing.fixtures as zf -import pytest OHLC = ["open", "high", "low", "close"] OHLCP = OHLC + ["price"] @@ -39,11 +37,8 @@ class WithHistory(zf.WithCreateBarData, zf.WithDataPortal): - TRADING_START_DT = TRADING_ENV_MIN_DATE = START_DATE = pd.Timestamp( - "2014-01-03", - tz="UTC", - ) - TRADING_END_DT = END_DATE = pd.Timestamp("2016-01-29", tz="UTC") + TRADING_START_DT = TRADING_ENV_MIN_DATE = START_DATE = pd.Timestamp("2014-01-03") + TRADING_END_DT = END_DATE = pd.Timestamp("2016-01-29") SPLIT_ASSET_SID = 4 DIVIDEND_ASSET_SID = 5 @@ -105,13 +100,13 @@ def init_class_fixtures(cls): @classmethod def make_equity_info(cls): - jan_5_2015 = pd.Timestamp("2015-01-05", tz="UTC") - day_after_12312015 = pd.Timestamp("2016-01-04", tz="UTC") + jan_5_2015 = pd.Timestamp("2015-01-05") + day_after_12312015 = pd.Timestamp("2016-01-04") return pd.DataFrame.from_dict( { 1: { - "start_date": pd.Timestamp("2014-01-03", tz="UTC"), + "start_date": pd.Timestamp("2014-01-03"), "end_date": cls.TRADING_END_DT, "symbol": "ASSET1", "exchange": "TEST", @@ -147,14 +142,14 @@ def make_equity_info(cls): "exchange": "TEST", }, cls.HALF_DAY_TEST_ASSET_SID: { - "start_date": pd.Timestamp("2014-07-02", tz="UTC"), + "start_date": pd.Timestamp("2014-07-02"), "end_date": day_after_12312015, "symbol": "HALF_DAY_TEST_ASSET", "exchange": "TEST", }, cls.SHORT_ASSET_SID: { - "start_date": pd.Timestamp("2015-01-05", tz="UTC"), - "end_date": pd.Timestamp("2015-01-06", tz="UTC"), + "start_date": pd.Timestamp("2015-01-05"), + "end_date": pd.Timestamp("2015-01-06"), "symbol": "SHORT_ASSET", "exchange": "TEST", }, @@ -202,22 +197,18 @@ def make_dividends_data(cls): [ { # only care about ex date, the other dates don't matter here - "ex_date": pd.Timestamp("2015-01-06", tz="UTC").to_datetime64(), - "record_date": pd.Timestamp("2015-01-06", tz="UTC").to_datetime64(), - "declared_date": pd.Timestamp( - "2015-01-06", tz="UTC" - ).to_datetime64(), - "pay_date": pd.Timestamp("2015-01-06", tz="UTC").to_datetime64(), + "ex_date": pd.Timestamp("2015-01-06").to_datetime64(), + "record_date": pd.Timestamp("2015-01-06").to_datetime64(), + "declared_date": pd.Timestamp("2015-01-06").to_datetime64(), + "pay_date": pd.Timestamp("2015-01-06").to_datetime64(), "amount": 2.0, "sid": cls.DIVIDEND_ASSET_SID, }, { - "ex_date": pd.Timestamp("2015-01-07", tz="UTC").to_datetime64(), - "record_date": pd.Timestamp("2015-01-07", tz="UTC").to_datetime64(), - "declared_date": pd.Timestamp( - "2015-01-07", tz="UTC" - ).to_datetime64(), - "pay_date": pd.Timestamp("2015-01-07", tz="UTC").to_datetime64(), + "ex_date": pd.Timestamp("2015-01-07").to_datetime64(), + "record_date": pd.Timestamp("2015-01-07").to_datetime64(), + "declared_date": pd.Timestamp("2015-01-07").to_datetime64(), + "pay_date": pd.Timestamp("2015-01-07").to_datetime64(), "amount": 4.0, "sid": cls.DIVIDEND_ASSET_SID, }, @@ -252,26 +243,22 @@ def verify_regular_dt(self, idx, dt, mode, fields=None, assets=None): # noqa: C equity_cal = self.trading_calendars[Equity] def reindex_to_primary_calendar(a, field): - """ - Reindex an array of prices from a window on the NYSE + """Reindex an array of prices from a window on the NYSE calendar by the window on the primary calendar with the same dt and window size. """ if mode == "daily": - dts = cal.sessions_window(dt, -9) + dts = cal.sessions_window(dt, -10) # `dt` may not be a session on the equity calendar, so # find the next valid session. - equity_sess = equity_cal.minute_to_session_label(dt) - equity_dts = equity_cal.sessions_window(equity_sess, -9) + equity_sess = equity_cal.minute_to_session(dt) + equity_dts = equity_cal.sessions_window(equity_sess, -10) elif mode == "minute": dts = cal.minutes_window(dt, -10) equity_dts = equity_cal.minutes_window(dt, -10) - output = pd.Series( - index=equity_dts, - data=a, - ).reindex(dts) + output = pd.Series(index=equity_dts, data=a).reindex(dts) # Fill after reindexing, to ensure we don't forward fill # with values that are being dropped. @@ -524,8 +511,8 @@ def make_equity_minute_bar_data(cls): data[1] = create_minute_df_for_asset( equities_cal, - pd.Timestamp("2014-01-03", tz="utc"), - pd.Timestamp("2016-01-29", tz="utc"), + pd.Timestamp("2014-01-03"), + pd.Timestamp("2016-01-29"), start_val=2, ) @@ -533,7 +520,7 @@ def make_equity_minute_bar_data(cls): data[asset2.sid] = create_minute_df_for_asset( equities_cal, asset2.start_date, - equities_cal.previous_session_label(asset2.end_date), + equities_cal.previous_session(asset2.end_date), start_val=2, minute_blacklist=[ pd.Timestamp("2015-01-08 14:31", tz="UTC"), @@ -550,26 +537,26 @@ def make_equity_minute_bar_data(cls): ( create_minute_df_for_asset( equities_cal, - pd.Timestamp("2015-01-05", tz="UTC"), - pd.Timestamp("2015-01-05", tz="UTC"), + pd.Timestamp("2015-01-05"), + pd.Timestamp("2015-01-05"), start_val=8000, ), create_minute_df_for_asset( equities_cal, - pd.Timestamp("2015-01-06", tz="UTC"), - pd.Timestamp("2015-01-06", tz="UTC"), + pd.Timestamp("2015-01-06"), + pd.Timestamp("2015-01-06"), start_val=2000, ), create_minute_df_for_asset( equities_cal, - pd.Timestamp("2015-01-07", tz="UTC"), - pd.Timestamp("2015-01-07", tz="UTC"), + pd.Timestamp("2015-01-07"), + pd.Timestamp("2015-01-07"), start_val=1000, ), create_minute_df_for_asset( equities_cal, - pd.Timestamp("2015-01-08", tz="UTC"), - pd.Timestamp("2015-01-08", tz="UTC"), + pd.Timestamp("2015-01-08"), + pd.Timestamp("2015-01-08"), start_val=1000, ), ) @@ -601,9 +588,7 @@ def make_equity_minute_bar_data(cls): # algo.run() def test_negative_bar_count(self): - """ - Negative bar counts leak future information. - """ + """Negative bar counts leak future information.""" with pytest.raises(ValueError, match="bar_count must be >= 1, but got -1"): self.data_portal.get_history_window( [self.ASSET1], @@ -618,13 +603,13 @@ def test_daily_splits_and_mergers(self): # self.SPLIT_ASSET and self.MERGER_ASSET had splits/mergers # on 1/6 and 1/7 - jan5 = pd.Timestamp("2015-01-05", tz="UTC") + jan5 = pd.Timestamp("2015-01-05") for asset in [self.SPLIT_ASSET, self.MERGER_ASSET]: # before any of the adjustments, 1/4 and 1/5 window1 = self.data_portal.get_history_window( [asset], - self.trading_calendar.open_and_close_for_session(jan5)[1], + self.trading_calendar.session_close(jan5), 2, "1d", "close", @@ -682,7 +667,7 @@ def test_daily_splits_and_mergers(self): def test_daily_dividends(self): # self.DIVIDEND_ASSET had dividends on 1/6 and 1/7 - jan5 = pd.Timestamp("2015-01-05", tz="UTC") + jan5 = pd.Timestamp("2015-01-05") asset = self.DIVIDEND_ASSET # before any of the dividends @@ -695,7 +680,7 @@ def test_daily_dividends(self): "minute", )[asset] - np.testing.assert_array_equal(np.array([nan, 391]), window1) + np.testing.assert_array_equal(np.array([np.nan, 391]), window1) # straddling the first event window2 = self.data_portal.get_history_window( @@ -751,10 +736,8 @@ def test_daily_dividends(self): def test_minute_before_assets_trading(self): # since asset2 and asset3 both started trading on 1/5/2015, let's do # some history windows that are completely before that - minutes = self.trading_calendar.minutes_for_session( - self.trading_calendar.previous_session_label( - pd.Timestamp("2015-01-05", tz="UTC") - ) + minutes = self.trading_calendar.session_minutes( + self.trading_calendar.previous_session(pd.Timestamp("2015-01-05")) )[0:60] for idx, minute in enumerate(minutes): @@ -799,8 +782,8 @@ def test_minute_regular(self, name, field, sid): asset = self.asset_finder.retrieve_asset(sid) # Check the first hour of equities trading. - minutes = self.trading_calendars[Equity].minutes_for_session( - pd.Timestamp("2015-01-05", tz="UTC") + minutes = self.trading_calendars[Equity].session_minutes( + pd.Timestamp("2015-01-05") )[0:60] for idx, minute in enumerate(minutes): @@ -815,7 +798,7 @@ def test_minute_sunday_midnight(self): # Find the closest prior minute when the trading calendar was # open (note that if the calendar is open at `sunday_midnight`, # this will be `sunday_midnight`). - trading_minutes = self.trading_calendar.all_minutes + trading_minutes = self.trading_calendar.minutes last_minute = trading_minutes[trading_minutes <= sunday_midnight][-1] sunday_midnight_bar_data = self.create_bardata(lambda: sunday_midnight) @@ -838,8 +821,8 @@ def test_minute_sunday_midnight(self): def test_minute_after_asset_stopped(self): # SHORT_ASSET's last day was 2015-01-06 # get some history windows that straddle the end - minutes = self.trading_calendars[Equity].minutes_for_session( - pd.Timestamp("2015-01-07", tz="UTC") + minutes = self.trading_calendars[Equity].session_minutes( + pd.Timestamp("2015-01-07") )[0:60] for idx, minute in enumerate(minutes): @@ -931,7 +914,7 @@ def test_minute_splits_and_mergers(self): # self.SPLIT_ASSET and self.MERGER_ASSET had splits/mergers # on 1/6 and 1/7 - jan5 = pd.Timestamp("2015-01-05", tz="UTC") + jan5 = pd.Timestamp("2015-01-05") # the assets' close column starts at 2 on the first minute of # 1/5, then goes up one per minute forever @@ -941,7 +924,7 @@ def test_minute_splits_and_mergers(self): equity_cal = self.trading_calendars[Equity] window1 = self.data_portal.get_history_window( [asset], - equity_cal.open_and_close_for_session(jan5)[1], + equity_cal.session_close(jan5), 10, "1m", "close", @@ -1275,7 +1258,7 @@ def test_minute_different_lifetimes(self): equity_cal = self.trading_calendars[Equity] # at trading start, only asset1 existed - day = self.trading_calendar.next_session_label(self.TRADING_START_DT) + day = self.trading_calendar.next_session(self.TRADING_START_DT) # Range containing 100 equity minutes, possibly more on other # calendars (i.e. futures). @@ -1284,9 +1267,9 @@ def test_minute_different_lifetimes(self): bar_count = len(cal.minutes_in_range(window_start, window_end)) equity_cal = self.trading_calendars[Equity] - first_equity_open, _ = equity_cal.open_and_close_for_session(day) + first_equity_open = equity_cal.session_first_minute(day) - asset1_minutes = equity_cal.minutes_for_sessions_in_range( + asset1_minutes = equity_cal.sessions_minutes( self.ASSET1.start_date, self.ASSET1.end_date ) asset1_idx = asset1_minutes.searchsorted(first_equity_open) @@ -1328,9 +1311,7 @@ def test_minute_different_lifetimes(self): def test_history_window_before_first_trading_day(self): # trading_start is 2/3/2014 # get a history window that starts before that, and ends after that - first_day_minutes = self.trading_calendar.minutes_for_session( - self.TRADING_START_DT - ) + first_day_minutes = self.trading_calendar.session_minutes(self.TRADING_START_DT) exp_msg = ( "History window extends before 2014-01-03. To use this history " "window, start the backtest on or after 2014-01-06." @@ -1351,11 +1332,11 @@ def test_daily_history_blended(self): # last day # January 2015 has both daily and minute data for ASSET2 - day = pd.Timestamp("2015-01-07", tz="UTC") - minutes = self.trading_calendar.minutes_for_session(day) + day = pd.Timestamp("2015-01-07") + minutes = self.trading_calendar.session_minutes(day) equity_cal = self.trading_calendars[Equity] - equity_minutes = equity_cal.minutes_for_session(day) + equity_minutes = equity_cal.session_minutes(day) equity_open, equity_close = equity_minutes[0], equity_minutes[-1] # minute data, baseline: @@ -1404,7 +1385,7 @@ def test_daily_history_blended(self): elif field == "price": last_val = window[1] else: - last_val = nan + last_val = np.nan elif field == "open": last_val = 783 elif field == "high": @@ -1433,11 +1414,11 @@ def test_daily_history_blended_gaps(self, field): # last day # January 2015 has both daily and minute data for ASSET2 - day = pd.Timestamp("2015-01-08", tz="UTC") - minutes = self.trading_calendar.minutes_for_session(day) + day = pd.Timestamp("2015-01-08") + minutes = self.trading_calendar.session_minutes(day) equity_cal = self.trading_calendars[Equity] - equity_minutes = equity_cal.minutes_for_session(day) + equity_minutes = equity_cal.session_minutes(day) equity_open, equity_close = equity_minutes[0], equity_minutes[-1] # minute data, baseline: @@ -1487,7 +1468,7 @@ def test_daily_history_blended_gaps(self, field): elif field == "price": last_val = window[1] else: - last_val = nan + last_val = np.nan elif field == "open": if idx == 0: last_val = np.nan @@ -1536,10 +1517,10 @@ def test_daily_history_blended_gaps(self, field): np.testing.assert_almost_equal( window[-1], last_val, - err_msg="field={0} minute={1}".format(field, minute), + err_msg=f"field={field} minute={minute}", ) - @parameterized.expand([(("bar_count%s" % x), x) for x in [1, 2, 3]]) + @parameterized.expand([((f"bar_count{x}"), x) for x in [1, 2, 3]]) def test_daily_history_minute_gaps_price_ffill(self, test_name, bar_count): # Make sure we use the previous day's value when there's been no volume # yet today. @@ -1555,16 +1536,16 @@ def test_daily_history_minute_gaps_price_ffill(self, test_name, bar_count): # day is not a trading day. for day_idx, day in enumerate( [ - pd.Timestamp("2015-01-05", tz="UTC"), - pd.Timestamp("2015-01-06", tz="UTC"), - pd.Timestamp("2015-01-12", tz="UTC"), + pd.Timestamp("2015-01-05"), + pd.Timestamp("2015-01-06"), + pd.Timestamp("2015-01-12"), ] ): - session_minutes = self.trading_calendar.minutes_for_session(day) + session_minutes = self.trading_calendar.session_minutes(day) equity_cal = self.trading_calendars[Equity] - equity_minutes = equity_cal.minutes_for_session(day) + equity_minutes = equity_cal.session_minutes(day) if day_idx == 0: # dedupe when session_minutes are same as equity_minutes @@ -1643,18 +1624,16 @@ class DailyEquityHistoryTestCase(WithHistory, zf.ZiplineTestCase): @classmethod def make_equity_daily_bar_data(cls, country_code, sids): - yield 1, cls.create_df_for_asset( - cls.START_DATE, pd.Timestamp("2016-01-30", tz="UTC") - ) + yield 1, cls.create_df_for_asset(cls.START_DATE, pd.Timestamp("2016-01-30")) yield 3, cls.create_df_for_asset( - pd.Timestamp("2015-01-05", tz="UTC"), - pd.Timestamp("2015-12-31", tz="UTC"), + pd.Timestamp("2015-01-05"), + pd.Timestamp("2015-12-31"), interval=10, force_zeroes=True, ) yield cls.SHORT_ASSET_SID, cls.create_df_for_asset( - pd.Timestamp("2015-01-05", tz="UTC"), - pd.Timestamp("2015-01-06", tz="UTC"), + pd.Timestamp("2015-01-05"), + pd.Timestamp("2015-01-06"), ) for sid in {2, 4, 5, 6}: @@ -1699,11 +1678,11 @@ def test_daily_before_assets_trading(self): # asset2 and asset3 both started trading in 2015 days = self.trading_calendar.sessions_in_range( - pd.Timestamp("2014-12-15", tz="UTC"), - pd.Timestamp("2014-12-18", tz="UTC"), + pd.Timestamp("2014-12-15"), + pd.Timestamp("2014-12-18"), ) - for idx, day in enumerate(days): + for _idx, day in enumerate(days): bar_data = self.create_bardata( simulation_dt_func=lambda: day, ) @@ -1734,7 +1713,7 @@ def test_daily_regular(self): # Regardless of the calendar used for this test, equities will # only have data on NYSE sessions. - days = self.trading_calendars[Equity].sessions_window(jan5, 30) + days = self.trading_calendars[Equity].sessions_window(jan5, 31) for idx, day in enumerate(days): self.verify_regular_dt(idx, day, "daily") @@ -1744,7 +1723,7 @@ def test_daily_some_assets_stopped(self): # asset2 ends on 2015-12-13 bar_data = self.create_bardata( - simulation_dt_func=lambda: pd.Timestamp("2016-01-06", tz="UTC"), + simulation_dt_func=lambda: pd.Timestamp("2016-01-06"), ) for field in OHLCP: @@ -1767,8 +1746,8 @@ def test_daily_after_asset_stopped(self): # SHORT_ASSET trades on 1/5, 1/6, that's it. days = self.trading_calendar.sessions_in_range( - pd.Timestamp("2015-01-07", tz="UTC"), - pd.Timestamp("2015-01-08", tz="UTC"), + pd.Timestamp("2015-01-07"), + pd.Timestamp("2015-01-08"), ) # days has 1/7, 1/8 @@ -1805,7 +1784,7 @@ def test_daily_splits_and_mergers(self): # before any of the adjustments window1 = self.data_portal.get_history_window( [asset], - pd.Timestamp("2015-01-05", tz="UTC"), + pd.Timestamp("2015-01-05"), 1, "1d", "close", @@ -1816,7 +1795,7 @@ def test_daily_splits_and_mergers(self): window1_volume = self.data_portal.get_history_window( [asset], - pd.Timestamp("2015-01-05", tz="UTC"), + pd.Timestamp("2015-01-05"), 1, "1d", "volume", @@ -1828,7 +1807,7 @@ def test_daily_splits_and_mergers(self): # straddling the first event window2 = self.data_portal.get_history_window( [asset], - pd.Timestamp("2015-01-06", tz="UTC"), + pd.Timestamp("2015-01-06"), 2, "1d", "close", @@ -1840,7 +1819,7 @@ def test_daily_splits_and_mergers(self): window2_volume = self.data_portal.get_history_window( [asset], - pd.Timestamp("2015-01-06", tz="UTC"), + pd.Timestamp("2015-01-06"), 2, "1d", "volume", @@ -1856,7 +1835,7 @@ def test_daily_splits_and_mergers(self): # straddling both events window3 = self.data_portal.get_history_window( [asset], - pd.Timestamp("2015-01-07", tz="UTC"), + pd.Timestamp("2015-01-07"), 3, "1d", "close", @@ -1867,7 +1846,7 @@ def test_daily_splits_and_mergers(self): window3_volume = self.data_portal.get_history_window( [asset], - pd.Timestamp("2015-01-07", tz="UTC"), + pd.Timestamp("2015-01-07"), 3, "1d", "volume", @@ -1885,7 +1864,7 @@ def test_daily_dividends(self): # before any dividend window1 = self.data_portal.get_history_window( [self.DIVIDEND_ASSET], - pd.Timestamp("2015-01-05", tz="UTC"), + pd.Timestamp("2015-01-05"), 1, "1d", "close", @@ -1897,7 +1876,7 @@ def test_daily_dividends(self): # straddling the first dividend window2 = self.data_portal.get_history_window( [self.DIVIDEND_ASSET], - pd.Timestamp("2015-01-06", tz="UTC"), + pd.Timestamp("2015-01-06"), 2, "1d", "close", @@ -1911,7 +1890,7 @@ def test_daily_dividends(self): # straddling both dividends window3 = self.data_portal.get_history_window( [self.DIVIDEND_ASSET], - pd.Timestamp("2015-01-07", tz="UTC"), + pd.Timestamp("2015-01-07"), 3, "1d", "close", @@ -1949,7 +1928,7 @@ def test_daily_blended_some_assets_stopped(self): def test_history_window_before_first_trading_day(self): # trading_start is 2/3/2014 # get a history window that starts before that, and ends after that - second_day = self.trading_calendar.next_session_label(self.TRADING_START_DT) + second_day = self.trading_calendar.next_session(self.TRADING_START_DT) exp_msg = ( "History window extends before 2014-01-03. To use this history " @@ -1977,7 +1956,7 @@ def test_history_window_before_first_trading_day(self): )[self.ASSET1] # Use a minute to force minute mode. - first_minute = self.trading_calendar.schedule.market_open[self.TRADING_START_DT] + first_minute = self.trading_calendar.first_minutes[self.TRADING_START_DT] with pytest.raises(HistoryWindowStartsBeforeData, match=exp_msg): self.data_portal.get_history_window( @@ -1990,8 +1969,7 @@ def test_history_window_before_first_trading_day(self): )[self.ASSET2] def test_history_window_different_order(self): - """ - Prevent regression on a bug where the passing the same assets, but + """Prevent regression on a bug where the passing the same assets, but in a different order would return a history window with the values, but not the keys, in order of the first history call. """ @@ -2024,8 +2002,7 @@ def test_history_window_different_order(self): ) def test_history_window_out_of_order_dates(self): - """ - Use a history window with non-monotonically increasing dates. + """Use a history window with non-monotonically increasing dates. A scenario which does not occur during simulations, but useful for using a history loader in a notebook. """ @@ -2089,7 +2066,7 @@ def assert_window_prices(window, prices): # If not on the NYSE calendar, it is possible that MLK day # (2014-01-20) is an active trading session. In that case, # we expect a nan value for this asset. - assert_window_prices(window_4, [12, nan, 13, 14]) + assert_window_prices(window_4, [12, np.nan, 13, 14]) class NoPrefetchDailyEquityHistoryTestCase(DailyEquityHistoryTestCase): diff --git a/tests/test_labelarray.py b/tests/test_labelarray.py index bb246c1a19..cd311a81e1 100644 --- a/tests/test_labelarray.py +++ b/tests/test_labelarray.py @@ -404,7 +404,7 @@ def create_categories(width, plus_one): return [ "".join(cs) for cs in take( - 2 ** width + plus_one, + 2**width + plus_one, product([chr(c) for c in range(256)], repeat=length), ) ] @@ -486,7 +486,7 @@ def test_known_categories_without_missing_at_boundary(self): assert arr.itemsize == 2 def test_narrow_condense_back_to_valid_size(self): - categories = ["a"] * (2 ** 8 + 1) + categories = ["a"] * (2**8 + 1) arr = LabelArray(categories, missing_value=categories[0]) assert arr.itemsize == 1 self.check_roundtrip(arr) diff --git a/tests/test_memoize.py b/tests/test_memoize.py index 1480194ced..0fe8ffcdcc 100644 --- a/tests/test_memoize.py +++ b/tests/test_memoize.py @@ -1,6 +1,4 @@ -""" -Tests for zipline.utils.memoize. -""" +"""Tests for zipline.utils.memoize.""" from collections import defaultdict import gc diff --git a/tests/test_ordering.py b/tests/test_ordering.py index 9bd9d48687..49d42694fc 100644 --- a/tests/test_ordering.py +++ b/tests/test_ordering.py @@ -11,10 +11,6 @@ import pytest -def T(s): - return pd.Timestamp(s, tz="UTC") - - class TestOrderMethods( zf.WithConstantEquityMinuteBarData, zf.WithConstantFutureMinuteBarData, @@ -28,9 +24,9 @@ class TestOrderMethods( # 15 16 17 18 19 20 21 # 22 23 24 25 26 27 28 # 29 30 31 - START_DATE = T("2006-01-03") - END_DATE = T("2006-01-06") - SIM_PARAMS_START_DATE = T("2006-01-04") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-06") + SIM_PARAMS_START_DATE = pd.Timestamp("2006-01-04") ASSET_FINDER_EQUITY_SIDS = (1,) @@ -322,9 +318,9 @@ class TestOrderMethodsDailyFrequency(zf.WithMakeAlgo, zf.ZiplineTestCase): # 15 16 17 18 19 20 21 # 22 23 24 25 26 27 28 # 29 30 31 - START_DATE = T("2006-01-03") - END_DATE = T("2006-01-06") - SIM_PARAMS_START_DATE = T("2006-01-04") + START_DATE = pd.Timestamp("2006-01-03") + END_DATE = pd.Timestamp("2006-01-06") + SIM_PARAMS_START_DATE = pd.Timestamp("2006-01-04") ASSET_FINDER_EQUITY_SIDS = (1,) SIM_PARAMS_DATA_FREQUENCY = "daily" diff --git a/tests/test_restrictions.py b/tests/test_restrictions.py index 5352feeb7c..7143718a56 100644 --- a/tests/test_restrictions.py +++ b/tests/test_restrictions.py @@ -72,8 +72,7 @@ def assert_many_restrictions(self, rl, assets, expected, dt): __fail_fast=True, ) def test_historical_restrictions(self, date_offset, restriction_order): - """ - Test historical restrictions for both interday and intraday + """Test historical restrictions for both interday and intraday restrictions, as well as restrictions defined in/not in order, for both single- and multi-asset queries """ @@ -158,9 +157,8 @@ def rdate(s): assert_all_restrictions([True, True, False], d3 + (MINUTE * 10000000)) def test_historical_restrictions_consecutive_states(self): - """ - Test that defining redundant consecutive restrictions still works - """ + """Test that defining redundant consecutive restrictions still works""" + rl = HistoricalRestrictions( [ Restriction(self.ASSET1, str_to_ts("2011-01-04"), ALLOWED), @@ -194,9 +192,7 @@ def test_historical_restrictions_consecutive_states(self): assert_is_restricted(self.ASSET1, str_to_ts("2011-01-07") + MINUTE) def test_static_restrictions(self): - """ - Test single- and multi-asset queries on static restrictions - """ + """Test single- and multi-asset queries on static restrictions""" restricted_a1 = self.ASSET1 restricted_a2 = self.ASSET2 @@ -218,8 +214,7 @@ def test_static_restrictions(self): assert_all_restrictions([True, True, False], dt) def test_security_list_restrictions(self): - """ - Test single- and multi-asset queries on restrictions defined by + """Test single- and multi-asset queries on restrictions defined by zipline.utils.security_list.SecurityList """ @@ -259,9 +254,7 @@ def current_securities(self, dt): assert_all_restrictions([True, True, True], str_to_ts("2011-01-05")) def test_noop_restrictions(self): - """ - Test single- and multi-asset queries on no-op restrictions - """ + """Test single- and multi-asset queries on no-op restrictions""" rl = NoRestrictions() assert_not_restricted = partial(self.assert_not_restricted, rl) @@ -276,8 +269,7 @@ def test_noop_restrictions(self): assert_all_restrictions([False, False, False], dt) def test_union_restrictions(self): - """ - Test that we appropriately union restrictions together, including + """Test that we appropriately union restrictions together, including eliminating redundancy (ignoring NoRestrictions) and flattening out the underlying sub-restrictions of _UnionRestrictions """ diff --git a/tests/test_security_list.py b/tests/test_security_list.py index 44a6b0c8c5..f958794043 100644 --- a/tests/test_security_list.py +++ b/tests/test_security_list.py @@ -78,16 +78,17 @@ def handle_data(self, data): class SecurityListTestCase(WithMakeAlgo, ZiplineTestCase): # XXX: This suite uses way more than it probably needs. - START_DATE = pd.Timestamp("2002-01-03", tz="UTC") + START_DATE = pd.Timestamp("2002-01-03") assert ( START_DATE == sorted(list(LEVERAGED_ETFS.keys()))[0] ), "START_DATE should match start of LEVERAGED_ETF data." - END_DATE = pd.Timestamp("2015-02-17", tz="utc") - extra_knowledge_date = pd.Timestamp("2015-01-27", tz="utc") - trading_day_before_first_kd = pd.Timestamp("2015-01-23", tz="utc") + END_DATE = pd.Timestamp("2015-02-17") - SIM_PARAMS_END = pd.Timestamp("2002-01-08", tz="UTC") + extra_knowledge_date = pd.Timestamp("2015-01-27") + trading_day_before_first_kd = pd.Timestamp("2015-01-23") + + SIM_PARAMS_END = pd.Timestamp("2002-01-08") SIM_PARAMS_DATA_FREQUENCY = "daily" DATA_PORTAL_USE_MINUTE_DATA = False @@ -138,7 +139,7 @@ def get_datetime(): def test_security_add(self): def get_datetime(): - return pd.Timestamp("2015-01-27", tz="UTC") + return pd.Timestamp("2015-01-27") with security_list_copy(): add_security_data(["AAPL", "GOOG"], []) @@ -159,7 +160,7 @@ def test_security_add_delete(self): with security_list_copy(): def get_datetime(): - return pd.Timestamp("2015-01-27", tz="UTC") + return pd.Timestamp("2015-01-27") rl = SecurityListSet(get_datetime, self.asset_finder) assert "BZQ" not in rl.leveraged_etf_list.current_securities(get_datetime()) diff --git a/tests/test_testing.py b/tests/test_testing.py index 77e543408c..da6165f7ae 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -32,12 +32,12 @@ class TestParameterSpace(TestCase): y_args = [3, 4] @classmethod - def setUpClass(cls): + def setup_class(cls): cls.xy_invocations = [] cls.yx_invocations = [] @classmethod - def tearDownClass(cls): + def teardown_class(cls): # This is the only actual test here. assert cls.xy_invocations == list(product(cls.x_args, cls.y_args)) assert cls.yx_invocations == list(product(cls.y_args, cls.x_args)) @@ -115,7 +115,7 @@ class TestTestingSlippage( def init_class_fixtures(cls): super(TestTestingSlippage, cls).init_class_fixtures() cls.asset = cls.asset_finder.retrieve_asset(1) - cls.minute, _ = cls.trading_calendar.open_and_close_for_session(cls.START_DATE) + cls.minute = cls.trading_calendar.session_first_minute(cls.START_DATE) def init_instance_fixtures(self): super(TestTestingSlippage, self).init_instance_fixtures() diff --git a/tests/test_tradesimulation.py b/tests/test_tradesimulation.py index 4ae3c41953..f0fd35f86a 100644 --- a/tests/test_tradesimulation.py +++ b/tests/test_tradesimulation.py @@ -39,8 +39,8 @@ class TestBeforeTradingStartTiming( # 13 14 15 16 17 18 19 # 20 21 22 23 24 25 26 # 27 28 29 30 31 - START_DATE = pd.Timestamp("2016-03-10", tz="UTC") - END_DATE = pd.Timestamp("2016-03-15", tz="UTC") + START_DATE = pd.Timestamp("2016-03-10") + END_DATE = pd.Timestamp("2016-03-15") @parameter_space( num_sessions=[1, 2, 3], diff --git a/tests/utils/test_date_utils.py b/tests/utils/test_date_utils.py index 343154f6b8..8982e10dc1 100644 --- a/tests/utils/test_date_utils.py +++ b/tests/utils/test_date_utils.py @@ -6,17 +6,13 @@ import pytest -def T(s, tz="UTC"): - """ - Helpful function to improve readability. - """ +def T(s, tz=None): + """Helpful function to improve readability.""" return pd.Timestamp(s, tz=tz) def DTI(start=None, end=None, periods=None, freq=None, tz=None, normalize=False): - """ - Creates DateTimeIndex using pd.date_range. - """ + """Creates DateTimeIndex using pd.date_range.""" return pd.date_range(start, end, periods, freq, tz, normalize) @@ -49,11 +45,11 @@ class TestDateUtils: ) def test_compute_date_range_chunks(self, chunksize, expected): # This date range results in 20 business days - start_date = T("2017-01-03") - end_date = T("2017-01-31") + start_date = pd.Timestamp("2017-01-03") + end_date = pd.Timestamp("2017-01-31") date_ranges = compute_date_range_chunks( - self.calendar.all_sessions, start_date, end_date, chunksize + self.calendar.sessions, start_date, end_date, chunksize ) assert list(date_ranges) == expected @@ -63,7 +59,7 @@ def test_compute_date_range_chunks_invalid_input(self): err_msg = "'Start date 2017-05-07 is not found in calendar.'" with pytest.raises(KeyError, match=err_msg): compute_date_range_chunks( - self.calendar.all_sessions, + self.calendar.sessions, T("2017-05-07"), # Sunday T("2017-06-01"), None, @@ -73,7 +69,7 @@ def test_compute_date_range_chunks_invalid_input(self): err_msg = "'End date 2017-05-27 is not found in calendar.'" with pytest.raises(KeyError, match=err_msg): compute_date_range_chunks( - self.calendar.all_sessions, + self.calendar.sessions, T("2017-05-01"), T("2017-05-27"), # Saturday None, @@ -83,7 +79,7 @@ def test_compute_date_range_chunks_invalid_input(self): err_msg = "End date 2017-05-01 cannot precede start date 2017-06-01." with pytest.raises(ValueError, match=err_msg): compute_date_range_chunks( - self.calendar.all_sessions, T("2017-06-01"), T("2017-05-01"), None + self.calendar.sessions, T("2017-06-01"), T("2017-05-01"), None ) diff --git a/tests/utils/test_final.py b/tests/utils/test_final.py index 6576702e2f..aeefbedb0e 100644 --- a/tests/utils/test_final.py +++ b/tests/utils/test_final.py @@ -13,7 +13,7 @@ class FinalMetaTestCase(TestCase): @classmethod - def setUpClass(cls): + def setup_class(cls): class ClassWithFinal(object, metaclass=FinalMeta): a = final("ClassWithFinal: a") b = "ClassWithFinal: b" @@ -169,7 +169,7 @@ class SubClass(self.class_): class FinalABCMetaTestCase(FinalMetaTestCase): # @classmethod - # def setUpClass(cls): + # def setup_class(cls): # FinalABCMeta = compose_types(FinalMeta, ABCMeta) # # class ABCWithFinal(with_metaclass(FinalABCMeta, object)): @@ -227,9 +227,7 @@ class FinalABCMetaTestCase(FinalMetaTestCase): # s.__setattr__ = lambda a, b: None def test_subclass_setattr(self): - """ - Tests that subclasses don't destroy the __setattr__. - """ + """Tests that subclasses don't destroy the __setattr__.""" class ClassWithFinal(object, metaclass=FinalMeta): @final From d018810c628a5779650b5e871621b073cae32942 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 09:12:46 +0100 Subject: [PATCH 10/15] mock is part of unittest --- pyproject.toml | 1 - src/zipline/testing/core.py | 2 +- tests/pipeline/test_frameload.py | 2 +- tests/pipeline/test_pipeline.py | 2 +- tests/test_cmdline.py | 2 +- tests/test_fetcher.py | 2 +- 6 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 70326cdff0..b9caf0c1f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,7 +87,6 @@ test = [ 'pytest-xdist >=2.5.0', 'pytest-timeout >=1.4.2', 'parameterized >=0.6.1', - 'mock >=4.0.3', 'testfixtures >=4.1.2', 'flake8 >=3.9.1', 'matplotlib >=1.5.3', diff --git a/src/zipline/testing/core.py b/src/zipline/testing/core.py index f02a67f89d..7dea408cf7 100644 --- a/src/zipline/testing/core.py +++ b/src/zipline/testing/core.py @@ -13,7 +13,7 @@ import numpy as np import pandas as pd -import mock +from unittest import mock from numpy.testing import assert_allclose, assert_array_equal from sqlalchemy import create_engine from testfixtures import TempDirectory diff --git a/tests/pipeline/test_frameload.py b/tests/pipeline/test_frameload.py index d0867f5adc..2afc62dcca 100644 --- a/tests/pipeline/test_frameload.py +++ b/tests/pipeline/test_frameload.py @@ -1,7 +1,7 @@ """ Tests for zipline.pipeline.loaders.frame.DataFrameLoader. """ -import mock +from unittest import mock import numpy as np import pandas as pd from numpy.testing import assert_array_equal diff --git a/tests/pipeline/test_pipeline.py b/tests/pipeline/test_pipeline.py index bb53d37ee7..a99f9bbcf7 100644 --- a/tests/pipeline/test_pipeline.py +++ b/tests/pipeline/test_pipeline.py @@ -1,6 +1,6 @@ """Tests for zipline.pipeline.Pipeline""" -import mock +from unittest import mock from zipline.pipeline import Factor, Filter, Pipeline from zipline.pipeline.data import Column, DataSet, USEquityPricing diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 9aab5a4c4b..413517c1d5 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock import zipline.__main__ as main import zipline diff --git a/tests/test_fetcher.py b/tests/test_fetcher.py index 1828cb359e..907a19439f 100644 --- a/tests/test_fetcher.py +++ b/tests/test_fetcher.py @@ -16,7 +16,7 @@ import pandas as pd import numpy as np -import mock +from unittest import mock from zipline.errors import UnsupportedOrderParameters from zipline.sources.requests_csv import mask_requests_args from zipline.utils import factory From 5e8cfcf9c8ebe24b2581c360bef4177863bfe707 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 09:21:08 +0100 Subject: [PATCH 11/15] fix cmdline start end --- src/zipline/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zipline/__main__.py b/src/zipline/__main__.py index 0e4d054ee8..582a604fab 100644 --- a/src/zipline/__main__.py +++ b/src/zipline/__main__.py @@ -200,13 +200,13 @@ def _(*args, **kwargs): @click.option( "-s", "--start", - type=Date(tz="utc", as_timestamp=True), + type=Date(as_timestamp=True), help="The start date of the simulation.", ) @click.option( "-e", "--end", - type=Date(tz="utc", as_timestamp=True), + type=Date(as_timestamp=True), help="The end date of the simulation.", ) @click.option( From 3f43467b52f83754b68f220277ec258b8de4fd03 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 20:55:22 +0100 Subject: [PATCH 12/15] separate lint from test workflow --- .github/workflows/ci_tests_quick.yml | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci_tests_quick.yml b/.github/workflows/ci_tests_quick.yml index b42b2aea24..5b4001253d 100644 --- a/.github/workflows/ci_tests_quick.yml +++ b/.github/workflows/ci_tests_quick.yml @@ -8,13 +8,35 @@ on: - main jobs: + # format: + # name: Formatting Check + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - uses: psf/black@stable + # with: + # options: "--check --diff" + # src: "./src ./tests" + + flake8-lint: + name: Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4.0.0 + with: + python-version: "3.10" + + - name: flake8 Lint + uses: py-actions/flake8@v2 + tests: runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - python-version: ['3.10'] + python-version: ["3.10"] steps: - name: Checkout Zipline @@ -50,10 +72,6 @@ jobs: python -m pip install tox tox-gh-actions python -m pip install .[test] - - name: Lint with flake8 - run: | - flake8 - - name: Unittests with tox & pytest uses: nick-fields/retry@v2 with: From 2264cabe54e911650d0141a3e485d548d4fb0df2 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 21:00:00 +0100 Subject: [PATCH 13/15] enable black formatting workflow check --- .devcontainer/requirements.txt | 1 - .github/workflows/ci_tests_quick.yml | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.devcontainer/requirements.txt b/.devcontainer/requirements.txt index 313f9bb2d8..478a34dc75 100644 --- a/.devcontainer/requirements.txt +++ b/.devcontainer/requirements.txt @@ -34,7 +34,6 @@ Mako==1.2.4 MarkupSafe==2.1.1 matplotlib==3.6.2 mccabe==0.7.0 -mock==4.0.3 multipledispatch==0.6.0 multitasking==0.0.11 mypy-extensions==0.4.3 diff --git a/.github/workflows/ci_tests_quick.yml b/.github/workflows/ci_tests_quick.yml index 5b4001253d..4f1b1e3384 100644 --- a/.github/workflows/ci_tests_quick.yml +++ b/.github/workflows/ci_tests_quick.yml @@ -8,18 +8,18 @@ on: - main jobs: - # format: - # name: Formatting Check - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v3 - # - uses: psf/black@stable - # with: - # options: "--check --diff" - # src: "./src ./tests" + black-format: + name: Formatting Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: psf/black@stable + with: + options: "--check --diff" + src: "./src ./tests" flake8-lint: - name: Lint + name: Lint Check runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 From adcb8f2f6d44bd627d807c19ec7b311050a2858f Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 21:04:13 +0100 Subject: [PATCH 14/15] separate black and lint from workflow --- .github/workflows/ci_tests_full.yml | 30 +++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci_tests_full.yml b/.github/workflows/ci_tests_full.yml index 9acf3029c8..738f41014a 100644 --- a/.github/workflows/ci_tests_full.yml +++ b/.github/workflows/ci_tests_full.yml @@ -6,13 +6,35 @@ on: - cron: "0 9 * * 6" jobs: + black-format: + name: Formatting Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: psf/black@stable + with: + options: "--check --diff" + src: "./src ./tests" + + flake8-lint: + name: Lint Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4.0.0 + with: + python-version: "3.10" + + - name: flake8 Lint + uses: py-actions/flake8@v2 + tests: runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ ubuntu-latest, windows-latest, macos-latest ] - python-version: [ '3.7', '3.8', '3.9', '3.10' ] + os: [ubuntu-latest, windows-latest, macos-latest] + python-version: ["3.7", "3.8", "3.9", "3.10"] exclude: - os: windows-latest python-version: 3.9 @@ -51,10 +73,6 @@ jobs: python -m pip install tox tox-gh-actions python -m pip install .[test] - - name: Lint with flake8 - run: | - flake8 - - name: Unittests with tox & pytest uses: nick-fields/retry@v2 with: From c51c8a009a26ef49797039f77dcebcd453e1be97 Mon Sep 17 00:00:00 2001 From: MBounouar Date: Wed, 21 Dec 2022 21:16:15 +0100 Subject: [PATCH 15/15] drop python 3.7 support --- .github/workflows/ci_tests_full.yml | 2 +- README.md | 10 +++++----- docs/source/install.rst | 2 +- pyproject.toml | 6 ++---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci_tests_full.yml b/.github/workflows/ci_tests_full.yml index 738f41014a..a1c73e3867 100644 --- a/.github/workflows/ci_tests_full.yml +++ b/.github/workflows/ci_tests_full.yml @@ -34,7 +34,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10"] exclude: - os: windows-latest python-version: 3.9 diff --git a/README.md b/README.md index 3d3542d4d5..e1aca5131f 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,10 @@ # Backtest your Trading Strategies -|Version Info| [![Python](https://img.shields.io/pypi/pyversions/zipline-reloaded.svg?cacheSeconds=2592000")](https://pypi.python.org/pypi/zipline-reloaded) [![Anaconda-Server Badge](https://anaconda.org/ml4t/zipline-reloaded/badges/platforms.svg)](https://anaconda.org/ml4t/zipline-reloaded) [![Release](https://img.shields.io/pypi/v/zipline-reloaded.svg?cacheSeconds=2592000)](https://pypi.org/project/zipline-reloaded/) [![Anaconda-Server Badge](https://anaconda.org/ml4t/zipline-reloaded/badges/version.svg)](https://anaconda.org/ml4t/zipline-reloaded)| -|----|----| -|**Test** **Status** | [![CI Tests](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/ci_tests_full.yml/badge.svg)](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/unit_tests.yml) [![PyPI](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/build_wheels.yml/badge.svg)](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/build_wheels.yml) [![Anaconda](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/conda_package.yml/badge.svg)](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/conda_package.yml) [![codecov](https://codecov.io/gh/stefan-jansen/zipline-reloaded/branch/main/graph/badge.svg)](https://codecov.io/gh/stefan-jansen/zipline-reloaded) | -|**Community**|[![Discourse](https://img.shields.io/discourse/topics?server=https%3A%2F%2Fexchange.ml4trading.io%2F)](https://exchange.ml4trading.io) [![ML4T](https://img.shields.io/badge/Powered%20by-ML4Trading-blue)](https://ml4trading.io) [![Twitter](https://img.shields.io/twitter/follow/ml4trading.svg?style=social)](https://twitter.com/ml4trading)| +| Version Info | [![Python](https://img.shields.io/pypi/pyversions/zipline-reloaded.svg?cacheSeconds=2592000")](https://pypi.python.org/pypi/zipline-reloaded) [![Anaconda-Server Badge](https://anaconda.org/ml4t/zipline-reloaded/badges/platforms.svg)](https://anaconda.org/ml4t/zipline-reloaded) [![Release](https://img.shields.io/pypi/v/zipline-reloaded.svg?cacheSeconds=2592000)](https://pypi.org/project/zipline-reloaded/) [![Anaconda-Server Badge](https://anaconda.org/ml4t/zipline-reloaded/badges/version.svg)](https://anaconda.org/ml4t/zipline-reloaded) | +| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| **Test** **Status** | [![CI Tests](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/ci_tests_full.yml/badge.svg)](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/unit_tests.yml) [![PyPI](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/build_wheels.yml/badge.svg)](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/build_wheels.yml) [![Anaconda](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/conda_package.yml/badge.svg)](https://github.com/stefan-jansen/zipline-reloaded/actions/workflows/conda_package.yml) [![codecov](https://codecov.io/gh/stefan-jansen/zipline-reloaded/branch/main/graph/badge.svg)](https://codecov.io/gh/stefan-jansen/zipline-reloaded) | +| **Community** | [![Discourse](https://img.shields.io/discourse/topics?server=https%3A%2F%2Fexchange.ml4trading.io%2F)](https://exchange.ml4trading.io) [![ML4T](https://img.shields.io/badge/Powered%20by-ML4Trading-blue)](https://ml4trading.io) [![Twitter](https://img.shields.io/twitter/follow/ml4trading.svg?style=social)](https://twitter.com/ml4trading) | Zipline is a Pythonic event-driven system for backtesting, developed and used as the backtesting and live-trading engine by [crowd-sourced investment fund Quantopian](https://www.bizjournals.com/boston/news/2020/11/10/quantopian-shuts-down-cofounders-head-elsewhere.html). Since it closed late 2020, the domain that had hosted these docs expired. The library is used extensively in the book [Machine Larning for Algorithmic Trading](https://ml4trading.io) by [Stefan Jansen](https://www.linkedin.com/in/applied-ai/) who is trying to keep the library up to date and available to his readers and the wider Python algotrading community. @@ -26,7 +26,7 @@ by [Stefan Jansen](https://www.linkedin.com/in/applied-ai/) who is trying to kee ## Installation -Zipline supports Python >= 3.7 and is compatible with current versions of the relevant [NumFOCUS](https://numfocus.org/sponsored-projects?_sft_project_category=python-interface) libraries, including [pandas](https://pandas.pydata.org/) and [scikit-learn](https://scikit-learn.org/stable/index.html). +Zipline supports Python >= 3.8 and is compatible with current versions of the relevant [NumFOCUS](https://numfocus.org/sponsored-projects?_sft_project_category=python-interface) libraries, including [pandas](https://pandas.pydata.org/) and [scikit-learn](https://scikit-learn.org/stable/index.html). If your system meets the pre-requisites described in the [installation instructions](https://zipline.ml4trading.io/install.html), you can install Zipline using pip by running: diff --git a/docs/source/install.rst b/docs/source/install.rst index 2a64859f87..5736c3589b 100644 --- a/docs/source/install.rst +++ b/docs/source/install.rst @@ -9,7 +9,7 @@ that runs on Windows, macOS, and Linux. In case you are installing `zipline-relo encounter [conflict errors](https://github.com/conda/conda/issues/9707), consider using [mamba](https://github.com/mamba-org/mamba) instead. -Zipline runs on Python 3.7, 3.8 and 3.9. To install and use different Python versions in parallel as well as create +Zipline runs on Python 3.8, 3.9 and 3.10. To install and use different Python versions in parallel as well as create a virtual environment, you may want to use `pyenv `_. Installing with ``pip`` diff --git a/pyproject.toml b/pyproject.toml index b9caf0c1f5..eff078278d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,6 @@ classifiers = [ 'License :: OSI Approved :: Apache Software License', 'Natural Language :: English', 'Programming Language :: Python', - 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', @@ -32,7 +31,7 @@ classifiers = [ license = { file = "LICENSE" } -requires-python = '>=3.7' +requires-python = '>=3.8' dependencies = [ 'alembic >=0.7.7', 'bcolz-zipline >=1.2.6', @@ -75,7 +74,7 @@ requires = [ "setuptools_scm[toml]>=6.2", 'wheel>=0.36.0', 'Cython>=0.29.21,<3', - 'oldest-supported-numpy; python_version>="3.7"', + 'oldest-supported-numpy; python_version>="3.8"', ] build-backend = 'setuptools.build_meta' @@ -179,7 +178,6 @@ minversion = 3.23.0 [gh-actions] python = - 3.7: py37 3.8: py38 3.9: py39 3.10: py310