def bar2h5(market='', date='', freq='D', asset='E', filepath=''): cons = get_apis() stks = get_stock_basics() fname = "%s%s%sbar%s.h5" % (filepath, market, date, freq) store = pd.HDFStore(fname, "a") if market in ['SH', 'SZ']: if market == 'SH': stks = stks.ix[stks.index.str[0] == '6', :] elif market == 'SZ': stks = stks.ix[stks.index.str[0] != '6', :] else: stks = '' market = 1 if market == 'SH' else 0 for stk in stks.index: symbol = '%s.SH' % stk if 'min' in freq: df = bar(stk, conn=cons, start_date=date, end_date=date, freq=freq, market=market, asset=asset) df['Time'] = df.index df['Time'] = df['Time'].apply(get_dt_time) df.index = df['Time'] df.drop(['code', 'Time'], axis=1, inplace=True) df.rename(columns={'open': 'OPEN'}, inplace=True) df.rename(columns={'close': 'CLOSE'}, inplace=True) df.rename(columns={'low': 'LOW'}, inplace=True) df.rename(columns={'high': 'HIGH'}, inplace=True) df.rename(columns={'vol': 'VOLUME'}, inplace=True) df.rename(columns={'amount': 'TURNOVER'}, inplace=True) df.loc[:, 'HIGH'] = df.loc[:, 'HIGH'].astype("int64") df.loc[:, 'LOW'] = df.loc[:, 'LOW'].astype("int64") df.loc[:, 'OPEN'] = df.loc[:, 'OPEN'].astype("int64") df.loc[:, 'CLOSE'] = df.loc[:, 'CLOSE'].astype("int64") df.loc[:, 'VOLUME'] = df.loc[:, 'VOLUME'].astype("int64") df.loc[:, 'TURNOVER'] = df.loc[:, 'TURNOVER'].astype("int64") df.loc[:, 'OPEN'] *= 10000 df.loc[:, 'CLOSE'] *= 10000 df.loc[:, 'HIGH'] *= 10000 df.loc[:, 'LOW'] *= 10000 df.loc[:, 'ASKPRICE1'] = 0 df.loc[:, 'ASKPRICE2'] = 0 df.loc[:, 'ASKPRICE3'] = 0 df.loc[:, 'ASKPRICE4'] = 0 df.loc[:, 'ASKPRICE5'] = 0 df.loc[:, 'ASKPRICE6'] = 0 df.loc[:, 'ASKPRICE7'] = 0 df.loc[:, 'ASKPRICE8'] = 0 df.loc[:, 'ASKPRICE9'] = 0 df.loc[:, 'ASKPRICE10'] = 0 df.loc[:, 'BIDPRICE1'] = 0 df.loc[:, 'BIDPRICE2'] = 0 df.loc[:, 'BIDPRICE3'] = 0 df.loc[:, 'BIDPRICE4'] = 0 df.loc[:, 'BIDPRICE5'] = 0 df.loc[:, 'BIDPRICE6'] = 0 df.loc[:, 'BIDPRICE7'] = 0 df.loc[:, 'BIDPRICE8'] = 0 df.loc[:, 'BIDPRICE9'] = 0 df.loc[:, 'BIDPRICE10'] = 0 df.loc[:, 'ASKVOL1'] = 0 df.loc[:, 'ASKVOL2'] = 0 df.loc[:, 'ASKVOL3'] = 0 df.loc[:, 'ASKVOL4'] = 0 df.loc[:, 'ASKVOL5'] = 0 df.loc[:, 'ASKVOL6'] = 0 df.loc[:, 'ASKVOL7'] = 0 df.loc[:, 'ASKVOL8'] = 0 df.loc[:, 'ASKVOL9'] = 0 df.loc[:, 'ASKVOL10'] = 0 df.loc[:, 'BIDVOL1'] = 0 df.loc[:, 'BIDVOL2'] = 0 df.loc[:, 'BIDVOL3'] = 0 df.loc[:, 'BIDVOL4'] = 0 df.loc[:, 'BIDVOL5'] = 0 df.loc[:, 'BIDVOL6'] = 0 df.loc[:, 'BIDVOL7'] = 0 df.loc[:, 'BIDVOL8'] = 0 df.loc[:, 'BIDVOL9'] = 0 df.loc[:, 'BIDVOL10'] = 0 df.loc[:, 'VWAP'] = 0.0 df.loc[:, 'VOL30'] = 0.0 df.loc[:, 'TOTAL_VOLUME'] = 0.0 df.loc[:, 'TOTAL_TURNOVER'] = 0.0 df.loc[:, 'INTEREST'] = 0.0 print(df) # if market == 1 and stk[0] == '6': # df = bar(stk, conn=cons, start_date=date, end_date=date, freq=freq, market=market, asset=asset) store[symbol] = df store.close() close_apis(cons)
df = pd.read_csv( ct.SHS_FAC_URL % (ct.P_TYPE['http'], ct.DOMAINS['oss'], code))[['datetime', 'floats']] df = df.set_index('datetime') return df def _random(n=13): from random import randint start = 10**(n - 1) end = (10**n) - 1 return str(randint(start, end)) # 调试入口 if __name__ == '__main__': cons = conns.get_apis() print(cons) pd = bar(code='000001', conn=cons, start_date='2017-01-01', end_date='2017-10-31', freq='1MIN', asset='E', market='', adj=None, ma=[], factors=[], retry_count=1) # print(pd)
def factor_adj(code): df = pd.read_csv(ct.ADJ_FAC_URL % (ct.P_TYPE['http'], ct.DOMAINS['oss'], code)) df = df.set_index('datetime') return df def factor_shares(code): df = pd.read_csv( ct.SHS_FAC_URL % (ct.P_TYPE['http'], ct.DOMAINS['oss'], code))[['datetime', 'floats']] df = df.set_index('datetime') return df def _random(n=13): from random import randint start = 10**(n - 1) end = (10**n) - 1 return str(randint(start, end)) if __name__ == '__main__': from tushare.util.conns import get_apis, close_apis cons = get_apis() df = bar('rb2001', conn=cons, asset='X', freq='1min') print(df) close_apis(cons)
def bar2h5(market='', date='', freq='D', asset='E', filepath=''): cons = get_apis() stks = get_stock_basics() fname = "%s%s%sbar%s.h5"%(filepath, market, date, freq) store = pd.HDFStore(fname, "a") if market in ['SH', 'SZ']: if market == 'SH': stks = stks.ix[stks.index.str[0]=='6', :] elif market == 'SZ': stks = stks.ix[stks.index.str[0]!='6', :] else: stks = '' market = 1 if market == 'SH' else 0 for stk in stks.index: symbol = '%s.SH'%stk if 'min' in freq: df = bar(stk, conn=cons, start_date=date, end_date=date, freq=freq, market=market, asset=asset) df['Time'] = df.index df['Time'] = df['Time'].apply(get_dt_time) df.index = df['Time'] df.drop(['code','Time'], axis = 1, inplace=True) df.rename(columns={'open':'OPEN'}, inplace=True) df.rename(columns={'close':'CLOSE'}, inplace=True) df.rename(columns={'low':'LOW'}, inplace=True) df.rename(columns={'high':'HIGH'}, inplace=True) df.rename(columns={'vol':'VOLUME'}, inplace=True) df.rename(columns={'amount':'TURNOVER'}, inplace=True) df.loc[:,'HIGH'] = df.loc[:,'HIGH'].astype("int64") df.loc[:,'LOW'] = df.loc[:,'LOW'].astype("int64") df.loc[:,'OPEN'] = df.loc[:,'OPEN'].astype("int64") df.loc[:,'CLOSE'] = df.loc[:,'CLOSE'].astype("int64") df.loc[:,'VOLUME'] = df.loc[:,'VOLUME'].astype("int64") df.loc[:,'TURNOVER'] = df.loc[:,'TURNOVER'].astype("int64") df.loc[:,'OPEN'] *= 10000 df.loc[:,'CLOSE'] *= 10000 df.loc[:,'HIGH'] *= 10000 df.loc[:,'LOW'] *= 10000 df.loc[:,'ASKPRICE1'] = 0 df.loc[:,'ASKPRICE2'] = 0 df.loc[:,'ASKPRICE3'] = 0 df.loc[:,'ASKPRICE4'] = 0 df.loc[:,'ASKPRICE5'] = 0 df.loc[:,'ASKPRICE6'] = 0 df.loc[:,'ASKPRICE7'] = 0 df.loc[:,'ASKPRICE8'] = 0 df.loc[:,'ASKPRICE9'] = 0 df.loc[:,'ASKPRICE10'] = 0 df.loc[:,'BIDPRICE1'] = 0 df.loc[:,'BIDPRICE2'] = 0 df.loc[:,'BIDPRICE3'] = 0 df.loc[:,'BIDPRICE4'] = 0 df.loc[:,'BIDPRICE5'] = 0 df.loc[:,'BIDPRICE6'] = 0 df.loc[:,'BIDPRICE7'] = 0 df.loc[:,'BIDPRICE8'] = 0 df.loc[:,'BIDPRICE9'] = 0 df.loc[:,'BIDPRICE10'] = 0 df.loc[:,'ASKVOL1'] = 0 df.loc[:,'ASKVOL2'] = 0 df.loc[:,'ASKVOL3'] = 0 df.loc[:,'ASKVOL4'] = 0 df.loc[:,'ASKVOL5'] = 0 df.loc[:,'ASKVOL6'] = 0 df.loc[:,'ASKVOL7'] = 0 df.loc[:,'ASKVOL8'] = 0 df.loc[:,'ASKVOL9'] = 0 df.loc[:,'ASKVOL10'] = 0 df.loc[:,'BIDVOL1'] = 0 df.loc[:,'BIDVOL2'] = 0 df.loc[:,'BIDVOL3'] = 0 df.loc[:,'BIDVOL4'] = 0 df.loc[:,'BIDVOL5'] = 0 df.loc[:,'BIDVOL6'] = 0 df.loc[:,'BIDVOL7'] = 0 df.loc[:,'BIDVOL8'] = 0 df.loc[:,'BIDVOL9'] = 0 df.loc[:,'BIDVOL10'] = 0 df.loc[:,'VWAP'] = 0.0 df.loc[:,'VOL30']=0.0 df.loc[:,'TOTAL_VOLUME']=0.0 df.loc[:,'TOTAL_TURNOVER']=0.0 df.loc[:,'INTEREST']=0.0 print(df) # if market == 1 and stk[0] == '6': # df = bar(stk, conn=cons, start_date=date, end_date=date, freq=freq, market=market, asset=asset) store[symbol] = df store.close() close_apis(cons)