在計算系列的平均真實範圍[ATR]時,我被卡住了。 ATR基本上TrueRange的精通企圖不平均[TR]需要PandasDataSeries上的平均真實範圍和指數移動平均函數
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)
在熊貓我們沒有一個內置的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中對同一dataseries計算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
這是我作爲一個樣本dataseries
start='2016-1-1'
end='2016-10-30'
auro=web.DataReader('AUROPHARMA.NS','yahoo',start,end)