fix: make volume column optional in feature engineering, skip MFI when absent
This commit is contained in:
parent
d3dcfcea7d
commit
9a549b8c7a
1 changed files with 12 additions and 4 deletions
|
|
@ -47,11 +47,13 @@ def compute_talib_indicators(
|
|||
"Then install the Python wrapper: pip install TA-Lib\n"
|
||||
)
|
||||
|
||||
# Validate required columns
|
||||
required_cols = ['open', 'high', 'low', 'close', 'volume']
|
||||
# Validate required columns (volume is optional)
|
||||
required_cols = ['open', 'high', 'low', 'close']
|
||||
missing_cols = [col for col in required_cols if col not in df.columns]
|
||||
if missing_cols:
|
||||
raise ValueError(f"Missing required OHLCV columns: {missing_cols}")
|
||||
raise ValueError(f"Missing required OHLC columns: {missing_cols}")
|
||||
|
||||
has_volume = 'volume' in df.columns
|
||||
|
||||
# Make a copy to avoid modifying the original
|
||||
result_df = df.copy()
|
||||
|
|
@ -61,7 +63,7 @@ def compute_talib_indicators(
|
|||
high_prices = df['high'].values.astype(np.float64)
|
||||
low_prices = df['low'].values.astype(np.float64)
|
||||
close_prices = df['close'].values.astype(np.float64)
|
||||
volume = df['volume'].values.astype(np.float64)
|
||||
volume = df['volume'].values.astype(np.float64) if has_volume else None
|
||||
|
||||
logger.info(f"Computing {len(indicators)} TA-Lib indicators")
|
||||
|
||||
|
|
@ -69,6 +71,12 @@ def compute_talib_indicators(
|
|||
indicator_name = indicator_config.name.upper()
|
||||
params = indicator_config.params
|
||||
|
||||
# Skip volume-dependent indicators when volume data is absent
|
||||
volume_indicators = {'OBV', 'AD', 'ADOSC', 'MFI'}
|
||||
if indicator_name in volume_indicators and volume is None:
|
||||
logger.warning(f"Skipping {indicator_name}: requires volume data")
|
||||
continue
|
||||
|
||||
# Check if indicator function exists
|
||||
if not hasattr(talib, indicator_name):
|
||||
raise AttributeError(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue