def get_report_data(year, quarter): """ 获取业绩报表数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame eps,每股收益(元) bvps,每股净资产(元) roe,净资产收益率(%) epcf,每股现金流量(元) net_profits,净利润(万元) code,代码 name,股票名称 exchange,交易所 epslastyear,去年同期每股收益 netprofitlastyear,去年同期净利润 eps_yoy,每股收益同比(%) profits_yoy,净利润同比(%) distrib,利润分配方案 detail,详情 """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_report_data(year, quarter,1,pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x:str(x).zfill(6)) return data
def get_cashflow_data(year, quarter): """ 获取现金流量数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame cf_sales,经营现金净流量对销售收入比率(%) rateofreturn,资产的经营现金流量回报率(%) cf_nm,经营现金净流量与净利润的比率(%) cf_liabilities,经营现金净流量对负债比率(%) cashflowratio,现金流量比率(%) code,代码 name,股票名称 """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_cashflow_data(year, quarter,1,pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x:str(x).zfill(6)) return data
def get_profit_data(year, quarter): """ 获取盈利能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame code,代码 name,股票名称 roe,净资产收益率(%) net_profit_ratio,净利率(%) net_profits,净利润(百万元) eps,每股收益(元) business_income,主营业务收入 gross_profit_rate,毛利率(%) bips,每股主营业务收入(元) """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_profit_data(year, quarter,1,pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x:str(x).zfill(6)) return data
def get_debtpaying_data(year, quarter): """ 获取偿债能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame currentratio,流动比率(%) quickratio,速动比率(%) cashratio,现金比率(%) icratio,利息支付倍数 sheqratio,股东权益比率(%) adratio,资产负债率(%) code,代码 name,股票名称 """ #nocache if ct._check_input(year, quarter) is True: ct._write_head() data = _get_debtpaying_data(year, quarter,1,pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x:str(x).zfill(6)) return data
def get_operation_data(year, quarter): """ 获取营运能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame code,代码 name,股票名称 arturnover,应收账款周转率(次) arturndays,应收账款周转天数(天) inventory_turnover,存货周转率(次) inventory_days,存货周转天数(天) currentasset_turnover,流动资产周转率(次) currentasset_days,流动资产周转天数(天) """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_operation_data(year, quarter, 1, pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x: str(x).zfill(6)) return data
def get_operation_data(year, quarter): """ 获取营运能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame code,代码 name,股票名称 arturnover,应收账款周转率(次) arturndays,应收账款周转天数(天) inventory_turnover,存货周转率(次) inventory_days,存货周转天数(天) currentasset_turnover,流动资产周转率(次) currentasset_days,流动资产周转天数(天) """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_operation_data(year, quarter,1,pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x:str(x).zfill(6)) return data
def get_profit_data(year, quarter): """ 获取盈利能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame code,代码 name,股票名称 roe,净资产收益率(%) net_profit_ratio,净利率(%) net_profits,净利润(百万元) eps,每股收益(元) business_income,主营业务收入 gross_profit_rate,毛利率(%) bips,每股主营业务收入(元) """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_profit_data(year, quarter, 1, pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x: str(x).zfill(6)) return data
def get_cashflow_data(year, quarter): """ 获取现金流量数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame cf_sales,经营现金净流量对销售收入比率(%) rateofreturn,资产的经营现金流量回报率(%) cf_nm,经营现金净流量与净利润的比率(%) cf_liabilities,经营现金净流量对负债比率(%) cashflowratio,现金流量比率(%) code,代码 name,股票名称 """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_cashflow_data(year, quarter, 1, pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x: str(x).zfill(6)) return data
def get_report_data(year, quarter): """ 获取业绩报表数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame eps,每股收益(元) bvps,每股净资产(元) roe,净资产收益率(%) epcf,每股现金流量(元) net_profits,净利润(万元) code,代码 name,股票名称 exchange,交易所 epslastyear,去年同期每股收益 netprofitlastyear,去年同期净利润 eps_yoy,每股收益同比(%) profits_yoy,净利润同比(%) distrib,利润分配方案 detail,详情 """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_report_data(year, quarter, 1, pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x: str(x).zfill(6)) return data
def get_growth_data(year, quarter): """ 获取成长能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame mbrg,主营业务收入增长率(%) nprg,净利润增长率(%) nav,净资产增长率(%) targ,总资产增长率(%) code,代码 name,股票名称 EXCHANGE,交易所 eps,每股收益 holderInterests,股东权益 epsLastYear,去年每股收益 holderInterestsLastYear,去年股东权益 epsg,每股收益增长率(%) seg,股东权益增长率(%) """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_growth_data(year, quarter, 1, pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x: str(x).zfill(6)) return data
def get_debtpaying_data(year, quarter): """ 获取偿债能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame currentratio,流动比率(%) quickratio,速动比率(%) cashratio,现金比率(%) icratio,利息支付倍数 sheqratio,股东权益比率(%) adratio,资产负债率(%) code,代码 name,股票名称 """ #nocache if ct._check_input(year, quarter) is True: ct._write_head() data = _get_debtpaying_data(year, quarter, 1, pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x: str(x).zfill(6)) return data
def get_growth_data(year, quarter): """ 获取成长能力数据 Parameters -------- year:int 年度 e.g:2014 quarter:int 季度 :1、2、3、4,只能输入这4个季度 Return -------- DataFrame mbrg,主营业务收入增长率(%) nprg,净利润增长率(%) nav,净资产增长率(%) targ,总资产增长率(%) code,代码 name,股票名称 EXCHANGE,交易所 eps,每股收益 holderInterests,股东权益 epsLastYear,去年每股收益 holderInterestsLastYear,去年股东权益 epsg,每股收益增长率(%) seg,股东权益增长率(%) """ if ct._check_input(year, quarter) is True: ct._write_head() data = _get_growth_data(year, quarter,1,pd.DataFrame()) if data is not None and not data.empty: data = data.drop_duplicates('code') data['code'] = data['code'].map(lambda x:str(x).zfill(6)) return data
def get_today_all(): """ 一次性获取最近一个日交易日所有股票的交易数据 return ------- DataFrame 属性:代码,名称,涨跌幅,现价,开盘价,最高价,最低价,最日收盘价,成交量,换手率 """ ct._write_head() df = _parsing_dayprice_json(1) if df is not None: for i in range(2, ct.PAGE_NUM[0]): newdf = _parsing_dayprice_json(i) df = df.append(newdf, ignore_index=True) return df
def get_today_ticks(code=None, retry_count=3, pause=0.001): """ 获取当日分笔明细数据 Parameters ------ code:string 股票代码 e.g. 600848 retry_count : int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 return ------- DataFrame 当日所有股票交易数据(DataFrame) 属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型 """ if code is None or len(code) != 6: return None symbol = _code_to_symbol(code) date = du.today() try: request = Request(ct.TODAY_TICKS_PAGE_URL % (date, symbol)) data_str = urlopen(request, timeout=10).read() data_str = data_str.decode('GBK') data_str = data_str[1:-1] data_str = eval( data_str, type('Dummy', (dict, ), dict(__getitem__=lambda s, n: n))()) data_str = json.dumps(data_str) data_str = json.loads(data_str) pages = len(data_str['detailPages']) data = pd.DataFrame() ct._write_head() for pNo in range(1, pages): data = data.append(_today_ticks(symbol, date, pNo, retry_count, pause), ignore_index=True) except Exception as er: print(str(er)) return data
def get_stock_hq_list(): """ 获取沪深上市公司列表和行情 Return -------- DataFrame guba,股吧地址 symbol,股票代号 code,股票代码 name,股票名称 trade,最新价 pricechange,涨跌额 changepercent,涨跌幅 buy,买入 sell,卖出 settlement,昨收 open,今开 high,最高 low,最低 volume,成交量(手) amount,成交额(万) ticktime, per,市盈率 per_d,动态市盈率 nta,每股净资产 pb,市净率 mktcap,总市值 nmc,流通市值 turnoverratio,换手率(%) favor, guba, """ ct._write_head() df = _get_stock_hq_list(1, pd.DataFrame()) if df is not None and not df.empty: df = df.drop_duplicates('code') df['code'] = df['code'].map(lambda x: str(x).zfill(6)) return df
def get_stock_hq_list(): """ 获取沪深上市公司列表和行情 Return -------- DataFrame guba,股吧地址 symbol,股票代号 code,股票代码 name,股票名称 trade,最新价 pricechange,涨跌额 changepercent,涨跌幅 buy,买入 sell,卖出 settlement,昨收 open,今开 high,最高 low,最低 volume,成交量(手) amount,成交额(万) ticktime, per,市盈率 per_d,动态市盈率 nta,每股净资产 pb,市净率 mktcap,总市值 nmc,流通市值 turnoverratio,换手率(%) favor, guba, """ ct._write_head() df = _get_stock_hq_list(1, pd.DataFrame()) if df is not None and not df.empty: df = df.drop_duplicates('code') df['code'] = df['code'].map(lambda x:str(x).zfill(6)) return df
def get_today_ticks(code=None, retry_count=3, pause=0.001): """ 获取当日分笔明细数据 Parameters ------ code:string 股票代码 e.g. 600848 retry_count : int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 return ------- DataFrame 当日所有股票交易数据(DataFrame) 属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型 """ if code is None or len(code)!=6 : return None symbol = _code_to_symbol(code) date = du.today() try: request = Request(ct.TODAY_TICKS_PAGE_URL % ( date,symbol)) data_str = urlopen(request, timeout=10).read() data_str = data_str.decode('GBK') data_str = data_str[1:-1] data_str = eval(data_str, type('Dummy', (dict,), dict(__getitem__ = lambda s, n:n))()) data_str = json.dumps(data_str) data_str = json.loads(data_str) pages = len(data_str['detailPages']) data = pd.DataFrame() ct._write_head() for pNo in range(1, pages): data = data.append(_today_ticks(symbol, date, pNo, retry_count, pause), ignore_index=True) except Exception as er: print(str(er)) return data
def get_h_data(code, start=None, end=None, autype='qfq', index=False, retry_count=3, pause=0.001): ''' 获取历史复权数据 Parameters ------ code:string 股票代码 e.g. 600848 start:string 开始日期 format:YYYY-MM-DD 为空时取当前日期 end:string 结束日期 format:YYYY-MM-DD 为空时取去年今日 autype:string 复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq retry_count : int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 return ------- DataFrame date 交易日期 (index) open 开盘价 high 最高价 close 收盘价 low 最低价 volume 成交量 amount 成交金额 factor 后复权因子 ''' start = du.today_last_year() if start is None else start end = du.today() if end is None else end qs = du.get_quarts(start, end) qt = qs[0] ct._write_head() data = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count, pause) if len(qs) > 1: for d in range(1, len(qs)): qt = qs[d] ct._write_console() df = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count, pause) data = data.append(df, ignore_index=True) if len(data) == 0 or len( data[(data.date >= start) & (data.date <= end)]) == 0: return None data = data.drop_duplicates('date') if index: data = data[(data.date >= start) & (data.date <= end)] data = data.set_index('date') data = data.sort_index(ascending=False) return data if autype == 'hfq': #data = data.drop('factor', axis=1) data = data[(data.date >= start) & (data.date <= end)] for label in ['open', 'high', 'close', 'low']: data[label] = data[label].map(ct.FORMAT) data[label] = data[label].astype(float) data = data.set_index('date') data = data.sort_index(ascending=False) return data else: if autype == 'qfq': #data = data.drop('factor', axis=1) df = _parase_fq_factor(code, start, end) df = df.drop_duplicates('date') df = df.sort('date', ascending=False) frow = df.head(1) rt = get_realtime_quotes(code) if rt is None: return None if ((float(rt['high']) == 0) & (float(rt['low']) == 0)): preClose = float(rt['pre_close']) else: if du.is_holiday(du.today()): preClose = float(rt['price']) else: if (du.get_hour() > 9) & (du.get_hour() < 18): preClose = float(rt['pre_close']) else: preClose = float(rt['price']) rate = float(frow['factor']) / preClose data = data[(data.date >= start) & (data.date <= end)] for label in ['open', 'high', 'low', 'close']: data[label] = data[label] / rate data[label] = data[label].map(ct.FORMAT) data[label] = data[label].astype(float) data = data.set_index('date') data = data.sort_index(ascending=False) return data else: for label in ['open', 'high', 'close', 'low']: data[label] = data[label] / data['factor'] #data = data.drop('factor', axis=1) data = data[(data.date >= start) & (data.date <= end)] for label in ['open', 'high', 'close', 'low']: data[label] = data[label].map(ct.FORMAT) data = data.set_index('date') data = data.sort_index(ascending=False) data = data.astype(float) return data
def get_h_data(code, start=None, end=None, autype='qfq', index=False, retry_count=3, pause=0.001): ''' 获取历史复权数据 Parameters ------ code:string 股票代码 e.g. 600848 start:string 开始日期 format:YYYY-MM-DD 为空时取当前日期 end:string 结束日期 format:YYYY-MM-DD 为空时取去年今日 autype:string 复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq retry_count : int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 return ------- DataFrame date 交易日期 (index) open 开盘价 high 最高价 close 收盘价 low 最低价 volume 成交量 amount 成交金额 factor 后复权因子 ''' start = du.today_last_year() if start is None else start end = du.today() if end is None else end qs = du.get_quarts(start, end) qt = qs[0] ct._write_head() data = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count, pause) if len(qs)>1: for d in range(1, len(qs)): qt = qs[d] ct._write_console() df = _parse_fq_data(_get_index_url(index, code, qt), index, retry_count, pause) data = data.append(df, ignore_index=True) if len(data) == 0 or len(data[(data.date>=start)&(data.date<=end)]) == 0: return None data = data.drop_duplicates('date') if index: data = data[(data.date>=start) & (data.date<=end)] data = data.set_index('date') data = data.sort_index(ascending=False) return data if autype == 'hfq': #data = data.drop('factor', axis=1) data = data[(data.date>=start) & (data.date<=end)] for label in ['open', 'high', 'close', 'low']: data[label] = data[label].map(ct.FORMAT) data[label] = data[label].astype(float) data = data.set_index('date') data = data.sort_index(ascending = False) return data else: if autype == 'qfq': #data = data.drop('factor', axis=1) df = _parase_fq_factor(code, start, end) df = df.drop_duplicates('date') df = df.sort('date', ascending=False) frow = df.head(1) rt = get_realtime_quotes(code) if rt is None: return None if ((float(rt['high']) == 0) & (float(rt['low']) == 0)): preClose = float(rt['pre_close']) else: if du.is_holiday(du.today()): preClose = float(rt['price']) else: if (du.get_hour() > 9) & (du.get_hour() < 18): preClose = float(rt['pre_close']) else: preClose = float(rt['price']) rate = float(frow['factor']) / preClose data = data[(data.date >= start) & (data.date <= end)] for label in ['open', 'high', 'low', 'close']: data[label] = data[label] / rate data[label] = data[label].map(ct.FORMAT) data[label] = data[label].astype(float) data = data.set_index('date') data = data.sort_index(ascending = False) return data else: for label in ['open', 'high', 'close', 'low']: data[label] = data[label] / data['factor'] #data = data.drop('factor', axis=1) data = data[(data.date>=start) & (data.date<=end)] for label in ['open', 'high', 'close', 'low']: data[label] = data[label].map(ct.FORMAT) data = data.set_index('date') data = data.sort_index(ascending=False) data = data.astype(float) return data