eshan6704 commited on
Commit
419762c
·
verified ·
1 Parent(s): 2cf0213

Create ta_indi_pat.py

Browse files
Files changed (1) hide show
  1. app/ta_indi_pat.py +65 -0
app/ta_indi_pat.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
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
+ print(df)
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
+ df=df.reset_index()
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