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.
This commit is contained in:
Eddie Hebert
2013-05-04 22:18:50 -04:00
parent 5ae1aab2af
commit 4b7afb43d2
5 changed files with 70 additions and 5 deletions
+26
View File
@@ -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
+15
View File
@@ -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
+5
View File
@@ -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:
+9
View File
@@ -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
+15 -5
View File
@@ -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):