forked from shaahinmr/ci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework6.py
More file actions
82 lines (68 loc) · 2.95 KB
/
Copy pathhomework6.py
File metadata and controls
82 lines (68 loc) · 2.95 KB
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
79
80
81
82
import numpy as np
import copy
import QSTK.qstkutil.qsdateutil as du
import QSTK.qstkutil.tsutil as tsu
import QSTK.qstkutil.DataAccess as da
import datetime as dt
import matplotlib.pyplot as plt
import QSTK.qstkstudy.EventProfiler as ep
import pandas
def find_bollinger_values(adjcloses, periods):
means = pandas.rolling_mean(adjcloses, periods, min_periods=periods)
stds = pandas.rolling_std(adjcloses, periods, min_periods=periods)
bands = (adjcloses - means) / stds
return bands
def find_events(ls_symbols, d_data):
''' Finding the event dataframe '''
df_close = d_data['close']
ts_market = df_close['SPY']
market_bands = find_bollinger_values(ts_market, 20)
print "-----------------SPY"
print market_bands
print "Finding Events"
# Creating an empty dataframe
df_events = copy.deepcopy(df_close)
df_events = df_events * np.NAN
# Time stamps for the event range
ldt_timestamps = df_close.index
for s_sym in ls_symbols:
if s_sym == "SPY":
continue
sym_bands = find_bollinger_values(df_close[s_sym], 20)
print "-----------------" + s_sym
print sym_bands
for i in range(1, len(ldt_timestamps)):
# Calculating the returns for this timestamp
f_symband_today = sym_bands.ix[ldt_timestamps[i]]
f_symband_yest = sym_bands.ix[ldt_timestamps[i - 1]]
f_marketband_today = market_bands.ix[ldt_timestamps[i]]
# Event is found if the symbol is down more then 3% while the
# market is up more then 2%
#if f_symreturn_today <= -0.03 and f_marketreturn_today >= 0.02:
if f_symband_today < -2.0 and f_symband_yest >= -2.0 and f_marketband_today >= 1.4:
df_events[s_sym].ix[ldt_timestamps[i]] = 1
return df_events
#
# Prepare to read the data
#
if __name__ == '__main__':
symbols = ["AAPL", "GOOG", "IBM", "MSFT"]
dt_start = dt.datetime(2008, 1, 1)
dt_end = dt.datetime(2009, 12, 31)
ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16))
dataobj = da.DataAccess('Yahoo')
ls_symbols = dataobj.get_symbols_from_list('sp5002012')
ls_symbols.append('SPY')
ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close']
ldf_data = dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys)
d_data = dict(zip(ls_keys, ldf_data))
for s_key in ls_keys:
d_data[s_key] = d_data[s_key].fillna(method = 'ffill')
d_data[s_key] = d_data[s_key].fillna(method = 'bfill')
d_data[s_key] = d_data[s_key].fillna(1.0)
df_events = find_events(ls_symbols, d_data)
print "Creating Study"
ep.eventprofiler(df_events, d_data, i_lookback=20, i_lookforward=20,
s_filename='MyEventStudy6.pdf', b_market_neutral=True, b_errorbars=True,
s_market_sym='SPY')
# Plot the prices