def get_area_classified(): """ 获取地域分类数据 Return -------- DataFrame code :股票代码 name :股票名称 area :地域名称 """ df = fd.get_stock_basics() df = df[['name', 'area']] df.reset_index(inplace=True) df = df.sort_values('area').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("code").reset_index(drop=True) return df
def get_gem_classified(file_path=None): """ 获取创业板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics(file_path) df.reset_index(level=0, inplace=True) df = df[['code', 'name']] df = df.ix[df.code.str[0] == '3'] df = df.sort('code').reset_index(drop=True) return df
def get_st_classified(): """ 获取风险警示板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics() df.reset_index(inplace=True) df = df[ct.FOR_CLASSIFY_COLS] df = df.loc[df.name.str.contains('ST')] df = df.sort_values('code').reset_index(drop=True) return df
def get_sme_classified(): """ 获取中小板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics() df.reset_index(inplace=True) df = df[ct.FOR_CLASSIFY_COLS] df = df.loc[df.code.str[0:3] == '002'] df = df.sort_values('code').reset_index(drop=True) return df
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_sme_classified(file_path=None): """ 获取中小板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics(file_path) 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('code').reset_index(drop=True) return df
def get_st_classified(file_path=None): """ 获取风险警示板股票 Return -------- DataFrame code :股票代码 name :股票名称 """ df = fd.get_stock_basics(file_path) 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_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_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_area_classified(file_path=None): """ 获取地域分类数据 Return -------- DataFrame code :股票代码 name :股票名称 area :地域名称 """ df = fd.get_stock_basics(file_path) 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')] # replace with new sort method df = df.sort_values('code').reset_index(drop=True) return df
def get_stock_cn_name(code=None): base_file = '%s/base.csv' % csv_dir df = pd.DataFrame try: if os.path.exists(base_file) is True: df = pd.read_csv(base_file, encoding='GBK') else: df = get_stock_basics() df.to_csv(base_file, encoding='GBK') except Exception as er: print(str(er)) else: names = df.loc[df['code'] == code]['name'].values if (len(names) > 0): return names[0] else: return ''
def get_hs300s(): """ 获取沪深300当前成份股及所占权重 Return -------- DataFrame code :股票代码 name :股票名称 date :日期 weight:权重 """ from tushare.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_zz500s(): """ 获取中证500成份股 Return -------- DataFrame code :股票代码 name :股票名称 """ from tushare.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 test_get_stock_basics(self): print(fd.get_stock_basics())
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)
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)