def get_history_dict(symbols, start, end, data_path): symbols_ls = list(symbols) for ticker in symbols: print ticker, try: #see if csv data available data = pd.read_csv(data_path + ticker + '.csv',\ index_col='Date', parse_dates=True) except: #if no csv data, create an empty dataframe data = pd.DataFrame(data=None, index=[start]) #check if there is data for the start-end data range if data.index[-1].toordinal() < end.toordinal() - 3 : print 'Refresh data.. ', try: new_data = get_yahoo_data(ticker, start, end) if new_data.empty==False: if data.empty==False: try: ticker_data = data.append(new_data)\ .groupby(level=0, by=['rownum']).last() except: print 'Merge failed.. ' else: ticker_data = new_data try: ticker_data.to_csv(data_path + ticker + '.csv') print ' UPDATED.. ' except: print 'Save failed.. ' else: print 'No new data.. ' except: print 'Download failed.. ' # remove symbol from list symbols_ls.remove(ticker) else: print 'OK.. ' pass pdata = dict((symbols_ls[i], pd.read_csv(data_path + symbols_ls[i] + '.csv',\ index_col='Date', parse_dates=True).sort(ascending=True)\ .ix[start:end]) for i in range(len(symbols_ls))) return pdata
def update_csv_data(symbols, data_path): # set start way back if no saved data yet start = dt.datetime(1990, 1, 1).date() end = dt.datetime.today().date() for ticker in symbols: print ticker, try: # see if csv data available data = pd.read_csv(data_path + ticker + ".csv", index_col="Date", parse_dates=True) except: # if no csv data, create an empty dataframe print "No saved data..", data = pd.DataFrame(data=None, index=[start]) if data.empty == False: start = data.index.max().date() # check if there is data for the start-end data range print data.index[-1].toordinal(), end.toordinal() - 3, if data.index[-1].toordinal() < end.toordinal() - 3: print "Get Yahoo data.. ", try: new_data = get_yahoo_data(ticker, start, end) except: print "Download failed.. " if new_data.empty == False: if data.empty == False: try: ticker_data = data.append(new_data).groupby(level=0, by=["rownum"]).last() except: print "Merge failed.. " else: ticker_data = new_data try: ticker_data.to_csv(data_path + ticker + ".csv") print " UPDATED.. " except: print "Save failed.. " else: print "No new data.. " else: print "OK.. " status = "OK" pass return