def _sh_mx(data, date="", start="", end="", symbol="", pageNo="", beginPage="", endPage="", retry_count=3, pause=0.001): for _ in range(retry_count): time.sleep(pause) ct._write_console() try: tail = "&pageHelp.pageNo=%s&pageHelp.beginPage=%s&pageHelp.endPage=%s" % (pageNo, beginPage, endPage) if pageNo == "": pageNo = 6 tail = "" else: pageNo += 5 beginPage = pageNo endPage = pageNo + 4 ref = rv.MAR_SH_HZ_REF_URL % (ct.P_TYPE["http"], ct.DOMAINS["sse"]) clt = Client( rv.MAR_SH_MX_URL % ( ct.P_TYPE["http"], ct.DOMAINS["sseq"], ct.PAGES["qmd"], _random(5), date, symbol, start, end, tail, _random(), ), ref=ref, cookie=rv.MAR_SH_COOKIESTR, ) lines = clt.gvalue() lines = lines.decode("utf-8") if ct.PY3 else lines lines = lines[19:-1] lines = json.loads(lines) pagecount = int(lines["pageHelp"].get("pageCount")) datapage = int(pagecount / 5 + 1 if pagecount % 5 > 0 else pagecount / 5) if pagecount == 0: return data if pageNo == 6: ct._write_tips(lines["pageHelp"].get("total")) df = pd.DataFrame(lines["result"], columns=rv.MAR_SH_MX_COLS) df["opDate"] = df["opDate"].map(lambda x: "%s-%s-%s" % (x[0:4], x[4:6], x[6:8])) data = data.append(df, ignore_index=True) if beginPage < datapage * 5: data = _sh_mx( data, start=start, end=end, pageNo=pageNo, beginPage=beginPage, endPage=endPage, retry_count=retry_count, pause=pause, ) except _network_error_classes: pass else: return data raise IOError(ct.NETWORK_URL_ERROR_MSG)
def moneyflow_hsgt(): """ 获取沪深港通资金流向 return: DataFrame,单位: 百万元 -------------- date: 交易日期 ggt_ss: 港股通(沪) ggt_sz: 港股通(深) hgt: 沪港通 sgt: 深港通 north_money: 北向资金流入 south_money: 南向资金流入 """ clt = Client(rv.HSGT_DATA%(ct.P_TYPE['http'], ct.DOMAINS['em']), ref=rv.HSGT_REF%(ct.P_TYPE['http'], ct.DOMAINS['em'], ct.PAGES['index'])) print(rv.HSGT_DATA%(ct.P_TYPE['http'], ct.DOMAINS['em'])) content = clt.gvalue() content = content.decode('utf-8') if ct.PY3 else content js = json.loads(content) df = pd.DataFrame(js) df['DateTime'] = df['DateTime'].map(lambda x: x[0:10]) df = df.replace('-', np.NaN) df = df[rv.HSGT_TEMP] df.columns = rv.HSGT_COLS df = df.sort_values('date', ascending=False) return df
def lpr_ma_data(year=None): """ 获取贷款基础利率均值数据 Parameters ------ year:年份(int) Return ------ date:日期 1Y_5:5日均值 1Y_10:10日均值 1Y_20:20日均值 """ year = du.get_year() if year is None else year lab = ct.SHIBOR_TYPE['LPR_Tendency'] lab = lab.encode('utf-8') if ct.PY3 else lab try: clt = Client(url=ct.SHIBOR_DATA_URL % (ct.P_TYPE['http'], ct.DOMAINS['shibor'], ct.PAGES['dw'], 'LPR_Tendency', year, lab, year)) content = clt.gvalue() df = pd.read_excel(StringIO(content), skiprows=[0]) df.columns = ct.LPR_MA_COLS df['date'] = df['date'].map(lambda x: x.date()) if pd.__version__ < '0.21': df['date'] = df['date'].astype(np.datetime64) else: df['date'] = df['date'].astype('datetime64[D]') return df except: return None
def lpr_ma_data(year=None): """ 获取贷款基础利率均值数据 Parameters ------ year:年份(int) Return ------ date:日期 1Y_5:5日均值 1Y_10:10日均值 1Y_20:20日均值 """ year = du.get_year() if year is None else year lab = ct.SHIBOR_TYPE['LPR_Tendency'] lab = lab.encode('utf-8') if ct.PY3 else lab try: clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'], ct.PAGES['dw'], 'LPR_Tendency', year, lab, year)) content = clt.gvalue() df = pd.read_excel(StringIO(content), skiprows=[0]) df.columns = ct.LPR_MA_COLS df['date'] = df['date'].map(lambda x: x.date()) if pd.__version__ < '0.21': df['date'] = df['date'].astype(np.datetime64) else: df['date'] = df['date'].astype('datetime64[D]') return df except: return None
def get_suspended(): """ 获取暂停上市股票列表 Return -------- DataFrame code :股票代码 name :股票名称 oDate:上市日期 tDate:终止上市日期 """ try: ref = ct.SSEQ_CQ_REF_URL%(ct.P_TYPE['http'], ct.DOMAINS['sse']) clt = Client(rv.SUSPENDED_URL%(ct.P_TYPE['http'], ct.DOMAINS['sseq'], ct.PAGES['ssecq'], _random(5), _random()), ref=ref, cookie=rv.MAR_SH_COOKIESTR) lines = clt.gvalue() lines = lines.decode('utf-8') if ct.PY3 else lines lines = lines[19:-1] lines = json.loads(lines) df = pd.DataFrame(lines['result'], columns=rv.TERMINATED_T_COLS) df.columns = rv.TERMINATED_COLS return df except Exception as er: print(str(er))
def moneyflow_hsgt(): """ 获取沪深港通资金流向 return: DataFrame,单位: 百万元 -------------- date: 交易日期 ggt_ss: 港股通(沪) ggt_sz: 港股通(深) hgt: 沪港通 sgt: 深港通 north_money: 北向资金流入 south_money: 南向资金流入 """ clt = Client(rv.HSGT_DATA%(ct.P_TYPE['http'], ct.DOMAINS['em']), ref=rv.HSGT_REF%(ct.P_TYPE['http'], ct.DOMAINS['em'], ct.PAGES['index'])) content = clt.gvalue() content = content.decode('utf-8') if ct.PY3 else content js = json.loads(content) df = pd.DataFrame(js) df['DateTime'] = df['DateTime'].map(lambda x: x[0:10]) df = df.replace('-', np.NaN) df = df[rv.HSGT_TEMP] df.columns = rv.HSGT_COLS df = df.sort_values('date', ascending=False) return df
def get_terminated(): """ 获取终止上市股票列表 Return -------- DataFrame code :股票代码 name :股票名称 oDate:上市日期 tDate:终止上市日期 """ try: ref = ct.SSEQ_CQ_REF_URL % (ct.P_TYPE["http"], ct.DOMAINS["sse"]) clt = Client( rv.TERMINATED_URL % (ct.P_TYPE["http"], ct.DOMAINS["sseq"], ct.PAGES["ssecq"], _random(5), _random()), ref=ref, cookie=rv.MAR_SH_COOKIESTR, ) lines = clt.gvalue() lines = lines.decode("utf-8") if ct.PY3 else lines lines = lines[19:-1] lines = json.loads(lines) df = pd.DataFrame(lines["result"], columns=rv.TERMINATED_T_COLS) df.columns = rv.TERMINATED_COLS return df except Exception as er: print(str(er))
def get_concepts(src='dfcf'): """ 获取概念板块行情数据 Return -------- DataFrame code :股票代码 name :股票名称 c_name :概念名称 """ clt = Client(ct.ET_CONCEPTS_INDEX_URL % (ct.P_TYPE['http'], ct.DOMAINS['dfcf'], _random(15)), ref='') content = clt.gvalue() content = content.decode('utf-8') if ct.PY3 else content js = json.loads(content) data = [] for row in js: cols = row.split(',') cs = cols[6].split('|') arr = [cols[2], cols[3], cs[0], cs[2], cols[7], cols[9]] data.append(arr) df = pd.DataFrame( data, columns=['concept', 'change', 'up', 'down', 'top_code', 'top_name']) return df
def get_halted(markets=['sz', 'sh']): """ 获取当天停牌的个股 Input markets = ['sz', 'sh'] default is both sh and sz Return -------- DataFrame code : 股票代码 market : 证券交易所 name : 股票名称 stopReason : 终止上市日期 """ try: res = None today = datetime.now().strftime('%Y-%m-%d') for market in markets: if market == 'sh': url = rv.HALTED_SH_URL % ( ct.P_TYPE['http'], ct.DOMAINS['sseq'], ct.PAGES['infodis'], ct.PAGES['ssesppq'], _random(5), today, _random()) ref = ct.SSEQ_CQ_REF_URL % (ct.P_TYPE['http'], ct.DOMAINS['sse']) clt = Client(url, ref=ref, cookie=rv.MAR_SH_COOKIESTR) lines = clt.gvalue() lines = lines.decode('utf-8') lines = json.loads(parse_jsonp(lines)) df = pd.DataFrame(lines['pageHelp']['data'], columns=rv.HALTED_T_COLS) df = df[[ 'productCode', 'productName', 'showDate', 'stopDate', 'stopReason', 'stopTime' ]] df.columns = rv.HALTED_COLS_SH else: url = "http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1798&TABKEY=tab1&txtKsrq=%s&txtZzrq=%s&txtKsrq-txtZzrq=%s&random=%s" % ( today, today, today, random.random()) html = urlopen(url).read() html = html.decode('utf-8') html_data = json.loads(html) obj_list = html_data[0]['data'] page_num = html_data[0]['metadata']['pagecount'] if 0 == page_num: continue for page_index in range(page_num - 1): url = "http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1798&TABKEY=tab1&PAGENO=%s&txtKsrq=%s&txtZzrq=%s&txtKsrq-txtZzrq=%s&random=%s" % ( page_index + 2, today, today, today, random.random()) html = urlopen(url).read() html = html.decode('utf-8') html_data = json.loads(html) tmp_list = html_data[0]['data'] obj_list.extend(tmp_list) df = pd.DataFrame(obj_list) df.columns = rv.HALTED_COLS_SZ df['market'] = market res = df if res is None else res.append(df) return res.reset_index(drop='True') except Exception as er: print(str(er)) return None
def _sh_mx(data, date='', start='', end='', symbol='', pageNo='', beginPage='', endPage='', retry_count=3, pause=0.001): for _ in range(retry_count): time.sleep(pause) ct._write_console() try: tail = '&pageHelp.pageNo=%s&pageHelp.beginPage=%s&pageHelp.endPage=%s' % ( pageNo, beginPage, endPage) if pageNo == '': pageNo = 6 tail = '' else: pageNo += 5 beginPage = pageNo endPage = pageNo + 4 ref = rv.MAR_SH_HZ_REF_URL % (ct.P_TYPE['http'], ct.DOMAINS['sse']) clt = Client( rv.MAR_SH_MX_URL % (ct.P_TYPE['http'], ct.DOMAINS['sseq'], ct.PAGES['qmd'], _random(5), date, symbol, start, end, tail, _random()), ref=ref, cookie=rv.MAR_SH_COOKIESTR) lines = clt.gvalue() lines = lines.decode('utf-8') if ct.PY3 else lines lines = lines[19:-1] lines = json.loads(lines) pagecount = int(lines['pageHelp'].get('pageCount')) datapage = int(pagecount / 5 + 1 if pagecount % 5 > 0 else pagecount / 5) if pagecount == 0: return data if pageNo == 6: ct._write_tips(lines['pageHelp'].get('total')) df = pd.DataFrame(lines['result'], columns=rv.MAR_SH_MX_COLS) df['opDate'] = df['opDate'].map(lambda x: '%s-%s-%s' % (x[0:4], x[4:6], x[6:8])) data = data.append(df, ignore_index=True) if beginPage < datapage * 5: data = _sh_mx(data, start=start, end=end, pageNo=pageNo, beginPage=beginPage, endPage=endPage, retry_count=retry_count, pause=pause) except Exception as e: print(e) else: return data raise IOError(ct.NETWORK_URL_ERROR_MSG)
def shibor_quote_data(year=None): """ 获取Shibor银行报价数据 Parameters ------ year:年份(int) Return ------ date:日期 bank:报价银行名称 ON:隔夜拆放利率 ON_B:隔夜拆放买入价 ON_A:隔夜拆放卖出价 1W_B:1周买入 1W_A:1周卖出 2W_B:买入 2W_A:卖出 1M_B:买入 1M_A:卖出 3M_B:买入 3M_A:卖出 6M_B:买入 6M_A:卖出 9M_B:买入 9M_A:卖出 1Y_B:买入 1Y_A:卖出 """ year = du.get_year() if year is None else year lab = ct.SHIBOR_TYPE['Quote'] lab = lab.encode('utf-8') if ct.PY3 else lab try: clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'], ct.PAGES['dw'], 'Quote', year, lab, year)) content = clt.gvalue() df = pd.read_excel(StringIO(content), skiprows=[0]) # df.columns = ct.QUOTE_COLS df.columns = ct.SHIBOR_Q_COLS df['date'] = df['date'].map(lambda x: x.date()) if pd.__version__ < '0.21': df['date'] = df['date'].astype(np.datetime64) else: df['date'] = df['date'].astype('datetime64[D]') return df except: return None
def _sh_mx(data, date='', start='', end='', symbol='', pageNo='', beginPage='', endPage='', retry_count=3, pause=0.001): for _ in range(retry_count): time.sleep(pause) ct._write_console() try: tail = '&pageHelp.pageNo=%s&pageHelp.beginPage=%s&pageHelp.endPage=%s'%(pageNo, beginPage, endPage) if pageNo == '': pageNo = 6 tail = '' else: pageNo += 5 beginPage = pageNo endPage = pageNo + 4 url = rv.MAR_SH_MX_URL%(ct.P_TYPE['http'], ct.DOMAINS['sseq'], ct.PAGES['qmd'], _random(5), date, symbol, start, end, tail, _random()) ref = rv.MAR_SH_HZ_REF_URL%(ct.P_TYPE['http'], ct.DOMAINS['sse']) clt = Client(url, ref=ref, cookie=rv.MAR_SH_COOKIESTR) lines = clt.gvalue() lines = lines.decode('utf-8') if ct.PY3 else lines lines = lines[19:-1] lines = json.loads(lines) pagecount = int(lines['pageHelp'].get('pageCount')) datapage = int(pagecount/5+1 if pagecount%5>0 else pagecount/5) if pagecount == 0: return data if pageNo == 6: ct._write_tips(lines['pageHelp'].get('total')) df = pd.DataFrame(lines['result'], columns=rv.MAR_SH_MX_COLS) df['opDate'] = df['opDate'].map(lambda x: '%s-%s-%s'%(x[0:4], x[4:6], x[6:8])) df = df.set_index('opDate') data = data.append(df, ignore_index=True) if beginPage < datapage*5: data = _sh_mx(data, start=start, end=end, pageNo=pageNo, beginPage=beginPage, endPage=endPage, retry_count=retry_count, pause=pause) except _network_error_classes: pass else: return data raise IOError(ct.NETWORK_URL_ERROR_MSG)
def _sh_hz(data, start=None, end=None, pageNo='', beginPage='', endPage='', retry_count=3, pause=0.001): for _ in range(retry_count): time.sleep(pause) ct._write_console() try: tail = rv.MAR_SH_HZ_TAIL_URL%(pageNo, beginPage, endPage) if not pageNo: pageNo = 6 tail = '' else: pageNo += 5 beginPage = pageNo endPage = pageNo + 4 url = rv.MAR_SH_HZ_URL%(ct.P_TYPE['http'], ct.DOMAINS['sseq'], ct.PAGES['qmd'], _random(5), start, end, tail, _random()) ref = rv.MAR_SH_HZ_REF_URL%(ct.P_TYPE['http'], ct.DOMAINS['sse']) clt = Client(url, ref=ref, cookie=rv.MAR_SH_COOKIESTR) lines = clt.gvalue() lines = lines.decode('utf-8') lines = lines[19:-1] lines = json.loads(lines) pagecount = int(lines['pageHelp'].get('pageCount')) datapage = int(pagecount/5+1 if pagecount%5>0 else pagecount/5) df = pd.DataFrame(lines['result'], columns=rv.MAR_SH_HZ_COLS) df['opDate'] = df['opDate'].map(lambda x: '%s-%s-%s'%(x[0:4], x[4:6], x[6:8])) data = data.append(df, ignore_index=True) if beginPage < datapage*5: data = _sh_hz(data, start=start, end=end, pageNo=pageNo, beginPage=beginPage, endPage=endPage, retry_count=retry_count, pause=pause) except Exception as e: print(e) else: return data raise IOError(ct.NETWORK_URL_ERROR_MSG)
def shibor_data(year=None): """ 获取上海银行间同业拆放利率(Shibor) Parameters ------ year:年份(int) Return ------ date:日期 ON:隔夜拆放利率 1W:1周拆放利率 2W:2周拆放利率 1M:1个月拆放利率 3M:3个月拆放利率 6M:6个月拆放利率 9M:9个月拆放利率 1Y:1年拆放利率 """ year = du.get_year() if year is None else year lab = ct.SHIBOR_TYPE['Shibor'] lab = lab.encode('utf-8') if ct.PY3 else lab try: clt = Client(url=ct.SHIBOR_DATA_URL%(ct.P_TYPE['http'], ct.DOMAINS['shibor'], ct.PAGES['dw'], 'Shibor', year, lab, year)) content = clt.gvalue() df = pd.read_excel(StringIO(content)) df.columns = ct.SHIBOR_COLS df['date'] = df['date'].map(lambda x: x.date()) if pd.__version__ < '0.21': df['date'] = df['date'].astype(np.datetime64) else: df['date'] = df['date'].astype('datetime64[D]') return df except: return None
def get_suspended(markets=['sz', 'sh']): """ 获取暂停上市股票列表 Return -------- DataFrame code :股票代码 name :股票名称 oDate:上市日期 tDate:终止上市日期 """ try: res = None for market in markets: if market == 'sh': ref = ct.SSEQ_CQ_REF_URL % (ct.P_TYPE['http'], ct.DOMAINS['sse']) clt = Client(rv.SUSPENDED_URL % (ct.P_TYPE['http'], ct.DOMAINS['sseq'], ct.PAGES['ssecq'], _random(5), _random()), ref=ref, cookie=rv.MAR_SH_COOKIESTR) lines = clt.gvalue() lines = lines.decode('utf-8') if ct.PY3 else lines lines = lines[19:-1] lines = json.loads(lines) df = pd.DataFrame(lines['result'], columns=rv.TERMINATED_T_COLS) else: url = "http://www.szse.cn/szseWeb/ShowReport.szse?SHOWTYPE=xlsx&CATALOGID=1793_ssgs&ENCODE=1&TABKEY=tab1" df = pd.read_excel(url, usecols=[0, 1, 2, 3]) df.columns = rv.TERMINATED_COLS df['code'] = df['code'].map(lambda x: str(x).zfill(6)) res = df if res is None else res.append(df) return res.reset_index(drop=True) except Exception as er: print(str(er)) return None