gpt4 book ai didi

python - Pandas 滚动标准差

转载 作者:太空狗 更新时间:2023-10-29 20:58:04 25 4
gpt4 key购买 nike

还有其他人在使用 pandas 中的新 rolling.std() 时遇到问题吗?弃用的方法是 rolling_std()。新方法运行良好,但会产生一个不随时间序列滚动的常数。

示例代码如下。如果您交易股票,您可能会认出布林带的公式。我从 rolling.std() 获得的输出每天跟踪库存,显然没有滚动。

这在 pandas 0.19.1 中。任何帮助,将不胜感激。

import datetime
import pandas as pd
import pandas_datareader.data as web

start = datetime.datetime(2012,1,1)
end = datetime.datetime(2012,12,31)
g = web.DataReader(['AAPL'], 'yahoo', start, end)
stocks = g['Close']
stocks['Date'] = pd.to_datetime(stocks.index)
stocks['AAPL_LO'] = stocks['AAPL'] - stocks['AAPL'].rolling(20).std() * 2
stocks['AAPL_HI'] = stocks['AAPL'] + stocks['AAPL'].rolling(20).std() * 2
stocks.dropna(axis=0, how='any', inplace=True)

最佳答案

import pandas as pd
from pandas_datareader import data as pdr
import numpy as np
import datetime

end = datetime.date.today()
begin=end-pd.DateOffset(365*10)
st=begin.strftime('%Y-%m-%d')
ed=end.strftime('%Y-%m-%d')


data = pdr.get_data_yahoo("AAPL",st,ed)

def bollinger_strat(data, window, no_of_std):
rolling_mean = data['Close'].rolling(window).mean()
rolling_std = data['Close'].rolling(window).std()

df['Bollinger High'] = rolling_mean + (rolling_std * no_of_std)
df['Bollinger Low'] = rolling_mean - (rolling_std * no_of_std)

bollinger_strat(data,20,2)

关于python - Pandas 滚动标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com