def get_zz500s(): """ 获取中证500成份股 Return -------- DataFrame code :股票代码 name :股票名称 """ from webdata.stock.fundamental import get_stock_basics try: # df = pd.read_excel(ct.HS300_CLASSIFY_URL_FTP%(ct.P_TYPE['ftp'], ct.DOMAINS['idxip'], # ct.PAGES['zz500b']), parse_cols=[0,1]) # df.columns = ct.FOR_CLASSIFY_B_COLS # df['code'] = df['code'].map(lambda x :str(x).zfill(6)) wt = pd.read_excel( ct.HS300_CLASSIFY_URL_FTP % (ct.P_TYPE['ftp'], ct.DOMAINS['idxip'], ct.PAGES['zz500wt']), parse_cols=[0, 3, 6]) wt.columns = ct.FOR_CLASSIFY_W_COLS wt['code'] = wt['code'].map(lambda x: str(x).zfill(6)) df = get_stock_basics()[['name']] df = df.reset_index() return pd.merge(df, wt) except Exception as er: print(str(er))
def get_gem_classified(): """ 获取创业板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics() df.reset_index(level=0, inplace=True) df = df[ct.FOR_CLASSIFY_B_COLS] df = df.ix[df.code.str[0] == '3'] df = df.sort('code').reset_index(drop=True) return df
def get_area_classified(): """ 获取地域分类数据 Return -------- DataFrame code :股票代码 name :股票名称 area :地域名称 """ df = fd.get_stock_basics() df = df[['name', 'area']] df.reset_index(level=0, inplace=True) df = df.sort('area').reset_index(drop=True) return df
def get_st_classified(): """ 获取风险警示板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics() df.reset_index(level=0, inplace=True) df = df[ct.FOR_CLASSIFY_B_COLS] df = df.ix[df.name.str.contains('ST')] df = df.sort('code').reset_index(drop=True) return df
def get_sme_classified(): """ 获取中小板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics() df.reset_index(level=0, inplace=True) df = df[ct.FOR_CLASSIFY_B_COLS] df = df.ix[df.code.str[0:3] == '002'] df = df.sort_values(by='code').reset_index(drop=True) return df
def get_open_h_hdf5(code, h5): """ 获取个股全部历史交易数据 """ if code[0] == '0' or code[0] == '3' or code[0] == '2': label = 'M/sz' + code elif code[0] == '6' or code[0] == '9': label = 'M/ss' + code try: df = h5[label] 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_h_data(code, start=bday, end=today) df = df.append(df1) #df.index=pd.to_datetime(df.index) #df=df.sort_index(ascending=True) #print(df) h5[label] = df except: tem = wf.get_stock_basics() date = tem.loc[code]['timeToMarket'] t = time.strptime(str(date), '%Y%m%d') startt = time.strftime('%Y-%m-%d', t) df = wt.get_h_data(code, start=startt, end=today) 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
def get_hs300s(): """ 获取沪深300当前成份股及所占权重 Return -------- DataFrame code :股票代码 name :股票名称 date :日期 weight:权重 """ from webdata.stock.fundamental import get_stock_basics try: wt = pd.read_excel( ct.HS300_CLASSIFY_URL_FTP % (ct.P_TYPE['ftp'], ct.DOMAINS['idxip'], ct.PAGES['hs300w']), parse_cols=[0, 3, 6]) wt.columns = ct.FOR_CLASSIFY_W_COLS wt['code'] = wt['code'].map(lambda x: str(x).zfill(6)) df = get_stock_basics()[['name']] df = df.reset_index() return pd.merge(df, wt) except Exception as er: print(str(er))
def get_h_csv(code, index=False, autype='qfq'): """ 获取历史复权数据,分为前复权和后复权数据,接口提供股票上市以来所有历史数据, 默认为前复权。如果不设定开始和结束日期,则返回近一年的复权数据,从性能上考虑, 推荐设定开始日期和结束日期,而且最好不要超过三年以上,获取全部历史数据, 请分年段分步获取,取到数据后,请及时在本地存储。 index:False(提取非指数的数据;True(提取指数的数据) autype:主要是提取复权类型,None 表示不复权;qfq 前复权;hfq 后复权 """ if index: if code[0] == '0': tick = 'SS_' + code else: tick = 'SZ_' + code fn = './stockdata/data/history/' + tick + '.csv' if not os.path.exists(fn): df = wt.get_h_data(code, index=True, start='1995-01-01', end=today) df = df.sort_index(ascending=True) df.to_csv(fn) else: df = pd.read_csv(fn, index_col='date') tem = str(df.index[-1])[0:10] #dftem=dftem.set_index('date') 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) bday = bd.strftime('%Y-%m-%d') all_data = wt.get_h_data(code, index=True, start=bday, end=today) if all_data is not None: all_data = all_data.sort_index(ascending=True) all_data.to_csv(fn, header=None, mode='a') df = df.append(all_data) df.index = pd.to_datetime(df.index) else: h5path = './stockdata/data/history/' + code + '.csv' if not os.path.exists(h5path): tem = wf.get_stock_basics() date = tem.loc[code]['timeToMarket'] t = time.strptime(str(date), '%Y%m%d') startt = time.strftime('%Y-%m-%d', t) df = wt.get_h_data(code, autype=autype, start=startt, end=today) 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_h_data(code, autype=autype, start=bday, end=today) if df1 is not None: 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