将
ewma60=pd.ewma(aapl_px,span=60)
改为
ewma60 = pd.DataFrame.ewm(aapl_px,span=60).mean()
ATR = pd.Series(TR_s.ewm(span=n, min_periods=n).mean())
UpI = pd.Series(UpI)
DoI = pd.Series(DoI)
# PosDI = pd.Series(pd.ewma(UpI, span=n, min_periods=n - 1) / ATR)
# NegDI = pd.Series(pd.ewma(DoI, span=n, min_periods=n - 1) / ATR)
# ds = pd.Series(pd.ewma(abs(PosDI - NegDI) / (PosDI + NegDI), span=n_ADX, min_periods=n_ADX - 1), name=xnam)
PosDI = pd.Series(UpI.ewm(span=n, min_periods=n - 1).mean() / ATR)
NegDI = pd.Series(DoI.ewm(span=n, min_periods=n - 1).mean()/ ATR)
ds = pd.Series(abs(PosDI - NegDI) / (PosDI + NegDI).ewm(span=n_ADX, min_periods=n_ADX - 1).mean(), name=xnam)
import logging
import pandas as pd
import numpy as np
log = logging.getLogger(__name__)
def moving_average(df, n):
"""计算股价的moving average.
参考: https://blog.csdn.net/FrankieHello/article/details/85938381
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
MA = pd.Series(df['close'].rolling(n, min_periods=n).mean(), name='MA_' + str(n))
df = df.join(MA)
return df
def exponential_moving_average(df, n):
"""
计算股价的exponential moving average
参考:https://www.cnblogs.com/wuliytTaotao/p/9479958.html
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
EMA = pd.Series(df['close'].ewm(span=n, min_periods=n).mean(), name='EMA_' + str(n))
df = df.join(EMA)
return df
def momentum(df, n):
"""
计算动力
参考:http://www.waihuibang.com/fxschool/technical/54505.html
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
M = pd.Series(df['close'].diff(n), name='Momentum_' + str(n))
df = df.join(M)
return df
def rate_of_change(df, n):
"""
计算变化率
参考:https://www.tradingview.com/wiki/Rate_of_Change_(ROC)
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
M = df['close'].diff(n - 1)
N = df['close'].shift(n - 1)
ROC = pd.Series(M / N, name='ROC_' + str(n))
df = df.join(ROC)
return df
def average_true_range(df, n):
"""
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
i = 0
TR_l = [0]
while i < df.index[-1]:
TR = max(df.loc[i + 1, 'high'], df.loc[i, 'close']) - min(df.loc[i + 1, 'low'], df.loc[i, 'close'])
TR_l.append(TR)
i = i + 1
TR_s = pd.Series(TR_l)
ATR = pd.Series(TR_s.ewm(span=n, min_periods=n).mean(), name='ATR_' + str(n))
df = df.join(ATR)
return df
def bollinger_bands(df, n):
"""
计算bolling bands
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
MA = pd.Series(df['close'].rolling(n, min_periods=n).mean())
MSD = pd.Series(df['close'].rolling(n, min_periods=n).std())
b1 = 4 * MSD / MA
B1 = pd.Series(b1, name='BollingerB_' + str(n))
df = df.join(B1)
b2 = (df['close'] - MA + 2 * MSD) / (4 * MSD)
B2 = pd.Series(b2, name='Bollinger%b_' + str(n))
df = df.join(B2)
return df
def stochastic_oscillator_k(df):
"""
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
SOk = pd.Series((df['close'] - df['low']) / (df['high'] - df['low']), name='SO%k')
df = df.join(SOk)
return df
def stochastic_oscillator_d(df, n):
"""
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
SOk = pd.Series((df['close'] - df['low']) / (df['high'] - df['low']), name='SO%k')
SOd = pd.Series(SOk.ewm(span=n, min_periods=n).mean(), name='SO%d_' + str(n))
df = df.join(SOd)
return df
def macd(df, n_fast, n_slow):
"""Calculate MACD, MACD Signal and MACD difference
:param df: pandas.DataFrame
:param n_fast:
:param n_slow:
:return: pandas.DataFrame
"""
EMAfast = pd.Series(df['close'].ewm(span=n_fast, min_periods=n_slow).mean())
EMAslow = pd.Series(df['close'].ewm(span=n_slow, min_periods=n_slow).mean())
MACD = pd.Series(EMAfast - EMAslow, name='MACD_' + str(n_fast) + '_' + str(n_slow))
MACDsign = pd.Series(MACD.ewm(span=9, min_periods=9).mean(), name='MACDsign_' + str(n_fast) + '_' + str(n_slow))
MACDdiff = pd.Series(MACD - MACDsign, name='MACDdiff_' + str(n_fast) + '_' + str(n_slow))
df = df.join(MACD)
df = df.join(MACDsign)
df = df.join(MACDdiff)
return df
def ease_of_movement(df, n):
"""Ease of Movement for given data.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
EoM = (df['high'].diff(1) + df['low'].diff(1)) * (df['high'] - df['low']) / (2 * df['volume'])
Eom_ma = pd.Series(EoM.rolling(n, min_periods=n).mean(), name='EoM_' + str(n))
df = df.join(Eom_ma)
return df
def standard_deviation(df, n):
"""计算标准差.
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
df = df.join(pd.Series(df['close'].rolling(n, min_periods=n).std(), name='STD_' + str(n)))
return df
————————————————
版权声明:本文为CSDN博主「Alphapeople」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38241876/article/details/110129990