如何创建返回移动平均线斜率的列?
在包含比特币价格的数据框中,我想通过在每行上显示移动平均线(计算超过 20 个周期)的斜率角度来衡量趋势的强度。
移动平均线允许您分析时间序列,消除瞬时波动以突出长期趋势。 为了计算用于交易目的的简单 20 周期移动平均线,我们采用最后 20 个收盘价,将它们相加并将结果除以 20。
我开始尝试使用 scipy 的 linregress 函数,但出现异常“len( )的未调整大小的对象”,我无法解决:
from scipy.stats import linregress
x = df.iloc[-1, 8] # -1:last row, 8: sma20
y = df['sma20']
df['slope_deg'] = df.apply(linregress(x, y))
然后我使用了数学模块的 atan 函数,但返回的结果始终为 nan,无论行是什么:
import math
df['sma20'] = df['Close'].rolling(20).mean()
slope=((df['sma20'][0]-df['sma20'][20])/20)
df['slope_deg'] = math.atan(slope) * 180 / math.pi
...或 45 :
import math
df['sma20'] = df['Close'].rolling(20).mean()
df['slope_deg'] = math.atan(1) * 180 / math.pi
df
这是一个代码示例,日期为一个指数、用于计算移动平均线的价格以及移动平均线(例如超过 5 个周期):
df= pd.DataFrame({'date':np.tile( pd.date_range('1/1/2011',
periods=25, freq='D'), 4 ),
'price':(np.random.randn(100).cumsum() + 10),
'sma5':df['price'].rolling(5).mean()
})
df.head(10)
有人可以帮助我创建一个返回移动平均线斜率的列吗?
on a dataframe that contains the price of bitcoin, I want to measure the strength of a trend by displaying the angle of the slope of a moving average (calculated over 20 periods) on each row.
A moving average allows you to analyze a time series, removing transient fluctuations in order to highlight longer term trends.
To calculate a simple 20-period moving average for trading purposes, we take the last 20 closing prices, add them together and divide the result by 20.
I started by trying to use the linregress function of scipy but I get the exception "len() of unsized object" that I could not solve:
from scipy.stats import linregress
x = df.iloc[-1, 8] # -1:last row, 8: sma20
y = df['sma20']
df['slope_deg'] = df.apply(linregress(x, y))
I then used the atan function of the math module but the result returned is always nan, whatever the row is:
import math
df['sma20'] = df['Close'].rolling(20).mean()
slope=((df['sma20'][0]-df['sma20'][20])/20)
df['slope_deg'] = math.atan(slope) * 180 / math.pi
... or 45 :
import math
df['sma20'] = df['Close'].rolling(20).mean()
df['slope_deg'] = math.atan(1) * 180 / math.pi
df
Here is an example of code with the date as an index, the price used to calculate the moving average, and the moving average (over 5 periods for the example):
df= pd.DataFrame({'date':np.tile( pd.date_range('1/1/2011',
periods=25, freq='D'), 4 ),
'price':(np.random.randn(100).cumsum() + 10),
'sma5':df['price'].rolling(5).mean()
})
df.head(10)
Can someone help me to create a column that returns the slope of a moving average?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我做了20天的sma,我不太确定斜率部分,因为你没有明确指定你需要什么。
我假设坡度值(以度为单位)如下:
arctan( (PriceToday - Price20daysAgo)/ 20 )
这里有代码:
EDIT 1
: 简化 '斜率'代码并根据#Oliver的建议进行了修改。输出:
正如您所看到的,斜率值的意义不大。这是因为 20 天垃圾邮件的价格变化远远大于 20 个单位,该值代表您选择使用的时间窗口。
绘制价格和 sma20 与日期的关系。
OK, I did the 20 day sma, I am not so sure about the slope part, since you didnt clearly specify what you need.
I am assuming slope values, in degrees, as follows:
arctan( (PriceToday - Price20daysAgo)/ 20 )
Here you have the code:
EDIT 1
: simplified 'slope' code and adapted following #Oliver 's suggestion.Output:
As you can see, the slope value means little as it is. Thats because the variation in price from a 20 days spam is far greater than 20 units, the value representing the time window you chose to use.
Plotting prices and sma20 vs date.