From ccddc86d6b92c27aa0e8230ed95d5581e0f1c7db Mon Sep 17 00:00:00 2001 From: Jean Bredeche Date: Tue, 24 Jan 2017 10:59:31 -0500 Subject: [PATCH] ENH: Solidify equality comparisons for SlippageModel classes --- tests/finance/test_slippage.py | 7 +++++++ zipline/finance/slippage.py | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/finance/test_slippage.py b/tests/finance/test_slippage.py index d1992f09..2ec37f8e 100644 --- a/tests/finance/test_slippage.py +++ b/tests/finance/test_slippage.py @@ -88,6 +88,13 @@ class SlippageTestCase(WithCreateBarData, super(SlippageTestCase, cls).init_class_fixtures() cls.ASSET133 = cls.env.asset_finder.retrieve_asset(133) + def test_equality_and_comparison(self): + vol1 = VolumeShareSlippage(volume_limit=0.2) + vol2 = VolumeShareSlippage(volume_limit=0.2) + + self.assertEqual(vol1, vol2) + self.assertEqual(hash(vol1), hash(vol2)) + def test_fill_price_worse_than_limit_price(self): non_limit_order = TestOrder(limit=None, direction=1) limit_buy = TestOrder(limit=1.5, direction=1) diff --git a/zipline/finance/slippage.py b/zipline/finance/slippage.py index 20beccfd..d9e4d7a2 100644 --- a/zipline/finance/slippage.py +++ b/zipline/finance/slippage.py @@ -16,7 +16,7 @@ from __future__ import division import abc import math -from six import with_metaclass +from six import with_metaclass, iteritems from pandas import isnull @@ -152,6 +152,18 @@ class SlippageModel(with_metaclass(abc.ABCMeta)): def __call__(self, bar_data, asset, current_orders): return self.simulate(bar_data, asset, current_orders) + def __eq__(self, other): + return self._attrs_to_check() == other._attrs_to_check() + + def __hash__(self): + return hash(( + type(self), + tuple(sorted(iteritems(self._attrs_to_check()))) + )) + + def _attrs_to_check(self): + return self.__dict__ + class VolumeShareSlippage(SlippageModel): """Model slippage as a function of the volume of shares traded.