mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-28 00:27:59 +08:00
TST: add test for 13d filings dataset
MAINT: add 13d filings to factors init MAINT: rename constant MAINT: add event_date_col field
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
"""
|
||||
Tests for the reference loader for 13d filings.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
|
||||
from contextlib2 import ExitStack
|
||||
import pandas as pd
|
||||
|
||||
from .base import EventLoaderCommonMixin
|
||||
from zipline.pipeline.common import(
|
||||
DAYS_SINCE_PREV_DISCLOSURE,
|
||||
DISCLOSURE_DATE,
|
||||
@@ -25,11 +21,25 @@ from zipline.pipeline.loaders.utils import (
|
||||
zip_with_floats,
|
||||
zip_with_dates
|
||||
)
|
||||
from zipline.testing import tmp_asset_finder
|
||||
from zipline.testing.fixtures import WithPipelineEventDataLoader
|
||||
from zipline.testing.fixtures import ZiplineTestCase
|
||||
|
||||
date_intervals = [[None, '2014-01-04'], ['2014-01-05', '2014-01-09'],
|
||||
date_intervals = [[None, '2014-01-04'],
|
||||
['2014-01-05', '2014-01-09'],
|
||||
['2014-01-10', None]]
|
||||
|
||||
empty_df = pd.DataFrame(
|
||||
columns=[NUM_SHARES,
|
||||
PERCENT_SHARES,
|
||||
DISCLOSURE_DATE,
|
||||
TS_FIELD_NAME],
|
||||
)
|
||||
|
||||
empty_df[NUM_SHARES] = empty_df[NUM_SHARES].astype('float')
|
||||
empty_df[PERCENT_SHARES] = empty_df[PERCENT_SHARES].astype('float')
|
||||
empty_df[TS_FIELD_NAME] = empty_df[TS_FIELD_NAME].astype('datetime64[ns]')
|
||||
empty_df[DISCLOSURE_DATE] = empty_df[DISCLOSURE_DATE].astype('datetime64[ns]')
|
||||
|
||||
_13d_filngs_cases = [
|
||||
pd.DataFrame({
|
||||
NUM_SHARES: [1, 15],
|
||||
@@ -37,29 +47,25 @@ _13d_filngs_cases = [
|
||||
TS_FIELD_NAME: pd.to_datetime(['2014-01-05', '2014-01-10']),
|
||||
DISCLOSURE_DATE: pd.to_datetime(['2014-01-04', '2014-01-09'])
|
||||
}),
|
||||
pd.DataFrame(
|
||||
columns=[NUM_SHARES,
|
||||
PERCENT_SHARES,
|
||||
DISCLOSURE_DATE,
|
||||
TS_FIELD_NAME],
|
||||
dtype='datetime64[ns]'
|
||||
),
|
||||
empty_df
|
||||
]
|
||||
|
||||
|
||||
def get_expected_previous_values(zip_date_index_with_vals,
|
||||
dates,
|
||||
vals_for_date_intervals):
|
||||
vals,
|
||||
date_intervals,
|
||||
dates):
|
||||
return pd.DataFrame({
|
||||
0: get_values_for_date_ranges(zip_date_index_with_vals,
|
||||
vals_for_date_intervals,
|
||||
vals,
|
||||
date_intervals,
|
||||
dates),
|
||||
1: zip_date_index_with_vals(dates, ['NaN'] * len(dates)),
|
||||
}, index=dates)
|
||||
|
||||
|
||||
class _13DFilingsLoaderTestCase(TestCase, EventLoaderCommonMixin):
|
||||
class _13DFilingsLoaderTestCase(WithPipelineEventDataLoader,
|
||||
ZiplineTestCase):
|
||||
"""
|
||||
Test for _13_filings dataset.
|
||||
"""
|
||||
@@ -79,37 +85,27 @@ class _13DFilingsLoaderTestCase(TestCase, EventLoaderCommonMixin):
|
||||
return range(2)
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls._cleanup_stack = stack = ExitStack()
|
||||
cls.finder = stack.enter_context(
|
||||
tmp_asset_finder(equities=cls.get_equity_info()),
|
||||
)
|
||||
cls.cols = {}
|
||||
cls.dataset = {sid:
|
||||
frame
|
||||
for sid, frame
|
||||
in enumerate(_13d_filngs_cases)}
|
||||
cls.loader_type = _13DFilingsLoader
|
||||
def get_dataset(cls):
|
||||
return {sid: frame
|
||||
for sid, frame
|
||||
in enumerate(_13d_filngs_cases)}
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls._cleanup_stack.close()
|
||||
loader_type = _13DFilingsLoader
|
||||
|
||||
def setup(self, dates):
|
||||
_expected_previous_num_shares = get_expected_previous_values(
|
||||
zip_with_floats, dates,
|
||||
['NaN', 1, 15]
|
||||
)
|
||||
_expected_previous_percent_shares = get_expected_previous_values(
|
||||
zip_with_floats, dates,
|
||||
['NaN', 10, 20]
|
||||
)
|
||||
self.cols[
|
||||
cols = {}
|
||||
cols[
|
||||
PREVIOUS_DISCLOSURE_DATE
|
||||
] = get_expected_previous_values(zip_with_dates, dates,
|
||||
['NaT', '2014-01-04', '2014-01-09'])
|
||||
self.cols[PREVIOUS_NUM_SHARES] = _expected_previous_num_shares
|
||||
self.cols[PREVIOUS_PERCENT_SHARES] = _expected_previous_percent_shares
|
||||
self.cols[DAYS_SINCE_PREV_DISCLOSURE] = self._compute_busday_offsets(
|
||||
self.cols[PREVIOUS_DISCLOSURE_DATE]
|
||||
] = get_expected_previous_values(zip_with_dates,
|
||||
['NaT', '2014-01-04', '2014-01-09'],
|
||||
date_intervals, dates)
|
||||
cols[PREVIOUS_NUM_SHARES] = get_expected_previous_values(
|
||||
zip_with_floats, ['NaN', 1, 15], date_intervals, dates
|
||||
)
|
||||
cols[PREVIOUS_PERCENT_SHARES] = get_expected_previous_values(
|
||||
zip_with_floats, ['NaN', 10, 20], date_intervals, dates
|
||||
)
|
||||
cols[DAYS_SINCE_PREV_DISCLOSURE] = self._compute_busday_offsets(
|
||||
cols[PREVIOUS_DISCLOSURE_DATE]
|
||||
)
|
||||
return cols
|
||||
|
||||
@@ -35,7 +35,7 @@ NUM_SHARES = 'number_shares'
|
||||
NEXT_RELEASE_DATE = 'next_release_date'
|
||||
NEXT_STANDARD_DEVIATION = 'next_standard_deviation'
|
||||
PAY_DATE_FIELD_NAME = 'pay_date'
|
||||
PERCENT_SHARES = 'percentage'
|
||||
PERCENT_SHARES = 'percent_shares'
|
||||
PREVIOUS_ACTUAL_VALUE = 'previous_actual_value'
|
||||
PREVIOUS_AMOUNT = 'previous_amount'
|
||||
PREVIOUS_ANNOUNCEMENT = 'previous_announcement'
|
||||
|
||||
@@ -5,6 +5,7 @@ from .factor import (
|
||||
RecarrayField,
|
||||
)
|
||||
from .events import (
|
||||
BusinessDaysSince13DFilingsDate,
|
||||
BusinessDaysSinceCashBuybackAuth,
|
||||
BusinessDaysSinceDividendAnnouncement,
|
||||
BusinessDaysUntilNextExDate,
|
||||
@@ -28,6 +29,7 @@ from .technical import (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
'BusinessDaysSince13DFilingsDate',
|
||||
'BusinessDaysSinceCashBuybackAuth',
|
||||
'BusinessDaysSinceDividendAnnouncement',
|
||||
'BusinessDaysUntilNextExDate',
|
||||
|
||||
@@ -24,6 +24,7 @@ class _13DFilingsLoader(EventsLoader):
|
||||
expected_cols = frozenset([DISCLOSURE_DATE,
|
||||
PERCENT_SHARES,
|
||||
NUM_SHARES])
|
||||
event_date_col = DISCLOSURE_DATE
|
||||
|
||||
def __init__(self, all_dates, events_by_sid,
|
||||
infer_timestamps=False,
|
||||
@@ -36,14 +37,12 @@ class _13DFilingsLoader(EventsLoader):
|
||||
def disclosure_date_loader(self):
|
||||
return self._previous_event_date_loader(
|
||||
self.dataset.disclosure_date,
|
||||
DISCLOSURE_DATE
|
||||
)
|
||||
|
||||
@lazyval
|
||||
def percent_shares_loader(self):
|
||||
return self._previous_event_value_loader(
|
||||
self.dataset.percent_shares,
|
||||
DISCLOSURE_DATE,
|
||||
PERCENT_SHARES
|
||||
)
|
||||
|
||||
@@ -51,6 +50,5 @@ class _13DFilingsLoader(EventsLoader):
|
||||
def number_shares_loader(self):
|
||||
return self._previous_event_value_loader(
|
||||
self.dataset.number_shares,
|
||||
DISCLOSURE_DATE,
|
||||
NUM_SHARES
|
||||
)
|
||||
|
||||
@@ -65,4 +65,4 @@ class Blaze_13DFilingsLoader(BlazeEventsLoader):
|
||||
})
|
||||
|
||||
concrete_loader = _13DFilingsLoader
|
||||
concrete_dataset=_13DFilings
|
||||
concrete_dataset = _13DFilings
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from ._13d_filings import _13DFilingsLoader
|
||||
from ._13d_filings import Blaze_13DFilingsLoader
|
||||
from .buyback_auth import (
|
||||
BlazeCashBuybackAuthorizationsLoader,
|
||||
BlazeShareBuybackAuthorizationsLoader
|
||||
@@ -20,7 +20,7 @@ from .earnings import (
|
||||
from .consensus_estimates import BlazeConsensusEstimatesLoader
|
||||
|
||||
__all__ = (
|
||||
'_13DFilingsLoader',
|
||||
'Blaze_13DFilingsLoader',
|
||||
'BlazeCashBuybackAuthorizationsLoader',
|
||||
'BlazeDividendsByAnnouncementDateLoader',
|
||||
'BlazeConsensusEstimatesLoader',
|
||||
|
||||
Reference in New Issue
Block a user