-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBB.py
More file actions
30 lines (22 loc) · 844 Bytes
/
BB.py
File metadata and controls
30 lines (22 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
################ Bollinger Bands #############################
# Load the necessary packages and modules
import pandas as pd
import pandas.io.data as web
# Compute the Bollinger Bands
def BBANDS(data, ndays):
MA = pd.Series(pd.rolling_mean(data['Close'], ndays))
SD = pd.Series(pd.rolling_std(data['Close'], ndays))
b1 = MA + (2 * SD)
B1 = pd.Series(b1, name = 'Upper BollingerBand')
data = data.join(B1)
b2 = MA - (2 * SD)
B2 = pd.Series(b2, name = 'Lower BollingerBand')
data = data.join(B2)
return data
# Retrieve the Nifty data from Yahoo finance:
data = web.DataReader('^NSEI',data_source='yahoo',start='1/1/2010', end='1/1/2016')
data = pd.DataFrame(data)
# Compute the Bollinger Bands for NIFTY using the 50-day Moving average
n = 50
NIFTY_BBANDS = BBANDS(data, n)
print(NIFTY_BBANDS)