-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfind_max_cum_return.py
178 lines (155 loc) · 6.25 KB
/
find_max_cum_return.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import datetime
import os
from pandas_datareader.nasdaq_trader import get_nasdaq_symbols
import argparse
import datetime
import random
import logging
import time
from tqdm import tqdm
def get_logger(log_dir, name):
"""Get a `logging.Logger` instance that prints to the console
and an auxiliary file.
Args:
log_dir (str): Directory in which to create the log file.
name (str): Name to identify the logs.
Returns:
logger (logging.Logger): Logger instance for logging events.
"""
class StreamHandlerWithTQDM(logging.Handler):
"""Let `logging` print without breaking `tqdm` progress bars.
See Also:
> https://stackoverflow.com/questions/38543506
"""
def emit(self, record):
try:
msg = self.format(record)
tqdm.write(msg)
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
# Create logger
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# Log everything (i.e., DEBUG level and above) to a file
log_path = os.path.join(log_dir, f'{name}.txt')
file_handler = logging.FileHandler(log_path)
file_handler.setLevel(logging.DEBUG)
# Log everything except DEBUG level (i.e., INFO level and above) to console
console_handler = StreamHandlerWithTQDM()
console_handler.setLevel(logging.INFO)
# Create format for the logs
file_formatter = logging.Formatter('[%(asctime)s] %(message)s',
datefmt='%m.%d.%y %H:%M:%S')
file_handler.setFormatter(file_formatter)
console_formatter = logging.Formatter('[%(asctime)s] %(message)s',
datefmt='%m.%d.%y %H:%M:%S')
console_handler.setFormatter(console_formatter)
# add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
def get_data(symbols,
add_ref=True,
data_source='quandl', #'yahoo'
price='AdjClose', #price='Adj Close',
start='1/21/2010',
end='4/15/2016'):
"""Read stock data (adjusted close) for given symbols from."""
if type(symbols) == list:
symbols = list(set(symbols))
if data_source.lower()=='quandl':
df = web.DataReader(symbols, 'quandl', start=start, end=end , api_key=os.getenv('QUANDL_API_KEY'))
return df[price]
elif data_source.lower()=='yahoo':
if add_ref and 'SPY' not in symbols: # add SPY for reference, if absent
symbols.insert(0, 'SPY')
df = web.DataReader(symbols,
data_source=data_source,
start=start,
end=end)
return df[price]
elif data_source.lower()=='av-daily':
df_all = None
for s in symbols:
df = web.DataReader(s, 'av-daily', start=start, end=end , api_key=os.getenv('ALPHAVANTAGE_API_KEY'))
if df_all is None:
df_all = pd.DataFrame( {s:df['close']} )
else:
df_all[s] = df['close']
return df_all
else:
raise Exception('data source not supportred:'+str(data_source))
def compute_daily_returns(df):
"""Compute and return the daily return values."""
# Note: Returned DataFrame must have the same number of rows
daily_returns = (df / df.shift(1)) - 1
daily_returns.ix[0,:] = 0
return daily_returns
def fill_missing_values(df_data,is_null_max_perc=0.3):
"""Fill missing values in data frame, in place."""
df_data = df_data[df_data.columns[df_data.isnull().mean() < is_null_max_perc]]
df_data.fillna(method='ffill',inplace=True)
df_data.fillna(method='backfill',inplace=True)
return df_data
def cumulative_returns(df):
return df/df.iloc[0,:] - 1
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--window-size', type=int, default=30)
parser.add_argument('--file-name', type=str, default='top_cumulative_return.csv')
parser.add_argument('--max-stocks', type=int, default=100)
parser.add_argument('--top_k', type=int, default=10)
args = parser.parse_args()
# print parameters
print('-' * 30)
print('Parameters .')
print('-' * 30)
for key, value in vars(args).items():
print('{:<20} := {}'.format(key, value))
print('-' * 30)
##
start_time_millis = time.time()
log = get_logger('.', 'find_max_cum_ret')
nasdaq_symbols = get_nasdaq_symbols()
stock_list = list(nasdaq_symbols.index)
random.shuffle(stock_list)
if args.max_stocks is not None and args.max_stocks > 0:
stock_list = stock_list[:args.max_stocks]
end_time = datetime.date.today().strftime("%m/%d/%Y")
start_time = (datetime.date.today() + datetime.timedelta(days=-1*args.window_size)).strftime("%m/%d/%Y")
# beffering
BUFF_SIZE = 50
n_batch = len(stock_list) // BUFF_SIZE
cum_df = None
for i in range(n_batch):
batch = stock_list[i*BUFF_SIZE:(i+1)*BUFF_SIZE]
try:
_df = fill_missing_values(get_data(batch, data_source='av-daily', price='close', start=start_time, end=end_time))
_cum_df = cumulative_returns(_df)
if cum_df is None:
cum_df = _cum_df
else:
cum_df = pd.concat([cum_df,_cum_df],axis=1)
# top-k
top_k = pd.DataFrame(cum_df.iloc[len(cum_df) - 1,:].sort_values(ascending=False)[:args.top_k])
print("***********",i+1,"/",n_batch)
print(top_k)
top_k.to_csv(args.file_name)
except Exception as err:
log.error('<<Error>>::'+str(err), exc_info=True)
##################
seconds = time.time() - start_time_millis
mins = seconds / 60
hours = mins / 60
days = hours / 24
print("------>>>>>>> elapsed seconds: " + str(seconds))
print("------>>>>>>> elapsed minutes: " + str(mins))
print("------>>>>>>> elapsed hours: " + str(hours))
print("------>>>>>>> elapsed days: " + str(days))