From a82415c77b87c0ef39f6f781c5cd13748229faf3 Mon Sep 17 00:00:00 2001 From: puppy Date: Thu, 5 Nov 2015 06:18:00 -0600 Subject: [PATCH] BUG: Address issue #801 and add test. Pass panel directly to object instead of data. --- tests/test_algorithm.py | 31 +++++++++++++++++++++++++++++++ zipline/algorithm.py | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 2ca03482..5504eda5 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -1891,3 +1891,34 @@ class TestFutureFlip(TestCase): actual_position, expected_positions[i], "position for day={0} not equal, actual={1}, expected={2}". format(i, actual_position, expected_positions[i])) + + +class TestTradingAlgorithm(TestCase): + def setUp(self): + self.env = TradingEnvironment() + self.days = self.env.trading_days[:4] + self.panel = pd.Panel({1: pd.DataFrame({ + 'price': [1, 1, 2, 4], 'volume': [1e9, 1e9, 1e9, 0], + 'type': [DATASOURCE_TYPE.TRADE, + DATASOURCE_TYPE.TRADE, + DATASOURCE_TYPE.TRADE, + DATASOURCE_TYPE.CLOSE_POSITION]}, + index=self.days) + }) + + def test_analyze_called(self): + results_ref = None + + def initialize(self, context): + pass + + def handel_data(self, context, data): + pass + + def analyze(self, perf): + self.perf_ref = perf + + algo = TradingAlgorithm(initialize = initialize, handle_data=handel_data, + analyze = analyze) + results = algo.run(self.panel) + self.assertEqual(results, results_ref) \ No newline at end of file diff --git a/zipline/algorithm.py b/zipline/algorithm.py index c0416ad2..c2e3a628 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -267,7 +267,6 @@ class TradingAlgorithm(object): self.algoscript = kwargs.pop('script', None) self._initialize = None - self._analyze = kwargs.pop('analyze', None) self._before_trading_start = None self._analyze = None @@ -298,6 +297,7 @@ class TradingAlgorithm(object): self._handle_data = kwargs.pop('handle_data') self._before_trading_start = kwargs.pop('before_trading_start', None) + self._analyze = kwargs.pop('analyze', None) self.event_manager.add_event( zipline.utils.events.Event(