Merge pull request #76 from quantopian/dividends_for_shorts

Implemented dividend costs for short positions.
This commit is contained in:
fawce
2013-02-07 10:55:14 -08:00
2 changed files with 16 additions and 18 deletions
+5 -5
View File
@@ -298,7 +298,7 @@ class TestDividendPerformance(unittest.TestCase):
[0, -1000, -1000, -1000, -1000]
)
def test_short_position_receives_no_dividend(self):
def test_short_position_pays_dividend(self):
#post some trades in the market
events = factory.create_trade_history(
1,
@@ -337,14 +337,14 @@ class TestDividendPerformance(unittest.TestCase):
self.assertEqual(len(results), 5)
cumulative_returns = \
[event['cumulative_perf']['returns'] for event in results]
self.assertEqual(cumulative_returns, [0.0, 0.0, 0.0, 0.0, 0.0])
self.assertEqual(cumulative_returns, [0.0, 0.0, -0.1, -0.1, -0.1])
daily_returns = [event['daily_perf']['returns'] for event in results]
self.assertEqual(daily_returns, [0.0, 0.0, 0.0, 0.0, 0.0])
self.assertEqual(daily_returns, [0.0, 0.0, -0.1, 0.0, 0.0])
cash_flows = [event['daily_perf']['capital_used'] for event in results]
self.assertEqual(cash_flows, [0, 1000, 0, 0, 0])
self.assertEqual(cash_flows, [0, 1000, -1000, 0, 0])
cumulative_cash_flows = \
[event['cumulative_perf']['capital_used'] for event in results]
self.assertEqual(cumulative_cash_flows, [0, 1000, 1000, 1000, 1000])
self.assertEqual(cumulative_cash_flows, [0, 1000, 0, 0, 0])
def test_no_position_receives_no_dividend(self):
#post some trades in the market
+11 -13
View File
@@ -265,8 +265,6 @@ class PerformanceTracker(object):
del event['TRANSACTION']
elif event.type == zp.DATASOURCE_TYPE.DIVIDEND:
# TODO: confirm with @ehebert that positions objects
# are shared. (and that it is ok).
self.cumulative_performance.add_dividend(event)
self.todays_performance.add_dividend(event)
@@ -371,8 +369,6 @@ class Position(object):
self.dividends = []
def update_dividends(self, dt):
# TODO: should I have asserts for the dt to be at
# midnight?
payment = 0.0
unpaid_dividends = []
for dividend in self.dividends:
@@ -518,16 +514,18 @@ class PerformancePeriod(object):
for sid, pos in self.positions.iteritems():
cash_payments += pos.update_dividends(todays_date)
if cash_payments > 0.0:
# credit our cash balance with the dividend payments
self.period_cash_flow += cash_payments
# debit our cumulative cash spent with the dividend
# payments
self.cumulative_capital_used -= cash_payments
# credit our cash balance with the dividend payments, or
# if we are short, debit our cash balance with the
# payments.
self.period_cash_flow += cash_payments
# debit our cumulative cash spent with the dividend
# payments, or credit our cumulative cash spent if we are
# short the stock.
self.cumulative_capital_used -= cash_payments
# recalculate performance, including the dividend
# paymtents
self.calculate_performance()
# recalculate performance, including the dividend
# paymtents
self.calculate_performance()
def calculate_performance(self):
self.ending_value = self.calculate_positions_value()