Skip to content

Commit

Permalink
DOC: Rename 'guid' to 'id' in dividend tracking logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanderson committed Jul 14, 2014
1 parent 95aba2b commit 93dc37f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion zipline/finance/performance/period.py
Expand Up @@ -232,7 +232,7 @@ def _maybe_pay_dividend(self, dividend):
with fields drawn from zipline.protocol.DIVIDEND_PAYMENT_FIELDS.
"""
try:
unpaid_dividend = self._unpaid_dividends.loc[dividend['guid']]
unpaid_dividend = self._unpaid_dividends.loc[dividend['id']]
return unpaid_dividend
except KeyError:
return zp.dividend_payment()
Expand Down
2 changes: 1 addition & 1 deletion zipline/finance/performance/position.py
Expand Up @@ -61,7 +61,7 @@ def earn_dividend(self, dividend):
that we can pay out the correct amount on the dividend's pay date.
"""
assert dividend['sid'] == self.sid
out = {'guid': dividend['guid']}
out = {'id': dividend['id']}

# stock dividend
if dividend['payment_sid']:
Expand Down
9 changes: 6 additions & 3 deletions zipline/finance/performance/tracker.py
Expand Up @@ -194,20 +194,23 @@ def set_date(self, date):

def update_dividends(self, new_dividends):
"""
Update our dividend frame with new dividends.
Update our dividend frame with new dividends. @new_dividends should be
a DataFrame with columns containing at least the entries in
zipline.protocol.DIVIDEND_FIELDS.
"""

# Mark each new dividend with a unique integer id. This ensures that
# we can differentiate dividends whose date/sid fields are otherwise
# identical.
new_dividends['guid'] = np.arange(
new_dividends['id'] = np.arange(
self._dividend_count,
self._dividend_count + len(new_dividends),
)
self._dividend_count += len(new_dividends)

self.dividend_frame = pd.concat(
[self.dividend_frame, new_dividends]
).sort(['pay_date', 'ex_date']).set_index('guid', drop=False)
).sort(['pay_date', 'ex_date']).set_index('id', drop=False)

def update_performance(self):
# calculate performance as of last trade
Expand Down
15 changes: 7 additions & 8 deletions zipline/protocol.py
Expand Up @@ -47,34 +47,33 @@
'sid',
]
# Expected fields/index values for a dividend payment Series.
DIVIDEND_PAYMENT_FIELDS = ['guid', 'payment_sid', 'cash_amount', 'share_count']
DIVIDEND_PAYMENT_FIELDS = ['id', 'payment_sid', 'cash_amount', 'share_count']


def dividend_payment(data=None):
"""
Take a dictionary whose values are in DIVIDEND_PAYMENT_FIELDS and return a
series representing the payment of a dividend.
Guids are assigned to each historical dividend in
Ids are assigned to each historical dividend in
PerformanceTracker.update_dividends. They are guaranteed to be unique
integers with the context of a single simulation. If @data is non-empty, a
guid is required to identify the historical dividend associated with this
id is required to identify the historical dividend associated with this
payment.
Additionally, if @data is non-empty, either data['cash_amount'] should be
nonzero or data['payment_sid'] should be a security identifier and
data['share_count'] should be nonzero.
The returned Series is given its guid value as a name so that concatenating
payments results in a DataFrame indexed by guid. (Note, however, that the
The returned Series is given its id value as a name so that concatenating
payments results in a DataFrame indexed by id. (Note, however, that the
name value is not used to construct an index when this series is returned
by function called by `DataFrame.apply`. In such a case, pandas preserves
by function passed to `DataFrame.apply`. In such a case, pandas preserves
the index of the DataFrame on which `apply` is being called.)
"""
return pd.Series(
data=data,
name=data['guid'] if data is not None else None,
name=data['id'] if data is not None else None,
index=DIVIDEND_PAYMENT_FIELDS,
dtype=object,
)
Expand Down

0 comments on commit 93dc37f

Please sign in to comment.