pandas 使用 Python 计算 OHLC 数据的平均真实范围 (ATR)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40256338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating Average True Range (ATR) on OHLC data with Python
提问by cardycakes
The ATR is the average of the True Range for a given period. True Range is (High-Low) meaning I have computed this with the following:
ATR 是给定时间段内真实范围的平均值。真实范围是(高-低)意味着我已经用以下方法计算了这个:
df['High'].subtract(df['Low']).rolling(distance).mean()
However if a short period (or 'distance' in the example above) is required the ATR can be very jumpy, i.e. with large sporadic gaps appearing between some numbers.
然而,如果需要一个短周期(或上例中的“距离”),ATR 可能会非常不稳定,即在某些数字之间会出现大的零星间隙。
The real ATR equation recognises this and smooths it out by doing the following:
真正的 ATR 方程认识到这一点,并通过执行以下操作使其平滑:
Current ATR = [(Prior ATR x 13) + Current TR] / 14
However I am unsure how to do this in the same manner as I did above, i.e. a column wide operation.
但是,我不确定如何以与上面相同的方式执行此操作,即列范围操作。
Sample data including the TR and ATR(10) from my original method:
示例数据包括来自我的原始方法的 TR 和 ATR(10):
Date Time Open High Low Close TR ATR
30/09/16 14:45:00+00:00 1.1216 1.1221 1.1208 1.1209 0.0013 0.0013
30/09/16 15:00:00+00:00 1.1209 1.1211 1.1203 1.1205 0.0008 0.0013
30/09/16 15:15:00+00:00 1.1205 1.1216 1.1204 1.1216 0.0012 0.0013
30/09/16 15:30:00+00:00 1.1217 1.1222 1.1213 1.1216 0.0008 0.0013
30/09/16 15:45:00+00:00 1.1216 1.1240 1.1216 1.1240 0.0025 0.0015
30/09/16 16:00:00+00:00 1.1239 1.1246 1.1228 1.1242 0.0019 0.0015
30/09/16 16:15:00+00:00 1.1242 1.1251 1.1235 1.1240 0.0016 0.0016
30/09/16 16:30:00+00:00 1.1240 1.1240 1.1234 1.1236 0.0007 0.0014
30/09/16 16:45:00+00:00 1.1237 1.1245 1.1235 1.1238 0.0009 0.0012
30/09/16 17:00:00+00:00 1.1238 1.1239 1.1231 1.1233 0.0008 0.0012
30/09/16 17:15:00+00:00 1.1233 1.1245 1.1232 1.1240 0.0013 0.0012
30/09/16 17:30:00+00:00 1.1240 1.1242 1.1228 1.1230 0.0013 0.0013
30/09/16 17:45:00+00:00 1.1230 1.1230 1.1221 1.1227 0.0009 0.0013
30/09/16 18:00:00+00:00 1.1227 1.1232 1.1227 1.1232 0.0005 0.0012
30/09/16 18:15:00+00:00 1.1232 1.1232 1.1227 1.1227 0.0005 0.0010
30/09/16 18:30:00+00:00 1.1227 1.1231 1.1225 1.1231 0.0006 0.0009
30/09/16 18:45:00+00:00 1.1231 1.1237 1.1230 1.1232 0.0007 0.0008
30/09/16 19:00:00+00:00 1.1232 1.1233 1.1229 1.1231 0.0004 0.0008
30/09/16 19:15:00+00:00 1.1231 1.1234 1.1230 1.1230 0.0004 0.0007
30/09/16 19:30:00+00:00 1.1231 1.1234 1.1230 1.1234 0.0004 0.0007
30/09/16 19:45:00+00:00 1.1233 1.1240 1.1230 1.1239 0.0010 0.0007
30/09/16 20:00:00+00:00 1.1239 1.1242 1.1237 1.1238 0.0005 0.0006
30/09/16 20:15:00+00:00 1.1238 1.1240 1.1235 1.1237 0.0005 0.0006
30/09/16 20:30:00+00:00 1.1237 1.1238 1.1235 1.1235 0.0003 0.0005
30/09/16 20:45:00+00:00 1.1235 1.1236 1.1233 1.1233 0.0003 0.0005
30/09/16 21:00:00+00:00 1.1233 1.1238 1.1233 1.1237 0.0006 0.0005
30/09/16 21:15:00+00:00 1.1237 1.1244 1.1237 1.1242 0.0008 0.0005
30/09/16 21:30:00+00:00 1.1242 1.1243 1.1239 1.1239 0.0004 0.0005
30/09/16 21:45:00+00:00 1.1239 1.1244 1.1236 1.1241 0.0008 0.0006
回答by Andrew Olson
For anyone else looking on how to do this, here is my answer.
对于其他正在寻找如何执行此操作的人,这是我的答案。
def wwma(values, n):
"""
J. Welles Wilder's EMA
"""
return values.ewm(alpha=1/n, adjust=False).mean()
def atr(df, n=14):
data = df.copy()
high = data[HIGH]
low = data[LOW]
close = data[CLOSE]
data['tr0'] = abs(high - low)
data['tr1'] = abs(high - close.shift())
data['tr2'] = abs(low - close.shift())
tr = data[['tr0', 'tr1', 'tr2']].max(axis=1)
atr = wwma(tr, n)
return atr
回答by G.Dep
That's not the right calculation for TR see - ATR, but here is how I would do it:
这不是 TR 的正确计算,请参阅 - ATR,但这是我的方法:
Where alpha = 2 / (span+1)
其中 alpha = 2 / (span+1)
df['ATR'] = df['TR'].ewm(span = 10).mean()
df['ATR'] = df['TR'].ewm(span = 10).mean()
Otherwise you should be able to easily do you're own smoothing like this:
否则,您应该能够轻松地进行自己的平滑处理:
df['ATR'] = ( df['ATR'].shift(1)*13 + df['TR'] ) / 14
df['ATR'] = ( df['ATR'].shift(1)*13 + df['TR'] ) / 14