From 97c88c3c30109de6197c4f586e5e36e23e4d47b0 Mon Sep 17 00:00:00 2001 From: Delaney Granizo-Mackenzie Date: Wed, 16 Jul 2014 15:41:18 -0400 Subject: [PATCH] BUG: Put initialization of perf_tracker back in __init__ The initialization of perf_tracker had been moved from __init__ in TradingAlgorithm to _create_generator. This caused perf_tracker to not be ready when portfolio requested it. portfolio was consequently not ready for access in init. portfolio can now be accessed in init again, assuming valid sim_params are passed. Otherwise it will be available in handle_data() after _create_generator() is called. --- tests/test_algorithm.py | 19 +++++++++++++++++++ zipline/algorithm.py | 8 +++----- zipline/test_algorithms.py | 9 +++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 3c7ee734..cf4145bb 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -34,6 +34,7 @@ from zipline.errors import ( TradingControlViolation, ) from zipline.test_algorithms import ( + access_portfolio_in_init, AmbitiousStopLimitAlgorithm, EmptyPositionsAlgorithm, InvalidOrderAlgorithm, @@ -617,6 +618,24 @@ def handle_data(context, data): ) set_algo_instance(test_algo) + def test_portfolio_in_init(self): + """ + Test that accessing portfolio in init doesn't break. + """ + test_algo = TradingAlgorithm( + script=access_portfolio_in_init, + sim_params=self.sim_params, + ) + set_algo_instance(test_algo) + + self.zipline_test_config['algorithm'] = test_algo + self.zipline_test_config['trade_count'] = 1 + + zipline = simfactory.create_test_zipline( + **self.zipline_test_config) + + output, _ = drain_zipline(self, zipline) + class TestHistory(TestCase): def test_history(self): diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 26bf71d9..0ed053e9 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -157,9 +157,7 @@ class TradingAlgorithm(object): self.sim_params = create_simulation_parameters( capital_base=self.capital_base ) - - # perf_tacker gets instantiated in ._create_generator() - self.perf_tracker = None + self.perf_tracker = PerformanceTracker(self.sim_params) self.blotter = kwargs.pop('blotter', None) if not self.blotter: @@ -318,8 +316,8 @@ class TradingAlgorithm(object): skipped. """ # Instantiate perf_tracker - if self.perf_tracker is None: - self.perf_tracker = PerformanceTracker(sim_params) + self.perf_tracker = PerformanceTracker(sim_params) + self.portfolio_needs_update = True self.data_gen = self._create_data_generator(source_filter, sim_params) diff --git a/zipline/test_algorithms.py b/zipline/test_algorithms.py index 0cdf380a..d3928d85 100644 --- a/zipline/test_algorithms.py +++ b/zipline/test_algorithms.py @@ -961,6 +961,15 @@ def handle_data(context, data): pass """ +access_portfolio_in_init = """ +def initialize(context): + var = context.portfolio.cash + pass + +def handle_data(context, data): + pass +""" + call_all_order_methods = """ from zipline.api import (order, order_value,