我在计算系列的平均真实范围 [ATR] 时卡住了。ATR 基本上是 TrueRange[TR] 的 Exp Movin Avg
TR is nothing but MAX of -
Method 1: Current High less the current Low
Method 2: Current High less the previous Close (absolute value)
Method 3: Current Low less the previous Close (absolute value)
在 Pandas 中,我们没有内置的 EMA 函数。相反,我们有 EWMA,它是一个加权移动平均线。
如果有人帮助计算EMA,那也足够了
def ATR(df,n):
df['H-L']=abs(df['High']-df['Low'])
df['H-PC']=abs(df['High']-df['Close'].shift(1))
df['L-PC']=abs(df['Low']-df['Close'].shift(1))
df['TR']=df[['H-L','H-PC','L-PC']].max(axis=1)
df['ATR_' + str(n)] =pd.ewma(df['TR'], span = n, min_periods = n)
return df
上面的代码没有给出错误,但也没有给出正确的值。我将它与手动计算 Excel 中相同数据系列的 ATR 值进行了比较,但值不同
ATR excel formula-
Current ATR = [(Prior ATR x 13) + Current TR] / 14
- Multiply the previous 14-day ATR by 13.
- Add the most recent day's TR value.
- Divide the total by 14
这是我用作样本的数据系列
start='2016-1-1'
end='2016-10-30'
auro=web.DataReader('AUROPHARMA.NS','yahoo',start,end)