Spaces:
Sleeping
Sleeping
Update ta_indi_pat.py
Browse files- ta_indi_pat.py +10 -15
ta_indi_pat.py
CHANGED
|
@@ -4,21 +4,18 @@ import numpy as np
|
|
| 4 |
|
| 5 |
def patterns(df):
|
| 6 |
"""
|
| 7 |
-
Return a DataFrame of all CDL patterns with 0/1,
|
|
|
|
| 8 |
"""
|
| 9 |
df = df.copy()
|
| 10 |
required_cols = ['Open','High','Low','Close']
|
| 11 |
|
| 12 |
-
# Ensure all required columns exist
|
| 13 |
for col in required_cols:
|
| 14 |
if col not in df.columns:
|
| 15 |
raise ValueError(f"Missing column: {col}")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
df.set_index('Date', inplace=True)
|
| 20 |
-
|
| 21 |
-
pattern_df = pd.DataFrame(index=df.index)
|
| 22 |
|
| 23 |
pattern_list = [f for f in dir(talib) if f.startswith("CDL")]
|
| 24 |
|
|
@@ -37,15 +34,12 @@ def patterns(df):
|
|
| 37 |
|
| 38 |
def indicators(df):
|
| 39 |
"""
|
| 40 |
-
Return a DataFrame of
|
|
|
|
| 41 |
"""
|
| 42 |
df_std = df.copy()
|
| 43 |
df_std.columns = [c.lower() for c in df_std.columns]
|
| 44 |
|
| 45 |
-
# Use Date as index if present
|
| 46 |
-
if 'date' in df_std.columns:
|
| 47 |
-
df_std.set_index('date', inplace=True)
|
| 48 |
-
|
| 49 |
ohlcv = {
|
| 50 |
'open': df_std.get('open'),
|
| 51 |
'high': df_std.get('high'),
|
|
@@ -60,6 +54,7 @@ def indicators(df):
|
|
| 60 |
]
|
| 61 |
|
| 62 |
df_list = []
|
|
|
|
| 63 |
|
| 64 |
for name in indicator_list:
|
| 65 |
func = getattr(talib, name)
|
|
@@ -72,10 +67,10 @@ def indicators(df):
|
|
| 72 |
if isinstance(result, tuple):
|
| 73 |
for i, arr in enumerate(result):
|
| 74 |
col_name = f"{name}_{i}"
|
| 75 |
-
temp_df = pd.DataFrame(arr, index=
|
| 76 |
df_list.append(temp_df)
|
| 77 |
else:
|
| 78 |
-
temp_df = pd.DataFrame(result, index=
|
| 79 |
df_list.append(temp_df)
|
| 80 |
except:
|
| 81 |
continue
|
|
@@ -83,6 +78,6 @@ def indicators(df):
|
|
| 83 |
if df_list:
|
| 84 |
indicator_df = pd.concat(df_list, axis=1)
|
| 85 |
else:
|
| 86 |
-
indicator_df = pd.DataFrame(index=
|
| 87 |
|
| 88 |
return indicator_df
|
|
|
|
| 4 |
|
| 5 |
def patterns(df):
|
| 6 |
"""
|
| 7 |
+
Return a DataFrame of all CDL patterns with 0/1,
|
| 8 |
+
preserving the original DataFrame index.
|
| 9 |
"""
|
| 10 |
df = df.copy()
|
| 11 |
required_cols = ['Open','High','Low','Close']
|
| 12 |
|
|
|
|
| 13 |
for col in required_cols:
|
| 14 |
if col not in df.columns:
|
| 15 |
raise ValueError(f"Missing column: {col}")
|
| 16 |
|
| 17 |
+
original_index = df.index # preserve original index
|
| 18 |
+
pattern_df = pd.DataFrame(index=original_index)
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
pattern_list = [f for f in dir(talib) if f.startswith("CDL")]
|
| 21 |
|
|
|
|
| 34 |
|
| 35 |
def indicators(df):
|
| 36 |
"""
|
| 37 |
+
Return a DataFrame of numeric TA-Lib indicators,
|
| 38 |
+
preserving the original DataFrame index.
|
| 39 |
"""
|
| 40 |
df_std = df.copy()
|
| 41 |
df_std.columns = [c.lower() for c in df_std.columns]
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
ohlcv = {
|
| 44 |
'open': df_std.get('open'),
|
| 45 |
'high': df_std.get('high'),
|
|
|
|
| 54 |
]
|
| 55 |
|
| 56 |
df_list = []
|
| 57 |
+
original_index = df.index # preserve original index
|
| 58 |
|
| 59 |
for name in indicator_list:
|
| 60 |
func = getattr(talib, name)
|
|
|
|
| 67 |
if isinstance(result, tuple):
|
| 68 |
for i, arr in enumerate(result):
|
| 69 |
col_name = f"{name}_{i}"
|
| 70 |
+
temp_df = pd.DataFrame(arr, index=original_index, columns=[col_name])
|
| 71 |
df_list.append(temp_df)
|
| 72 |
else:
|
| 73 |
+
temp_df = pd.DataFrame(result, index=original_index, columns=[name])
|
| 74 |
df_list.append(temp_df)
|
| 75 |
except:
|
| 76 |
continue
|
|
|
|
| 78 |
if df_list:
|
| 79 |
indicator_df = pd.concat(df_list, axis=1)
|
| 80 |
else:
|
| 81 |
+
indicator_df = pd.DataFrame(index=original_index)
|
| 82 |
|
| 83 |
return indicator_df
|