mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-18 12:20:12 +08:00
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:
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user