我正在尝试从头开始实施Chaikin Oscillator,但与真正的一个API(例如TradingView)相比,它给我的结果错误
代码:
def exponential_moving_average_series(ts, n):
"""
:param df: pandas.DataFrame
:param n:
:return: pandas.DataFrame
"""
EMA = pd.Series(pd.ewma(ts, span = n, min_periods = n - 1), name = 'EMA_' + str(n))
return EMA
def chaikin_oscillator(df):
"""Calculate Chaikin Oscillator for given data.
:param df: pandas.DataFrame
:return: pandas.DataFrame
"""
ad = (2 * df['Close'] - df['High'] - df['Low']) * df['Volume'] / (df['High'] - df['Low'])
ad = ad + ad.shift(1)
ema3 = exponential_moving_average_series(ad,3);
ema10 = exponential_moving_average_series(ad,10);
Chaikin = pd.Series((ema3-ema10), name='Chaikin')
df = df.join(Chaikin)
return df
结果:
Chaikin oscillator from this code comparing to asset's closing positions graph
Chaikin oscillator reference from TradingView
数据格式为:
index时间戳打开关闭高低容量Chaikin