使用 Python 计算 OHLC 数据的平均真实范围 (ATR) [英] Calculating Average True Range (ATR) on OHLC data with Python

查看:147
本文介绍了使用 Python 计算 OHLC 数据的平均真实范围 (ATR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ATR 是给定时间段内真实范围的平均值.真实范围是(高-低)意味着我已经用以下方法计算了这个:

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:

df['High'].subtract(df['Low']).rolling(distance).mean()

但是,如果需要短时间段(或上例中的距离"),ATR 可能会非常不稳定,即某些数字之间会出现较大的零星间隙.

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:

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.

包括来自我原始方法的 TR 和 ATR(10) 的样本数据:

Sample data including the TR and ATR(10) from my original method:

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

推荐答案

这不是 TR 的正确计算,请参阅 - ATR,但我会这样做:

That's not the right calculation for TR see - ATR, but here is how I would do it:

其中 alpha = 2/(span+1)

Where alpha = 2 / (span+1)

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

熊猫 ewm

这篇关于使用 Python 计算 OHLC 数据的平均真实范围 (ATR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