Skip to content

Commit

Permalink
Merge 0d7f9c0 into 7056a90
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Sanderson committed Jun 22, 2018
2 parents 7056a90 + 0d7f9c0 commit a2b5733
Show file tree
Hide file tree
Showing 29 changed files with 493 additions and 1,312 deletions.
16 changes: 0 additions & 16 deletions tests/data/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from zipline.data.resample import (
minute_frame_to_session_frame,
minute_panel_to_session_panel,
DailyHistoryAggregator,
MinuteResampleSessionBarReader,
ReindexMinuteBarReader,
Expand Down Expand Up @@ -564,21 +563,6 @@ def test_minute_to_session(self):
result.values,
err_msg='sid={0}'.format(sid))

def test_minute_panel_to_session_panel(self):
minute_panel = pd.Panel(
{sid: self.equity_frames[sid]
for sid in self.ASSET_FINDER_EQUITY_SIDS}
)
expected = pd.Panel(
{sid: EXPECTED_SESSIONS[sid]
for sid in self.ASSET_FINDER_EQUITY_SIDS}
)
result = minute_panel_to_session_panel(
minute_panel,
self.nyse_calendar
)
assert_almost_equal(expected.values, result.values)


class TestResampleSessionBars(WithBcolzFutureMinuteBarReader,
ZiplineTestCase):
Expand Down
12 changes: 7 additions & 5 deletions tests/finance/test_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from zipline.utils import factory

from zipline.finance.trading import SimulationParameters
from zipline.testing.fixtures import WithTradingEnvironment, ZiplineTestCase
import zipline.testing.fixtures as zf

from zipline.finance.metrics import _ClassicRiskMetrics as ClassicRiskMetrics

Expand All @@ -39,7 +39,7 @@
]


class TestRisk(WithTradingEnvironment, ZiplineTestCase):
class TestRisk(zf.WithBenchmarkReturns, zf.ZiplineTestCase):

def init_instance_fixtures(self):
super(TestRisk, self).init_instance_fixtures()
Expand Down Expand Up @@ -172,7 +172,7 @@ def test_treasury_returns(self):
returns = factory.create_returns_from_range(self.sim_params)
metrics = ClassicRiskMetrics.risk_report(
algorithm_returns=returns,
benchmark_returns=self.env.benchmark_returns,
benchmark_returns=self.benchmark_returns,
algorithm_leverages=pd.Series(0.0, index=returns.index)
)

Expand Down Expand Up @@ -202,7 +202,8 @@ def test_benchmarkrange(self):
returns = factory.create_returns_from_range(sim_params)
metrics = ClassicRiskMetrics.risk_report(
algorithm_returns=returns,
benchmark_returns=self.env.benchmark_returns,
# use returns from the fixture to ensure that we have enough data.
benchmark_returns=self.BENCHMARK_RETURNS,
algorithm_leverages=pd.Series(0.0, index=returns.index)
)

Expand All @@ -227,7 +228,8 @@ def test_partial_month(self):
returns = returns[:-10] # truncate the returns series to end mid-month
metrics = ClassicRiskMetrics.risk_report(
algorithm_returns=returns,
benchmark_returns=self.env.benchmark_returns,
# use returns from the fixture to ensure that we have enough data.
benchmark_returns=self.BENCHMARK_RETURNS,
algorithm_leverages=pd.Series(0.0, index=returns.index)
)
total_months = 60
Expand Down
16 changes: 9 additions & 7 deletions tests/finance/test_slippage.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
tmp_bcolz_equity_minute_bar_reader,
)
from zipline.testing.fixtures import (
WithAssetFinder,
WithCreateBarData,
WithDataPortal,
WithSimParams,
WithTradingEnvironment,
WithTradingCalendars,
ZiplineTestCase,
)
from zipline.utils.classproperty import classproperty
Expand Down Expand Up @@ -96,7 +97,7 @@ def make_equity_minute_bar_data(cls):
@classmethod
def init_class_fixtures(cls):
super(SlippageTestCase, cls).init_class_fixtures()
cls.ASSET133 = cls.env.asset_finder.retrieve_asset(133)
cls.ASSET133 = cls.asset_finder.retrieve_asset(133)

def test_allowed_asset_types(self):
# Custom equities model.
Expand Down Expand Up @@ -628,8 +629,8 @@ def make_future_minute_bar_data(cls):
@classmethod
def init_class_fixtures(cls):
super(VolumeShareSlippageTestCase, cls).init_class_fixtures()
cls.ASSET133 = cls.env.asset_finder.retrieve_asset(133)
cls.ASSET1000 = cls.env.asset_finder.retrieve_asset(1000)
cls.ASSET133 = cls.asset_finder.retrieve_asset(133)
cls.ASSET1000 = cls.asset_finder.retrieve_asset(1000)

def test_volume_share_slippage(self):

Expand Down Expand Up @@ -943,7 +944,8 @@ def test_window_data(self):


class OrdersStopTestCase(WithSimParams,
WithTradingEnvironment,
WithAssetFinder,
WithTradingCalendars,
ZiplineTestCase):

START_DATE = pd.Timestamp('2006-01-05 14:31', tz='utc')
Expand All @@ -961,7 +963,7 @@ class OrdersStopTestCase(WithSimParams,
@classmethod
def init_class_fixtures(cls):
super(OrdersStopTestCase, cls).init_class_fixtures()
cls.ASSET133 = cls.env.asset_finder.retrieve_asset(133)
cls.ASSET133 = cls.asset_finder.retrieve_asset(133)

STOP_ORDER_CASES = {
# Stop orders can be long/short and have their price greater or
Expand Down Expand Up @@ -1111,7 +1113,7 @@ def test_orders_stop(self, name, order_data, event_data, expected):
with tmp_bcolz_equity_minute_bar_reader(
self.trading_calendar, days, assets) as reader:
data_portal = DataPortal(
self.env.asset_finder, self.trading_calendar,
self.asset_finder, self.trading_calendar,
first_trading_day=reader.first_trading_day,
equity_minute_reader=reader,
)
Expand Down
11 changes: 7 additions & 4 deletions tests/history/generate_csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import numpy as np
import pandas as pd

from zipline.finance.trading import TradingEnvironment
from zipline.data.us_equity_minutes import BcolzMinuteBarWriter
from zipline.utils.calendars import get_calendar


def generate_daily_test_data(first_day,
Expand All @@ -28,7 +28,8 @@ def generate_daily_test_data(first_day,
multipliers_list,
path):

days = TradingEnvironment.instance().days_in_range(first_day, last_day)
cal = get_calendar('NYSE')
days = cal.days_in_range(first_day, last_day)

days_count = len(days)
o = np.zeros(days_count, dtype=np.uint32)
Expand Down Expand Up @@ -108,8 +109,10 @@ def generate_minute_test_data(first_day,
first_day, last_day)
minutes_count = len(full_minutes)

minutes = TradingEnvironment.instance().minutes_for_days_in_range(
first_day, last_day)
cal = get_calendar('NYSE')
minutes = cal.minutes_for_sessions_in_range(
first_day, last_day
)

o = np.zeros(minutes_count, dtype=np.uint32)
h = np.zeros(minutes_count, dtype=np.uint32)
Expand Down
49 changes: 27 additions & 22 deletions tests/pipeline/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@
parameter_space,
product_upper_triangle,
)
from zipline.testing.fixtures import (
WithAdjustmentReader,
WithEquityPricingPipelineEngine,
WithSeededRandomPipelineEngine,
WithTradingEnvironment,
ZiplineTestCase,
)
import zipline.testing.fixtures as zf
from zipline.testing.predicates import assert_equal
from zipline.utils.memoize import lazyval
from zipline.utils.numpy_utils import bool_dtype, datetime64ns_dtype
Expand Down Expand Up @@ -168,7 +162,7 @@ def compute(self, today, assets, out, *inputs):
out[:] = sum(inputs).sum(axis=0)


class WithConstantInputs(WithTradingEnvironment):
class WithConstantInputs(object):
asset_ids = ASSET_FINDER_EQUITY_SIDS = 1, 2, 3, 4
START_DATE = Timestamp('2014-01-01', tz='utc')
END_DATE = Timestamp('2014-03-01', tz='utc')
Expand Down Expand Up @@ -198,7 +192,10 @@ def init_class_fixtures(cls):
cls.assets = cls.asset_finder.retrieve_all(cls.asset_ids)


class ConstantInputTestCase(WithConstantInputs, ZiplineTestCase):
class ConstantInputTestCase(WithConstantInputs,
zf.WithAssetFinder,
zf.WithTradingCalendars,
zf.ZiplineTestCase):
def test_bad_dates(self):
loader = self.loader
engine = SimplePipelineEngine(
Expand Down Expand Up @@ -815,7 +812,9 @@ def expected_for_col(col):
Loader2DataSet.col2)})


class FrameInputTestCase(WithTradingEnvironment, ZiplineTestCase):
class FrameInputTestCase(zf.WithAssetFinder,
zf.WithTradingCalendars,
zf.ZiplineTestCase):
asset_ids = ASSET_FINDER_EQUITY_SIDS = 1, 2, 3
start = START_DATE = Timestamp('2015-01-01', tz='utc')
end = END_DATE = Timestamp('2015-01-31', tz='utc')
Expand Down Expand Up @@ -919,8 +918,9 @@ def apply_date(idx, offset=0):
assert_frame_equal(high_results, high_base.iloc[iloc_bounds])


