简体   繁体   English

每周样本移动平均线

[英]Weekly Sample Moving Average

I am trying to get the data from yahoo finance (yfinance) and calculate the simple moving average(SMA) but it only works for daily data instead of weekly data.我正在尝试从 yahoo Finance (yfinance) 获取数据并计算简单移动平均线 (SMA),但它仅适用于每日数据而不是每周数据。 Daily data of SMA returned calculated SMA20 but weekly data only returned NaN. SMA 的每日数据返回计算的 SMA20,但每周数据仅返回 NaN。 Anything wrong in my code below?我下面的代码有什么问题吗? Grateful if you could help.如果您能提供帮助,将不胜感激。

import yfinance as yf
#Weekly
stock = yf.download(tickers= 'MSFT',interval='1wk')
stock['SMA_20'] = stock['Close'].rolling(window=20).mean()
print(stock)

#Daily
stock = yf.download(tickers= 'MSFT',interval='1d')
stock['SMA_20'] = stock['Close'].rolling(window=20).mean()
print(stock)

Specify the min_periods parameter in the rolling() method to show results earlier than the first 20 intervals.在 rolling() 方法中指定 min_periods 参数以显示早于前 20 个间隔的结果。 Please check the documentation on the method for additional details:请查看有关该方法的文档以获取更多详细信息:

Also note that your SMA_20 is over weeks rather than days when you specify the interval to be 1wk.另请注意,当您将间隔指定为 1wk 时,您的 SMA_20 是数周而不是数天。

import yfinance as yf
#Weekly
stock = yf.download(tickers= 'MSFT', interval='1wk')
stock['SMA_20'] = stock['Close'].rolling(window=20, min_periods = 1).mean()
print(stock)

#Daily
stock = yf.download(tickers= 'MSFT', interval='1d')
stock['SMA_20'] = stock['Close'].rolling(window=20,min_periods = 1).mean()
print(stock)

Output: Output: 在此处输入图像描述 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM