MAINT: For capital changes, support input of delta or target value

For target changes, calculate the delta using the portfolio value
of the current minute
This commit is contained in:
Andrew Liang
2016-07-20 15:44:41 -04:00
parent 52cb0fc70a
commit f146d6d8c1
3 changed files with 23 additions and 10 deletions
+1 -1
View File
@@ -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):
+19 -4
View File
@@ -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
+3 -5
View File
@@ -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: