diff --git a/zipline/algorithm.py b/zipline/algorithm.py index bf8c5935..663103a7 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -408,7 +408,7 @@ class TradingAlgorithm(object): self.benchmark_sid = kwargs.pop('benchmark_sid', None) - # A dictionary of capital change values keyed by timestamp + # A dictionary of capital changes keyed by timestamp self.capital_changes = kwargs.pop('capital_changes', {}) def init_engine(self, get_loader): diff --git a/zipline/finance/performance/tracker.py b/zipline/finance/performance/tracker.py index f4a3cb82..7e39a1da 100644 --- a/zipline/finance/performance/tracker.py +++ b/zipline/finance/performance/tracker.py @@ -238,16 +238,31 @@ class PerformanceTracker(object): return _dict - def process_capital_changes(self, capital_change, is_interday): - self.cumulative_performance.subdivide_period(capital_change) + def process_capital_changes(self, capital_change, dt, is_interday): + if capital_change['type'] == 'target': + capital_change_amount = capital_change['value'] - \ + self.cumulative_performance.as_portfolio().portfolio_value + log.info('Processing capital change to target %s at %s. Capital ' + 'change delta is %s' % (capital_change['value'], dt, + capital_change_amount)) + elif capital_change['type'] == 'delta': + capital_change_amount = capital_change['delta'] + log.info('Processing capital change of delta %s at %s' + % (capital_change_amount, dt)) + else: + log.error("Capital change %s does not indicate a valid type " + "('target' or 'delta')" % capital_change) + return + + self.cumulative_performance.subdivide_period(capital_change_amount) if is_interday: # Change comes between days self.todays_performance.adjust_period_starting_capital( - capital_change) + capital_change_amount) else: # Change comes in the middle of day - self.todays_performance.subdivide_period(capital_change) + self.todays_performance.subdivide_period(capital_change_amount) def process_transaction(self, transaction): self.txn_count += 1 diff --git a/zipline/gens/tradesimulation.py b/zipline/gens/tradesimulation.py index 101fdf34..8c0736b0 100644 --- a/zipline/gens/tradesimulation.py +++ b/zipline/gens/tradesimulation.py @@ -152,9 +152,8 @@ class AlgorithmSimulator(object): if midnight_dt in algo.capital_changes: # process any capital changes that came overnight change = algo.capital_changes[midnight_dt] - log.info('Processing capital change of %s at %s' % - (change, midnight_dt)) - perf_tracker.process_capital_changes(change, is_interday=True) + perf_tracker.process_capital_changes(change, dt, + is_interday=True) # Get the positions before updating the date so that prices are # fetched for trading close instead of midnight @@ -221,10 +220,9 @@ class AlgorithmSimulator(object): # process any capital changes that came between the last # and current minutes change = algo.capital_changes[dt] - log.info('Processing capital change of %s at %s' % - (change, dt)) algo.perf_tracker.process_capital_changes( change, + dt, is_interday=False ) else: