def function(params):
#这里包含函数实现代码,至少3行
for code in df['code'].unique():
# Select rows with the current stock code
df_code = df[df['code'] == code]
# Calculate MACD indicator using the closing price
exp1 = df_code['close'].ewm(span=12, adjust=False).mean()
exp2 = df_code['close'].ewm(span=26, adjust=False).mean()
macd = exp1 - exp2
# Calculate the signal line as the 9-day EMA of the MACD
signal = macd.ewm(span=9, adjust=False).mean()
# Calculate the MACD histogram as the difference between the MACD and signal line
macd_hist = macd - signal
# Add MACD indicators to the DataFrame
df.loc[df['code'] == code, 'macd'] = macd
df.loc[df['code'] == code, 'macdsignal'] = signal
df.loc[df['code'] == code, 'macdhist'] = macd_hist