class SyntheticBcolzTestCase(WithAdjustmentReader,
ZiplineTestCase):
class SyntheticBcolzTestCase(zf.WithAdjustmentReader,
zf.WithAssetFinder,
zf.ZiplineTestCase):
first_asset_start = Timestamp('2015-04-01', tz='UTC')
START_DATE = Timestamp('2015-01-01', tz='utc')
END_DATE = Timestamp('2015-08-01', tz='utc')
Expand Down Expand Up @@ -1077,7 +1077,9 @@ def test_drawdown(self):
assert_frame_equal(expected, result)


class ParameterizedFactorTestCase(WithTradingEnvironment, ZiplineTestCase):
class ParameterizedFactorTestCase(zf.WithAssetFinder,
zf.WithTradingCalendars,
zf.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')
Expand Down Expand Up @@ -1294,8 +1296,8 @@ def test_dollar_volume(self):
assert_frame_equal(results['dv5_nan'].unstack(), expected_5_nan)


class StringColumnTestCase(WithSeededRandomPipelineEngine,
ZiplineTestCase):
class StringColumnTestCase(zf.WithSeededRandomPipelineEngine,
zf.ZiplineTestCase):

def test_string_classifiers_produce_categoricals(self):
"""
Expand Down Expand Up @@ -1324,8 +1326,8 @@ def test_string_classifiers_produce_categoricals(self):
assert_frame_equal(result.c.unstack(), expected_final_result)


class WindowSafetyPropagationTestCase(WithSeededRandomPipelineEngine,
ZiplineTestCase):
class WindowSafetyPropagationTestCase(zf.WithSeededRandomPipelineEngine,
zf.ZiplineTestCase):

SEEDED_RANDOM_PIPELINE_SEED = 5

Expand Down Expand Up @@ -1376,7 +1378,10 @@ def test_window_safety_propagation(self):
assert_equal(expected_result, results[colname])


class PopulateInitialWorkspaceTestCase(WithConstantInputs, ZiplineTestCase):
class PopulateInitialWorkspaceTestCase(WithConstantInputs,
zf.WithAssetFinder,
zf.WithTradingCalendars,
zf.ZiplineTestCase):

@parameter_space(window_length=[3, 5], pipeline_length=[5, 10])
def test_populate_initial_workspace(self, window_length, pipeline_length):
Expand Down Expand Up @@ -1499,8 +1504,8 @@ def dispatcher(c):
)


class ChunkedPipelineTestCase(WithEquityPricingPipelineEngine,
ZiplineTestCase):
class ChunkedPipelineTestCase(zf.WithEquityPricingPipelineEngine,
zf.ZiplineTestCase):

PIPELINE_START_DATE = Timestamp('2006-01-05', tz='UTC')
END_DATE = Timestamp('2006-12-29', tz='UTC')
Expand Down Expand Up @@ -1531,8 +1536,8 @@ def test_run_chunked_pipeline(self):
self.assertTrue(chunked_result.equals(pipeline_result))


class MaximumRegressionTest(WithSeededRandomPipelineEngine,
ZiplineTestCase):
class MaximumRegressionTest(zf.WithSeededRandomPipelineEngine,
zf.ZiplineTestCase):
ASSET_FINDER_EQUITY_SIDS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

def test_no_groupby_maximum(self):
Expand Down
16 changes: 7 additions & 9 deletions tests/pipeline/test_statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
make_cascading_boolean_array,
parameter_space,
)
from zipline.testing.fixtures import (
WithSeededRandomPipelineEngine,
WithTradingEnvironment,
ZiplineTestCase,
)
import zipline.testing.fixtures as zf
from zipline.testing.predicates import assert_equal
from zipline.utils.numpy_utils import (
as_column,
Expand All @@ -58,7 +54,9 @@
)


class StatisticalBuiltInsTestCase(WithTradingEnvironment, ZiplineTestCase):
class StatisticalBuiltInsTestCase(zf.WithAssetFinder,
zf.WithTradingCalendars,
zf.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')
Expand Down Expand Up @@ -495,8 +493,8 @@ def test_simple_beta_short_repr(self):
self.assertEqual(result, expected)


class StatisticalMethodsTestCase(WithSeededRandomPipelineEngine,
ZiplineTestCase):
class StatisticalMethodsTestCase(zf.WithSeededRandomPipelineEngine,
zf.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')
Expand Down Expand Up @@ -912,7 +910,7 @@ def test_factor_regression_method_two_factors(self, regression_length):
assert_frame_equal(output_result, expected_output_result)


class VectorizedBetaTestCase(ZiplineTestCase):
class VectorizedBetaTestCase(zf.ZiplineTestCase):

def compare_with_empyrical(self, dependents, independent):
INFINITY = 1000000 # close enough
Expand Down
Loading

0 comments on commit a2b5733

Please sign in to comment.