i trying calculate bollinger band of facebook stock. found rm_fb (the calculated rolling mean) nan
def get_rolling_mean(values, window): """return rolling mean of given values, using specified window size.""" t = pd.date_range('2016-02-01', '2016-06-06', freq='d') # print("hey") # print(values); d = pd.series(values, t) return d.rolling(window=20,center=false).mean() def test_run(): # read data dates = pd.date_range('2016-02-01', '2016-06-06') symbols = ['fb'] df = get_data(symbols, dates) # compute bollinger bands # 1. compute rolling mean rm_fb = get_rolling_mean(df['fb'], window=20) print("hey") print(rm_fb) if __name__ == "__main__": test_run()
i confused how asked. manufactured data , created function hope helps.
import pandas pd import numpy np def bollinger_bands(s, k=2, n=20): """get_bollinger_bands dataframe s series of values k multiple of standard deviations n rolling window """ b = pd.concat([s, s.rolling(n).agg([np.mean, np.std])], axis=1) b['upper'] = b['mean'] + b['std'] * k b['lower'] = b['mean'] - b['std'] * k return b.drop('std', axis=1)
demonstration
np.random.seed([3,1415]) s = pd.series(np.random.randn(100) / 100, name='price').add(1.001).cumprod() bollinger_bands(s).plot()