forked from aliciawyy/CompInvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw5_bollingerbands.py
78 lines (56 loc) · 2.13 KB
/
hw5_bollingerbands.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
This file contains the function to compute the bollinger bands
for a certain symbole.
@author Alicia Wang
@date 30 oct 2014
'''
# Third Party Imports
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame, Series
import pandas.stats.moments as ts
# Internal Imports
from GetDataLocal import GetDataLocalYahoo
## ------------------------------------------------------------
def ComputeBollingerBands(ls_symbols, startdate, enddate, period, filename = ''):
# Get the data from local repository
d_data = GetDataLocalYahoo(startdate, enddate, ls_symbols)
print 'Symbol : ', ls_symbols
print 'Start date : ', startdate
print 'Start date : ', enddate
print 'Lookback period : ', period
df_close = d_data['close']
bol_mean = ts.rolling_mean(df_close, period)
bol_std = ts.rolling_std(df_close, period)
bolband_up = bol_mean + bol_std
bolband_dw = bol_mean - bol_std
# Plotting the prices with x-axis=timestamps
if filename is not '':
plt.clf()
plt.plot(df_close.index, df_close.values)
plt.plot(df_close.index, bolband_up)
plt.plot(df_close.index, bolband_dw)
plt.legend(['Stock adjusted price', 'Bollinger band', 'Bollinger band'])
plt.ylabel('Price')
plt.xlabel('Date')
plt.savefig(filename, format='pdf')
bol_val = (df_close - bol_mean)/bol_std
val = DataFrame(bol_val, index = df_close.index,
columns = ls_symbols)
# print val[-5:]
val.to_csv('bol.csv')
# return the bollinger value
return val
## ------------------------------------------------------------
def mainTest():
'''Main Function'''
ls_symbols = ['AAPL', 'GOOG', 'IBM', 'MSFT']
startd = dt.datetime(2010, 1, 1)
endd = dt.datetime(2010, 12, 31)
period = 20
ComputeBollingerBands(ls_symbols, startd, endd, period,
"GoogleBollingerBand.pdf")
## ------------------------------------------------------------
if __name__ == '__main__':
main()