eshan6704 commited on
Commit
f618895
·
verified ·
1 Parent(s): 8c074ca

Update ta_indi_pat.py

Browse files
Files changed (1) hide show
  1. ta_indi_pat.py +10 -6
ta_indi_pat.py CHANGED
@@ -5,7 +5,7 @@ import numpy as np
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']
@@ -14,9 +14,7 @@ def patterns(df):
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
 
22
  for pattern in pattern_list:
@@ -29,13 +27,16 @@ def patterns(df):
29
  )
30
  pattern_df[pattern] = (result != 0).astype(int)
31
 
 
 
 
32
  return pattern_df
33
 
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]
@@ -54,7 +55,7 @@ def indicators(df):
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)
@@ -80,4 +81,7 @@ def indicators(df):
80
  else:
81
  indicator_df = pd.DataFrame(index=original_index)
82
 
 
 
 
83
  return indicator_df
 
5
  def patterns(df):
6
  """
7
  Return a DataFrame of all CDL patterns with 0/1,
8
+ adding the original 'Date' column as the first column.
9
  """
10
  df = df.copy()
11
  required_cols = ['Open','High','Low','Close']
 
14
  if col not in df.columns:
15
  raise ValueError(f"Missing column: {col}")
16
 
17
+ pattern_df = pd.DataFrame(index=df.index)
 
 
18
  pattern_list = [f for f in dir(talib) if f.startswith("CDL")]
19
 
20
  for pattern in pattern_list:
 
27
  )
28
  pattern_df[pattern] = (result != 0).astype(int)
29
 
30
+ # Add original Date as first column
31
+ pattern_df.insert(0, 'Date', df['Date'].values)
32
+
33
  return pattern_df
34
 
35
 
36
  def indicators(df):
37
  """
38
  Return a DataFrame of numeric TA-Lib indicators,
39
+ adding the original 'Date' column as the first column.
40
  """
41
  df_std = df.copy()
42
  df_std.columns = [c.lower() for c in df_std.columns]
 
55
  ]
56
 
57
  df_list = []
58
+ original_index = df.index
59
 
60
  for name in indicator_list:
61
  func = getattr(talib, name)
 
81
  else:
82
  indicator_df = pd.DataFrame(index=original_index)
83
 
84
+ # Add original Date as first column
85
+ indicator_df.insert(0, 'Date', df['Date'].values)
86
+
87
  return indicator_df