sign | ATR | 犀牛的博客
source link: https://benpaodewoniu.github.io/2022/11/22/sign1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
sign | ATR
ATR
又称 Average true range
平均真实波动范围,简称 ATR
指标
{A=High−LowB=abs(Closel−High)C=abs(Closel−Low)TR=MAX(A,B,C)
- High
- 该周期的最高价
- Low
- 该周期的最低价
- Closel
- 上一周期的收盘价
举一个例子来说。
import talib
from MyTT import *
high = np.array([4.0, 5.0, 6.0, 4.0, 7.0])
low = np.array([2.0, 3.0, 4.0, 2.0, 1.0])
close = np.array([3.0, 5.0, 5.0, 4.0, 6.0])
a = talib.ATR(
high,
low,
close,
timeperiod=4)
print(a)
print(ATR(close, high, low, 4))
[ nan nan nan nan 3.25]
[ nan nan nan nan 3.25]
我们手动计算各个周期的 TR
。
有一,第一个周期前面没有 close
,所以,TR_1 = Nan
第二个周期的计算如下
- 最高价和最低价差值
2
- 上一周期的收盘价和该周期的最高价的绝对值为
2
- 上一周期的收盘价和该周期的最低价的绝对值为
0
所以 TR
为 2
,这样比较下来,所有的 TR
值为
[Nan,2,2,3,6]
ATR
是他们的平均值为 (2 + 2 + 3 + 6) / 4 = 3.25
有一点需要注意,ta-lib
和 MyTT
不一定一样,比如
import talib
from MyTT import *
high = np.array([4.0, 5.0, 6.0, 4.0, 7.0])
low = np.array([2.0, 3.0, 4.0, 2.0, 1.0])
close = np.array([3.0, 5.0, 5.0, 4.0, 6.0])
a = talib.ATR(
high,
low,
close,
timeperiod=3)
print(a)
print(ATR(close, high, low, 3))
[ nan nan nan 2.33333333 3.55555556]
[ nan nan nan 2.33333333 3.66666667]
我们来手动计算一下, TR 的值为
[Nan,2,2,3,6]
第一个 ATR = (2 + 2 + 3) / 3 = 2.33333
第二个 ATR 的计算,如果是
(2.33333 * 2 + 6) / 3 = 3.5555556
(2 + 3 + 6) / 3 = 3.6666666
所以,Ta-lib
和 MyTT
采用的是两个不一样的计算路径,这一点要尤为注意。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK