Subclassing of TradingAlgorithm not functioning with history functions
When I try to define my algo by subclassing TradingAlgorithm and defining initialize and handle_data in the class I get the following error when utilising add_history in initialize and history in handle_data:
File "../zipline/utils/api_support.py", line 51, in wrapped
return getattr(get_algo_instance(), f.__name__)(*args, **kwargs)
AttributeError: 'NoneType' object has no attribute 'add_history'
but the algo works fine when when I pass initialize and handle_data to TradingAlgorithm.
Is this a bug or is the second method the prefered way to define an algo?
@warren-oneill probably both. The "subclass and override" method is more or less unofficially deprecated. But until it's actually officially deprecated, this is a bug. Unfortunately, I'm not sure how to fix this bug. The issue is that in TradingAlgorithm.initialize, we do this:
def initialize(self, *args, **kwargs):
"""
Call self._initialize with `self` made available to Zipline API
functions.
"""
with ZiplineAPI(self):
self._initialize(self)
The ZiplineAPI contextmanager is what makes all the API methods work when you pass in functions or a script. It works by converting calls to zipline.api.method_name(...) into method calls on the current TradingAlgorithm instance. But since you're overriding initialize, nothing is setting that context manager, so add_history can't find the current TradingAlgorithm. This is part of why the subclass API is deprecated.
The short term workaround for this is to either use the alternative construction mechanism, or to call self.add_history instead of zipline.api.add_history. I'd say the former option is preferable.
@ssanderson thanks for the quick answer Scott. I'll make sure to start using the 2nd method from now on. I've been mainly using the subclass method to write tests (as is also done in zipline I believe) as trying to write tests using the other method is a bit of a pain.
Can you point me to a unittests which uses second method to define the algo?
@warren-oneill the easiest way to find them is to do a search for TradingAlgorithm(. For me this brings up a fair number of results in test_algorithm and test_history.
git --no-pager grep -n -e TradingAlgorithm\( --
...
tests/modelling/test_modelling_algo.py:200: algo = TradingAlgorithm(
tests/test_algorithm.py:155: algo = TradingAlgorithm(
tests/test_algorithm.py:190: algo = TradingAlgorithm(initialize=initialize,
tests/test_algorithm.py:240: algo = TradingAlgorithm(initialize=initialize,
tests/test_algorithm.py:274: algo = TradingAlgorithm(
tests/test_algorithm.py:292: algo = TradingAlgorithm(
tests/test_algorithm.py:328: algo = TradingAlgorithm(asset_metadata=metadata)
tests/test_algorithm.py:395: algo = TradingAlgorithm(asset_metadata=metadata)
tests/test_algorithm.py:658: algo = TradingAlgorithm(initialize=initialize_noop,
tests/test_algorithm.py:663: algo = TradingAlgorithm(script=noop_algo)
tests/test_algorithm.py:667: algo = TradingAlgorithm(initialize=initialize_api,
tests/test_algorithm.py:672: algo = TradingAlgorithm(script=api_algo)
tests/test_algorithm.py:679: algo = TradingAlgorithm(script=api_get_environment_algo,
tests/test_algorithm.py:688: algo = TradingAlgorithm(script=api_symbol_algo,
tests/test_algorithm.py:695: test_algo = TradingAlgorithm(
tests/test_algorithm.py:749: test_algo = TradingAlgorithm(
tests/test_algorithm.py:807: test_algo = TradingAlgorithm(
tests/test_algorithm.py:833: test_algo = TradingAlgorithm(
tests/test_algorithm.py:842: test_algo = TradingAlgorithm(
tests/test_algorithm.py:868: test_algo = TradingAlgorithm(
tests/test_algorithm.py:888: test_algo = TradingAlgorithm(
tests/test_algorithm.py:899: test_algo = TradingAlgorithm(
tests/test_algorithm.py:917: test_algo = TradingAlgorithm(
tests/test_algorithm.py:956: algo = TradingAlgorithm(
tests/test_algorithm.py:967: algo = TradingAlgorithm(
tests/test_algorithm.py:981: algo = TradingAlgorithm(
tests/test_algorithm.py:1034: algo = TradingAlgorithm(
tests/test_algorithm.py:1290: algo = TradingAlgorithm(initialize=initialize,
tests/test_history.py:442: test_algo = TradingAlgorithm(
tests/test_history.py:475: algo = TradingAlgorithm(
tests/test_history.py:509: test_algo = TradingAlgorithm(
tests/test_history.py:560: test_algo = TradingAlgorithm(
tests/test_history.py:616: test_algo = TradingAlgorithm(
tests/test_history.py:671: test_algo = TradingAlgorithm(
tests/test_history.py:716: test_algo = TradingAlgorithm(
tests/test_history.py:761: test_algo = TradingAlgorithm(
tests/test_history.py:806: test_algo = TradingAlgorithm(
tests/test_history.py:856: test_algo = TradingAlgorithm(
tests/test_history.py:915: test_algo = TradingAlgorithm(
tests/test_history.py:957: test_algo = TradingAlgorithm(
tests/test_history.py:1027: test_algo = TradingAlgorithm(
tests/test_history.py:1076: test_algo = TradingAlgorithm(
Note that some of those hits pass a string to exec rather than functions.
Seems like self.add_history doesn't work but it works if I pass the 2 functions as in the examples.
Thanks for the help!