From e50858315bb07f4eb4513f28d5b6a5df4c505a5d Mon Sep 17 00:00:00 2001 From: puppy Date: Sun, 1 Nov 2015 09:10:17 -0600 Subject: [PATCH 1/5] BUG: Issue #801 Initializing TradingAlgorithm Object Does Not Set _analyze Method in algorithm.py Added one line to check for the keyword argument 'analyze' and set the the _analyze method when a TradingAlgorithm object is initialized within a script. --- zipline/algorithm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 38daf6e7..c0416ad2 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -267,6 +267,7 @@ 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 From a82415c77b87c0ef39f6f781c5cd13748229faf3 Mon Sep 17 00:00:00 2001 From: puppy Date: Thu, 5 Nov 2015 06:18:00 -0600 Subject: [PATCH 2/5] 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( From 6b245ab06af71d27193ea9319a2a1c6b5ead1601 Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Thu, 5 Nov 2015 10:12:02 -0500 Subject: [PATCH 3/5] TST: Removed extra parameters --- tests/test_algorithm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 5504eda5..7e081a72 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -1909,10 +1909,10 @@ class TestTradingAlgorithm(TestCase): def test_analyze_called(self): results_ref = None - def initialize(self, context): + def initialize(context): pass - def handel_data(self, context, data): + def handel_data(context, data): pass def analyze(self, perf): From d2ba11dac04b90b5cd68cba1ada7960300f44198 Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Thu, 5 Nov 2015 10:12:41 -0500 Subject: [PATCH 4/5] STY: Fixed typo and appeased flake8 --- tests/test_algorithm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 7e081a72..5ebdf920 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -1912,13 +1912,13 @@ class TestTradingAlgorithm(TestCase): def initialize(context): pass - def handel_data(context, data): + def handle_data(context, data): pass def analyze(self, perf): self.perf_ref = perf - algo = TradingAlgorithm(initialize = initialize, handle_data=handel_data, - analyze = analyze) + algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, + analyze=analyze) results = algo.run(self.panel) - self.assertEqual(results, results_ref) \ No newline at end of file + self.assertEqual(results, results_ref) From ec5318ec882e84fa240ef73217e7180adfae9d11 Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Thu, 5 Nov 2015 10:15:01 -0500 Subject: [PATCH 5/5] TST: Fixed up test result comparison --- tests/test_algorithm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 5ebdf920..18e1c444 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -1907,7 +1907,7 @@ class TestTradingAlgorithm(TestCase): }) def test_analyze_called(self): - results_ref = None + self.perf_ref = None def initialize(context): pass @@ -1915,10 +1915,10 @@ class TestTradingAlgorithm(TestCase): def handle_data(context, data): pass - def analyze(self, perf): + def analyze(context, perf): self.perf_ref = perf algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, analyze=analyze) results = algo.run(self.panel) - self.assertEqual(results, results_ref) + self.assertIs(results, self.perf_ref)