[英]Moving average for multiple stock in python
I have been calculating the simple moving average of a stock using the code below:我一直在使用以下代码计算股票的简单移动平均线:
def SMA(data, period =30, column='Close'):
return data[column].rolling(window=period).mean()
df['SMA20']=SMA(df,20)
df['SMA50']=SMA(df,50)
How do I calculate the moving average for various stocks that are apart of the same data set?如何计算属于同一数据集的各种股票的移动平均线? If I apply the above code then the moving average for the second or third stock in the row will be incorrect.
如果我应用上述代码,则该行中第二只或第三只股票的移动平均线将不正确。
Can you try this, if this works for you你可以试试这个,如果这对你有用
df = (
df.fillna(0).groupby(["ticker_exchange"])[["price"]]
.rolling(30)
.corr().iloc[0::2,-1]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.