mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-09 04:46:03 +08:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user