From e56457c671dd7c28029bfd273deec3836c26493e Mon Sep 17 00:00:00 2001 From: Joe Jevnik Date: Mon, 10 Nov 2014 10:14:56 -0500 Subject: [PATCH] ENH: Makes breakpoints work in algoscripts started with the run_algo.py script. Adds an option kwarg to TradingAlgorithm named 'algo_filename' that represents the file where the algoscript came from (if any). The run_algo.py script will pass this argument with the value passed to the '-f' flag. The default name is '' to represent that the script is coming from a string in python and not a file. This matches the behavior of exec and the python convention for compiling code objects. --- zipline/algorithm.py | 4 +++- zipline/utils/cli.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index 30149ccd..fc7396fb 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -197,7 +197,9 @@ class TradingAlgorithm(object): self.event_manager = EventManager() if self.algoscript is not None: - exec_(self.algoscript, self.namespace) + filename = kwargs.pop('algo_filename', '') + code = compile(self.algoscript, filename, 'exec') + exec_(code, self.namespace) self._initialize = self.namespace.get('initialize') if 'handle_data' not in self.namespace: raise ValueError('You must define a handle_data function.') diff --git a/zipline/utils/cli.py b/zipline/utils/cli.py index 7af986e8..017b011f 100644 --- a/zipline/utils/cli.py +++ b/zipline/utils/cli.py @@ -187,7 +187,8 @@ def run_pipeline(print_algo=True, **kwargs): algo = zipline.TradingAlgorithm(script=algo_text, namespace=kwargs.get('namespace', {}), - capital_base=float(kwargs['capital_base'])) + capital_base=float(kwargs['capital_base']), + algo_filename=kwargs.get('algofile')) perf = algo.run(source)