File size: 2,086 Bytes
a4d29b7 10e04ef 5cfce9b 10e04ef 5cfce9b a4d29b7 9ace307 10e04ef 5cfce9b b29ff00 10e04ef a4d29b7 10e04ef a4d29b7 b43adb0 10e04ef a4d29b7 b43adb0 a4d29b7 10e04ef a4d29b7 10e04ef a4d29b7 10e04ef a4d29b7 10e04ef f618895 10e04ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import pandas as pd
import talib
import numpy as np
def talib_df(df):
"""
Return a single DataFrame containing:
- Original Date + OHLCV columns
- All numeric TA-Lib indicators
- All CDL patterns (0/1)
"""
df = df.copy()
print(df)
# Ensure OHLCV columns exist
for col in ['Open','High','Low','Close','Volume']:
if col not in df.columns:
raise ValueError(f"Missing column: {col}")
df=df.reset_index()
# Base DF with Date + OHLCV
result_df = df[['Date','Close','High','Low','Open','Volume']].copy()
# --- Indicators ---
df_std = df.copy()
df_std.columns = [c.lower() for c in df_std.columns]
ohlcv = {k: df_std.get(k) for k in ['open','high','low','close','volume']}
indicator_list = [
f for f in dir(talib)
if not f.startswith("CDL") and not f.startswith("_")
and f not in ["wraps", "wrapped_func"]
]
indicator_dfs = []
for name in indicator_list:
func = getattr(talib, name)
try:
if ohlcv['close'] is None:
continue
res = func(ohlcv['close'].values.astype(float))
if isinstance(res, tuple):
for i, arr in enumerate(res):
indicator_dfs.append(pd.DataFrame(arr, index=df.index, columns=[f"{name}_{i}"]))
else:
indicator_dfs.append(pd.DataFrame(res, index=df.index, columns=[name]))
except:
continue
if indicator_dfs:
result_df = pd.concat([result_df] + indicator_dfs, axis=1)
# --- CDL Patterns ---
pattern_list = [f for f in dir(talib) if f.startswith("CDL")]
for p in pattern_list:
func = getattr(talib, p)
try:
res = func(
df['Open'].values.astype(float),
df['High'].values.astype(float),
df['Low'].values.astype(float),
df['Close'].values.astype(float)
)
result_df[p] = (res != 0).astype(int)
except:
continue
return result_df
|