From 93dc37fa59a30f6313a07d09f985101b4a082534 Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Mon, 14 Jul 2014 02:23:24 -0400 Subject: [PATCH] DOC: Rename 'guid' to 'id' in dividend tracking logic. --- zipline/finance/performance/period.py | 2 +- zipline/finance/performance/position.py | 2 +- zipline/finance/performance/tracker.py | 9 ++++++--- zipline/protocol.py | 15 +++++++-------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/zipline/finance/performance/period.py b/zipline/finance/performance/period.py index d943c5ed9f..aa8e752ce1 100644 --- a/zipline/finance/performance/period.py +++ b/zipline/finance/performance/period.py @@ -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() diff --git a/zipline/finance/performance/position.py b/zipline/finance/performance/position.py index da2a41660d..008ced15d8 100644 --- a/zipline/finance/performance/position.py +++ b/zipline/finance/performance/position.py @@ -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']: diff --git a/zipline/finance/performance/tracker.py b/zipline/finance/performance/tracker.py index 7a3d18664e..50d2cd4bc8 100644 --- a/zipline/finance/performance/tracker.py +++ b/zipline/finance/performance/tracker.py @@ -194,12 +194,15 @@ 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), ) @@ -207,7 +210,7 @@ def update_dividends(self, 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 diff --git a/zipline/protocol.py b/zipline/protocol.py index 4684953752..9aa7fa5c4a 100644 --- a/zipline/protocol.py +++ b/zipline/protocol.py @@ -47,7 +47,7 @@ '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): @@ -55,26 +55,25 @@ 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, )