def a_stock_attr() -> pd.DataFrame: """ code and attributes table for China's stock A market Returns ------- stock_a : pd.DataFrame ["code", "name", 'listing_date', 'market'] """ stock_sh = ak.stock_info_sh_name_code(indicator="主板A股") stock_sh = stock_sh[["SECURITY_CODE_A", "SECURITY_ABBR_A", "LISTING_DATE"]] stock_sh.columns = ["stock_code", "name", 'listing_date'] stock_sh['market'] = '上证' stock_sz = ak.stock_info_sz_name_code(indicator="A股列表") stock_sz["A股代码"] = stock_sz["A股代码"].astype(str).str.zfill(6) stock_sz = stock_sz[["A股代码", "A股简称", "A股上市日期", "板块"]] stock_sz.columns = ["stock_code", "name", 'listing_date', 'market'] stock_kcb = ak.stock_info_sh_name_code( '科创板')[['SECURITY_CODE_A', 'SECURITY_ABBR_A', "LISTING_DATE"]] stock_kcb.columns = ["stock_code", "name", 'listing_date'] stock_kcb['market'] = '科创版' stock_a = pd.concat([stock_sh, stock_sz, stock_kcb], axis=0) return stock_a
def update_codes(self): """获取股票代码到本地""" #文件中 _path = Util.path_add_file(self.dir_config, "stocks.txt") try: time_start = time.time() #上证 sh_df = ak.stock_info_sh_name_code(indicator="主板A股") raw_codes = sh_df["SECURITY_CODE_A"].values raw_formater = map(lambda x: "sh" + x, raw_codes) self.symbols.extend(raw_formater) #深证 sz_df = ak.stock_info_sz_name_code(indicator="A股列表") raw_codes = sz_df["公司代码"].values raw_formater = map(lambda x: "sz" + x, raw_codes) self.symbols.extend(raw_formater) #写 with open(_path, "w", encoding="utf8") as fw: for symbol in self.symbols: fw.write(symbol) fw.write("\n") time_end = time.time() log.info("A股代码列表跟新完毕\n 路径:{}\n 时间(s):{} ".format( _path, time_end - time_start)) except Exception as e: log.error("A股代码列表跟新错误:{}".format(e))
def get_stock_info_sh_name_code(self, indicator): ''' :param indicator: indicator="主板A股"; choice of {"主板A股", "主板B股", "科创板"} :return: ''' stock_info_sh_df = ak.stock_info_sh_name_code(indicator="主板A股") return stock_info_sh_df
def stock_info_all(): stock_info_sh_df = ak.stock_info_sh_name_code(indicator="主板A股") df = stock_info_sh_df[['company_abbr', 'stockCode', 'list_date']] stock_info_sz_df = ak.stock_info_sz_name_code(indicator="A股列表")[[ 'A股简称', 'A股代码', 'A股上市日期' ]] stock_info_sz_df.columns = ['companyAbbr', 'stockCode', 'listDate'] df = df.append(stock_info_sz_df) return jsonify_dataframe(df)
def stock_info_sh_name_code(cls, indicator="主板A股"): """获取上证证券交易所股票代码和简称数据;不包含退市代码 :param indicator: indicator="主板A股"; choice of {"主板A股", "主板B股", "科创板"} """ df = ak.stock_info_sh_name_code(indicator) df = df[["COMPANY_CODE", "LISTING_DATE", "CHANGE_DATE"]] df.columns = ["code", "LISTING_DATE", "CHANGE_DATE"] # print(df.columns) # print(df.head(10)) return df
def _listing_date(): """上市日期""" sh_df = ak.stock_info_sh_name_code(indicator="主板A股") sh = { code: pd.to_datetime(dt, errors='coerce') for code, dt in zip(sh_df['SECURITY_CODE_A'], sh_df['LISTING_DATE']) } sz_df = ak.stock_info_sz_name_code(indicator="A股列表") sz = { code: pd.to_datetime(dt, errors='coerce') for code, dt in zip(sz_df['A股代码'], sz_df['A股上市日期']) } return merge(sh, sz)
@basic.route('/stock_stop', methods=['GET']) def stock_stop(): stock_info_sz_delist_df = ak.stock_info_sz_delist(indicator="终止上市公司") stock_info_sz_delist_df["status"] = "终止上市" stock_info_sz_delist_df_1 = ak.stock_info_sz_delist(indicator="暂停上市公司") stock_info_sz_delist_df_1["status"] = "暂停上市" df = stock_info_sz_delist_df.append(stock_info_sz_delist_df_1) stock_info_sh_delist_df = ak.stock_info_sh_delist(indicator="终止上市公司") stock_info_sh_delist_df = stock_info_sh_delist_df[[ "SECURITY_CODE_A", "SECURITY_ABBR_A", "changeDate" ]] stock_info_sh_delist_df["status"] = "终止上市" stock_info_sh_delist_df1 = ak.stock_info_sh_delist(indicator="暂停上市公司") stock_info_sh_delist_df1 = stock_info_sh_delist_df1[[ "SECURITY_CODE_A", "SECURITY_ABBR_A", "QIANYI_DATE" ]] df = df.append(stock_info_sh_delist_df1) df.columns = ['stockCode', 'companyAbbr', "changeDate", 'status'] return jsonify_dataframe(df) if __name__ == '__main__': stock_info_sh_df = ak.stock_info_sh_name_code(indicator="主板A股") pprint.pprint(stock_info_sh_df)
def update(config=None): import akshare as ak import pandas as pd import traceback import time today = dt.date.today() today_weekday = today.isoweekday() if today_weekday in [6, 7]: print(f'{today} has no update!') return config = config or load_json_settings('mongodb_settings.json') if not config: raise Exception('请先配置mongodb') print(f'connect to {config["host"]}:{config["port"]}') client = connect(alias='CNStock', db='CNStock', host=config['host'], port=config['port'], username=config['user'], password=config['password'], authentication_source='admin') client.get_database('admin').authenticate(name=config['user'], password=config['password']) db = client.get_database('CNStock') col_ohlcv = db.get_collection('stock_ohlcv') col_hfq = db.get_collection('stock_hfq_factor') col_qfq = db.get_collection('stock_qfq_factor') col_ohlcv.create_index([("code", 1)]) col_hfq.create_index([("code", 1)]) col_qfq.create_index([("code", 1)]) col_ohlcv.create_index([("code", 1), ("datetime", 1)], unique=True) col_hfq.create_index([("code", 1), ("datetime", 1)], unique=True) col_qfq.create_index([("code", 1), ("datetime", 1)], unique=True) print(f'update code info!') big_df = pd.DataFrame() stock_sh = ak.stock_info_sh_name_code(indicator="主板A股") stock_sh = stock_sh[["SECURITY_CODE_A", "SECURITY_ABBR_A"]] stock_sh.columns = ["code", "name"] stock_sh["code"] = 'sh' + stock_sh["code"] stock_sh["market"] = 'sh' stock_sz = ak.stock_info_sz_name_code(indicator="A股列表") stock_sz["A股代码"] = stock_sz["A股代码"].astype(str).str.zfill(6) stock_sz = stock_sz[["A股代码", "A股简称"]] stock_sz.columns = ["code", "name"] stock_sz["code"] = 'sz' + stock_sz["code"] stock_sz["market"] = 'sz' stock_kcb = ak.stock_info_sh_name_code(indicator="科创板") stock_kcb = stock_kcb[["SECURITY_CODE_A", "SECURITY_ABBR_A"]] stock_kcb.columns = ["code", "name"] stock_kcb["code"] = 'sh' + stock_kcb["code"] stock_kcb["market"] = 'kcb' big_df = big_df.append(stock_sh, ignore_index=True) big_df = big_df.append(stock_sz, ignore_index = True) big_df = big_df.append(stock_kcb, ignore_index=True) CNStock_Info_Code_Name.drop_collection() for _, data in big_df.iterrows(): qs = CNStock_Info_Code_Name(code=data['code'], name=data['name'], market=data['market']) qs.save() all_info = CNStock_Info_Code_Name.objects() print('start update stock data') for info in all_info: try: ohlcv_list = [] hfq_list = [] qfq_list = [] last_day = (col_ohlcv.find_one({'code': info.code}, sort=[('datetime', -1)]) or {}).get('datetime', dt.datetime(1970, 1, 1)) + dt.timedelta(days=1) if last_day.date() >= today: continue daily_func_name = {'sh': 'stock_zh_a_daily', 'sz': 'stock_zh_a_daily', 'kcb':'stock_zh_kcb_daily'}.get(info.market) if not daily_func_name: raise Exception("unknown market flag") get_daily = getattr(ak, daily_func_name) data = get_daily(info.code) for d, v in data[last_day:].iterrows(): ohlcv_list.append({'datetime': d.to_pydatetime(), 'code': info.code, **v.to_dict()}) if ohlcv_list: col_ohlcv.insert_many(ohlcv_list, ordered=False) hfq_factor = get_daily(info.code, adjust='hfq-factor') for d, r in hfq_factor.iterrows(): hfq_list.append({'datetime': d.to_pydatetime(), 'code': info.code, 'factor': float(r.hfq_factor)}) col_hfq.delete_many({'code': info.code}) col_hfq.insert_many(hfq_list) qfq_factor = get_daily(info.code, adjust='qfq-factor') for d, r in qfq_factor.iterrows(): qfq_list.append({'datetime': d.to_pydatetime(), 'code': info.code, 'factor': float(r.qfq_factor)}) col_qfq.delete_many({'code': info.code}) col_qfq.insert_many(qfq_list) time.sleep(1) # except KeyError: # continue except Exception: print(f'update {info.code} failed with error:\n {traceback.format_exc()})')
import akshare as ak # stock_info_sz_df = ak.stock_info_sz_name_code(indicator="A股列表") stock_info_sh_df = ak.stock_info_sh_name_code(indicator="A股列表") # print(stock_info_sz_df) # stock_info_sz_df.to_csv('../aszshare.csv',index = False) stock_info_sh_df.to_csv('../ashshare.csv', index=False)