熊猫滚动标准差

时间:2016-11-22 12:51:29

标签: python pandas standard-deviation

是否有其他人在熊猫中遇到新的rolling.std()?不推荐使用的方法是rolling_std()。新方法运行正常,但产生一个不随时间序列滚动的常数。

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

大熊猫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)

1 个答案:

答案 0 :(得分:6)

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)