-
Notifications
You must be signed in to change notification settings - Fork 6
/
015_get_stock_prices
executable file
·52 lines (44 loc) · 1.8 KB
/
015_get_stock_prices
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
#!/usr/bin/env python2
import pandas_datareader.data as web
import datetime as dt
from dateutil.relativedelta import relativedelta
import os
import time
import modules.config_details as conf_info
import modules.tickers_helpers as helpers
if not os.path.exists(conf_info.stock_prices_dir):
os.makedirs(conf_info.stock_prices_dir)
def get_before_2016_02(tickers, store_dir):
start = dt.date(2009,1,1)
end = dt.date(2016,1,31)
CLOSE_FIELD = 'Close'
ADJUSTED_CLOSE_FIELD = 'Adj Close'
for ticker in tickers:
stock_price_filename = store_dir + conf_info.get_stock_prices_pre_2016_02_filename(ticker)
print 'Saving to', stock_price_filename
time.sleep(1)
try:
ticker_df = web.DataReader(ticker, 'yahoo', start, end)
# drop rows with at least 1 NaN
df_to_save = ticker_df[[CLOSE_FIELD, ADJUSTED_CLOSE_FIELD]].dropna(thresh=1)
df_to_save.to_csv(stock_price_filename)
except Exception, e:
print 'Failed ticker: ' + ticker + ' ~~~', str(e)
def get_after_2016_02(tickers, store_dir):
start = dt.date(2016,2,1)
end = dt.date.today()
CLOSE_FIELD = 'Close'
ADJUSTED_CLOSE_FIELD = 'Adj Close'
for ticker in tickers:
stock_price_filename = store_dir + conf_info.get_stock_prices_post_2016_02_filename(ticker)
print 'Saving to', stock_price_filename
time.sleep(1)
try:
ticker_df = web.DataReader(ticker, 'yahoo', start, end)
# drop rows with at least 1 NaN
df_to_save = ticker_df[[CLOSE_FIELD, ADJUSTED_CLOSE_FIELD]].dropna(thresh=1)
df_to_save.to_csv(stock_price_filename)
except Exception, e:
print 'Failed ticker: ' + ticker + ' ~~~', str(e)
# get_before_2016_02(helpers.get_all_tickers_lists_csv_files(),conf_info.stock_prices_dir)
get_after_2016_02(helpers.get_all_tickers_lists_csv_files(),conf_info.stock_prices_dir)