I am getting NaN

Hello All,



What am I doing wrong here



FUNCTION:

def Bollinger_Bands (data, n): 

    

    #MA = data['Close'].rolling(window=n).mean() # Calculating the moving average

    MA = pd.rolling_mean(data['Close'],n)



    #SD = data['Close'].rolling(window=n).std() # Calculating the standard deviation

    SD = pd.rolling_std(data['Close'],n)



    data['Lower_BB'] = MA - (2 * SD) # Lower Bollinger Band

    data['Upper_BB'] = MA + (2 * SD) # Upper Bollinger Band

   

    return data





IMPORTING DATA:

start = datetime(2018,6,1)

end = datetime(2018,6,30)

infy= get_history(symbol='INFY',start=start,end=end)





n=21

infy_bb = Bollinger_Bands(infy, n)

print(infy_bb)





OUTPUT:

Deliverable Volume  %Deliverble     Lower_BB     Upper_BB  
Date                                                                   
2018-06-01             1396541       0.5588          NaN          NaN  

Hi Saurabh,



Thanks for raising your query.



You have set n=21. Therefore, the computation of mean, standard deviation, lower bb and upper bb will start from 21st row (29-06-2018). 



In your code, you can print the last row (since you have only fetched 21 days of data) to see the values.



Code:

print(infy_bb.iloc[-1])



Thank you

Team Quantra