From e85306524b6d08c4b5a1055b76e42011c7dd5004 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Mon, 5 Aug 2013 18:17:20 -0400 Subject: [PATCH] BUG: Prevent crashes for TALib functions when stocks have nans. If a stock stops gettign updated values, e.g. if a stock rolls out of a universe strategy, currently the underlying batch transform for TALib may have nans (which is another issue that could be addressed), the nans cause crashes when passed to some TALib function, e.g. Bollinger Bands are incompatible with all nan values. So, drop sids that only have nan values for the current data panel. --- zipline/transforms/ta.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/zipline/transforms/ta.py b/zipline/transforms/ta.py index 70a01808..8a15ae13 100644 --- a/zipline/transforms/ta.py +++ b/zipline/transforms/ta.py @@ -16,6 +16,7 @@ import functools import math import numpy as np +import pandas as pd import talib import copy from zipline.transforms import BatchTransform @@ -38,7 +39,13 @@ def zipline_wrapper(talib_fn, key_map, data): for talib_key, zipline_key in key_map.iteritems(): # if zipline_key is found, add it to talib_data if zipline_key in data: - talib_data[talib_key] = data[zipline_key][sid].values + values = data[zipline_key][sid].values + # Do not include sids that have only nans, passing only nans + # is incompatible with many of the underlying TALib functions. + if pd.isnull(values).all(): + break + else: + talib_data[talib_key] = data[zipline_key][sid].values # if zipline_key is not found and not required, add zeros elif talib_key not in req_inputs: talib_data[talib_key] = np.zeros(data.shape[1]) @@ -51,15 +58,16 @@ def zipline_wrapper(talib_fn, key_map, data): talib_key, zipline_key)) # call talib - talib_result = talib_fn(talib_data) + if talib_data: + talib_result = talib_fn(talib_data) - # keep only the most recent result - if isinstance(talib_result, (list, tuple)): - sid_result = tuple([r[-1] for r in talib_result]) - else: - sid_result = talib_result[-1] + # keep only the most recent result + if isinstance(talib_result, (list, tuple)): + sid_result = tuple([r[-1] for r in talib_result]) + else: + sid_result = talib_result[-1] - all_results[sid] = sid_result + all_results[sid] = sid_result return all_results