From 4b7afb43d2bfdd34d0dcc50cf2bad6b0ef20eac4 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Sat, 4 May 2013 22:18:50 -0400 Subject: [PATCH] MAINT: Change repr's so that they are both human and machine readable. For printability in the repr when debugging algo config and state, change the repr of TradingAlgorithm and the objects it contains so that the more closely adhere to the repr interface of being able to recreate an object instance. --- zipline/algorithm.py | 26 ++++++++++++++++++++++++++ zipline/finance/blotter.py | 15 +++++++++++++++ zipline/finance/commission.py | 5 +++++ zipline/finance/slippage.py | 9 +++++++++ zipline/finance/trading.py | 20 +++++++++++++++----- 5 files changed, 70 insertions(+), 5 deletions(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index bdd20ef4..46157010 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -125,6 +125,32 @@ class TradingAlgorithm(object): # call to user-defined constructor method self.initialize(*args, **kwargs) + def __repr__(self): + """ + N.B. this does not yet represent a string that can be used + to instantiate an exact copy of an algorithm. + + However, it is getting close, and provides some value as something + that can be inspected interactively. + """ + return """ +{class_name}( + captial_base={capital_base} + sim_params={sim_params}, + initialized={initialized}, + slippage={slippage}, + commission={commission}, + blotter={blotter}, + recorded_vars={recorded_vars}) +""".strip().format(class_name=self.__class__.__name__, + capital_base=self.capital_base, + sim_params=repr(self.sim_params), + initialized=self.initialized, + slippage=repr(self.slippage), + commission=repr(self.commission), + blotter=repr(self.blotter), + recorded_vars=repr(self.recorded_vars)) + def _create_data_generator(self, source_filter, sim_params): """ Create a merged data generator using the sources and diff --git a/zipline/finance/blotter.py b/zipline/finance/blotter.py index f3741e7b..6ba90bec 100644 --- a/zipline/finance/blotter.py +++ b/zipline/finance/blotter.py @@ -54,6 +54,21 @@ class Blotter(object): self.new_orders = [] self.current_dt = None + def __repr__(self): + return """ +{class_name}( + transact_partial={transact_partial}, + open_orders={open_orders}, + orders={orders}, + new_orders={new_orders}, + current_dt={current_dt}) +""".strip().format(class_name=self.__class__.__name__, + transact_partial=self.transact.args, + open_orders=self.open_orders, + orders=self.orders, + new_orders=self.new_orders, + current_dt=self.current_dt) + def set_date(self, dt): self.current_dt = dt diff --git a/zipline/finance/commission.py b/zipline/finance/commission.py index 58a3df3e..057e84f4 100644 --- a/zipline/finance/commission.py +++ b/zipline/finance/commission.py @@ -28,6 +28,11 @@ class PerShare(object): """ self.cost = float(cost) + def __repr__(self): + return "{class_name}(cost={cost})".format( + class_name=self.__class__.__name__, + cost=self.cost) + def calculate(self, transaction): """ returns a tuple of: diff --git a/zipline/finance/slippage.py b/zipline/finance/slippage.py index d0ce0bdf..879756ad 100644 --- a/zipline/finance/slippage.py +++ b/zipline/finance/slippage.py @@ -129,6 +129,15 @@ class VolumeShareSlippage(object): self.volume_limit = volume_limit self.price_impact = price_impact + def __repr__(self): + return """ +{class_name}( + volume_limit={volume_limit}, + price_impact={price_impact}) +""".strip().format(class_name=self.__class__.__name__, + volume_limit=self.volume_limit, + price_impact=self.price_impact) + def simulate(self, event, current_orders): dt = event.dt diff --git a/zipline/finance/trading.py b/zipline/finance/trading.py index 9292595f..b610ea65 100644 --- a/zipline/finance/trading.py +++ b/zipline/finance/trading.py @@ -299,11 +299,21 @@ class SimulationParameters(object): return len(self.trading_days) def __repr__(self): - return "%s(%r)" % ( - self.__class__.__name__, - {'first_open': self.first_open, - 'last_close': self.last_close - }) + return """ +{class_name}( + period_start={period_start}, + period_end={period_end}, + capital_base={capital_base}, + emission_rate={emission_rate}, + first_open={first_open}, + last_close={last_close})\ +""".format(class_name=self.__class__.__name__, + period_start=self.period_start, + period_end=self.period_end, + capital_base=self.capital_base, + emission_rate=self.emission_rate, + first_open=self.first_open, + last_close=self.last_close) class use_environment(object):