mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-08 10:05:23 +08:00
MAINT: Refactored serialization parent class out.
Previously the class SerializeableZiplineObject was used to house basic __setstate__ and __getstate__ methods. It wasn't really doing much that was helpful, so it is now gone.
This commit is contained in:
@@ -36,7 +36,6 @@ log = Logger('Blotter')
|
||||
|
||||
from zipline.utils.protocol_utils import Enum
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -49,7 +48,7 @@ ORDER_STATUS = Enum(
|
||||
)
|
||||
|
||||
|
||||
class Blotter(SerializeableZiplineObject):
|
||||
class Blotter(object):
|
||||
|
||||
def __init__(self):
|
||||
self.transact = transact_partial(VolumeShareSlippage(), PerShare())
|
||||
@@ -280,10 +279,10 @@ class Blotter(SerializeableZiplineObject):
|
||||
open_orders.update(state.pop('open_orders'))
|
||||
self.open_orders = open_orders
|
||||
|
||||
super(Blotter, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class Order(SerializeableZiplineObject):
|
||||
class Order(object):
|
||||
def __init__(self, dt, sid, amount, stop=None, limit=None, filled=0,
|
||||
commission=None, id=None):
|
||||
"""
|
||||
@@ -423,7 +422,9 @@ class Order(SerializeableZiplineObject):
|
||||
|
||||
def __getstate__(self):
|
||||
|
||||
state_dict = super(Order, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
state_dict['_status'] = self._status
|
||||
|
||||
@@ -440,4 +441,4 @@ class Order(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Order saved state is too old.")
|
||||
|
||||
super(Order, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
@@ -14,12 +14,11 @@
|
||||
# limitations under the License.
|
||||
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
|
||||
class PerShare(SerializeableZiplineObject):
|
||||
class PerShare(object):
|
||||
"""
|
||||
Calculates a commission for a transaction based on a per
|
||||
share cost with an optional minimum cost per trade.
|
||||
@@ -57,7 +56,9 @@ class PerShare(SerializeableZiplineObject):
|
||||
|
||||
def __getstate__(self):
|
||||
|
||||
state_dict = super(PerShare, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
STATE_VERSION = 1
|
||||
state_dict[VERSION_LABEL] = STATE_VERSION
|
||||
@@ -72,10 +73,10 @@ class PerShare(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("PerShare saved state is too old.")
|
||||
|
||||
super(PerShare, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class PerTrade(SerializeableZiplineObject):
|
||||
class PerTrade(object):
|
||||
"""
|
||||
Calculates a commission for a transaction based on a per
|
||||
trade cost.
|
||||
@@ -103,7 +104,9 @@ class PerTrade(SerializeableZiplineObject):
|
||||
|
||||
def __getstate__(self):
|
||||
|
||||
state_dict = super(PerTrade, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
STATE_VERSION = 1
|
||||
state_dict[VERSION_LABEL] = STATE_VERSION
|
||||
@@ -118,10 +121,10 @@ class PerTrade(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("PerTrade saved state is too old.")
|
||||
|
||||
super(PerTrade, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class PerDollar(SerializeableZiplineObject):
|
||||
class PerDollar(object):
|
||||
"""
|
||||
Calculates a commission for a transaction based on a per
|
||||
dollar cost.
|
||||
@@ -149,7 +152,9 @@ class PerDollar(SerializeableZiplineObject):
|
||||
|
||||
def __getstate__(self):
|
||||
|
||||
state_dict = super(PerDollar, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
STATE_VERSION = 1
|
||||
state_dict[VERSION_LABEL] = STATE_VERSION
|
||||
@@ -164,4 +169,4 @@ class PerDollar(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("PerDollar saved state is too old.")
|
||||
|
||||
super(PerDollar, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
@@ -93,7 +93,6 @@ import zipline.protocol as zp
|
||||
from . position import positiondict
|
||||
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -101,7 +100,7 @@ log = logbook.Logger('Performance')
|
||||
TRADE_TYPE = zp.DATASOURCE_TYPE.TRADE
|
||||
|
||||
|
||||
class PerformancePeriod(SerializeableZiplineObject):
|
||||
class PerformancePeriod(object):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -581,7 +580,9 @@ class PerformancePeriod(SerializeableZiplineObject):
|
||||
return positions
|
||||
|
||||
def __getstate__(self):
|
||||
state_dict = super(PerformancePeriod, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
state_dict['_portfolio_store'] = self._portfolio_store
|
||||
state_dict['_account_store'] = self._account_store
|
||||
@@ -628,6 +629,6 @@ class PerformancePeriod(SerializeableZiplineObject):
|
||||
self.positions = positions
|
||||
self._positions_store = _positions_store
|
||||
|
||||
super(PerformancePeriod, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
self.initialize_position_calc_arrays()
|
||||
|
||||
@@ -44,14 +44,13 @@ import logbook
|
||||
import zipline.protocol as zp
|
||||
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
log = logbook.Logger('Performance')
|
||||
|
||||
|
||||
class Position(SerializeableZiplineObject):
|
||||
class Position(object):
|
||||
|
||||
def __init__(self, sid, amount=0, cost_basis=0.0,
|
||||
last_sale_price=0.0, last_sale_date=None):
|
||||
@@ -231,7 +230,7 @@ last_sale_price: {last_sale_price}"
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Position saved state is too old.")
|
||||
|
||||
super(Position, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class positiondict(dict):
|
||||
|
||||
@@ -72,14 +72,13 @@ from . period import PerformancePeriod
|
||||
|
||||
from zipline.finance.trading import with_environment
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
log = logbook.Logger('Performance')
|
||||
|
||||
|
||||
class PerformanceTracker(SerializeableZiplineObject):
|
||||
class PerformanceTracker(object):
|
||||
"""
|
||||
Tracks the performance of the algorithm.
|
||||
"""
|
||||
@@ -490,7 +489,9 @@ class PerformanceTracker(SerializeableZiplineObject):
|
||||
return risk_dict
|
||||
|
||||
def __getstate__(self):
|
||||
state_dict = super(PerformanceTracker, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
state_dict['dividend_frame'] = pickle.dumps(self.dividend_frame)
|
||||
|
||||
@@ -509,7 +510,7 @@ class PerformanceTracker(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("PerformanceTracker saved state is too old.")
|
||||
|
||||
super(PerformanceTracker, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
# Handle the dividend frame specially
|
||||
self.dividend_frame = pickle.loads(state['dividend_frame'])
|
||||
|
||||
@@ -36,7 +36,6 @@ from . risk import (
|
||||
)
|
||||
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -76,7 +75,7 @@ def information_ratio(algo_volatility, algorithm_return, benchmark_return):
|
||||
/ algo_volatility)
|
||||
|
||||
|
||||
class RiskMetricsCumulative(SerializeableZiplineObject):
|
||||
class RiskMetricsCumulative(object):
|
||||
"""
|
||||
:Usage:
|
||||
Instantiate RiskMetricsCumulative once.
|
||||
@@ -479,7 +478,7 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}"
|
||||
raise BaseException("RiskMetricsCumulative \
|
||||
saved state is too old.")
|
||||
|
||||
super(RiskMetricsCumulative, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
# This are big and we don't need to serialize them
|
||||
# pop them back in now
|
||||
|
||||
@@ -37,7 +37,6 @@ from . risk import (
|
||||
)
|
||||
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -47,7 +46,7 @@ choose_treasury = functools.partial(risk.choose_treasury,
|
||||
risk.select_treasury_duration)
|
||||
|
||||
|
||||
class RiskMetricsPeriod(SerializeableZiplineObject):
|
||||
class RiskMetricsPeriod(object):
|
||||
def __init__(self, start_date, end_date, returns,
|
||||
benchmark_returns=None):
|
||||
|
||||
@@ -329,6 +328,6 @@ class RiskMetricsPeriod(SerializeableZiplineObject):
|
||||
raise BaseException("RiskMetricsPeriod saved state \
|
||||
is too old.")
|
||||
|
||||
super(RiskMetricsPeriod, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
self.treasury_curves = trading.environment.treasury_curves
|
||||
|
||||
@@ -62,14 +62,13 @@ from dateutil.relativedelta import relativedelta
|
||||
from . period import RiskMetricsPeriod
|
||||
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
log = logbook.Logger('Risk Report')
|
||||
|
||||
|
||||
class RiskReport(SerializeableZiplineObject):
|
||||
class RiskReport(object):
|
||||
def __init__(self, algorithm_returns, sim_params, benchmark_returns=None):
|
||||
"""
|
||||
algorithm_returns needs to be a list of daily_return objects
|
||||
@@ -145,7 +144,9 @@ class RiskReport(SerializeableZiplineObject):
|
||||
return ends
|
||||
|
||||
def __getstate__(self):
|
||||
state_dict = super(RiskReport, self).__getstate__()
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
|
||||
if '_dividend_count' in dir(self):
|
||||
state_dict['_dividend_count'] = self._dividend_count
|
||||
@@ -163,4 +164,4 @@ class RiskReport(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("RiskReport saved state is too old.")
|
||||
|
||||
super(RiskReport, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
@@ -25,7 +25,6 @@ from six import with_metaclass
|
||||
|
||||
from zipline.protocol import DATASOURCE_TYPE
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -113,7 +112,7 @@ def transact_partial(slippage, commission):
|
||||
return partial(transact_stub, slippage, commission)
|
||||
|
||||
|
||||
class Transaction(SerializeableZiplineObject):
|
||||
class Transaction(object):
|
||||
|
||||
def __init__(self, sid, amount, dt, price, order_id, commission=None):
|
||||
self.sid = sid
|
||||
@@ -149,7 +148,7 @@ class Transaction(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Transaction saved state is too old.")
|
||||
|
||||
super(Transaction, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
def create_transaction(event, order, price, amount):
|
||||
@@ -213,7 +212,7 @@ class SlippageModel(with_metaclass(abc.ABCMeta)):
|
||||
return self.simulate(event, current_orders, **kwargs)
|
||||
|
||||
|
||||
class VolumeShareSlippage(SlippageModel, SerializeableZiplineObject):
|
||||
class VolumeShareSlippage(SlippageModel):
|
||||
|
||||
def __init__(self,
|
||||
volume_limit=.25,
|
||||
@@ -289,7 +288,7 @@ class VolumeShareSlippage(SlippageModel, SerializeableZiplineObject):
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class FixedSlippage(SlippageModel, SerializeableZiplineObject):
|
||||
class FixedSlippage(SlippageModel):
|
||||
|
||||
def __init__(self, spread=0.0):
|
||||
"""
|
||||
|
||||
+6
-7
@@ -25,7 +25,6 @@ from . utils.math_utils import nanstd, nanmean, nansum
|
||||
from zipline.finance.trading import with_environment
|
||||
from zipline.utils.algo_instance import get_algo_instance
|
||||
from zipline.utils.serialization_utils import (
|
||||
SerializeableZiplineObject,
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -125,7 +124,7 @@ class Order(Event):
|
||||
pass
|
||||
|
||||
|
||||
class Portfolio(SerializeableZiplineObject):
|
||||
class Portfolio(object):
|
||||
|
||||
def __init__(self):
|
||||
self.capital_used = 0.0
|
||||
@@ -161,10 +160,10 @@ class Portfolio(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Portfolio saved state is too old.")
|
||||
|
||||
super(Portfolio, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class Account(SerializeableZiplineObject):
|
||||
class Account(object):
|
||||
'''
|
||||
The account object tracks information about the trading account. The
|
||||
values are updated as the algorithm runs and its keys remain unchanged.
|
||||
@@ -213,10 +212,10 @@ class Account(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Account saved state is too old.")
|
||||
|
||||
super(Account, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class Position(SerializeableZiplineObject):
|
||||
class Position(object):
|
||||
|
||||
def __init__(self, sid):
|
||||
self.sid = sid
|
||||
@@ -247,7 +246,7 @@ class Position(SerializeableZiplineObject):
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Protocol Position saved state is too old.")
|
||||
|
||||
super(Position, self).__setstate__(state)
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
class Positions(dict):
|
||||
|
||||
@@ -16,27 +16,3 @@
|
||||
# Label for the serialization version field in the state returned by
|
||||
# __getstate__.
|
||||
VERSION_LABEL = '_stateversion_'
|
||||
|
||||
|
||||
class SerializeableZiplineObject(object):
|
||||
"""
|
||||
This class implements the basic set and get state methods used for
|
||||
serialization. It also serves as a demarkation of which objects we
|
||||
serialize.
|
||||
"""
|
||||
|
||||
def __getstate__(self):
|
||||
"""
|
||||
Many get_state methods need this one line of code.
|
||||
This method deduplicates the code calls.
|
||||
"""
|
||||
state_dict = \
|
||||
{k: v for k, v in self.__dict__.iteritems()
|
||||
if not k.startswith('_')}
|
||||
return state_dict
|
||||
|
||||
def __setstate__(self, state):
|
||||
"""
|
||||
Many objects require only this code.
|
||||
"""
|
||||
self.__dict__.update(state)
|
||||
|
||||
Reference in New Issue
Block a user