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