Completed
Push — master ( ebb4fb...323695 )
by
unknown
01:25
created

_compute()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 11
rs 9.4286
1
"""
2
Factors describing information about event data (e.g. earnings
3
announcements, acquisitions, dividends, etc.).
4
"""
5
from numpy import newaxis
6
from zipline.pipeline.data.earnings import EarningsCalendar
7
from zipline.utils.numpy_utils import (
8
    np_NaT,
9
    busday_count_mask_NaT,
10
    datetime64D_dtype,
11
    float64_dtype,
12
)
13
14
from .factor import Factor
15
16
17
class BusinessDaysUntilNextEarnings(Factor):
18
    """
19
    Factor returning the number of **business days** (not trading days!) until
20
    the next known earnings date for each asset.
21
22
    This doesn't use trading days because the trading calendar includes
23
    information that may not have been available to the algorithm at the time
24
    when `compute` is called.
25
26
    For example, the NYSE closings September 11th 2001, would not have been
27
    known to the algorithm on September 10th.
28
29
    Assets that announced or will announce earnings today will produce a value
30
    of 0.0.  Assets that will announce earnings on the next upcoming business
31
    day will produce a value of 1.0.
32
33
    Assets for which `EarningsCalendar.next_announcement` is `NaT` will produce
34
    a value of `NaN`.
35
36
37
    See Also
38
    --------
39
    zipline.pipeline.factors.BusinessDaysSincePreviousEarnings
40
    """
41
    inputs = [EarningsCalendar.next_announcement]
42
    window_length = 0
43
    dtype = float64_dtype
44
45
    def _compute(self, arrays, dates, assets, mask):
46
47
        # Coerce from [ns] to [D] for numpy busday_count.
48
        announce_dates = arrays[0].astype(datetime64D_dtype)
49
50
        # Set masked values to NaT.
51
        announce_dates[~mask] = np_NaT
52
53
        # Convert row labels into a column vector for broadcasted comparison.
54
        reference_dates = dates.values.astype(datetime64D_dtype)[:, newaxis]
55
        return busday_count_mask_NaT(reference_dates, announce_dates)
56
57
58
class BusinessDaysSincePreviousEarnings(Factor):
59
    """
60
    Factor returning the number of **business days** (not trading days!) since
61
    the most recent earnings date for each asset.
62
63
    This doesn't use trading days for symmetry with
64
    BusinessDaysUntilNextEarnings.
65
66
    Assets which announced or will announce earnings today will produce a value
67
    of 0.0.  Assets that announced earnings on the previous business day will
68
    produce a value of 1.0.
69
70
    Assets for which `EarningsCalendar.previous_announcement` is `NaT` will
71
    produce a value of `NaN`.
72
73
    See Also
74
    --------
75
    zipline.pipeline.factors.BusinessDaysUntilNextEarnings
76
    """
77
    inputs = [EarningsCalendar.previous_announcement]
78
    window_length = 0
79
    dtype = float64_dtype
80
81
    def _compute(self, arrays, dates, assets, mask):
82
83
        # Coerce from [ns] to [D] for numpy busday_count.
84
        announce_dates = arrays[0].astype(datetime64D_dtype)
85
86
        # Set masked values to NaT.
87
        announce_dates[~mask] = np_NaT
88
89
        # Convert row labels into a column vector for broadcasted comparison.
90
        reference_dates = dates.values.astype(datetime64D_dtype)[:, newaxis]
91
        return busday_count_mask_NaT(announce_dates, reference_dates)
92