MAINT: Refactor application of capital changes

Previously, on the dt of a capital change, we use the un-updated
prices to find the ending performance of the previous subperiod and
then got the new prices to determine the portfolio value used to
calculate the delta, without actually updating the performance
before applying the capital change. This logic is confusing and
unintuitive. Instead, save the ending performance as we do previously,
but have temp values for the starting current subperiod value.
Update those temp values after processing the capital change
This commit is contained in:
Andrew Liang
2016-07-28 14:52:29 -04:00
parent a937d6e6b1
commit 98f3fc9326
5 changed files with 42 additions and 35 deletions
+4 -17
View File
@@ -821,25 +821,12 @@ class TradingAlgorithm(object):
self.data_portal
)
# Calculate performance before we sync prices price for the current dt
self.perf_tracker.cumulative_performance.calculate_performance()
self.perf_tracker.todays_performance.calculate_performance()
self.perf_tracker.prepare_capital_change(is_interday)
if capital_change['type'] == 'target':
# Get an updated portfolio value as of this dt, but do it in a way
# so that the performance is not recalculated. This is done so
# that `process_capital_change` can find the performance values
# for the end of the subperiod, which is the previous dt
self.perf_tracker.position_tracker.sync_last_sale_prices(
dt,
self._in_before_trading_start,
self.data_portal
)
portfolio_value = \
self.perf_tracker.position_tracker.stats().net_value + \
self.perf_tracker.cumulative_performance.ending_cash
capital_change_amount = capital_change['value'] - portfolio_value
capital_change_amount = capital_change['value'] - \
self.updated_portfolio().portfolio_value
self.portfolio_needs_update = True
log.info('Processing capital change to target %s at %s. Capital '
'change delta is %s' % (capital_change['value'], dt,