股票列表格式
股票列表文件
代码
- 2024-1-31更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import baostock as bs import pandas as pd class StockData: def __init__(self): self.df = pd.read_csv('all_stocks.csv', encoding='gbk', index_col='serial') self.code_list = self.df['code'].tolist() self.cols = ['name', 'code', 'close', 'peTTM', 'pbMRQ', 'psTTM', 'pcfNcfTTM', 'roeAvg', 'npMargin', 'gpMargin', 'netProfit', 'epsTTM', 'MBRevenue', 'totalShare', 'liqaShare', 'NRTurnRatio', 'NRTurnDays', 'INVTurnRatio', 'INVTurnDays', 'CATurnRatio', 'AssetTurnRatio', 'YOYEquity', 'YOYAsset', 'YOYNI', 'YOYEPSBasic', 'YOYPNI', 'currentRatio', 'quickRatio', 'cashRatio', 'YOYLiability', 'liabilityToAsset', 'assetToEquity', 'CAToAsset','NCAToAsset', 'tangibleAssetToAsset', 'ebitToInterest', 'CFOToOR', 'CFOToNP', 'CFOToGr','dupontROE', 'dupontAssetStoEquity', 'dupontAssetTurn', 'dupontPnitoni'] def down_data(self): '''下载数据''' valuation_df = pd.DataFrame(columns=['code', 'close', 'peTTM', 'pbMRQ', 'psTTM', 'pcfNcfTTM']) profit_df = pd.DataFrame( columns=['code', 'pubDate', 'statDate', 'roeAvg', 'npMargin', 'gpMargin', 'netProfit', 'epsTTM', 'MBRevenue', 'totalShare', 'liqaShare'] ) operation_df = pd.DataFrame( columns=['code', 'pubDate', 'statDate', 'NRTurnRatio', 'NRTurnDays', 'INVTurnRatio', 'INVTurnDays', 'CATurnRatio', 'AssetTurnRatio'] ) growth_df = pd.DataFrame( columns=['code', 'pubDate', 'statDate', 'YOYEquity', 'YOYAsset', 'YOYNI', 'YOYEPSBasic', 'YOYPNI'] ) balance_df = pd.DataFrame( columns=['code', 'pubDate', 'statDate', 'currentRatio', 'quickRatio', 'cashRatio', 'YOYLiability', 'liabilityToAsset', 'assetToEquity'] ) cash_flow_df = pd.DataFrame( columns=['code', 'pubDate', 'statDate', 'CAToAsset','NCAToAsset', 'tangibleAssetToAsset', 'ebitToInterest', 'CFOToOR', 'CFOToNP', 'CFOToGr'] ) dupont_df = pd.DataFrame( columns=['code', 'pubDate', 'statDate', 'dupontROE', 'dupontAssetStoEquity', 'dupontAssetTurn', 'dupontPnitoni'] ) bs.login() for code in self.code_list: # 下载估值指标 valuation_df = pd.concat([valuation_df, self.down_valuation(str(code))], ignore_index=True) # 下载季频盈利能力 profit_df = pd.concat([profit_df, self.down_profit(str(code))], ignore_index=True) # 下载季频营运能力 operation_df = pd.concat([operation_df, self.down_operation(str(code))], ignore_index=True) # 下载季频成长能力 growth_df = pd.concat([growth_df, self.down_growth(str(code))], ignore_index=True) # 下载季频偿债能力 balance_df = pd.concat([balance_df, self.down_balance(str(code))], ignore_index=True) # 下载季频现金流量 cash_flow_df = pd.concat([cash_flow_df, self.down_cash_flow(str(code))], ignore_index=True) # 下载杜邦指数 dupont_df = pd.concat([dupont_df, self.down_dupont(str(code))], ignore_index=True) bs.logout() all_data_list = [valuation_df, profit_df, operation_df, growth_df, balance_df, cash_flow_df, dupont_df] for d in all_data_list: self.df = self.merge_df(self.df, d) return self.df # 估值指标 def down_valuation(self, code, start='2023-6-2', end='2023-12-31'): results = bs.query_history_k_data_plus(code, 'code,close,peTTM,pbMRQ,psTTM,pcfNcfTTM', start_date=start, end_date=end, frequency="d", adjustflag="3") result_list = [] while (results.error_code == '0') & results.next(): result_list.append(results.get_row_data()) if result_list: print('下载{}估值指标成功!'.format(code), result_list) results_valuation = pd.DataFrame([result_list[-1]], columns=results.fields) return results_valuation # 季频盈利能力 def down_profit(self, code): profit_list = [] rs_profit = bs.query_profit_data(code, year=2023, quarter=1) while (rs_profit.error_code == '0') & rs_profit.next(): profit_list.append(rs_profit.get_row_data()) if profit_list: result_profit = pd.DataFrame([profit_list[-1]], columns=rs_profit.fields) print('下载{}季频盈利能力成功!'.format(code), profit_list) return result_profit # 季频营运能力 def down_operation(self, code): operation_list = [] rs_operation = bs.query_operation_data(code, year=2023, quarter=1) while (rs_operation.error_code == '0') & rs_operation.next(): operation_list.append(rs_operation.get_row_data()) if operation_list: result_operation = pd.DataFrame([operation_list[-1]], columns=rs_operation.fields) print('下载{}季频营运能力成功!'.format(code), operation_list) return result_operation # 季频成长能力 def down_growth(self, code): growth_list = [] rs_growth = bs.query_growth_data(code, year=2023, quarter=1) while (rs_growth.error_code == '0') & rs_growth.next(): growth_list.append(rs_growth.get_row_data()) if growth_list: result_growth = pd.DataFrame([growth_list[-1]], columns=rs_growth.fields) print('下载{}季频成长能力成功!'.format(code), growth_list) return result_growth # 季频偿债能力 def down_balance(self, code): balance_list = [] rs_balance = bs.query_balance_data(code, year=2023, quarter=1) while (rs_balance.error_code == '0') & rs_balance.next(): balance_list.append(rs_balance.get_row_data()) if balance_list: result_balance = pd.DataFrame([balance_list[-1]], columns=rs_balance.fields) print('下载{}季频偿债能力成功!'.format(code), balance_list) return result_balance # 季频现金流量 def down_cash_flow(self, code): cash_flow_list = [] rs_cash_flow = bs.query_cash_flow_data(code, year=2023, quarter=1) while (rs_cash_flow.error_code == '0') & rs_cash_flow.next(): cash_flow_list.append(rs_cash_flow.get_row_data()) if cash_flow_list: result_cash_flow = pd.DataFrame([cash_flow_list[-1]], columns=rs_cash_flow.fields) print('下载{}季频现金流量成功!'.format(code), cash_flow_list) return result_cash_flow # 杜邦指数 def down_dupont(self, code): dupont_list = [] rs_dupont = bs.query_dupont_data(code, year=2023, quarter=1) while (rs_dupont.error_code == '0') & rs_dupont.next(): dupont_list.append(rs_dupont.get_row_data()) if dupont_list: result_profit = pd.DataFrame([dupont_list[-1]], columns=rs_dupont.fields) print('下载{}杜邦指数成功!'.format(code), dupont_list) return result_profit # 合并表格 # 合并表格 # 合并表格 def merge_df(self, df1, df2): # 先删除可能会冲突的列 df1 = df1.drop(['pubDate', 'statDate'], axis=1, errors='ignore') df2 = df2.drop(['pubDate', 'statDate'], axis=1, errors='ignore') # 合并数据框,指定suffixes参数 all_df = pd.merge(df1, df2, on='code', suffixes=('_left', '_right')) return all_df # def merge_df(self, df1, df2): # all_df = pd.merge(df1, df2, on='code') # return all_df def to_file(self, df): df.to_csv('allStockData.csv', index=False) if __name__ == '__main__': stockdata = StockData() daf = stockdata.down_data() daf = daf.loc[:, stockdata.cols] stockdata.to_file(daf) |
近期评论