def get_hist_csv(code): """ 获取个股历史交易数据(包括均线数据),可以通过参数设置获取 日k线、周k线、月k线,以及5分钟、15分钟、30分钟和60分钟k线数据 """ h5path = './stockdata/data/last3year/' + code + '.csv' if not os.path.exists(h5path): df = wt.get_hist_data(code) if df is not None: #df.index=pd.to_datetime(df.index) df = df.sort_index(ascending=True) df.to_csv(h5path) else: df = pd.read_csv(h5path, index_col='date') tem = str(df.index[-1])[0:10] if tem != today: #if datetime.datetime.today().isoweekday() in [1,2,3,4,5]: t = time.strptime(tem, '%Y-%m-%d') y, m, d = t[0:3] tt = datetime.datetime(y, m, d) bd = tt + datetime.timedelta(days=1) bday = bd.strftime('%Y-%m-%d') df1 = wt.get_hist_data(code, start=str(bday), end=str(today)) if df1 is not None: #df1.index=pd.to_datetime(df.index) df1 = df1.sort_index(ascending=True) df = df.append(df1) df1.to_csv(h5path, mode='a', header=None) df.index = pd.to_datetime(df.index) return df
def get_data_last3year_mp(code): h5path = './testh5df/stockdata_last3year.h5' if os.path.exists(h5path): h5 = pd.HDFStore(h5path, 'a', complevel=4, complib='blosc') else: h5 = pd.HDFStore(h5path, 'w', complevel=4, complib='blosc') try: dff = h5[code] tem = str(dff.index[-1])[0:10] if tem < bftoday: print('\nUpdating data from %s for %s:' % (tem, code)) t = time.strptime(tem, "%Y-%m-%d") y, m, d = t[0:3] tt = datetime.datetime(y, m, d) bd = tt + datetime.timedelta(days=1) #if bd.weekday()==5: # bd = bd+datetime.timedelta(days=2) # print 'It is Sat' bday = bd.strftime('%Y-%m-%d') all_data1 = pd.DataFrame() try: all_data = wt.get_hist_data(code, start=bday, end=today) #print all_data all_data1 = all_data1.append(all_data) if all_data1.empty == False: #all_data1.sort_index(ascending=True,inplace=True) df = dff.append(all_data1) #print df h5[code] = df return df except Exception as e: print(e, 'No data for %s to upadate' % code) return except Exception as e: print(e) print('Getting the data for %s: \n' % code) df = wt.get_hist_data(code) #df.sort_index(ascending=True,inplace=True) h5[code] = df #h5.close() return df
def get_hist_hdf5(code): """ 获取个股历史交易数据(包括均线数据),可以通过参数设置获取日k线、周k线、月k线,以及5分钟、15分钟、30分钟和60分钟k线数据 """ h5path = './testh5df/stockdata.h5' if os.path.exists(h5path): h5 = pd.HDFStore(h5path, 'a', complevel=4, complib='blosc') else: h5 = pd.HDFStore(h5path, 'w', complevel=4, complib='blosc') if code[0] == '0' or code[0] == '3' or code[0] == '2': label = 'l3y/sz' + code elif code[0] == '6' or code[0] == '9': label = 'l3y/ss' + code try: dd = h5[label] tem = str(dd.index[-1])[0:10] if tem != today: if datetime.datetime.today().isoweekday() in [1, 2, 3, 4, 5]: #print 'Updating the data from%s for %s:'%(tem,code) t = time.strptime(tem, '%Y-%m-%d') y, m, d = t[0:3] tt = datetime.datetime(y, m, d) bd = tt + datetime.timedelta(days=1) bday = bd.strftime('%Y-%m-%d') df1 = wt.get_hist_data(code, start=bday, end=today) df = dd.append(df1) #df=df.sort_index(ascending=True,inplace=True) h5.append(label, df, data_columns=df.columns) except: df = wt.get_hist_data(code) #df=df.sort_index(ascending=True,inplace=True) h5.append(label, df, data_columns=df.columns) #finally: # h5.close() df.index = pd.to_datetime(df.index) return df
def get_open_hist_hdf5(code, h5): """ 获取个股历史交易数据(包括均线数据),可以通过参数设置获取日k线、周k线、月k线,以及5分钟、15分钟、30分钟和60分钟k线数据 """ if code[0] == '0' or code[0] == '3' or code[0] == '2': label = 'l3y/sz' + code elif code[0] == '6' or code[0] == '9': label = 'l3y/ss' + code try: df = h5[label] #print(df) tem = str(df.index[-1])[0:10] if tem < today: #if datetime.datetime.today().isoweekday() in [1,2,3,4,5]: t = time.strptime(tem, '%Y-%m-%d') y, m, d = t[0:3] tt = datetime.datetime(y, m, d) bd = tt + datetime.timedelta(days=1) bday = bd.strftime('%Y-%m-%d') df1 = wt.get_hist_data(code, start=bday, end=today) #print(df1) df = df.append(df1) df.index = pd.to_datetime(df.index) #df=df.sort_index(ascending=True) #print(df) h5[label] = df except: df = wt.get_hist_data(code) #print (df) if df is not None: #df.index=pd.to_datetime(df.index) #df=df.sort_index(ascending=True) h5[label] = df finally: pass return df