eshan6704 commited on
Commit
5cfce9b
·
verified ·
1 Parent(s): cdbe2b1

Update ta_indi_pat.py

Browse files
Files changed (1) hide show
  1. ta_indi_pat.py +26 -18
ta_indi_pat.py CHANGED
@@ -1,21 +1,27 @@
1
-
2
  import pandas as pd
3
  import talib
4
  import numpy as np
5
 
6
  def patterns(df):
7
-
8
- # Copy and normalize columns
 
9
  df = df.copy()
10
  required_cols = ['Open','High','Low','Close']
11
 
12
- # DataFrame to store patterns only
 
 
 
 
 
 
 
 
13
  pattern_df = pd.DataFrame(index=df.index)
14
 
15
- # Get all CDL pattern functions
16
  pattern_list = [f for f in dir(talib) if f.startswith("CDL")]
17
 
18
- # Apply each pattern function
19
  for pattern in pattern_list:
20
  func = getattr(talib, pattern)
21
  result = func(
@@ -24,17 +30,22 @@ def patterns(df):
24
  df['Low'].values.astype(float),
25
  df['Close'].values.astype(float)
26
  )
27
-
28
- # Convert +100/-100 → 1, 0 stays 0
29
  pattern_df[pattern] = (result != 0).astype(int)
30
 
31
  return pattern_df
32
 
33
- def indicators(df):
34
 
 
 
 
 
35
  df_std = df.copy()
36
  df_std.columns = [c.lower() for c in df_std.columns]
37
 
 
 
 
 
38
  ohlcv = {
39
  'open': df_std.get('open'),
40
  'high': df_std.get('high'),
@@ -43,38 +54,35 @@ def indicators(df):
43
  'volume': df_std.get('volume')
44
  }
45
 
46
- #indicator_list = [f for f in dir(talib) if not f.startswith("CDL") and not f.startswith("_")]
47
  indicator_list = [
48
  f for f in dir(talib)
49
  if not f.startswith("CDL") and not f.startswith("_") and f not in ["wraps", "wrapped_func"]
50
- ]
51
 
52
- df_list = [] # store all indicator columns as separate DataFrames
53
 
54
  for name in indicator_list:
55
  func = getattr(talib, name)
56
  try:
57
- if 'close' in ohlcv and ohlcv['close'] is not None:
58
  result = func(ohlcv['close'].values.astype(float))
59
  else:
60
  continue
61
 
62
- # If function returns tuple, add each output separately
63
  if isinstance(result, tuple):
64
  for i, arr in enumerate(result):
65
  col_name = f"{name}_{i}"
66
- temp_df = pd.DataFrame(arr, index=df.index, columns=[col_name])
67
  df_list.append(temp_df)
68
  else:
69
- temp_df = pd.DataFrame(result, index=df.index, columns=[name])
70
  df_list.append(temp_df)
71
  except:
72
  continue
73
 
74
- # Concatenate all columns at once
75
  if df_list:
76
  indicator_df = pd.concat(df_list, axis=1)
77
  else:
78
- indicator_df = pd.DataFrame(index=df.index)
79
 
80
  return indicator_df
 
 
1
  import pandas as pd
2
  import talib
3
  import numpy as np
4
 
5
  def patterns(df):
6
+ """
7
+ Return a DataFrame of all CDL patterns with 0/1, keeping Date as index.
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
+ # Use Date as index if present
18
+ if 'Date' in df.columns:
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
 
 
25
  for pattern in pattern_list:
26
  func = getattr(talib, pattern)
27
  result = func(
 
30
  df['Low'].values.astype(float),
31
  df['Close'].values.astype(float)
32
  )
 
 
33
  pattern_df[pattern] = (result != 0).astype(int)
34
 
35
  return pattern_df
36
 
 
37
 
38
+ def indicators(df):
39
+ """
40
+ Return a DataFrame of all numeric TA-Lib indicators, keeping Date as index.
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'),
 
54
  'volume': df_std.get('volume')
55
  }
56
 
 
57
  indicator_list = [
58
  f for f in dir(talib)
59
  if not f.startswith("CDL") and not f.startswith("_") and f not in ["wraps", "wrapped_func"]
60
+ ]
61
 
62
+ df_list = []
63
 
64
  for name in indicator_list:
65
  func = getattr(talib, name)
66
  try:
67
+ if ohlcv['close'] is not None:
68
  result = func(ohlcv['close'].values.astype(float))
69
  else:
70
  continue
71
 
 
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=df_std.index, columns=[col_name])
76
  df_list.append(temp_df)
77
  else:
78
+ temp_df = pd.DataFrame(result, index=df_std.index, columns=[name])
79
  df_list.append(temp_df)
80
  except:
81
  continue
82
 
 
83
  if df_list:
84
  indicator_df = pd.concat(df_list, axis=1)
85
  else:
86
+ indicator_df = pd.DataFrame(index=df_std.index)
87
 
88
  return indicator_df