未完待續...
def relative_strength_idx(df, n=14):
close = df['close']
delta = close.diff()
delta = delta[1:]
pricesUp = delta.copy()
pricesDown = delta.copy()
pricesUp[pricesUp < 0] = 0
pricesDown[pricesDown > 0] = 0
rollUp = pricesUp.rolling(n).mean()
rollDown = pricesDown.abs().rolling(n).mean()
rs = rollUp / rollDown
rsi = 100.0 - (100.0 / (1.0 + rs))
return rsi
# SMA
df['EMA_3'] = df['close'].ewm(3).mean().shift()
df['EMA_7'] = df['close'].ewm(7).mean().shift()
df['EMA_30'] = df['close'].ewm(30).mean().shift()
# EMA
df['SMA_3'] = df['close'].rolling(3).mean().shift()
df['SMA_7'] = df['close'].rolling(7).mean().shift()
df['SMA_30'] = df['close'].rolling(30).mean().shift()
# RSI
df['RSI'] = relative_strength_idx(df).fillna(0)
# MACD
EMA_12 = pd.Series(df['close'].ewm(span=12, min_periods=12).mean())
EMA_26 = pd.Series(df['close'].ewm(span=26, min_periods=26).mean())
df['MACD'] = pd.Series(EMA_12 - EMA_26)
df['MACD_signal'] = pd.Series(df.MACD.ewm(span=9, min_periods=9).mean())