/
download_hist2.py
58 lines (41 loc) · 1.32 KB
/
download_hist2.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
'''
download last three year history data
'''
from pyalgotrade.tools import yahoofinance
from pyalgotrade.utils import dt
import threadpool
import pandas as pd
import datetime
#nasdaq
nasdaq_df = pd.read_csv("data/nasdaq.csv")
nasdaq_df = nasdaq_df[nasdaq_df.Industry != 'n/a']
#amex
amex_df = pd.read_csv("data/amex.csv")
amex_df = amex_df[amex_df.Industry != 'n/a']
#ntse
nyse_df = pd.read_csv("data/nyse.csv")
nyse_df = nyse_df[nyse_df.Industry != 'n/a']
now = datetime.datetime.now()
year = now.year
begin = dt.get_first_monday(year-3)
end = dt.get_last_monday(year) + datetime.timedelta(days=6)
data = nasdaq_df['Symbol'].tolist() + amex_df['Symbol'].tolist() + nyse_df['Symbol'].tolist()
def callback(request, result):
symbol = request.args[0]
csvFile = "result/%s.csv"%(symbol)
if result is not None:
f = open(csvFile, "w")
f.write(result)
f.close()
def run(symbol):
try:
return yahoofinance.download_csv(symbol,begin, end, "w")
except Exception:
pass
n = 150
pool = threadpool.ThreadPool(n)
requests = threadpool.makeRequests(run, data, callback)
[pool.putRequest(req) for req in requests]
pool.wait()
pool.dismissWorkers(n, do_join=True)
#yahoofinance.download_weekly_bars(instrument, year, csvFile)