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
|
|
|
See Also |
37
|
|
|
-------- |
38
|
|
|
BusinessDaysSincePreviousEarnings |
39
|
|
|
""" |
40
|
|
|
inputs = [EarningsCalendar.next_announcement] |
41
|
|
|
window_length = 0 |
42
|
|
|
dtype = float64_dtype |
43
|
|
|
|
44
|
|
|
def _compute(self, arrays, dates, assets, mask): |
45
|
|
|
|
46
|
|
|
# Coerce from [ns] to [D] for numpy busday_count. |
47
|
|
|
announce_dates = arrays[0].astype(datetime64D_dtype) |
48
|
|
|
|
49
|
|
|
# Set masked values to NaT. |
50
|
|
|
announce_dates[~mask] = np_NaT |
51
|
|
|
|
52
|
|
|
# Convert row labels into a column vector for broadcasted comparison. |
53
|
|
|
reference_dates = dates.values.astype(datetime64D_dtype)[:, newaxis] |
54
|
|
|
return busday_count_mask_NaT(reference_dates, announce_dates) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class BusinessDaysSincePreviousEarnings(Factor): |
58
|
|
|
""" |
59
|
|
|
Factor returning the number of **business days** (not trading days!) since |
60
|
|
|
the most recent earnings date for each asset. |
61
|
|
|
|
62
|
|
|
This doesn't use trading days for symmetry with |
63
|
|
|
BusinessDaysUntilNextEarnings. |
64
|
|
|
|
65
|
|
|
Assets which announced or will announce earnings today will produce a value |
66
|
|
|
of 0.0. Assets that announced earnings on the previous business day will |
67
|
|
|
produce a value of 1.0. |
68
|
|
|
|
69
|
|
|
Assets for which `EarningsCalendar.previous_announcement` is `NaT` will |
70
|
|
|
produce a value of `NaN`. |
71
|
|
|
|
72
|
|
|
See Also |
73
|
|
|
-------- |
74
|
|
|
BusinessDaysUntilNextEarnings |
75
|
|
|
""" |
76
|
|
|
inputs = [EarningsCalendar.previous_announcement] |
77
|
|
|
window_length = 0 |
78
|
|
|
dtype = float64_dtype |
79
|
|
|
|
80
|
|
|
def _compute(self, arrays, dates, assets, mask): |
81
|
|
|
|
82
|
|
|
# Coerce from [ns] to [D] for numpy busday_count. |
83
|
|
|
announce_dates = arrays[0].astype(datetime64D_dtype) |
84
|
|
|
|
85
|
|
|
# Set masked values to NaT. |
86
|
|
|
announce_dates[~mask] = np_NaT |
87
|
|
|
|
88
|
|
|
# Convert row labels into a column vector for broadcasted comparison. |
89
|
|
|
reference_dates = dates.values.astype(datetime64D_dtype)[:, newaxis] |
90
|
|
|
return busday_count_mask_NaT(announce_dates, reference_dates) |
91
|
|
|
|