1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ import os
5
+ import argparse
6
+ from datetime import datetime , timedelta
7
+ import pandas as pd
8
+ import numpy as np
9
+ import pandas_datareader .data as web
10
+ # import yfinance as yf
11
+
12
+ def save (df , fn ):
13
+ df = df [['Open' , 'High' , 'Low' , 'Close' , 'Adj Close' , 'Volume' ]]
14
+ df .to_csv (fn )
15
+
16
+ def run (args ):
17
+ current_path = os .path .dirname (os .path .abspath (__file__ ))
18
+ hist_path = os .path .join (current_path , '..\data' )
19
+ end_date = datetime .today ()
20
+ #start_date = end_date + timedelta(days=-5 * 365)
21
+ start_date = datetime (2006 , 1 , 1 )
22
+
23
+ if args .index :
24
+ print ('Index downloading .............' )
25
+ data = web .DataReader (name = '^GSPC' , data_source = 'yahoo' , start = start_date , end = end_date )
26
+ save (data , os .path .join (hist_path , f'SPX.csv' ))
27
+ data = web .DataReader (name = '^DJI' , data_source = 'yahoo' , start = start_date , end = end_date )
28
+ save (data , os .path .join (hist_path , f'DJI.csv' ))
29
+ data = web .DataReader (name = '^NDX' , data_source = 'yahoo' , start = start_date , end = end_date )
30
+ save (data , os .path .join (hist_path , f'NDX.csv' ))
31
+ data = web .DataReader (name = '^RUT' , data_source = 'yahoo' , start = start_date , end = end_date )
32
+ save (data , os .path .join (hist_path , f'RUT.csv' ))
33
+ print ('Index downloaded' )
34
+
35
+ if args .dow :
36
+ print ('Dow30 downloading .............' )
37
+ df = pd .read_csv (os .path .join (hist_path , 'dow30.csv' ), header = None )
38
+ for idx , row in df .iterrows ():
39
+ try :
40
+ data = web .DataReader (name = row [0 ], data_source = 'yahoo' , start = start_date , end = end_date )
41
+ save (data , os .path .join (hist_path , f'{ row [0 ]} .csv' ))
42
+ except Exception as e :
43
+ print (f'{ row [0 ]} failed. { str (e )} ' )
44
+ print ('Dow30 downloaded' )
45
+
46
+ if args .sector :
47
+ print ('Sector ETF downloading .............' )
48
+ df = pd .read_csv (os .path .join (hist_path , 'sectoretf.csv' ), header = None )
49
+ for idx , row in df .iterrows ():
50
+ try :
51
+ data = web .DataReader (name = row [0 ], data_source = 'yahoo' , start = start_date , end = end_date )
52
+ save (data , os .path .join (hist_path , f'{ row [0 ]} .csv' ))
53
+ except Exception as e :
54
+ print (f'{ row [0 ]} failed. { str (e )} ' )
55
+ print ('Sector ETF downloaded' )
56
+
57
+ if args .country :
58
+ print ('Country ETF downloading .............' )
59
+ df = pd .read_csv (os .path .join (hist_path , 'countryetf.csv' ), header = None )
60
+ for idx , row in df .iterrows ():
61
+ try :
62
+ data = web .DataReader (name = row [0 ], data_source = 'yahoo' , start = start_date , end = end_date )
63
+ save (data , os .path .join (hist_path , f'{ row [0 ]} .csv' ))
64
+ except Exception as e :
65
+ print (f'{ row [0 ]} failed. { str (e )} ' )
66
+ print ('Country ETF downloaded' )
67
+
68
+ if args .taa :
69
+ print ('Mebane Faber TAA downloading .............' )
70
+ symbols = ['SPY' , 'EFA' , 'AGG' , 'VNQ' , 'GLD' ] # sp, em, bond, real estate, gold
71
+ for sym in symbols :
72
+ try :
73
+ data = web .DataReader (name = sym , data_source = 'yahoo' , start = start_date , end = end_date )
74
+ save (data , os .path .join (hist_path , f'{ sym } .csv' ))
75
+ except Exception as e :
76
+ print (f'{ sym } failed. { str (e )} ' )
77
+ print ('Mebane Faber TAA downloaded' )
78
+
79
+ if args .sym :
80
+ print (f'{ args .sym } downloading .............' )
81
+ symbols = args .sym .split ('+' )
82
+ for sym in symbols :
83
+ try :
84
+ data = web .DataReader (name = sym , data_source = 'yahoo' , start = start_date , end = end_date )
85
+ save (data , os .path .join (hist_path , f'{ sym } .csv' ))
86
+ except Exception as e :
87
+ print (f'{ sym } failed. { str (e )} ' )
88
+ print (f'{ args .sym } downloaded' )
89
+
90
+
91
+ if __name__ == "__main__" :
92
+ parser = argparse .ArgumentParser (description = 'Historical Downloader' )
93
+ parser .add_argument ('--index' , action = 'store_true' )
94
+ parser .add_argument ('--dow' , action = 'store_true' )
95
+ parser .add_argument ('--sector' , action = 'store_true' )
96
+ parser .add_argument ('--country' , action = 'store_true' )
97
+ parser .add_argument ('--taa' , action = 'store_true' )
98
+ parser .add_argument ('--sym' , help = 'symbol' )
99
+
100
+ args = parser .parse_args ()
101
+ run (args )
0 commit comments