05.金融特征工程

特征工程过程的自动化

创建一个函数来自动化特征工程过程。首先,我们需要定义我们的函数,所以我将它命名为“feature_engineering”

我们只需要设置一个包含“Close”的列的dataframe,并且我们要做一个原始数据的副本,这样可以保证原始数据的完整性。

def feature_engineering(df):
  """Create new variables"""

  # we copy the dataframe to avoid interferances in the data
  df_copy = df.copy()

  # Create the returns
  df_copy['returns'] = df_copy['Close'].pct_change(1)

  # Create the SMAs
  df_copy['SMA 15'] = df_copy[['Close']].rolling(15).mean().shift(1)
  df_copy['SMA 60'] = df_copy[['Close']].rolling(60).mean().shift(1)

  # Create the volatilities
  df_copy['MSD 10'] = df_copy[['returns']].rolling(10).std().shift(1)
  df_copy['MSD 30'] = df_copy[['returns']].rolling(30).std().shift(1)

  # Create the RSI
  RSI = ta.momentum.RSIIndicator(df_copy['Close'], window =14, fillna = False)
  df_copy['rsi'] = RSI.rsi()

  return df_copy 
feature_engineering(df)
05.金融特征工程

原创文章,作者:朋远方,如若转载,请注明出处:https://caovan.com/05-jinrongtezhenggongcheng/.html

(0)
打赏 微信扫一扫 微信扫一扫
朋远方的头像朋远方
上一篇 2022年11月13日 下午3:42
下一篇 2022年11月14日 下午8:12

相关推荐

发表回复

登录后才能评论