fix: make volume column optional in feature engineering, skip MFI when absent

This commit is contained in:
Marko Djordjevic 2026-02-18 01:01:32 +01:00
parent d3dcfcea7d
commit 9a549b8c7a

View file

@ -47,11 +47,13 @@ def compute_talib_indicators(
"Then install the Python wrapper: pip install TA-Lib\n" "Then install the Python wrapper: pip install TA-Lib\n"
) )
# Validate required columns # Validate required columns (volume is optional)
required_cols = ['open', 'high', 'low', 'close', 'volume'] required_cols = ['open', 'high', 'low', 'close']
missing_cols = [col for col in required_cols if col not in df.columns] missing_cols = [col for col in required_cols if col not in df.columns]
if missing_cols: 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 # Make a copy to avoid modifying the original
result_df = df.copy() result_df = df.copy()
@ -61,7 +63,7 @@ def compute_talib_indicators(
high_prices = df['high'].values.astype(np.float64) high_prices = df['high'].values.astype(np.float64)
low_prices = df['low'].values.astype(np.float64) low_prices = df['low'].values.astype(np.float64)
close_prices = df['close'].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") logger.info(f"Computing {len(indicators)} TA-Lib indicators")
@ -69,6 +71,12 @@ def compute_talib_indicators(
indicator_name = indicator_config.name.upper() indicator_name = indicator_config.name.upper()
params = indicator_config.params 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 # Check if indicator function exists
if not hasattr(talib, indicator_name): if not hasattr(talib, indicator_name):
raise AttributeError( raise AttributeError(