siat 3.10.132__py3-none-any.whl → 3.11.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. siat/__init__.py +0 -0
  2. siat/allin.py +8 -0
  3. siat/assets_liquidity.py +0 -0
  4. siat/beta_adjustment.py +0 -0
  5. siat/beta_adjustment_china.py +0 -0
  6. siat/blockchain.py +0 -0
  7. siat/bond.py +0 -0
  8. siat/bond_base.py +0 -0
  9. siat/bond_china.py +0 -0
  10. siat/bond_zh_sina.py +0 -0
  11. siat/capm_beta.py +0 -0
  12. siat/capm_beta2.py +4 -4
  13. siat/common.py +9 -6
  14. siat/compare_cross.py +0 -0
  15. siat/copyrights.py +0 -0
  16. siat/cryptocurrency.py +0 -0
  17. siat/economy.py +0 -0
  18. siat/economy2.py +0 -0
  19. siat/esg.py +0 -0
  20. siat/event_study.py +0 -0
  21. siat/exchange_bond_china.pickle +0 -0
  22. siat/fama_french.py +0 -0
  23. siat/fin_stmt2_yahoo.py +0 -0
  24. siat/financial_base.py +0 -0
  25. siat/financial_statements.py +0 -0
  26. siat/financials.py +0 -0
  27. siat/financials2.py +0 -0
  28. siat/financials_china.py +0 -0
  29. siat/financials_china2.py +0 -0
  30. siat/fund.py +0 -0
  31. siat/fund_china.pickle +0 -0
  32. siat/fund_china.py +0 -0
  33. siat/future_china.py +0 -0
  34. siat/google_authenticator.py +0 -0
  35. siat/grafix.py +55 -4
  36. siat/holding_risk.py +0 -0
  37. siat/luchy_draw.py +0 -0
  38. siat/market_china.py +0 -0
  39. siat/markowitz.py +0 -0
  40. siat/markowitz2.py +1 -0
  41. siat/markowitz2_20250704.py +0 -0
  42. siat/markowitz2_20250705.py +0 -0
  43. siat/markowitz_simple.py +0 -0
  44. siat/ml_cases.py +0 -0
  45. siat/ml_cases_example.py +0 -0
  46. siat/option_china.py +0 -0
  47. siat/option_pricing.py +0 -0
  48. siat/other_indexes.py +0 -0
  49. siat/risk_adjusted_return.py +0 -0
  50. siat/risk_adjusted_return2.py +8 -4
  51. siat/risk_evaluation.py +0 -0
  52. siat/risk_free_rate.py +0 -0
  53. siat/save2docx.py +345 -0
  54. siat/save2pdf.py +145 -0
  55. siat/sector_china.py +0 -0
  56. siat/security_price2.py +0 -0
  57. siat/security_prices.py +168 -6
  58. siat/security_trend.py +0 -0
  59. siat/security_trend2.py +2 -2
  60. siat/stock.py +11 -1
  61. siat/stock_advice_linear.py +0 -0
  62. siat/stock_base.py +0 -0
  63. siat/stock_china.py +0 -0
  64. siat/stock_info.pickle +0 -0
  65. siat/stock_prices_kneighbors.py +0 -0
  66. siat/stock_prices_linear.py +0 -0
  67. siat/stock_profile.py +0 -0
  68. siat/stock_technical.py +0 -0
  69. siat/stooq.py +0 -0
  70. siat/transaction.py +0 -0
  71. siat/translate.py +0 -0
  72. siat/valuation.py +0 -0
  73. siat/valuation_china.py +0 -0
  74. siat/var_model_validation.py +0 -0
  75. siat/yf_name.py +0 -0
  76. {siat-3.10.132.dist-info/licenses → siat-3.11.1.dist-info}/LICENSE +0 -0
  77. {siat-3.10.132.dist-info → siat-3.11.1.dist-info}/METADATA +234 -235
  78. siat-3.11.1.dist-info/RECORD +80 -0
  79. {siat-3.10.132.dist-info → siat-3.11.1.dist-info}/WHEEL +1 -1
  80. {siat-3.10.132.dist-info → siat-3.11.1.dist-info}/top_level.txt +0 -1
  81. build/lib/build/lib/siat/__init__.py +0 -75
  82. build/lib/build/lib/siat/allin.py +0 -137
  83. build/lib/build/lib/siat/assets_liquidity.py +0 -915
  84. build/lib/build/lib/siat/beta_adjustment.py +0 -1058
  85. build/lib/build/lib/siat/beta_adjustment_china.py +0 -548
  86. build/lib/build/lib/siat/blockchain.py +0 -143
  87. build/lib/build/lib/siat/bond.py +0 -2900
  88. build/lib/build/lib/siat/bond_base.py +0 -992
  89. build/lib/build/lib/siat/bond_china.py +0 -100
  90. build/lib/build/lib/siat/bond_zh_sina.py +0 -143
  91. build/lib/build/lib/siat/capm_beta.py +0 -783
  92. build/lib/build/lib/siat/capm_beta2.py +0 -887
  93. build/lib/build/lib/siat/common.py +0 -5360
  94. build/lib/build/lib/siat/compare_cross.py +0 -642
  95. build/lib/build/lib/siat/copyrights.py +0 -18
  96. build/lib/build/lib/siat/cryptocurrency.py +0 -667
  97. build/lib/build/lib/siat/economy.py +0 -1471
  98. build/lib/build/lib/siat/economy2.py +0 -1853
  99. build/lib/build/lib/siat/esg.py +0 -536
  100. build/lib/build/lib/siat/event_study.py +0 -815
  101. build/lib/build/lib/siat/fama_french.py +0 -1521
  102. build/lib/build/lib/siat/fin_stmt2_yahoo.py +0 -982
  103. build/lib/build/lib/siat/financial_base.py +0 -1160
  104. build/lib/build/lib/siat/financial_statements.py +0 -598
  105. build/lib/build/lib/siat/financials.py +0 -2339
  106. build/lib/build/lib/siat/financials2.py +0 -1278
  107. build/lib/build/lib/siat/financials_china.py +0 -4433
  108. build/lib/build/lib/siat/financials_china2.py +0 -2212
  109. build/lib/build/lib/siat/fund.py +0 -629
  110. build/lib/build/lib/siat/fund_china.py +0 -3307
  111. build/lib/build/lib/siat/future_china.py +0 -551
  112. build/lib/build/lib/siat/google_authenticator.py +0 -47
  113. build/lib/build/lib/siat/grafix.py +0 -3636
  114. build/lib/build/lib/siat/holding_risk.py +0 -867
  115. build/lib/build/lib/siat/luchy_draw.py +0 -638
  116. build/lib/build/lib/siat/market_china.py +0 -1168
  117. build/lib/build/lib/siat/markowitz.py +0 -2363
  118. build/lib/build/lib/siat/markowitz2.py +0 -3150
  119. build/lib/build/lib/siat/markowitz2_20250704.py +0 -2969
  120. build/lib/build/lib/siat/markowitz2_20250705.py +0 -3158
  121. build/lib/build/lib/siat/markowitz_simple.py +0 -373
  122. build/lib/build/lib/siat/ml_cases.py +0 -2291
  123. build/lib/build/lib/siat/ml_cases_example.py +0 -60
  124. build/lib/build/lib/siat/option_china.py +0 -3069
  125. build/lib/build/lib/siat/option_pricing.py +0 -1925
  126. build/lib/build/lib/siat/other_indexes.py +0 -409
  127. build/lib/build/lib/siat/risk_adjusted_return.py +0 -1576
  128. build/lib/build/lib/siat/risk_adjusted_return2.py +0 -1900
  129. build/lib/build/lib/siat/risk_evaluation.py +0 -2218
  130. build/lib/build/lib/siat/risk_free_rate.py +0 -351
  131. build/lib/build/lib/siat/sector_china.py +0 -4140
  132. build/lib/build/lib/siat/security_price2.py +0 -727
  133. build/lib/build/lib/siat/security_prices.py +0 -3408
  134. build/lib/build/lib/siat/security_trend.py +0 -402
  135. build/lib/build/lib/siat/security_trend2.py +0 -646
  136. build/lib/build/lib/siat/stock.py +0 -4284
  137. build/lib/build/lib/siat/stock_advice_linear.py +0 -934
  138. build/lib/build/lib/siat/stock_base.py +0 -26
  139. build/lib/build/lib/siat/stock_china.py +0 -2095
  140. build/lib/build/lib/siat/stock_prices_kneighbors.py +0 -910
  141. build/lib/build/lib/siat/stock_prices_linear.py +0 -386
  142. build/lib/build/lib/siat/stock_profile.py +0 -707
  143. build/lib/build/lib/siat/stock_technical.py +0 -3305
  144. build/lib/build/lib/siat/stooq.py +0 -74
  145. build/lib/build/lib/siat/transaction.py +0 -347
  146. build/lib/build/lib/siat/translate.py +0 -5183
  147. build/lib/build/lib/siat/valuation.py +0 -1378
  148. build/lib/build/lib/siat/valuation_china.py +0 -2076
  149. build/lib/build/lib/siat/var_model_validation.py +0 -444
  150. build/lib/build/lib/siat/yf_name.py +0 -811
  151. build/lib/siat/__init__.py +0 -75
  152. build/lib/siat/allin.py +0 -137
  153. build/lib/siat/assets_liquidity.py +0 -915
  154. build/lib/siat/beta_adjustment.py +0 -1058
  155. build/lib/siat/beta_adjustment_china.py +0 -548
  156. build/lib/siat/blockchain.py +0 -143
  157. build/lib/siat/bond.py +0 -2900
  158. build/lib/siat/bond_base.py +0 -992
  159. build/lib/siat/bond_china.py +0 -100
  160. build/lib/siat/bond_zh_sina.py +0 -143
  161. build/lib/siat/capm_beta.py +0 -783
  162. build/lib/siat/capm_beta2.py +0 -887
  163. build/lib/siat/common.py +0 -5360
  164. build/lib/siat/compare_cross.py +0 -642
  165. build/lib/siat/copyrights.py +0 -18
  166. build/lib/siat/cryptocurrency.py +0 -667
  167. build/lib/siat/economy.py +0 -1471
  168. build/lib/siat/economy2.py +0 -1853
  169. build/lib/siat/esg.py +0 -536
  170. build/lib/siat/event_study.py +0 -815
  171. build/lib/siat/fama_french.py +0 -1521
  172. build/lib/siat/fin_stmt2_yahoo.py +0 -982
  173. build/lib/siat/financial_base.py +0 -1160
  174. build/lib/siat/financial_statements.py +0 -598
  175. build/lib/siat/financials.py +0 -2339
  176. build/lib/siat/financials2.py +0 -1278
  177. build/lib/siat/financials_china.py +0 -4433
  178. build/lib/siat/financials_china2.py +0 -2212
  179. build/lib/siat/fund.py +0 -629
  180. build/lib/siat/fund_china.py +0 -3307
  181. build/lib/siat/future_china.py +0 -551
  182. build/lib/siat/google_authenticator.py +0 -47
  183. build/lib/siat/grafix.py +0 -3636
  184. build/lib/siat/holding_risk.py +0 -867
  185. build/lib/siat/luchy_draw.py +0 -638
  186. build/lib/siat/market_china.py +0 -1168
  187. build/lib/siat/markowitz.py +0 -2363
  188. build/lib/siat/markowitz2.py +0 -3150
  189. build/lib/siat/markowitz2_20250704.py +0 -2969
  190. build/lib/siat/markowitz2_20250705.py +0 -3158
  191. build/lib/siat/markowitz_simple.py +0 -373
  192. build/lib/siat/ml_cases.py +0 -2291
  193. build/lib/siat/ml_cases_example.py +0 -60
  194. build/lib/siat/option_china.py +0 -3069
  195. build/lib/siat/option_pricing.py +0 -1925
  196. build/lib/siat/other_indexes.py +0 -409
  197. build/lib/siat/risk_adjusted_return.py +0 -1576
  198. build/lib/siat/risk_adjusted_return2.py +0 -1900
  199. build/lib/siat/risk_evaluation.py +0 -2218
  200. build/lib/siat/risk_free_rate.py +0 -351
  201. build/lib/siat/sector_china.py +0 -4140
  202. build/lib/siat/security_price2.py +0 -727
  203. build/lib/siat/security_prices.py +0 -3408
  204. build/lib/siat/security_trend.py +0 -402
  205. build/lib/siat/security_trend2.py +0 -646
  206. build/lib/siat/stock.py +0 -4284
  207. build/lib/siat/stock_advice_linear.py +0 -934
  208. build/lib/siat/stock_base.py +0 -26
  209. build/lib/siat/stock_china.py +0 -2095
  210. build/lib/siat/stock_prices_kneighbors.py +0 -910
  211. build/lib/siat/stock_prices_linear.py +0 -386
  212. build/lib/siat/stock_profile.py +0 -707
  213. build/lib/siat/stock_technical.py +0 -3305
  214. build/lib/siat/stooq.py +0 -74
  215. build/lib/siat/transaction.py +0 -347
  216. build/lib/siat/translate.py +0 -5183
  217. build/lib/siat/valuation.py +0 -1378
  218. build/lib/siat/valuation_china.py +0 -2076
  219. build/lib/siat/var_model_validation.py +0 -444
  220. build/lib/siat/yf_name.py +0 -811
  221. siat-3.10.132.dist-info/RECORD +0 -218
@@ -1,4433 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- 本模块功能:计算财务报表比例,应用层,仅用于中国大陆上市的企业
4
- 所属工具包:证券投资分析工具SIAT
5
- SIAT:Security Investment Analysis Tool
6
- 创建日期:2020年9月8日
7
- 最新修订日期:2024年4月21日
8
- 作者:王德宏 (WANG Dehong, Peter)
9
- 作者单位:北京外国语大学国际商学院
10
- 作者邮件:wdehong2000@163.com
11
- 版权所有:王德宏
12
- 用途限制:仅限研究与教学使用,不可商用!商用需要额外授权。
13
- 特别声明:作者不对使用本工具进行证券投资导致的任何损益负责!
14
- """
15
- #==============================================================================
16
- #关闭所有警告
17
- import warnings; warnings.filterwarnings('ignore')
18
- #==============================================================================
19
- #本模块的公共引用
20
- from siat.common import *
21
- from siat.translate import *
22
- from siat.grafix import *
23
- from siat.beta_adjustment_china import *
24
- from siat.financials_china2 import *
25
- #==============================================================================
26
- import matplotlib.pyplot as plt
27
-
28
- #处理绘图汉字乱码问题
29
- import sys; czxt=sys.platform
30
- if czxt in ['win32','win64']:
31
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置默认字体
32
- mpfrc={'font.family': 'SimHei'}
33
-
34
- if czxt in ['darwin']: #MacOSX
35
- plt.rcParams['font.family']= ['Heiti TC']
36
- mpfrc={'font.family': 'Heiti TC'}
37
-
38
- if czxt in ['linux']: #website Jupyter
39
- plt.rcParams['font.family']= ['Heiti TC']
40
- mpfrc={'font.family':'Heiti TC'}
41
-
42
- # 解决保存图像时'-'显示为方块的问题
43
- plt.rcParams['axes.unicode_minus'] = False
44
- #==============================================================================
45
- import pandas as pd
46
- import akshare as ak
47
-
48
- #SUFFIX_LIST_CN=['SS','SZ','BJ','NQ']
49
- #==============================================================================
50
- #==============================================================================
51
- if __name__=='__main__':
52
- ticker="603589.SS"
53
- ticker='002415.SZ'
54
-
55
- ticker='000002.SZ'
56
- ticker='601398.SS'
57
- ticker='600791.SS'
58
-
59
- ticker="601375.SS"
60
- ticker="600305.SS"
61
-
62
- akfs=get_fin_stmt_ak(ticker)
63
-
64
- def get_fin_stmt_ak(ticker):
65
- """
66
- 从akshare获取财务报表数据,合成df,限于中国A股
67
- 获取的项目:所有原始项目
68
- 注意:抓取所有的报表,不过滤日期
69
-
70
- 注意:需要akshare版本1.9.59及以上,字段有变化
71
- """
72
- print(" Searching financial statements for",ticker,"...")
73
- #是否中国股票
74
- result,prefix,suffix=split_prefix_suffix(ticker)
75
- if not (suffix in SUFFIX_LIST_CN):
76
- print(" #Error(get_fin_stmt_ak): not a stock in China",ticker)
77
- return None
78
-
79
- #抓取三大报表
80
- import akshare as ak
81
- import time
82
- try:
83
- fbs = ak.stock_financial_report_sina(stock=prefix, symbol="资产负债表")
84
- except:
85
- time.sleep(3)
86
- try:
87
- fbs = ak.stock_financial_report_sina(stock=prefix, symbol="资产负债表")
88
- except:
89
- print(" #Error(get_fin_stmt_ak): balance sheet currently inaccessible for",ticker,'\b, try later')
90
- return None
91
-
92
- try:
93
- fis = ak.stock_financial_report_sina(stock=prefix, symbol="利润表")
94
- except:
95
- time.sleep(3)
96
- try:
97
- fis = ak.stock_financial_report_sina(stock=prefix, symbol="利润表")
98
- except:
99
- print(" #Error(get_fin_stmt_ak): income statement currently inaccessible for",ticker,'\b, try later')
100
- return None
101
-
102
- try:
103
- fcf = ak.stock_financial_report_sina(stock=prefix, symbol="现金流量表")
104
- except:
105
- time.sleep(3)
106
- try:
107
- fcf = ak.stock_financial_report_sina(stock=prefix, symbol="现金流量表")
108
- except:
109
- print(" #Error(get_fin_stmt_ak): cashflow statement currently inaccessible for",ticker,'\b, try later')
110
- return None
111
-
112
- #若报表为空,则返回
113
- if fbs is None:
114
- print(" #Warning(get_fin_stmt_ak): balance sheets inaccessible for",ticker)
115
- return None
116
- if fis is None:
117
- print(" #Warning(get_fin_stmt_ak): income statements inaccessible for",ticker)
118
- return None
119
- if fcf is None:
120
- print(" #Warning(get_fin_stmt_ak): cash flow statements inaccessible for",ticker)
121
- return None
122
-
123
- #若报表无数据,则返回
124
- if len(fbs)==0:
125
- print(" #Warning(get_fin_stmt_ak): zero record of balance sheets found for",ticker)
126
- return None
127
- if len(fis)==0:
128
- print(" #Warning(get_fin_stmt_ak): zero record of income statements found for",ticker)
129
- return None
130
- if len(fcf)==0:
131
- print(" #Warning(get_fin_stmt_ak): zero record of cash flow found for",ticker)
132
- return None
133
-
134
- # 报告日/报表日期
135
- rptdate=list(fbs)[0] #报告日
136
-
137
- fbs1=fbs.drop_duplicates(subset=[rptdate],keep='first')
138
- fbs1.sort_values(by=[rptdate],ascending=True,inplace=True)
139
- fbs1['date']=pd.to_datetime(fbs1[rptdate])
140
- fbs1.set_index('date',inplace=True)
141
-
142
- fis1=fis.drop_duplicates(subset=[rptdate],keep='first')
143
- fis1.sort_values(by=[rptdate],ascending=True,inplace=True)
144
- fis1['date']=pd.to_datetime(fis1[rptdate])
145
- fis1.set_index('date',inplace=True)
146
-
147
- dropcollist=['报告日','数据源','公告日期','更新日期']
148
- try:
149
- fis1.drop(labels=dropcollist,axis=1,inplace=True)
150
- except:
151
- pass
152
-
153
- fcf1=fcf.drop_duplicates(subset=[rptdate],keep='first')
154
- fcf1.sort_values(by=[rptdate],ascending=True,inplace=True)
155
- fcf1['date']=pd.to_datetime(fcf1[rptdate])
156
- fcf1.set_index('date',inplace=True)
157
- try:
158
- fcf1.drop(labels=dropcollist,axis=1,inplace=True)
159
- except:
160
- pass
161
-
162
- #合成:内连接
163
- fs1=pd.merge(fbs1,fis1,how='inner',left_index=True,right_index=True)
164
- fs2=pd.merge(fs1,fcf1,how='inner',left_index=True,right_index=True)
165
-
166
- if len(fs2) == 0:
167
- print(" #Warning(get_fin_stmt_ak): zero reports found for",ticker)
168
- return None
169
- #按照日期升序排序
170
- fs2.sort_index(inplace=True)
171
-
172
- #数据清洗:删除因两次合并可能产生的重复列
173
- dup_col_list=[]
174
- for c in fs2.columns:
175
- if '_y' in c:
176
- dup_col_list=dup_col_list+[c]
177
-
178
- c2=c[:-2]
179
- c2y=c2+'_x'
180
- fs2.rename(columns={c2y:c2},inplace=True)
181
-
182
- fs2.drop(labels= dup_col_list, axis=1, inplace=True)
183
-
184
- #数据清洗:将空值替换为0
185
- fs3=fs2.fillna('0')
186
-
187
- #数据清洗:转换数值类型
188
- for i in fs3.columns:
189
- try:
190
- fs3[i]=fs3[i].astype('float')
191
- except:
192
- pass
193
-
194
- fs3['ticker']=ticker
195
- fs3['endDate']=fs3.index.strftime('%Y-%m-%d')
196
-
197
- # 删除重复的列
198
- fs3t=fs3.T
199
- fs3t['check']=fs3t.index #给每行增加不同,防止drop_duplicates误删除
200
- fs3td=fs3t.drop_duplicates(subset=['check'],keep='first')
201
-
202
- # 删除含有check的行和check列
203
- #fs3td1=fs3td[fs3td.index != 'check']
204
- fs3td.drop("check", axis=1, inplace=True)
205
-
206
- fs4=fs3td.T
207
- """
208
- fs4=fs3.T.drop_duplicates(keep='first').T
209
- """
210
-
211
- fs4.fillna(0,inplace=True)
212
- #以下专门针对银行业报表
213
- import numpy as np
214
- fslist=list(fs4)
215
-
216
- #银行资产负债表
217
- if not ('货币资金' in fslist):
218
- fs4['货币资金']=fs4['现金及存放中央银行款项']
219
-
220
- if not ('预收款项' in fslist):
221
- fs4['预收款项']=fs4['预收账款']
222
-
223
- if not ('实收资本(或股本)' in fslist):
224
- if '股本' in fslist:
225
- fs4['实收资本(或股本)']=fs4['股本']
226
- if '实收资本净额' in fslist:
227
- fs4['实收资本(或股本)']=fs4['实收资本净额']
228
-
229
-
230
- if not ('流动资产合计' in fslist):
231
- if '其他资产' in fslist:
232
- fs4['流动资产合计']=fs4['资产总计']-fs4['固定资产合计']-fs4['无形资产']-fs4['商誉']-fs4['递延税款借项']-fs4['其他资产']
233
- else:
234
- fs4['流动资产合计']=fs4['资产总计']-fs4['固定资产合计']-fs4['无形资产']-fs4['商誉']-fs4['递延税款借项']
235
-
236
- if not ('速动资产合计' in fslist):
237
- if not('存货' in fslist) and ('存货净额' in fslist):
238
- fs4['存货']=fs4['存货净额']
239
-
240
- fs4['速动资产合计']=fs4['流动资产合计']-fs4['存货']
241
-
242
- if not ('流动负债合计' in fslist):
243
- if not ('应付债券' in fslist) and ('应付债券款' in fslist):
244
- fs4['应付债券']=fs4['应付债券款']
245
-
246
- if not ('递延所得税负债' in fslist) and ('递延税款贷项' in fslist):
247
- fs4['递延所得税负债']=fs4['递延税款贷项']
248
-
249
- if '其他负债' in fslist:
250
- fs4['流动负债合计']=fs4['负债合计']-fs4['应付债券']-fs4['递延所得税负债']-fs4['其他负债']
251
- else:
252
- fs4['流动负债合计']=fs4['负债合计']-fs4['应付债券']-fs4['递延所得税负债']
253
-
254
- #银行利润表
255
- if not ('营业总收入' in fslist):
256
- fs4['营业总收入']=fs4['营业收入']+fs4['加:营业外收入']
257
-
258
- if not ('营业成本' in fslist):
259
- fs4['营业成本']=fs4['营业支出']
260
-
261
- if not ('营业总成本' in fslist):
262
- fs4['营业总成本']=fs4['营业成本']+fs4['减:营业外支出']
263
-
264
- if not ('归母净利润' in fslist):
265
- for gm in ['归属于母公司的净利润','归属于母公司所有者的净利润','归属于母公司所有者的综合收益总额']:
266
- try:
267
- fs4['归母净利润']=fs4[gm]
268
- break
269
- except:
270
- continue
271
-
272
- if not ('销售费用' in fslist):
273
- fs4['销售费用']=0
274
-
275
- if not ('管理费用' in fslist):
276
- fs4['管理费用']=fs4['业务及管理费用']
277
-
278
- #银行现金流量表
279
- if not ('经营活动现金流入' in fslist):
280
- fs4['经营活动现金流入']= fs4['经营活动现金流入小计']
281
-
282
- if not ('经营活动现金流出' in fslist):
283
- fs4['经营活动现金流出']= fs4['经营活动现金流出小计']
284
-
285
- if not ('经营活动现金流净额' in fslist):
286
- fs4['经营活动现金流净额']= fs4['经营活动产生的现金流量净额']
287
-
288
- #现金分析比率
289
- if not ('销售现金比率%' in fslist):
290
- #fs4['销售现金比率%']=round((fs4['经营活动现金流入'] / fs4['营业总收入'])*100,2)
291
- fs4['销售现金比率%']=fs4.apply(lambda x: round((x['经营活动现金流入'] / x['营业总收入'])*100,2) if x['营业总收入'] !=0 else np.nan,axis=1)
292
-
293
- if not ('现金购销比率%' in fslist):
294
- #fs4['现金购销比率%']=round((fs4['经营活动现金流出'] / fs4['经营活动现金流入'])*100,2)
295
- fs4['现金购销比率%']=fs4.apply(lambda x: round((x['经营活动现金流出'] / x['经营活动现金流入'])*100,2) if x['经营活动现金流入'] !=0 else np.nan,axis=1)
296
-
297
- if not ('营业现金回笼率%' in fslist):
298
- #fs4['营业现金回笼率%']=round((fs4['经营活动现金流入'] / fs4['营业总收入'])*100,2)
299
- fs4['营业现金回笼率%']=fs4.apply(lambda x: round((x['经营活动现金流入'] / x['营业总收入'])*100,2) if x['营业总收入'] !=0 else np.nan,axis=1)
300
-
301
- if not ('短期现金偿债能力%' in fslist):
302
- #fs4['短期现金偿债能力%']=round((fs4['经营活动现金流净额'] / fs4['流动负债合计'])*100,2)
303
- try:
304
- fs4['短期现金偿债能力%']=fs4.apply(lambda x: round((x['经营活动现金流净额'] / x['流动负债合计'])*100,2) if x['流动负债合计'] !=0 else np.nan,axis=1)
305
- except:
306
- fs4['短期现金偿债能力%']=np.nan
307
-
308
- if not ('长期现金偿债能力%' in fslist):
309
- #fs4['长期现金偿债能力%']=round((fs4['经营活动现金流净额'] / fs4['负债合计'])*100,2)
310
- try:
311
- fs4['长期现金偿债能力%']=fs4.apply(lambda x: round((x['经营活动现金流净额'] / x['负债合计'])*100,2) if x['负债合计'] !=0 else np.nan,axis=1)
312
- except:
313
- fs4['长期现金偿债能力%']==np.nan
314
-
315
- if not ('流通股股数' in fslist):
316
- for gs in ['实收资本(或股本)','股本']:
317
- try:
318
- fs4['流通股股数']=fs4[gs]
319
- break
320
- except:
321
- continue
322
-
323
- if not ('现金支付股利能力(元)' in fslist):
324
- #fs4['现金支付股利能力(元)']=round((fs4['经营活动现金流净额'] / fs4['流通股股数'])*100,2)
325
- fs4['现金支付股利能力(元)']=fs4.apply(lambda x: round((x['经营活动现金流净额'] / x['流通股股数'])*100,2) if x['流通股股数'] !=0 else np.nan,axis=1)
326
-
327
- if not ('所有者权益合计' in fslist):
328
- for loe in ['资产总计','资产合计']:
329
- try:
330
- fs4['所有者权益合计']=fs4[loe] - fs4['负债合计']
331
- break
332
- except:
333
- continue
334
-
335
- if not ('现金综合支付能力%' in fslist):
336
- #fs4['现金综合支付能力%']=round((fs4['经营活动现金流净额'] / fs4['所有者权益合计'])*100,2)
337
- fs4['现金综合支付能力%']=fs4.apply(lambda x: round((x['经营活动现金流净额'] / x['所有者权益合计'])*100,2) if x['所有者权益合计'] !=0 else np.nan,axis=1)
338
-
339
- if not ('支付给(为)职工支付的现金' in fslist):
340
- fs4['支付给(为)职工支付的现金']=fs4['支付给职工以及为职工支付的现金']
341
-
342
- if not ('支付给职工的现金比率%' in fslist):
343
- #fs4['支付给职工的现金比率%']=round((fs4['支付给(为)职工支付的现金'] / fs4['经营活动现金流入'])*100,2)
344
- fs4['支付给职工的现金比率%']=fs4.apply(lambda x: round((x['支付给(为)职工支付的现金'] / x['经营活动现金流入'])*100,2) if x['经营活动现金流入'] !=0 else np.nan,axis=1)
345
-
346
- if not ('盈利现金比率%' in fslist):
347
- #fs4['盈利现金比率%']=round((fs4['经营活动现金流净额'] / fs4['净利润'])*100,2)
348
- fs4['盈利现金比率%']=fs4.apply(lambda x: round((x['经营活动现金流净额'] / x['净利润'])*100,2) if x['净利润'] !=0 else np.nan,axis=1)
349
-
350
- if not ('现金流入流出比率%' in fslist):
351
- #fs4['现金流入流出比率%']=round((fs4['经营活动现金流入'] / fs4['经营活动现金流出'])*100,2)
352
- fs4['现金流入流出比率%']=fs4.apply(lambda x: round((x['经营活动现金流入'] / x['经营活动现金流出'])*100,2) if x['经营活动现金流出'] !=0 else np.nan,axis=1)
353
-
354
- if not ('资产现金回收率%' in fslist):
355
- #fs4['资产现金回收率%']=round((fs4['经营活动现金流净额'] / fs4['资产总计'])*100,2)
356
- fs4['资产现金回收率%']=fs4.apply(lambda x: round((x['经营活动现金流净额'] / x['资产总计'])*100,2) if x['资产总计'] !=0 else np.nan,axis=1)
357
-
358
-
359
- return fs4
360
-
361
- """
362
- ['流动资产', '货币资金', '交易性金融资产', '衍生金融资产', '应收票据及应收账款',
363
- '应收票据', '应收账款', '应收款项融资', '预付款项', '其他应收款(合计)', '应收利息',
364
- '应收股利', '其他应收款', '买入返售金融资产', '存货', '划分为持有待售的资产',
365
- '一年内到期的非流动资产', '待摊费用', '待处理流动资产损益', '其他流动资产',
366
- '流动资产合计',
367
- '非流动资产', '发放贷款及垫款', '可供出售金融资产', '持有至到期投资', '长期应收款',
368
- '长期股权投资', '投资性房地产', '在建工程(合计)', '在建工程', '工程物资',
369
- '固定资产及清理(合计)', '固定资产净额', '固定资产清理', '生产性生物资产',
370
- '公益性生物资产', '油气资产', '使用权资产', '无形资产', '开发支出', '商誉',
371
- '长期待摊费用', '递延所得税资产', '其他非流动资产',
372
- '非流动资产合计',
373
- '资产总计',
374
- '流动负债', '短期借款', '交易性金融负债', '应付票据及应付账款', '应付票据',
375
- '应付账款', '预收款项', '应付手续费及佣金', '应付职工薪酬', '应交税费',
376
- '其他应付款(合计)', '应付利息', '应付股利', '其他应付款', '预提费用', '一年内的递延收益',
377
- '应付短期债券', '一年内到期的非流动负债', '其他流动负债',
378
- '流动负债合计',
379
- '非流动负债', '长期借款', '应付债券', '租赁负债', '长期应付职工薪酬', '长期应付款(合计)',
380
- '长期应付款', '专项应付款', '预计非流动负债', '递延所得税负债', '长期递延收益',
381
- '其他非流动负债',
382
- '非流动负债合计',
383
- '负债合计',
384
- '所有者权益', '实收资本(或股本)', '资本公积', '减:库存股', '其他综合收益',
385
- '专项储备', '盈余公积', '一般风险准备', '未分配利润', '归属于母公司股东权益合计',
386
- '所有者权益(或股东权益)合计',
387
- '负债和所有者权益(或股东权益)总计',
388
-
389
- '营业总收入', '营业收入',
390
- '营业总成本', '营业成本', '营业税金及附加', '销售费用', '管理费用', '研发费用',
391
- '资产减值损失', '公允价值变动收益', '投资收益', '其中:对联营企业和合营企业的投资收益',
392
- '汇兑收益',
393
- '营业利润', '加:营业外收入', '减:营业外支出', '其中:非流动资产处置损失',
394
- '四、利润总额', '减:所得税费用',
395
- '净利润', '归属于母公司所有者的净利润', '少数股东损益',
396
- '每股收益', '基本每股收益(元/股)', '稀释每股收益(元/股)',
397
- '其他综合收益',
398
- '综合收益总额', '归属于母公司所有者的综合收益总额', '归属于少数股东的综合收益总额',
399
- '报表日期', '单位',
400
-
401
- '经营活动产生的现金流量', '销售商品、提供劳务收到的现金', '收到的税费返还',
402
- '收到的其他与经营活动有关的现金', '经营活动现金流入小计', '购买商品、接受劳务支付的现金',
403
- '支付给职工以及为职工支付的现金', '支付的各项税费', '支付的其他与经营活动有关的现金',
404
- '经营活动现金流出小计', '经营活动产生的现金流量净额',
405
- '投资活动产生的现金流量', '收回投资所收到的现金', '取得投资收益所收到的现金',
406
- '处置固定资产、无形资产和其他长期资产所收回的现金净额',
407
- '处置子公司及其他营业单位收到的现金净额', '收到的其他与投资活动有关的现金',
408
- '投资活动现金流入小计',
409
- '购建固定资产、无形资产和其他长期资产所支付的现金', '投资所支付的现金',
410
- '取得子公司及其他营业单位支付的现金净额', '支付的其他与投资活动有关的现金',
411
- '投资活动现金流出小计',
412
- '投资活动产生的现金流量净额',
413
- '筹资活动产生的现金流量', '吸收投资收到的现金', '其中:子公司吸收少数股东投资收到的现金',
414
- '取得借款收到的现金', '发行债券收到的现金', '收到其他与筹资活动有关的现金',
415
- '筹资活动现金流入小计',
416
- '偿还债务支付的现金', '分配股利、利润或偿付利息所支付的现金',
417
- '其中:子公司支付给少数股东的股利、利润', '支付其他与筹资活动有关的现金',
418
- '筹资活动现金流出小计',
419
- '筹资活动产生的现金流量净额',
420
- '汇率变动对现金及现金等价物的影响',
421
- '现金及现金等价物净增加额', '加:期初现金及现金等价物余额',
422
- '期末现金及现金等价物余额',
423
-
424
- '附注',
425
- '净利润',
426
- '未确认的投资损失', '资产减值准备',
427
- '固定资产折旧、油气资产折耗、生产性物资折旧', '无形资产摊销', '长期待摊费用摊销',
428
- '待摊费用的减少', '预提费用的增加', '处置固定资产、无形资产和其他长期资产的损失',
429
- '固定资产报废损失', '公允价值变动损失', '递延收益增加(减:减少)', '预计负债',
430
- '投资损失', '递延所得税资产减少', '递延所得税负债增加', '存货的减少',
431
- '经营性应收项目的减少', '经营性应付项目的增加', '已完工尚未结算款的减少(减:增加)',
432
- '已结算尚未完工款的增加(减:减少)',
433
- '其他',
434
- '经营活动产生现金流量净额',
435
- '债务转为资本', '一年内到期的可转换公司债券', '融资租入固定资产',
436
- '现金的期末余额', '现金的期初余额',
437
- '现金等价物的期末余额', '现金等价物的期初余额',
438
- '现金及现金等价物的净增加额',
439
-
440
- 'ticker',
441
- 'endDate']
442
- """
443
-
444
- if __name__=='__main__':
445
- fstmt=get_fin_stmt_ak('600519.SS')
446
-
447
- #==============================================================================
448
- if __name__=='__main__':
449
- endDate="2020-12-31"
450
- top=10
451
-
452
- def liability_rank_china(endDate='latest',top=5):
453
- """
454
- 获得某个报表日期资产负债率排行榜,限于中国A股
455
- 获取的项目:所有原始项目
456
- 注意:
457
- """
458
- error_flag=False
459
-
460
- #获取最近的报表日期
461
- if endDate == 'latest':
462
- import datetime; endDate=datetime.date.today()
463
- else:
464
- #检查日期
465
- valid_date=check_date(endDate)
466
- if not valid_date:
467
- error_flag=True
468
- print(" #Error(liability_rank_china): invalid date",endDate)
469
- if error_flag: return None
470
-
471
- start=date_adjust(endDate, adjust=-365)
472
- fs_dates=cvt_fs_dates(start,endDate,'all')
473
- endDate=fs_dates[-1:][0]
474
-
475
- #获取A股名单:代码,简称
476
- print(" Searching assets info of all A shares, it may take several hours ...")
477
- import akshare as ak
478
- a_shares= ak.stock_info_a_code_name()
479
- a_len=len(a_shares)
480
- print('\b'*99,' Collected China A-share stocks, altogether',a_len)
481
-
482
- #遍历所有A股上市公司的资产负债表,计算资产负债率
483
- a_share_codes=list(a_shares['code'])
484
- liab_df=pd.DataFrame()
485
- for t in a_share_codes:
486
- #抓取资产负债表
487
- try:
488
- #df= ak.stock_financial_report_sina(stock=t, symbol="资产负债表")
489
- # 上述命令运行一定次数(不到300次)后出错,无法继续!
490
- df = ak.stock_financial_analysis_indicator(stock=t)
491
- except:
492
- print(" #Warning(liability_rank_china): failed to get liability info of",t)
493
- continue
494
- sub1=df[df.index == endDate]
495
- sub2=sub1[['资产负债率(%)']]
496
- sub2['股票代码']=t
497
- try:
498
- liab_df=liab_df.append(sub2)
499
- except:
500
- liab_df=liab_df._append(sub2)
501
-
502
- #显示进度
503
- print_progress_percent2(t,a_share_codes,steps=10,leading_blanks=4)
504
-
505
-
506
- #获取全体股票的业绩报表,指定截止日期
507
- print('\b'*99," Retrieved financial info of",len(liab_df),"stocks ended on",endDate)
508
- #转换日期格式
509
- ak_date=convert_date_ts(endDate)
510
- a_share_perf= ak.stock_em_yjbb(date=ak_date)
511
- a_share_industry=a_share_perf[['股票代码','股票简称','所处行业']]
512
-
513
- #合成
514
- liab_df['资产负债率(%)']=round(liab_df['资产负债率(%)'].astype('float'),2)
515
- alr_info_tmp=pd.merge(liab_df,a_share_industry,how='left',on='股票代码')
516
- alr_cols=['股票简称','股票代码','资产负债率(%)','所处行业']
517
- alr_info=alr_info_tmp[alr_cols]
518
-
519
- #后续处理:排序,找出资产负债率最低、最高的企业和行业
520
- alr_info.sort_values(by=['资产负债率(%)'],ascending=True,inplace=True)
521
- firm_top_lowest=alr_info.head(top)
522
- alr_info.sort_values(by=['资产负债率(%)'],ascending=False,inplace=True)
523
- firm_top_highest=alr_info.head(top)
524
-
525
- agg_cols={'资产负债率(%)':['mean','median']}
526
- group_df=alr_info.groupby('所处行业').agg(agg_cols)
527
- group_df.columns=['均值%','中位数%']
528
- group_df['均值%']=round(group_df['均值%'],2)
529
- group_df['中位数%']=round(group_df['中位数%'],2)
530
-
531
- group_df.sort_values(by=['均值%'],ascending=True,inplace=True)
532
- industry_lowest=group_df.head(top5)
533
- group_df.sort_values(by=['均值%'],ascending=False,inplace=True)
534
- industry_highest=group_df.head(top5)
535
-
536
-
537
- #设置打印对齐
538
- pd.set_option('display.max_columns', 1000)
539
- pd.set_option('display.width', 1000)
540
- pd.set_option('display.max_colwidth', 1000)
541
- pd.set_option('display.unicode.ambiguous_as_wide', True)
542
- pd.set_option('display.unicode.east_asian_width', True)
543
-
544
- #打印:负债率最低最高的企业
545
- print("\n=== 企业排名:资产负债率,前"+str(top)+"名最低,截止"+endDate+" ===")
546
- print(firm_top_lowest.to_string(index=False))
547
- print("\n=== 企业排名:资产负债率,前"+str(top)+"名最高,截止"+endDate+" ===")
548
- print(firm_top_highest.to_string(index=False))
549
-
550
- #打印:负债率最低最高的行业
551
- print("\n=== 行业排名:资产负债率,前"+str(top)+"名最低,截止"+endDate+" ===")
552
- print(industry_top_lowest.to_string(index=False))
553
- print("\n=== 行业排名:资产负债率,前"+str(top)+"名最高,截止"+endDate+" ===")
554
- print(industry_top_highest.to_string(index=False))
555
-
556
- import datetime; today=datetime.date.today()
557
- print("\n*** 数据来源:sina/EM,"+str(today))
558
-
559
- return alr_info,group_df
560
-
561
- if __name__=='__main__':
562
- liability_rank_china(endDate='2020-12-31')
563
- #==============================================================================
564
- if __name__=='__main__':
565
- ticker='603589.SS'
566
- start='2018-1-1'
567
- end='2022-12-31'
568
- period_type='all'
569
-
570
- def calc_dupont_china(ticker,start,end,period_type='all'):
571
- """
572
- 功能:计算股票ticker的杜邦分析项目,基于财报期末数直接计算,仅限于中国A股
573
- """
574
- fsr2=get_fin_stmt_ak(ticker)
575
- if fsr2 is None:
576
- print(" #Error(calc_dupont_china): failed to retrieved reports for",ticker)
577
- return None
578
-
579
- fsr3=fsr2[(fsr2['endDate'] >= start) & (fsr2['endDate'] <= end)]
580
-
581
- #字段变换与计算
582
- #oelist=['所有者权益(或股东权益)合计','所有者权益合计','归属于母公司股东权益合计']
583
- oelist=['所有者权益(或股东权益)合计','所有者权益合计']
584
- for oe in oelist:
585
- try:
586
- fsr3['ROE']=fsr3['净利润']/fsr3[oe]
587
- break
588
- except:
589
- continue
590
- """
591
- try:
592
- oe='所有者权益(或股东权益)合计'
593
- fsr3['ROE']=fsr3['净利润']/fsr3[oe]
594
- except:
595
- oe='所有者权益合计'
596
- fsr3['ROE']=fsr3['净利润']/fsr3[oe]
597
- """
598
-
599
- try:
600
- tor='营业总收入'
601
- fsr3['Profit Margin']=fsr3['净利润']/fsr3[tor]
602
- except:
603
- tor='营业收入'
604
- fsr3['Profit Margin']=fsr3['净利润']/fsr3[tor]
605
- fsr3['Total Assets Turnover']=fsr3[tor]/fsr3['资产总计']
606
- fsr3['Equity Multiplier']=fsr3['资产总计']/fsr3[oe]
607
-
608
- dpidf=fsr3[['ticker','endDate','ROE','Profit Margin','Total Assets Turnover','Equity Multiplier']]
609
- dpidf['pROE']=dpidf['Profit Margin']*dpidf['Total Assets Turnover']*dpidf['Equity Multiplier']
610
-
611
- return dpidf
612
-
613
- if __name__=='__main__':
614
- df1=calc_dupont_china('600519.SS','2018-1-1','2021-12-31')
615
- df2=calc_dupont_china('600519.SS','2018-1-1','2021-12-31',period_type='annual')
616
-
617
- #==============================================================================
618
- if __name__=='__main__':
619
- ticker='600606.SS'
620
- start='2018-1-1'
621
- end='2021-11-24'
622
- period_type='all'
623
-
624
- def calc_dupont_china_indicator(ticker,start,end,period_type='all'):
625
- """
626
- 功能:计算股票ticker的杜邦分析项目,基于新浪、东方财富的财报指标抓取,仅限于中国A股
627
- """
628
- rates=['ROE','Profit Margin','Total Assets Turnover','Debts to Assets']
629
- rdf_list=prepare_fin_rate1tmr_china(ticker,rates,start,end,period_type)
630
-
631
- for rdf in rdf_list:
632
- if rdf is None:
633
- print(" #Error(calc_dupont_china): failed to retrieved reports for",ticker)
634
- return None
635
- if len(rdf) == 0:
636
- print(" #Error(calc_dupont_china): zero record retrieved reports for",ticker)
637
- return None
638
-
639
- #取出各项指标
640
- df_roe=rdf_list[rates.index('ROE')]
641
- df_roe['ROE']=df_roe['净资产收益率(%)']/100
642
-
643
- df_pm=rdf_list[rates.index('Profit Margin')]
644
- df_pm['Profit Margin']=df_pm['销售净利率(%)']/100
645
-
646
- df_tat=rdf_list[rates.index('Total Assets Turnover')]
647
- df_tat['Total Assets Turnover']=df_tat['总资产周转率(次)']
648
-
649
- df_em=rdf_list[rates.index('Debts to Assets')]
650
- df_em['Equity Multiplier']=1/(1-df_em['资产负债率(%)']/100)
651
-
652
- #多个数据表合并:合并列
653
- fsr=pd.concat([df_roe,df_pm,df_tat,df_em],axis=1,sort=True,join='inner')
654
- cols=['ROE','Profit Margin','Total Assets Turnover','Equity Multiplier']
655
- fsr2=fsr[cols]
656
- fsr2['ticker']=ticker
657
- fsr2['endDate']=fsr2.index.strftime('%Y-%m-%d')
658
-
659
- fsr2['month']=fsr2.index.month
660
- fsr2['periodType']=fsr2['month'].apply(lambda x: '年报' if x==12 else('中报' if x==6 else '季报'))
661
-
662
- #检查是否符合杜邦公式
663
- #fsr2['pROE']=fsr2['Profit Margin']*fsr2['Total Assets Turnover']*fsr2['Equity Multiplier']
664
- #注意:实际财务指标计算中,由于ROE和Profit Margin等指标中可能蕴含了加加减减等各种调整,ROE并非一定遵从杜邦公式
665
-
666
- return fsr2
667
-
668
- if __name__=='__main__':
669
- df1=calc_dupont_china('600519.SS','2018-1-1','2021-12-31')
670
- df2=calc_dupont_china('600519.SS','2018-1-1','2021-12-31',period_type='annual')
671
-
672
-
673
- #==============================================================================
674
- if __name__=='__main__':
675
- tickerlist=['603589.SS','600519.SS','000002.SZ']
676
- fsdate='2022-12-31'
677
- scale1 = 10
678
- scale2 = 10
679
- hatchlist=['.', 'o', '\\']
680
-
681
- def compare_dupont_china(tickerlist,fsdate='latest',scale1 = 10,scale2 = 10, \
682
- hatchlist=['.', 'o', '\\'],printout=True,sort='PM', \
683
- facecolor='papayawhip',font_size='16px',
684
- loc='best'):
685
- """
686
- 功能:获得tickerlist中每只股票的杜邦分析项目,绘制柱状叠加比较图
687
- tickerlist:股票代码列表,建议在10只以内
688
- fsdate:财报日期,默认为最新一期季报/年报,也可规定具体日期,格式:YYYY-MM-DD
689
- scale1:用于放大销售净利率,避免与权益乘数数量级不一致导致绘图难看问题,可自行调整
690
- scale2:用于放大总资产周转率,避免与权益乘数数量级不一致导致绘图难看问题,可自行调整
691
- hatchlist:绘制柱状图的纹理,用于黑白打印时区分,可自定义,
692
- 可用的符号:'-', '+', 'x', '\\', '*', 'o', 'O', '.'
693
- """
694
- error_flag=False
695
- if fsdate in ['latest','annual','quarterly']:
696
- import datetime as dt; end=str(dt.date.today())
697
- start=date_adjust(end, adjust=-365)
698
- else:
699
- valid=check_date(fsdate)
700
- if valid:
701
- end=fsdate
702
- start=date_adjust(end, adjust=-365)
703
- else:
704
- error_flag=True
705
- if error_flag: return None
706
-
707
- ticker = '公司'
708
- name1 = '销售净利率'
709
- name2 = '总资产周转率'
710
- name3 = '权益乘数'
711
- name4 = '净资产收益率'
712
- name5 = '财报日期'
713
-
714
- dpidflist,dpilist,fsdatelist,fstypelist=[],[],[],[]
715
- name1list,name2list,name3list,name4list,name5list,name6list=[],[],[],[],[],[]
716
- newtickerlist=[]
717
- for t in tickerlist:
718
- try:
719
- dpidf=calc_dupont_china(t,start,end)
720
- #dpidf=calc_dupont_china_indicator(t,start,end)
721
- except:
722
- print(" #Warning(compare_dupont_china): failed to get financial info for",t)
723
- continue
724
- if dpidf is None:
725
- print(" #Warning(compare_dupont_china): financial statements not found for",t,'@',fsdate)
726
- continue
727
- if len(dpidf)==0:
728
- print(" #Warning(compare_dupont_china): financial statements not available for",t,'@',fsdate)
729
- continue
730
- dpi=dpidf.tail(1)
731
-
732
- newtickerlist=newtickerlist+[t]
733
- dpidflist=dpidflist+[dpidf]
734
- dpilist=dpilist+[dpi]
735
- fsdatelist=fsdatelist+[dpi['endDate'][0]]
736
-
737
- name1list=name1list+[dpi['Profit Margin'][0]*scale1]
738
- name2list=name2list+[dpi['Total Assets Turnover'][0]*scale2]
739
- name3list=name3list+[dpi['Equity Multiplier'][0]]
740
- name4list=name4list+[dpi['ROE'][0]]
741
- name5list=name5list+[dpi['endDate'][0]]
742
-
743
- tickerlist=newtickerlist
744
- raw_data = {ticker:tickerlist,
745
- name1:name1list,
746
- name2:name2list,
747
- name3:name3list,
748
- name4:name4list,
749
- name5:name5list,
750
- }
751
-
752
- df = pd.DataFrame(raw_data,columns=[ticker,name1,name2,name3,name4,name5])
753
- if len(df)==0:
754
- print('')
755
- print(" #Error(compare_dupont_china): no data to plot dupont identity bar chart.")
756
- print(" If the stock code is correct, you may suffer from anti-spyder from data source. Try later")
757
- return None
758
-
759
- if sort=='PM':
760
- df.sort_values(name1,ascending=False,inplace=True)
761
- elif sort=='TAT':
762
- df.sort_values(name2,ascending=False,inplace=True)
763
- elif sort=='EM':
764
- df.sort_values(name3,ascending=False,inplace=True)
765
- else:
766
- df.sort_values(name1,ascending=False,inplace=True)
767
-
768
- num=len(df['公司'])
769
- for i in range(num):
770
- code=df.loc[i,'公司']
771
- df.loc[i,'公司']=ticker_name(code,'stock').replace("(A股)",'')
772
-
773
- #f,ax1 = plt.subplots(1,figsize=(10,5))
774
- f,ax1 = plt.subplots(1,figsize=(12.8,6.4))
775
- w = 0.75
776
- x = [i+1 for i in range(len(df[name1]))]
777
- #tick_pos = [i+(w/2.) for i in x]
778
- tick_pos = [i for i in x]
779
-
780
- ax1.bar(x,df[name3],width=w,bottom=[i+j for i,j in zip(df[name1],df[name2])], \
781
- label=name3,alpha=0.5,color='green',hatch=hatchlist[0], \
782
- edgecolor='black',align='center')
783
- ax1.bar(x,df[name2],width=w,bottom=df[name1],label=name2,alpha=0.5,color='red', \
784
- hatch=hatchlist[1], edgecolor='black',align='center')
785
- ax1.bar(x,df[name1],width=w,label=name1,alpha=0.5,color='blue', \
786
- hatch=hatchlist[2], edgecolor='black',align='center')
787
-
788
- #判断是否绘制零线
789
- pm_max=df[name1].max(); pm_min=df[name1].min()
790
- if pm_max * pm_min < 0:
791
- plt.axhline(y=0,ls=":",c="black",linewidth=2,label='')
792
-
793
- plt.xticks(tick_pos,df[ticker])
794
- plt.ylabel("杜邦分析分解项目")
795
-
796
- try:
797
- endDate=df['财报日期'].values[0]
798
- except:
799
- print(" #Error(compare_dupont_china): no fs date records to illustrate")
800
- return None
801
-
802
- footnote='【财报日期】'+endDate
803
-
804
- import datetime; today=datetime.date.today()
805
- footnote1="【图示放大比例】"+name1+':x'+str(scale1)+','+name2+':x'+str(scale2)
806
- footnote2=footnote+'\n'+footnote1+'\n'+"数据来源:sina/EM,"+str(today)
807
- plt.xlabel(footnote2)
808
-
809
- plt.legend(loc=loc)
810
-
811
- titletxt1="杜邦分析对比图:"
812
- if sort=='PM':
813
- titletxt2="按照"+name1+"降序排列"
814
- elif sort=='TAT':
815
- titletxt2="按照"+name2+"降序排列"
816
- else:
817
- titletxt2="按照"+name3+"降序排列"
818
-
819
- plt.title(titletxt1+titletxt2)
820
- plt.xlim([min(tick_pos)-w,max(tick_pos)+w])
821
-
822
- plt.gca().set_facecolor('whitesmoke')
823
- plt.show()
824
-
825
- if printout:
826
- df[name1]=df[name1]/scale1
827
- df[name2]=df[name2]/scale2
828
-
829
- cols=['销售净利率','总资产周转率','权益乘数','净资产收益率']
830
- for c in cols:
831
- df[c]=df[c].apply(lambda x: round(x,4))
832
-
833
- """
834
- #设置打印对齐
835
- pd.set_option('display.max_columns', 1000)
836
- pd.set_option('display.width', 1000)
837
- pd.set_option('display.max_colwidth', 1000)
838
- pd.set_option('display.unicode.ambiguous_as_wide', True)
839
- pd.set_option('display.unicode.east_asian_width', True)
840
-
841
- print("===== 杜邦分析分项数据表 =====")
842
- print("*** 数据来源:sina/EM,"+str(today))
843
- """
844
- title_txt="杜邦分析分项数据表:"+titletxt2
845
- footnote0="1、表中各个上市公司的财报日期可能存在差异,但均为可获得(已公布)的最新财报"
846
- footnote1="2、表中数值基于期末数字直接计算,而非期初期末均值,可能与公告数字存在差异。"
847
- footnote2="*** 数据来源:sina/EM,"+str(today)
848
- footnote=footnote0+'\n'+footnote1+'\n'+footnote2
849
-
850
- #确定表格字体大小
851
- titile_font_size=font_size
852
- heading_font_size=data_font_size=str(int(font_size.replace('px',''))-1)+'px'
853
-
854
- #print(df.to_string(index=False))
855
- #df_directprint(df,title_txt,footnote)
856
- df_display_CSS(df=df,titletxt=title_txt,footnote=footnote, \
857
- facecolor=facecolor,decimals=4, \
858
- titile_font_size=titile_font_size,heading_font_size=heading_font_size, \
859
- data_font_size=data_font_size)
860
-
861
-
862
- #合并所有历史记录
863
- alldf=pd.concat(dpidflist)
864
- alldf.dropna(inplace=True)
865
- #del alldf['pROE']
866
-
867
- """
868
- allnum=len(alldf)
869
- for i in range(allnum):
870
- code=alldf.loc[i,'periodType']
871
- if code == '3M': alldf.loc[i,'periodType']='Quarterly'
872
- else: alldf.loc[i,'periodType']='Annual'
873
- """
874
- return alldf
875
-
876
- if __name__=='__main__':
877
- tickerlist=['600606.SS','600519.SS','000002.SZ']
878
- df=compare_dupont_china(tickerlist,fsdate='latest',scale1 = 100,scale2 = 10)
879
-
880
- #==============================================================================
881
- #==============================================================================
882
- # 以上基于财报期末数字直接构造,以下基于获取的财务指标构造================================
883
- #==============================================================================
884
- #==============================================================================
885
- if __name__=='__main__':
886
- ticker="600606.SS"
887
-
888
- def get_fin_abstract_ak(ticker):
889
- """
890
- 从akshare获取财报摘要,限于中国A股
891
- 获取的项目:所有原始项目
892
- 注意:不过滤日期
893
- """
894
- print(" Searching financial abstract for",ticker,"...")
895
-
896
- #是否中国股票
897
- result,prefix,suffix=split_prefix_suffix(ticker)
898
- if not (suffix in SUFFIX_LIST_CN):
899
- print(" #Warning(get_fin_abstract_ak): not a stock in China",ticker)
900
- return None
901
-
902
- #财务报告摘要
903
- try:
904
- df1 = ak.stock_financial_abstract(stock=prefix)
905
- except:
906
- print(" #Warning(get_fin_abstract_ak): no financial information found for",ticker)
907
- return None
908
- """
909
- ['截止日期','每股净资产-摊薄/期末股数','每股现金流','每股资本公积金','固定资产合计',
910
- '流动资产合计','资产总计','长期负债合计','主营业务收入','财务费用','净利润']
911
- """
912
- if df1 is None:
913
- print(" #Warning(get_fin_abstract_ak): reports inaccessible for",ticker)
914
- return None
915
-
916
- if len(df1) == 0:
917
- print(" #Warning(get_fin_abstract_ak): zero reports found for",ticker)
918
- return None
919
-
920
- #数据清洗:去掉数值中的“元”字
921
- for c in df1.columns:
922
- try:
923
- df1[c]=df1[c].apply(lambda x: str(x).replace('元',''))
924
- except:
925
- continue
926
-
927
- #数据清洗:将空值替换为0
928
- df1b=df1.fillna('0')
929
- df2=df1b.replace('nan','0')
930
-
931
- #数据清洗:转换数值类型
932
- for c in df2.columns:
933
- try:
934
- df2[c]=df2[c].astype('float')
935
- except:
936
- continue
937
-
938
- #设置索引
939
- df2['date']=pd.to_datetime(df2['截止日期'])
940
- df2.set_index('date',inplace=True)
941
- #按照日期升序排序
942
- df2.sort_index(inplace=True)
943
-
944
- df2['ticker']=ticker
945
- df2['endDate']=df2.index.strftime('%Y-%m-%d')
946
-
947
- return df2
948
-
949
- if __name__=='__main__':
950
- fabs=get_fin_abstract_ak('600519.SS')
951
-
952
- #==============================================================================
953
- if __name__=='__main__':
954
- ticker="600606.SS"
955
-
956
- def get_fin_indicator_ak(ticker):
957
- """
958
- 从akshare获取财报重要指标,限于中国A股,历史数据
959
- 获取的项目:所有原始项目
960
- 注意:不过滤日期
961
- """
962
- print('\b'*99," Searching financial indicators for",ticker,"...")
963
-
964
- #是否中国股票
965
- result,prefix,suffix=split_prefix_suffix(ticker)
966
- if not (suffix in SUFFIX_LIST_CN):
967
- print(" #Warning(get_fin_indicator_ak): not a stock in China",ticker)
968
- return None
969
-
970
- #财务报告重要指标
971
- try:
972
- df1 = ak.stock_financial_analysis_indicator(stock=prefix)
973
- print('\b'*99," Calculating financial indicators for the above stock ...")
974
- except:
975
- print('\b'*99," #Warning(get_fin_indicator_ak): failed to get financial info for",ticker)
976
- return None
977
- """
978
- ['摊薄每股收益(元)','加权每股收益(元)','每股收益_调整后(元)','扣除非经常性损益后的每股收益(元)',
979
- '每股净资产_调整前(元)','每股净资产_调整后(元)','调整后的每股净资产(元)',
980
- '每股经营性现金流(元)',
981
- '每股资本公积金(元)','每股未分配利润(元)',
982
- '总资产利润率(%)','总资产净利润率(%)','资产报酬率(%)',
983
- '主营业务利润率(%)','成本费用利润率(%)','营业利润率(%)','主营业务成本率(%)','销售净利率(%)',
984
- '股本报酬率(%)','净资产报酬率(%)','净资产收益率(%)','加权净资产收益率(%)',
985
- '投资收益率(%)',
986
- '主营业务收入增长率(%)','净利润增长率(%)','净资产增长率(%)','总资产增长率(%)',
987
- '销售毛利率(%)','主营业务利润(元)',
988
- '三项费用比重',
989
- '非主营比重','主营利润比重','股息发放率(%)',
990
- '扣除非经常性损益后的净利润(元)',
991
- '应收账款周转率(次)','应收账款周转天数(天)','存货周转天数(天)','存货周转率(次)',
992
- '固定资产周转率(次)','总资产周转率(次)','总资产周转天数(天)','流动资产周转率(次)',
993
- '流动资产周转天数(天)','股东权益周转率(次)','流动比率','速动比率','现金比率(%)',
994
- '利息支付倍数','长期债务与营运资金比率(%)','股东权益比率(%)','长期负债比率(%)',
995
- '股东权益与固定资产比率(%)','负债与所有者权益比率(%)','长期资产与长期资金比率(%)',
996
- '资本化比率(%)','固定资产净值率(%)','资本固定化比率(%)','产权比率(%)',
997
- '清算价值比率(%)','固定资产比重(%)','资产负债率(%)','总资产(元)',
998
- '经营现金净流量对销售收入比率(%)','资产的经营现金流量回报率(%)',
999
- '经营现金净流量与净利润的比率(%)','经营现金净流量对负债比率(%)','现金流量比率(%)',
1000
- '短期股票投资(元)','短期债券投资(元)','短期其它经营性投资(元)',
1001
- '长期股票投资(元)','长期债券投资(元)','长期其它经营性投资(元)',
1002
- '1年以内应收帐款(元)','1-2年以内应收帐款(元)','2-3年以内应收帐款(元)',
1003
- '3年以内应收帐款(元)',
1004
- '1年以内预付货款(元)','1-2年以内预付货款(元)','2-3年以内预付货款(元)',
1005
- '3年以内预付货款(元)',
1006
- '1年以内其它应收款(元)','1-2年以内其它应收款(元)','2-3年以内其它应收款(元)',
1007
- '3年以内其它应收款(元)']
1008
- """
1009
- if df1 is None:
1010
- print('\b'*99," #Warning(get_fin_indicator_ak): reports inaccessible for",ticker)
1011
- return None
1012
-
1013
- if len(df1) == 0:
1014
- print('\b'*99," #Warning(get_fin_indicator_ak): zero reports found for",ticker)
1015
- return None
1016
-
1017
- #设置索引
1018
- df1['截止日期']=df1.index
1019
- df1['date']=pd.to_datetime(df1['截止日期'])
1020
- df1.set_index('date',inplace=True)
1021
- #按照日期升序排序
1022
- df1.sort_index(inplace=True)
1023
-
1024
- #数据清洗:将空值替换为0
1025
- df1b=df1.fillna('0')
1026
- #数据清洗:将"--"值替换为0
1027
- df2=df1b.replace('--','0')
1028
-
1029
- #数据清洗:转换数值类型
1030
- for c in df2.columns:
1031
- try:
1032
- df2[c]=df2[c].astype('float')
1033
- except:
1034
- continue
1035
-
1036
- df2['ticker']=ticker
1037
- df2['endDate']=df2.index.strftime('%Y-%m-%d')
1038
-
1039
- return df2
1040
-
1041
- if __name__=='__main__':
1042
- find=get_fin_indicator_ak('600606.SS')
1043
-
1044
- #==============================================================================
1045
- if __name__=='__main__':
1046
- ticker="600606.SS"
1047
- endDate="2020-12-31"
1048
-
1049
- def get_fin_performance_ak(ticker,endDate):
1050
- """
1051
- 从akshare获取业绩报表,限于中国A股
1052
- 获取的项目:所有原始项目
1053
- 注意:不过滤日期
1054
- """
1055
- #是否中国股票
1056
- result,prefix,suffix=split_prefix_suffix(ticker)
1057
- if not (suffix in SUFFIX_LIST_CN):
1058
- print(" #Warning(get_fin_performance_ak): not a stock in China",ticker)
1059
- return None
1060
-
1061
- #转换日期格式
1062
- ak_date=convert_date_ts(endDate)
1063
- print('\b'*99," Retrieving financial performance for",ticker,'ended on',endDate)
1064
- #获取全体股票的业绩报表,指定截止日期
1065
- df1 = ak.stock_em_yjbb(date=ak_date)
1066
- """
1067
- ['序号', '股票代码', '股票简称', '每股收益', '营业收入-营业收入', '营业收入-同比增长',
1068
- '营业收入-季度环比增长', '净利润-净利润', '净利润-同比增长', '净利润-季度环比增长',
1069
- '每股净资产', '净资产收益率', '每股经营现金流量', '销售毛利率',
1070
- '所处行业', '最新公告日期']
1071
- """
1072
- print('\b'*99," Calculating financial performance in the above period ...")
1073
-
1074
- if df1 is None:
1075
- print(" #Warning(get_fin_performance_ak): reports inaccessible for",ticker)
1076
- return None
1077
- if len(df1) == 0:
1078
- print(" #Warning(get_fin_performance_ak): zero reports found for",ticker)
1079
- return None
1080
-
1081
- #删除B股股票,只保留A股
1082
- df1a = df1.drop(df1[df1['股票简称'].str.contains('B')].index)
1083
-
1084
- #按照股票代码升序+最新公告日期降序排序
1085
- df1b=df1a.sort_values(by=['股票代码','最新公告日期'],ascending=[True,False])
1086
- #去掉重复记录,保留第一条
1087
- df1c=df1b.drop_duplicates(subset=['股票代码'],keep='first')
1088
-
1089
- #替换行业
1090
- df1c['所处行业']=df1c['所处行业'].apply(lambda x: '其他行业' if x == 'None' else x)
1091
-
1092
- #数据清洗:将空值替换为0
1093
- df1d=df1c.fillna('0')
1094
- #数据清洗:将"--"值替换为0
1095
- df1e=df1d.replace('--','0')
1096
- df2=df1e.replace('nan','0')
1097
-
1098
- #修改列名
1099
- df2.rename(columns={'营业收入-营业收入':'营业收入','净利润-净利润':'净利润'},inplace=True)
1100
-
1101
- #数据清洗:转换数值类型
1102
- for c in df2.columns:
1103
- if c == '股票代码': continue
1104
- try:
1105
- df2[c]=df2[c].astype('float')
1106
- except:
1107
- continue
1108
-
1109
- #设置索引
1110
- df2['截止日期']=endDate
1111
- df2['date']=pd.to_datetime(df2['截止日期'])
1112
- df2.set_index('date',inplace=True)
1113
-
1114
- df2['ticker']=df2['股票代码']
1115
- df2['endDate']=df2.index.strftime('%Y-%m-%d')
1116
- df2['最新公告日期']=df2['最新公告日期'].apply(lambda x: x[:11])
1117
-
1118
- #筛选特定股票的数据
1119
- tickerdf=df2[df2['ticker'] == prefix]
1120
- industry=tickerdf['所处行业'].values[0]
1121
- df_industry=df2[df2['所处行业'] == industry]
1122
- num_industry=len(df_industry)
1123
-
1124
- rates_industry=['每股收益','营业收入','营业收入-同比增长','营业收入-季度环比增长',
1125
- '净利润','净利润-同比增长','净利润-季度环比增长','每股净资产',
1126
- '净资产收益率','每股经营现金流量','销售毛利率']
1127
- for r in rates_industry:
1128
- i_min=df_industry[r].min()
1129
- i_max=df_industry[r].max()
1130
- i_avg=df_industry[r].mean()
1131
- i_med=df_industry[r].median()
1132
-
1133
- tickerdf[r+"-行业最小值"]=i_min
1134
- tickerdf[r+"-行业最大值"]=i_max
1135
- tickerdf[r+"-行业均值"]=i_avg
1136
- tickerdf[r+"-行业中位数"]=i_med
1137
-
1138
- x=tickerdf[r].values[0]
1139
- x_quantile=arg_percentile(df_industry[r], x)
1140
- tickerdf[r+"-行业分位数"]=x_quantile*100
1141
-
1142
- if r in ['营业收入','净利润']:
1143
- i_sum=df_industry[r].sum()
1144
- tickerdf[r+"-占行业份额"]=tickerdf[r]/i_sum*100
1145
- tickerdf['同行数量']=num_industry
1146
-
1147
- #排序字段
1148
- cols=list(tickerdf)
1149
- cols.sort(reverse=False)
1150
- tickerdf2=tickerdf[cols]
1151
-
1152
- return tickerdf2
1153
-
1154
- """
1155
- ['endDate', 'ticker',
1156
- '净利润', '净利润-占行业份额', '净利润-行业中位数', '净利润-行业分位数', '净利润-行业均值',
1157
- '净利润-行业最大值', '净利润-行业最小值',
1158
-
1159
- '净利润-同比增长', '净利润-同比增长-行业中位数', '净利润-同比增长-行业分位数',
1160
- '净利润-同比增长-行业均值', '净利润-同比增长-行业最大值', '净利润-同比增长-行业最小值',
1161
-
1162
- '净利润-季度环比增长', '净利润-季度环比增长-行业中位数', '净利润-季度环比增长-行业分位数',
1163
- '净利润-季度环比增长-行业均值', '净利润-季度环比增长-行业最大值',
1164
- '净利润-季度环比增长-行业最小值',
1165
-
1166
- '净资产收益率', '净资产收益率-行业中位数', '净资产收益率-行业分位数', '净资产收益率-行业均值',
1167
- '净资产收益率-行业最大值', '净资产收益率-行业最小值',
1168
- '序号', '截止日期', '所处行业', '最新公告日期',
1169
-
1170
- '每股净资产', '每股净资产-行业中位数', '每股净资产-行业分位数', '每股净资产-行业均值',
1171
- '每股净资产-行业最大值', '每股净资产-行业最小值',
1172
-
1173
- '每股收益', '每股收益-行业中位数', '每股收益-行业分位数', '每股收益-行业均值',
1174
- '每股收益-行业最大值', '每股收益-行业最小值',
1175
-
1176
- '每股经营现金流量', '每股经营现金流量-行业中位数', '每股经营现金流量-行业分位数',
1177
- '每股经营现金流量-行业均值', '每股经营现金流量-行业最大值', '每股经营现金流量-行业最小值',
1178
- '股票代码', '股票简称',
1179
-
1180
- '营业收入', '营业收入-占行业份额', '营业收入-行业中位数', '营业收入-行业分位数', '营业收入-行业均值', '营业收入-行业最大值',
1181
- '营业收入-行业最小值',
1182
-
1183
- '营业收入-同比增长', '营业收入-同比增长-行业中位数', '营业收入-同比增长-行业分位数',
1184
- '营业收入-同比增长-行业均值', '营业收入-同比增长-行业最大值', '营业收入-同比增长-行业最小值',
1185
-
1186
- '营业收入-季度环比增长', '营业收入-季度环比增长-行业中位数', '营业收入-季度环比增长-行业分位数',
1187
- '营业收入-季度环比增长-行业均值', '营业收入-季度环比增长-行业最大值',
1188
- '营业收入-季度环比增长-行业最小值',
1189
-
1190
- '销售毛利率', '销售毛利率-行业中位数', '销售毛利率-行业分位数', '销售毛利率-行业均值',
1191
- '销售毛利率-行业最大值', '销售毛利率-行业最小值']
1192
- """
1193
-
1194
- if __name__=='__main__':
1195
- fpfm=get_fin_performance_ak('600519.SS','2020-12-31')
1196
-
1197
- #==============================================================================
1198
-
1199
- def industry_name_em():
1200
- """
1201
- 功能:从东方财富获取行业分类名称,限于中国A股
1202
- """
1203
- #获取最近的报表日期
1204
- import datetime; today=datetime.date.today()
1205
- start=date_adjust(today, adjust=-365)
1206
- fs_dates=cvt_fs_dates(start,today,'all')
1207
- endDate=fs_dates[-1:][0]
1208
-
1209
- #转换日期格式
1210
- ak_date=convert_date_ts(endDate)
1211
- print('\b'*99," Retrieving EM industry names ended on",endDate)
1212
- #获取全体股票的业绩报表,指定截止日期
1213
- df1 = ak.stock_em_yjbb(date=ak_date)
1214
- """
1215
- ['序号', '股票代码', '股票简称', '每股收益', '营业收入-营业收入', '营业收入-同比增长',
1216
- '营业收入-季度环比增长', '净利润-净利润', '净利润-同比增长', '净利润-季度环比增长',
1217
- '每股净资产', '净资产收益率', '每股经营现金流量', '销售毛利率',
1218
- '所处行业', '最新公告日期']
1219
- """
1220
- print('\b'*99," Summerizing industry names for the above period ...")
1221
-
1222
- #替换行业
1223
- df1c['所处行业']=df1c['所处行业'].apply(lambda x: '其他行业' if x == 'None' else x)
1224
-
1225
- industry_list=list(set(list(df2['所处行业'])))
1226
- try:
1227
- industry_list.remove('0')
1228
- except: pass
1229
-
1230
- #对列表中的中文字符串排序
1231
- from pypinyin import pinyin, Style
1232
- industry_list.sort(key = lambda keys:[pinyin(i, style=Style.TONE3) for i in keys])
1233
-
1234
- print("\n===== 行业分类:东方财富 =====\n")
1235
- n=0
1236
- for i in industry_list:
1237
- n=n+1
1238
- print(i,end=' ')
1239
- if n==5:
1240
- n=0
1241
- print('')
1242
- if n <5: print('')
1243
- #import datetime; today=datetime.date.today()
1244
- print("\n***来源:东方财富,",endDate)
1245
-
1246
- return industry_list
1247
-
1248
- if __name__=='__main__':
1249
- df=industry_name_em()
1250
- #==============================================================================
1251
- if __name__=='__main__':
1252
- industry="银行"
1253
- rate="营业收入"
1254
- top=5
1255
-
1256
- def industry_rank_em(industry="银行",rate="oper revenue",top=5):
1257
- """
1258
- 功能:从东方财富获取最新行业排名前几名,按照财务指标,限于中国A股
1259
- """
1260
- #获取最近的报表日期
1261
- import datetime; today=datetime.date.today()
1262
- start=date_adjust(today, adjust=-365)
1263
- fs_dates=cvt_fs_dates(start,today,'all')
1264
- endDate=fs_dates[-1:][0]
1265
-
1266
- rate_check=['eps','oper revenue','oper revenue growth','net earnings',
1267
- 'earnings growth','naps','rona','oper cfps', 'gross margin']
1268
- rate_cols=['每股收益','营业收入','营业收入-同比增长','净利润',
1269
- '净利润-同比增长','每股净资产','净资产收益率',
1270
- '每股经营现金流量', '销售毛利率']
1271
- if not (rate in rate_check):
1272
- print(" #Warning(industry_rank_em): unsupported financial rate",rate)
1273
- print(" Supported ranking rates:",rate_check)
1274
- return None
1275
-
1276
- #转换日期格式
1277
- ak_date=convert_date_ts(endDate)
1278
- print('\b'*99," Retrieving EM industry names ended on",endDate)
1279
- #获取全体股票的业绩报表,指定截止日期
1280
- df1 = ak.stock_em_yjbb(date=ak_date)
1281
- """
1282
- ['序号', '股票代码', '股票简称', '每股收益', '营业收入-营业收入', '营业收入-同比增长',
1283
- '营业收入-季度环比增长', '净利润-净利润', '净利润-同比增长', '净利润-季度环比增长',
1284
- '每股净资产', '净资产收益率', '每股经营现金流量', '销售毛利率',
1285
- '所处行业', '最新公告日期']
1286
- """
1287
- print('\b'*99," Summerizing stock performance for industry",industry,'by',rate)
1288
-
1289
- #删除B股股票,只保留A股
1290
- df1a = df1.drop(df1[df1['股票简称'].str.contains('B')].index)
1291
-
1292
- #按照所处行业升序
1293
- df1b=df1a.sort_values(by=['所处行业','股票代码'],ascending=[True,False])
1294
- #去掉重复记录,保留第一条
1295
- df1c=df1b.drop_duplicates(subset=['股票代码'],keep='first')
1296
-
1297
- #替换行业
1298
- df1c['所处行业']=df1c['所处行业'].apply(lambda x: '其他行业' if x == 'None' else x)
1299
-
1300
- #数据清洗:将空值替换为0
1301
- df1d=df1c.fillna('0')
1302
- #数据清洗:将"--"值替换为0
1303
- df1e=df1d.replace('--','0')
1304
- df2=df1e.replace('nan','0')
1305
-
1306
- #修改列名
1307
- df2.rename(columns={'营业收入-营业收入':'营业收入','净利润-净利润':'净利润'},inplace=True)
1308
-
1309
- #数据清洗:转换数值类型
1310
- for c in df2.columns:
1311
- if c == '股票代码': continue
1312
- try:
1313
- df2[c]=round(df2[c].astype('float'),2)
1314
- except:
1315
- continue
1316
-
1317
- #设置索引
1318
- df2['截止日期']=endDate
1319
- df2['date']=pd.to_datetime(df2['截止日期'])
1320
- df2.set_index('date',inplace=True)
1321
-
1322
- df2['ticker']=df2['股票代码']
1323
- df2['endDate']=df2.index.strftime('%Y-%m-%d')
1324
- df2['最新公告日期']=df2['最新公告日期'].apply(lambda x: x[:11])
1325
-
1326
- #筛选特定行业的股票
1327
- industry_check=list(set(list(df2['所处行业'])))
1328
- if not (industry in industry_check):
1329
- print(" #Warning(industry_rank_em): unsupported em industry name",industry)
1330
- print(" See supported industry by command: df=industry_name_em()")
1331
- return None
1332
-
1333
- df3=df2[df2['所处行业'] == industry]
1334
- rpos=rate_check.index(rate)
1335
- rate=rate_cols[rpos] #将财务指标名称从英文转为中文
1336
-
1337
- cols=['股票代码','股票简称','最新公告日期']+[rate]
1338
- df_industry=df3[cols]
1339
- i_min=df_industry[rate].min()
1340
- i_max=df_industry[rate].max()
1341
- i_avg=df_industry[rate].mean()
1342
- i_med=df_industry[rate].median()
1343
- i_sum=df_industry[rate].sum()
1344
- num_industry=len(df_industry)
1345
-
1346
- ticker_list=list(df_industry['股票代码'])
1347
- ticker_result=pd.DataFrame()
1348
- for t in ticker_list:
1349
- tickerdf=df_industry[df_industry['股票代码'] == t]
1350
- x=tickerdf[rate].values[0]
1351
- x_quantile=arg_percentile(df_industry[rate], x)
1352
- tickerdf[rate+"-行业分位数%"]=x_quantile*100
1353
-
1354
- if rate in ['营业收入','净利润']:
1355
- tickerdf[rate+"-占行业份额%"]=tickerdf[rate]/i_sum*100
1356
-
1357
- try:
1358
- ticker_result=ticker_result.append(tickerdf)
1359
- except:
1360
- ticker_result=ticker_result._append(tickerdf)
1361
-
1362
- #排序:降序
1363
- ticker_result["排名"]=ticker_result[rate].rank(ascending=False).astype('int')
1364
- ticker_result.sort_values(by=rate,ascending=False,inplace=True)
1365
- for c in ticker_result.columns:
1366
- try:
1367
- ticker_result[c]=ticker_result[c].apply(lambda x: simple_number(x))
1368
- except:
1369
- continue
1370
-
1371
- #打印
1372
- pd.set_option('display.max_columns', 1000)
1373
- pd.set_option('display.width', 1000)
1374
- pd.set_option('display.max_colwidth', 1000)
1375
- pd.set_option('display.unicode.ambiguous_as_wide', True)
1376
- pd.set_option('display.unicode.east_asian_width', True)
1377
-
1378
- if top > 0:
1379
- rank_prefix="前"
1380
- topn=top
1381
- printdf=ticker_result.head(topn)
1382
- else:
1383
- rank_prefix="后"
1384
- topn=-top
1385
- printdf=ticker_result.tail(topn)
1386
-
1387
- print("\n===== 行业排名:"+rate+","+rank_prefix+str(topn)+"名"+" =====")
1388
- print("行业内企业个数 :",num_industry)
1389
- print("行业最小/最大值:",simple_number(i_min),'/',simple_number(i_max))
1390
- print("行业均值/中位数:",simple_number(i_avg),'/',simple_number(i_med))
1391
- print('')
1392
- print(printdf.to_string(index=False))
1393
-
1394
- return ticker_result
1395
-
1396
- if __name__=='__main__':
1397
- df=industry_rank_em("银行","oper revenue",top=10)
1398
- df=industry_rank_em("银行","oper revenue",top=-10)
1399
-
1400
- df=industry_rank_em("银行","?",top=-10)
1401
-
1402
- df=industry_rank_em("银行",'eps',top=20)
1403
- df=industry_rank_em("银行",'eps',top=-25)
1404
- df=industry_rank_em("银行",'naps',top=20)
1405
- df=industry_rank_em("银行",'oper cfps',top=20)
1406
-
1407
- df=industry_rank_em("银行",'rona',top=20)
1408
-
1409
- df=industry_rank_em("银行",'oper revenue growth',top=5)
1410
- df=industry_rank_em("银行",'earnings growth',top=5)
1411
-
1412
- #==============================================================================
1413
- if __name__=='__main__':
1414
- industry="银行"
1415
- tickers=['600036.SS','000001.SZ','601328.SS','601939.SS','601288.SS','601398.SS','601988.SS']
1416
- rates=['eps','naps','oper cfps','oper revenue growth','earnings growth']
1417
-
1418
- tickers=''
1419
-
1420
- def industry_rank_em2(tickers,rates=['eps','naps'],industry="银行",top=10):
1421
- """
1422
- 功能:从东方财富获取某些股票在某些财务指标方面的行业排名,限于中国A股
1423
- 注意:当tickers为''时列出排名前或后top的股票
1424
- """
1425
- error_flag=False
1426
- #获取最近的报表日期
1427
- import datetime; today=datetime.date.today()
1428
- start=date_adjust(today, adjust=-365)
1429
- fs_dates=cvt_fs_dates(start,today,'all')
1430
- endDate=fs_dates[-1:][0]
1431
-
1432
- if isinstance(tickers,str): tickers_selected=[tickers]
1433
- elif isinstance(tickers,list): tickers_selected=tickers
1434
- else:
1435
- print(" #Warning(industry_rank_em2): unsupported stock codes",tickers)
1436
- print(" Supporting a stock code or a list of stock codes")
1437
- error_flag=True
1438
- if error_flag: return
1439
-
1440
- stocks_selected=[]
1441
- for t in tickers_selected:
1442
- #是否中国股票
1443
- result,prefix,suffix=split_prefix_suffix(t)
1444
- if not (suffix in SUFFIX_LIST_CN) and not (tickers == ''):
1445
- print(" #Warning(industry_rank_em2): not a stock in China",t)
1446
- error_flag=True
1447
- stocks_selected=stocks_selected+[prefix]
1448
- if error_flag: return
1449
-
1450
- rate_check=['eps','oper revenue','oper revenue growth','net earnings',
1451
- 'earnings growth','naps','rona','oper cfps', 'gross margin']
1452
- rate_cols=['每股收益','营业收入','营业收入-同比增长','净利润',
1453
- '净利润-同比增长','每股净资产','净资产收益率',
1454
- '每股经营现金流量', '销售毛利率']
1455
- if isinstance(rates,str): rate_list=[rates]
1456
- elif isinstance(rates,list): rate_list=rates
1457
- else:
1458
- print(" #Warning(industry_rank_em2): unsupported financial rates",rates)
1459
- print(" Supporting a financial rate or a list of rates as follows:",rate_check)
1460
- error_flag=True
1461
- if error_flag: return
1462
-
1463
- for rate in rate_list:
1464
- if not (rate in rate_check):
1465
- print(" #Warning(industry_rank_em2): unsupported financial rate",rate)
1466
- print(" Supported ranking rates:",rate_check)
1467
- error_flag=True
1468
- if error_flag: return
1469
-
1470
- #转换日期格式
1471
- ak_date=convert_date_ts(endDate)
1472
- print('\b'*99," Retrieving EM industry names ended on",endDate)
1473
- #获取全体股票的业绩报表,指定截止日期
1474
- df1 = ak.stock_em_yjbb(date=ak_date)
1475
- """
1476
- ['序号', '股票代码', '股票简称', '每股收益', '营业收入-营业收入', '营业收入-同比增长',
1477
- '营业收入-季度环比增长', '净利润-净利润', '净利润-同比增长', '净利润-季度环比增长',
1478
- '每股净资产', '净资产收益率', '每股经营现金流量', '销售毛利率',
1479
- '所处行业', '最新公告日期']
1480
- """
1481
- print('\b'*99," Summerizing stock performance for industry",industry,'by',rate)
1482
-
1483
- #删除B股股票,只保留A股
1484
- df1a = df1.drop(df1[df1['股票简称'].str.contains('B')].index)
1485
-
1486
- #按照所处行业升序
1487
- df1b=df1a.sort_values(by=['所处行业','股票代码'],ascending=[True,False])
1488
- #去掉重复记录,保留第一条
1489
- df1c=df1b.drop_duplicates(subset=['股票代码'],keep='first')
1490
-
1491
- #替换行业
1492
- df1c['所处行业']=df1c['所处行业'].apply(lambda x: '其他行业' if x == 'None' else x)
1493
-
1494
- #数据清洗:将空值替换为0
1495
- df1d=df1c.fillna('0')
1496
- #数据清洗:将"--"值替换为0
1497
- df1e=df1d.replace('--','0')
1498
- df2=df1e.replace('nan','0')
1499
-
1500
- #修改列名
1501
- df2.rename(columns={'营业收入-营业收入':'营业收入','净利润-净利润':'净利润'},inplace=True)
1502
-
1503
- #数据清洗:转换数值类型
1504
- for c in df2.columns:
1505
- if c == '股票代码': continue
1506
- try:
1507
- df2[c]=round(df2[c].astype('float'),2)
1508
- except:
1509
- continue
1510
-
1511
- #设置索引
1512
- df2['截止日期']=endDate
1513
- df2['date']=pd.to_datetime(df2['截止日期'])
1514
- df2.set_index('date',inplace=True)
1515
-
1516
- df2['ticker']=df2['股票代码']
1517
- df2['endDate']=df2.index.strftime('%Y-%m-%d')
1518
- df2['最新公告日期']=df2['最新公告日期'].apply(lambda x: x[:11])
1519
-
1520
- #检查是否支持特定行业
1521
- industry_check=list(set(list(df2['所处行业'])))
1522
- if not (industry in industry_check):
1523
- print(" #Warning(industry_rank_em2): unsupported em industry name",industry)
1524
- print(" See supported industry by command: df=industry_name_em()")
1525
- error_flag=True
1526
- if error_flag: return
1527
-
1528
- df3=df2[df2['所处行业'] == industry]
1529
-
1530
- #筛选特定行业的股票
1531
- for r in rate_list:
1532
- rpos=rate_check.index(r)
1533
- rate=rate_cols[rpos]
1534
-
1535
- cols=['股票代码','股票简称','最新公告日期']+[rate]
1536
- df_industry=df3[cols]
1537
- i_min=df_industry[rate].min()
1538
- i_max=df_industry[rate].max()
1539
- i_avg=df_industry[rate].mean()
1540
- i_med=df_industry[rate].median()
1541
- i_sum=df_industry[rate].sum()
1542
- num_industry=len(df_industry)
1543
-
1544
- ticker_list=list(df_industry['股票代码'])
1545
- ticker_result=pd.DataFrame()
1546
- for t in ticker_list:
1547
- tickerdf=df_industry[df_industry['股票代码'] == t]
1548
- x=tickerdf[rate].values[0]
1549
- x_quantile=arg_percentile(df_industry[rate], x)
1550
- tickerdf[rate+"-行业分位数%"]=x_quantile*100
1551
-
1552
- if rate in ['营业收入','净利润']:
1553
- tickerdf[rate+"-占行业份额%"]=tickerdf[rate]/i_sum*100
1554
-
1555
- try:
1556
- ticker_result=ticker_result.append(tickerdf)
1557
- except:
1558
- ticker_result=ticker_result._append(tickerdf)
1559
-
1560
- #排序:降序
1561
- ticker_result["排名"]=ticker_result[rate].rank(ascending=False).astype('int')
1562
- ticker_result.sort_values(by=rate,ascending=False,inplace=True)
1563
- for c in ticker_result.columns:
1564
- try:
1565
- ticker_result[c]=ticker_result[c].apply(lambda x: simple_number(x))
1566
- except:
1567
- continue
1568
-
1569
- #打印
1570
- pd.set_option('display.max_columns', 1000)
1571
- pd.set_option('display.width', 1000)
1572
- pd.set_option('display.max_colwidth', 1000)
1573
- pd.set_option('display.unicode.ambiguous_as_wide', True)
1574
- pd.set_option('display.unicode.east_asian_width', True)
1575
-
1576
- if (len(stocks_selected) >= 1) and not (tickers == ''):
1577
- printdf=ticker_result[ticker_result['股票代码'].isin(stocks_selected)]
1578
- else:
1579
- if top > 0:
1580
- printdf=ticker_result.head(top)
1581
- else:
1582
- printdf=ticker_result.tail(-top)
1583
-
1584
- print("\n===== 行业排名:"+industry+','+rate+" =====")
1585
- print("行业内企业个数 :",num_industry)
1586
- print("行业最小/最大值:",simple_number(i_min),'/',simple_number(i_max))
1587
- print("行业均值/中位数:",simple_number(i_avg),'/',simple_number(i_med))
1588
- print('')
1589
- print(printdf.to_string(index=False))
1590
-
1591
- return
1592
-
1593
- if __name__=='__main__':
1594
- industry="银行"
1595
- tickers1=['600036.SS','000001.SZ','601009.SS','001227.SZ']
1596
- tickers2=['601328.SS','601939.SS','601288.SS','601398.SS','601988.SS']
1597
- rates=['eps','naps','oper cfps','oper revenue growth','earnings growth']
1598
-
1599
- industry_rank_em2(tickers1+tickers2,rates,industry)
1600
- industry_rank_em2('',rates,industry,top=5) #每项指标的行业前几名
1601
-
1602
- #==============================================================================
1603
- def simple_number(number):
1604
- """
1605
- 功能:将数字表示为易读的字符化数值,并截取小数点
1606
- """
1607
-
1608
- if number < 0.001:
1609
- number1=round(number,5)
1610
- suff=''
1611
-
1612
- if number < 1:
1613
- number1=round(number,4)
1614
- suff=''
1615
-
1616
- if number >= 1:
1617
- number1=round(number,2)
1618
- suff=''
1619
-
1620
- if number >= 10000:
1621
- number1=round(number/10000,2)
1622
- suff='万'
1623
-
1624
- if number >= 1000000:
1625
- number1=round(number/1000000,2)
1626
- suff='百万'
1627
-
1628
- if number >= 100000000:
1629
- number1=round(number/100000000,2)
1630
- suff='亿'
1631
-
1632
- if number >= 1000000000000:
1633
- number1=round(number/1000000000000,2)
1634
- suff='万亿'
1635
-
1636
- number2=str(number1)+suff
1637
-
1638
- return number2
1639
-
1640
- if __name__=='__main__':
1641
- simple_number(0.03257)
1642
- simple_number(0.58726)
1643
- simple_number(1.3289)
1644
- simple_number(13283.569)
1645
- simple_number(1234569874123)
1646
- #==============================================================================
1647
-
1648
- if __name__=='__main__':
1649
- tickers=["600606.SS","600606.SS"]
1650
- endDate="2020-12-31"
1651
-
1652
- def get_fin_performance_akm(tickers,endDate):
1653
- """
1654
- 从akshare获取业绩报表,多个股票,1个截止日期,限于中国A股
1655
- 获取的项目:所有原始项目
1656
- 注意:
1657
- """
1658
-
1659
- #是否中国股票
1660
- prefix_list=[]
1661
- for t in tickers:
1662
- result,prefix,suffix=split_prefix_suffix(t)
1663
- if not (suffix in SUFFIX_LIST_CN):
1664
- print(" #Warning(get_fin_performance_akm): not a stock in China",t)
1665
- return None
1666
- prefix_list=prefix_list+[prefix]
1667
-
1668
- #转换日期格式
1669
- ak_date=convert_date_ts(endDate)
1670
- print('\b'*99," Retrieving financial performance ended on",endDate)
1671
- #获取全体股票的业绩报表,指定截止日期
1672
- df1 = ak.stock_em_yjbb(date=ak_date)
1673
- print('\b'*99," Calculating financial performance for the above period")
1674
-
1675
- if df1 is None:
1676
- print(" #Warning(get_fin_performance_akm): reports inaccessible for",endDate)
1677
- return None
1678
- if len(df1) == 0:
1679
- print(" #Warning(get_fin_performance_akm): zero reports found for",endDate)
1680
- return None
1681
-
1682
- #删除B股股票,只保留A股
1683
- df1a = df1.drop(df1[df1['股票简称'].str.contains('B')].index)
1684
-
1685
- #按照股票代码升序+最新公告日期降序排序
1686
- df1b=df1a.sort_values(by=['股票代码','最新公告日期'],ascending=[True,False])
1687
- #去掉重复记录,保留第一条
1688
- df1c=df1b.drop_duplicates(subset=['股票代码'],keep='first')
1689
-
1690
- #替换行业
1691
- df1c['所处行业']=df1c['所处行业'].apply(lambda x: '其他行业' if x == 'None' else x)
1692
-
1693
- #数据清洗:将空值替换为0
1694
- df1d=df1c.fillna('0')
1695
- #数据清洗:将"--"值替换为0
1696
- df1e=df1d.replace('--','0')
1697
- df2=df1e.replace('nan','0')
1698
-
1699
- #修改列名
1700
- df2.rename(columns={'营业收入-营业收入':'营业收入','净利润-净利润':'净利润'},inplace=True)
1701
-
1702
- #数据清洗:转换数值类型
1703
- for c in df2.columns:
1704
- if c == '股票代码': continue
1705
- try:
1706
- df2[c]=df2[c].astype('float')
1707
- except:
1708
- continue
1709
-
1710
- #设置索引
1711
- df2['截止日期']=endDate
1712
- df2['date']=pd.to_datetime(df2['截止日期'])
1713
- df2.set_index('date',inplace=True)
1714
-
1715
- df2['ticker']=df2['股票代码']
1716
- df2['endDate']=df2.index.strftime('%Y-%m-%d')
1717
- df2['最新公告日期']=df2['最新公告日期'].apply(lambda x: x[:11])
1718
-
1719
- #筛选特定股票的数据
1720
- mtdf=pd.DataFrame()
1721
- for prefix in prefix_list:
1722
- tickerdf=df2[df2['ticker'] == prefix]
1723
- industry=tickerdf['所处行业'].values[0]
1724
- df_industry=df2[df2['所处行业'] == industry]
1725
- num_industry=len(df_industry)
1726
-
1727
- rates_industry=['每股收益','营业收入','营业收入-同比增长','营业收入-季度环比增长',
1728
- '净利润','净利润-同比增长','净利润-季度环比增长','每股净资产',
1729
- '净资产收益率','每股经营现金流量','销售毛利率']
1730
- for r in rates_industry:
1731
- i_min=df_industry[r].min()
1732
- i_max=df_industry[r].max()
1733
- i_avg=df_industry[r].mean()
1734
- i_med=df_industry[r].median()
1735
-
1736
- tickerdf[r+"-行业最小值"]=i_min
1737
- tickerdf[r+"-行业最大值"]=i_max
1738
- tickerdf[r+"-行业均值"]=i_avg
1739
- tickerdf[r+"-行业中位数"]=i_med
1740
-
1741
- x=tickerdf[r].values[0]
1742
- x_quantile=arg_percentile(df_industry[r], x)
1743
- tickerdf[r+"-行业分位数"]=x_quantile*100
1744
-
1745
- if r in ['营业收入','净利润']:
1746
- i_sum=df_industry[r].sum()
1747
- tickerdf[r+"-占行业份额"]=tickerdf[r]/i_sum*100
1748
- tickerdf['同行数量']=num_industry
1749
-
1750
- #排序字段
1751
- cols=list(tickerdf)
1752
- cols.sort(reverse=False)
1753
- tickerdf2=tickerdf[cols]
1754
-
1755
- #加入结果数据表,用于返回
1756
- try:
1757
- mtdf=mtdf.append(tickerdf2)
1758
- except:
1759
- mtdf=mtdf._append(tickerdf2)
1760
-
1761
- return mtdf
1762
-
1763
- if __name__=='__main__':
1764
- tickers=["600519.SS","600606.SS"]
1765
- mtdf=get_fin_performance_akm(tickers,'2020-12-31')
1766
-
1767
- #==============================================================================
1768
- if __name__=='__main__':
1769
- fin_rate="Current Ratio"
1770
- prompt=False
1771
-
1772
- def cvt_fin_rate(fin_rate,prompt=False,printout=True):
1773
- """
1774
- 功能:查表获得财务指标的计算来源以及字段名称
1775
- """
1776
-
1777
- #财务指标结构字典
1778
- rate_dict={
1779
- #数据源函数:get_fin_indicator_ak
1780
- "diluted eps":("get_fin_indicator_ak","摊薄每股收益(元)"),
1781
- "weighted eps":("get_fin_indicator_ak","加权每股收益(元)"),
1782
- "adjusted eps":("get_fin_indicator_ak","每股收益_调整后(元)"),
1783
- "recurring eps":("get_fin_indicator_ak","扣除非经常性损益后的每股收益(元)"),
1784
-
1785
- "naps":("get_fin_indicator_ak","每股净资产_调整前(元)"),
1786
- "net assets per share":("get_fin_indicator_ak","每股净资产_调整前(元)"),
1787
- "adjusted naps":("get_fin_indicator_ak","每股净资产_调整后(元)"),
1788
- "capital reserve per share":("get_fin_indicator_ak","每股资本公积金(元)"),
1789
- "undistributed profit per share":("get_fin_indicator_ak","每股未分配利润(元)"),
1790
-
1791
- "roa":("get_fin_indicator_ak","资产报酬率(%)"),
1792
-
1793
- "reward on shareholder equity":("get_fin_indicator_ak","股本报酬率(%)"),
1794
- "reward on net assets":("get_fin_indicator_ak","净资产报酬率(%)"),
1795
- "return on net assets":("get_fin_indicator_ak","净资产收益率(%)"),
1796
- "rona":("get_fin_indicator_ak","净资产收益率(%)"),
1797
- "weighted return on net assets":("get_fin_indicator_ak","加权净资产收益率(%)"),
1798
- "weighted rona":("get_fin_indicator_ak","加权净资产收益率(%)"),
1799
- "return on investment":("get_fin_indicator_ak","投资收益率(%)"),
1800
- "roi":("get_fin_indicator_ak","投资收益率(%)"),
1801
-
1802
- "profit margin":("get_fin_indicator_ak","销售净利率(%)"),
1803
- "gross margin":("get_fin_indicator_ak","销售毛利率(%)"),
1804
- "oper profit share":("get_fin_indicator_ak","主营利润比重"),
1805
- "payout ratio":("get_fin_indicator_ak","股息发放率(%)"),
1806
- "roi":("get_fin_indicator_ak","投资收益率(%)"),
1807
-
1808
- "oper revenue growth":("get_fin_indicator_ak","主营业务收入增长率(%)"),
1809
- "profit margin growth":("get_fin_indicator_ak","净利润增长率(%)"),
1810
- "net assets growth":("get_fin_indicator_ak","净资产增长率(%)"),
1811
- "total assets growth":("get_fin_indicator_ak","总资产增长率(%)"),
1812
-
1813
- "receivables turnover":("get_fin_indicator_ak","应收账款周转率(次)"),
1814
- "inventory turnover":("get_fin_indicator_ak","存货周转率(次)"),
1815
- "fixed assets turnover":("get_fin_indicator_ak","固定资产周转率(次)"),
1816
- "total assets turnover":("get_fin_indicator_ak","总资产周转率(次)"),
1817
- "current assets turnover":("get_fin_indicator_ak","流动资产周转率(次)"),
1818
- "equity assets turnover":("get_fin_indicator_ak","股东权益周转率(次)"),
1819
-
1820
- "current ratio":("get_fin_indicator_ak","流动比率"),
1821
- "quick ratio":("get_fin_indicator_ak","速动比率"),
1822
- "cash ratio":("get_fin_indicator_ak","现金比率(%)"),
1823
- "tie":("get_fin_indicator_ak","利息支付倍数"),
1824
- "times interest earned":("get_fin_indicator_ak","利息支付倍数"),
1825
- "equity to assets":("get_fin_indicator_ak","股东权益比率(%)"),
1826
- "ltd%":("get_fin_indicator_ak","长期负债比率(%)"),
1827
- "long-term debts%":("get_fin_indicator_ak","长期负债比率(%)"),
1828
- "debts to equity":("get_fin_indicator_ak","负债与所有者权益比率(%)"),
1829
- "liabilities to equity":("get_fin_indicator_ak","产权比率(%)"),
1830
- "capitalization%":("get_fin_indicator_ak","资本化比率(%)"),
1831
- "ppe residual":("get_fin_indicator_ak","固定资产净值率(%)"),
1832
- "tangible assets to debts":("get_fin_indicator_ak","清算价值比率(%)"),
1833
- "fixed assets%":("get_fin_indicator_ak","固定资产比重(%)"),
1834
- "debts to assets":("get_fin_indicator_ak","资产负债率(%)"),
1835
- "cash flow ratio":("get_fin_indicator_ak","现金流量比率(%)"),
1836
- "cashflow ratio":("get_fin_indicator_ak","现金流量比率(%)"),
1837
- "net oper cashflow to revenue":("get_fin_indicator_ak","经营现金净流量对销售收入比率(%)"),
1838
-
1839
- #数据源函数:get_fin_performance_ak,百分比
1840
- "net earnings":("get_fin_performance_ak","净利润"),
1841
- "earnings industry share":("get_fin_performance_ak","净利润-占行业份额"),
1842
- "earnings industry quantile":("get_fin_performance_ak","净利润-行业分位数"),
1843
- "earnings growth":("get_fin_performance_ak","净利润-同比增长"),
1844
- "earnings growth industry quantile":("get_fin_performance_ak","净利润-同比增长-行业分位数"),
1845
-
1846
- "roe":("get_fin_performance_ak","净资产收益率"),
1847
- "roe industry quantile":("get_fin_performance_ak","净资产收益率-行业分位数"),
1848
- "naps":("get_fin_performance_ak","每股净资产"),
1849
- "naps industry quantile":("get_fin_performance_ak","每股净资产-行业分位数"),
1850
- "eps":("get_fin_performance_ak","每股收益"),
1851
- "eps industry quantile":("get_fin_performance_ak","每股收益-行业分位数"),
1852
-
1853
- "oper cfps":("get_fin_performance_ak","每股经营现金流量"),
1854
- "oper cfps industry quantile":("get_fin_performance_ak","每股经营现金流量-行业分位数"),
1855
-
1856
- "oper revenue":("get_fin_performance_ak","营业收入"),
1857
- "oper revenue industry share":("get_fin_performance_ak","营业收入-占行业份额"),
1858
- "oper revenue industry quantile":("get_fin_performance_ak","营业收入-行业分位数"),
1859
- "oper revenue growth":("get_fin_performance_ak","营业收入-同比增长"),
1860
- "oper revenue growth industry quantile":("get_fin_performance_ak","营业收入-同比增长-行业分位数"),
1861
-
1862
- "gross margin industry quantile":("get_fin_performance_ak","销售毛利率-行业分位数"),
1863
-
1864
- #数据源函数:get_fin_abstract_ak
1865
- "diluted naps":("get_fin_abstract_ak","每股净资产-摊薄/期末股数"),
1866
- "cfps":("get_fin_abstract_ak","每股现金流"),
1867
- }
1868
-
1869
- #是否需要提示?
1870
- if prompt or (fin_rate in ['?','?']):
1871
- promptdf=pd.DataFrame(columns=('财务指标代码', '财务指标名称'))
1872
- key_list=rate_dict.keys()
1873
- for k in key_list:
1874
- #print(k)
1875
- result=rate_dict.get(k)
1876
- (source,name_cn)=result
1877
- s=pd.Series({'财务指标代码':k, '财务指标名称':name_cn})
1878
- try:
1879
- promptdf=promptdf.append(s, ignore_index=True)
1880
- except:
1881
- promptdf=promptdf._append(s, ignore_index=True)
1882
- promptdf.sort_values('财务指标代码',ascending=True,inplace=True)
1883
-
1884
- #打印对齐
1885
- if printout:
1886
- pd.set_option('display.max_columns', 1000)
1887
- pd.set_option('display.width', 1000)
1888
- pd.set_option('display.max_colwidth', 1000)
1889
- pd.set_option('display.unicode.ambiguous_as_wide', True)
1890
- pd.set_option('display.unicode.east_asian_width', True)
1891
- print(promptdf.to_string(index=False))
1892
- return None,None
1893
- else:
1894
- return promptdf,None
1895
-
1896
- #搜索字典
1897
- rate=fin_rate.lower()
1898
- result=rate_dict.get(rate)
1899
- if result is None:
1900
- return None,None
1901
- (source,name_cn)=result
1902
-
1903
- return source,name_cn
1904
-
1905
- if __name__=='__main__':
1906
- cvt_fin_rate("?",prompt=True)
1907
- cvt_fin_rate("Current Ratio")
1908
- cvt_fin_rate("Quick Ratio")
1909
- #==============================================================================
1910
- def arg_percentile(series, x):
1911
- """
1912
- 功能:求x在序列series中的分位数
1913
- """
1914
- import numpy as np
1915
- # 分位数的启始区间
1916
- a, b = 0, 1
1917
- while True:
1918
- # m是a、b的终点
1919
- m = (a+b)/2
1920
- # 可以打印查看求解过程
1921
- # print(np.percentile(series, 100*m), x)
1922
- if np.percentile(series, 100*m) >= x:
1923
- b = m
1924
- elif np.percentile(series, 100*m) < x:
1925
- a = m
1926
- # 如果区间左右端点足够靠近,则退出循环。
1927
- if np.abs(a-b) <= 0.000001:
1928
- break
1929
- return m
1930
-
1931
- #==============================================================================
1932
- if __name__=='__main__':
1933
- start='2020-1-1'
1934
- end='2021-6-30'
1935
- period_type='all'
1936
- period_type='annual'
1937
- period_type='quarterly'
1938
- period_type='semiannual'
1939
-
1940
- def cvt_fs_dates(start,end,period_type='all'):
1941
- """
1942
- 功能:基于年报类型给出期间内财报的各个截止日期列表
1943
- """
1944
- #检查期间的合理性
1945
- valid,start1,end1=check_period(start,end)
1946
- if not valid:
1947
- print(" #Warning(get_fs_dates): invalid period",start,end)
1948
- return None
1949
-
1950
- #构造所有年报日期
1951
- start_year=start1.year
1952
- end_year=end1.year
1953
-
1954
- fs_dates_all=[]
1955
- q1_str='-03-31'; q2_str='-06-30'; q3_str='-09-30'; q4_str='-12-31'
1956
- for y in range(start_year,end_year+1):
1957
- #print(y)
1958
- fs_dates_all=fs_dates_all+[str(y)+q1_str,str(y)+q2_str,str(y)+q3_str,str(y)+q4_str]
1959
-
1960
- #过滤年报日期
1961
- fs_dates=[]
1962
- for d in fs_dates_all:
1963
- dd=pd.to_datetime(d)
1964
- #print(dd,start1,end1)
1965
- if (dd < start1) or (dd > end1): continue
1966
-
1967
- #区分年报季报
1968
- dd_month=dd.month
1969
- if period_type == 'annual':
1970
- if not (dd_month == 12): continue
1971
- if period_type == 'semiannual':
1972
- if not (dd_month in [6,12]): continue
1973
-
1974
- fs_dates=fs_dates+[d]
1975
-
1976
- return fs_dates
1977
-
1978
- if __name__=='__main__':
1979
- cvt_fs_dates('2020-7-1','2021-10-1')
1980
- cvt_fs_dates('2020-7-1','2021-10-1',period_type='annual')
1981
- cvt_fs_dates('2020-7-1','2021-10-1',period_type='semiannual')
1982
- cvt_fs_dates('2020-7-1','2021-10-1',period_type='quarterly')
1983
-
1984
- #==============================================================================
1985
- if __name__=='__main__':
1986
- ticker="600606.SS"
1987
- rate1='ROA'
1988
- rate2='Oper Revenue Industry Share'
1989
- start='2020-1-1'
1990
- end='2021-6-30'
1991
- period_type='all'
1992
-
1993
- period_type='annual'
1994
- period_type='quarterly'
1995
- period_type='semiannual'
1996
-
1997
- def prepare_fin_rate1t2r_china(ticker,rate1,rate2,start,end,period_type='all'):
1998
- """
1999
- 功能:准备财务准备,1个股票,2个指标,限于中国A股
2000
- 注意:过滤期间,过滤财报类型
2001
-
2002
- """
2003
- #检查期间的合理性
2004
- valid,start1,end1=check_period(start,end)
2005
- if not valid:
2006
- print(" #Warning(prepare_fin_rate1t2r_china): invalid period",start,end)
2007
- return None,None
2008
-
2009
- #是否中国股票
2010
- result,prefix,suffix=split_prefix_suffix(ticker)
2011
- if not (suffix in SUFFIX_LIST_CN):
2012
- print(" #Warning(prepare_fin_rate1t2r_china): not a stock in China",ticker)
2013
- return None,None
2014
-
2015
- #检查指标是否支持
2016
- fin_rates,_=cvt_fin_rate('?',prompt=True,printout=False)
2017
- rate_list=list(fin_rates['财务指标代码'])
2018
- if not (rate1.lower() in rate_list):
2019
- print(" #Warning(prepare_fin_rate1t2r_china): unsupported financial rate",rate1)
2020
- return None,None
2021
- if not (rate2.lower() in rate_list):
2022
- print(" #Warning(prepare_fin_rate1t2r_china): unsupported financial rate",rate2)
2023
- return None,None
2024
- #--------------------------------------------------------------------------
2025
- func1,name1=cvt_fin_rate(rate1)
2026
- func2,name2=cvt_fin_rate(rate2)
2027
-
2028
- if func1 == 'get_fin_indicator_ak':
2029
- find1=get_fin_indicator_ak(ticker)
2030
- ratedf1=find1[['ticker',name1]]
2031
- if func2 == func1:
2032
- ratedf2=find1[['ticker',name2]]
2033
-
2034
- if func1 == 'get_fin_abstract_ak':
2035
- fabs1=get_fin_abstract_ak(ticker)
2036
- ratedf1=fabs1[['ticker',name1]]
2037
- if func2 == func1:
2038
- ratedf2=fabs1[['ticker',name2]]
2039
-
2040
- if func1 == 'get_fin_performance_ak':
2041
- fs_dates=cvt_fs_dates(start,end,period_type)
2042
- #合成各个财报日期
2043
- fpfm1=pd.DataFrame()
2044
- for enddate in fs_dates:
2045
- tmp=get_fin_performance_ak(ticker,enddate)
2046
- try:
2047
- fpfm1=fpfm1.append(tmp)
2048
- except:
2049
- fpfm1=fpfm1._append(tmp)
2050
- ratedf1=fpfm1[['ticker',name1,'股票简称','所处行业','同行数量']]
2051
- if func2 == func1:
2052
- ratedf2=fpfm1[['ticker',name2,'股票简称','所处行业','同行数量']]
2053
-
2054
- #若ratedf2尚未定义
2055
- if not ('ratedf2' in locals().keys()):
2056
- if func2 == 'get_fin_indicator_ak':
2057
- find2=get_fin_indicator_ak(ticker)
2058
- ratedf2=find2[['ticker',name2]]
2059
-
2060
- if func2 == 'get_fin_abstract_ak':
2061
- fabs2=get_fin_abstract_ak(ticker)
2062
- ratedf2=fabs2[['ticker',name2]]
2063
-
2064
- if func2 == 'get_fin_performance_ak':
2065
- fs_dates=cvt_fs_dates(start,end,period_type)
2066
- #合成各个财报日期
2067
- fpfm2=pd.DataFrame()
2068
- for enddate in fs_dates:
2069
- tmp=get_fin_performance_ak(ticker,enddate)
2070
- try:
2071
- fpfm2=fpfm2.append(tmp)
2072
- except:
2073
- fpfm2=fpfm2._append(tmp)
2074
- ratedf2=fpfm2[['ticker',name2,'股票简称','所处行业','同行数量']]
2075
-
2076
- #过滤起始日期:
2077
- ratedf1b=ratedf1[(ratedf1.index >= start1) & (ratedf1.index <= end1)]
2078
- ratedf2b=ratedf2[(ratedf2.index >= start1) & (ratedf2.index <= end1)]
2079
-
2080
- #过滤年报类型
2081
- ratedf1b['month']=ratedf1b.index.month
2082
- if period_type == 'annual':
2083
- ratedf1c=ratedf1b[ratedf1b['month'] == 12]
2084
- elif period_type == 'semiannual':
2085
- ratedf1c=ratedf1b[ratedf1b['month'].isin([6,12])]
2086
- else:
2087
- ratedf1c=ratedf1b
2088
- del ratedf1c['month']
2089
-
2090
- ratedf2b['month']=ratedf2b.index.month
2091
- if period_type == 'annual':
2092
- ratedf2c=ratedf2b[ratedf2b['month'] == 12]
2093
-
2094
- elif period_type == 'semiannual':
2095
- ratedf2c=ratedf2b[ratedf2b['month'].isin([6,12])]
2096
- else:
2097
- ratedf2c=ratedf2b
2098
- del ratedf2c['month']
2099
-
2100
- print(" Retrieved",len(ratedf1c),rate1,"and",len(ratedf2c),rate2,"records for",ticker)
2101
-
2102
- return ratedf1c,ratedf2c
2103
-
2104
- if __name__=='__main__':
2105
- ticker='600519.SS'
2106
- start='2021-5-1'
2107
- end='2021-11-30'
2108
- df1=prepare_fin_rate1t2r_china(ticker,'ROA','ROE',start,end,period_type='all')
2109
- df2=prepare_fin_rate1t2r_china(ticker,'ROA','CFPS',start,end,period_type='all')
2110
- df3=prepare_fin_rate1t2r_china(ticker,'ROA','Oper Revenue Industry Share',start,end,period_type='all')
2111
- #==============================================================================
2112
- if __name__=='__main__':
2113
- ticker='600519.SS'
2114
- start='2021-5-1'
2115
- end='2021-11-30'
2116
- period_type='all'
2117
-
2118
- def prepare_fin_rate1tmr_china(ticker,rates,start,end,period_type='all'):
2119
- """
2120
- 功能:准备财务准备,1个股票,多个指标,限于中国A股
2121
- 注意:过滤期间,过滤财报类型
2122
-
2123
- """
2124
- #检查期间的合理性
2125
- valid,start1,end1=check_period(start,end)
2126
- if not valid:
2127
- print(" #Warning(prepare_fin_rate1tmr_china): invalid period",start,end)
2128
- return None
2129
-
2130
- #是否中国股票
2131
- result,prefix,suffix=split_prefix_suffix(ticker)
2132
- if not (suffix in SUFFIX_LIST_CN):
2133
- print(" #Warning(prepare_fin_rate1tmr_china): not a stock in China",ticker)
2134
- return None
2135
-
2136
- #检查是否多个指标
2137
- if isinstance(rates,str):
2138
- mrate_list=[rates]
2139
- elif isinstance(rates,list):
2140
- mrate_list=rates
2141
- else:
2142
- print(" #Warning(prepare_fin_rate1tmr_china): invalid financial rate/rate list",rates)
2143
- return None
2144
-
2145
- #检查指标是否支持
2146
- sup_fin_rates,_=cvt_fin_rate('?',prompt=True,printout=False)
2147
- sup_rate_list=list(sup_fin_rates['财务指标代码'])
2148
- for r in mrate_list:
2149
- if not (r.lower() in sup_rate_list):
2150
- print(" #Warning(prepare_fin_rate1tmr_china): unsupported financial rate",r)
2151
- return None
2152
- #--------------------------------------------------------------------------
2153
- #逐个检查财务指标,若不存在则抓取,若存在则直接利用,避免重复抓取
2154
- rdf_list=[]
2155
- for r in mrate_list:
2156
- func,name=cvt_fin_rate(r)
2157
- if func == 'get_fin_indicator_ak':
2158
- if not ('find' in locals().keys()):
2159
- find=get_fin_indicator_ak(ticker)
2160
- rdf=find[['ticker',name]]
2161
-
2162
- if func == 'get_fin_abstract_ak':
2163
- if not ('fabs' in locals().keys()):
2164
- fabs=get_fin_abstract_ak(ticker)
2165
- rdf=fabs[['ticker',name]]
2166
-
2167
- if func == 'get_fin_performance_ak':
2168
- if not ('fpfm' in locals().keys()):
2169
- fs_dates=cvt_fs_dates(start,end,period_type)
2170
- #合成各个财报日期
2171
- fpfm=pd.DataFrame()
2172
- for enddate in fs_dates:
2173
- tmp=get_fin_performance_ak(ticker,enddate)
2174
- try:
2175
- fpfm=fpfm.append(tmp)
2176
- except:
2177
- fpfm=fpfm._append(tmp)
2178
- rdf=fpfm[['ticker',name,'股票简称','所处行业','同行数量']]
2179
-
2180
- rdf_list=rdf_list+[rdf]
2181
-
2182
- rdf_list1=[]
2183
- for rdf in rdf_list:
2184
- #过滤起始日期:
2185
- rdf1=rdf[(rdf.index >= start1) & (rdf.index <= end1)]
2186
-
2187
- #过滤年报类型
2188
- rdf1['month']=rdf1.index.month
2189
- if period_type == 'annual':
2190
- rdf1b=rdf1[rdf1['month'] == 12]
2191
- elif period_type == 'semiannual':
2192
- rdf1b=rdf1[rdf1['month'].isin([6,12])]
2193
- else:
2194
- rdf1b=rdf1
2195
- del rdf1b['month']
2196
- print(" Retrieved",len(rdf1b),"records for",ticker,list(rdf1b)[1])
2197
-
2198
- rdf_list1=rdf_list1+[rdf1b]
2199
-
2200
- return rdf_list1
2201
-
2202
- if __name__=='__main__':
2203
- ticker='600519.SS'
2204
- start='2021-5-1'
2205
- end='2021-11-30'
2206
- rates=['oper profit share','Oper Revenue Industry Share','earnings industry share']
2207
- df1=prepare_fin_rate1tmr_china(ticker,['ROA','ROE'],start,end,period_type='all')
2208
- df2=prepare_fin_rate1tmr_china(ticker,['ROA','CFPS'],start,end,period_type='all')
2209
- df3=prepare_fin_rate1tmr_china(ticker,rates,start,end,period_type='all')
2210
- #==============================================================================
2211
- if __name__=='__main__':
2212
- tickers=['600519.SS','600606.SS']
2213
- rate='oper revenue industry share'
2214
- start='2021-5-1'
2215
- end='2021-11-30'
2216
- period_type='all'
2217
-
2218
- def prepare_fin_ratemt1r_china(tickers,rate,start,end,period_type='all'):
2219
- """
2220
- 功能:准备财务指标,多个股票,1个指标,限于中国A股
2221
- 注意:过滤期间,过滤财报类型
2222
-
2223
- """
2224
- #检查期间的合理性
2225
- valid,start1,end1=check_period(start,end)
2226
- if not valid:
2227
- print(" #Warning(prepare_fin_ratemt1r_china): invalid period",start,end)
2228
- return None
2229
-
2230
- #检查是否多个股票
2231
- if isinstance(tickers,str):
2232
- mticker_list=[tickers]
2233
- elif isinstance(tickers,list):
2234
- mticker_list=tickers
2235
- else:
2236
- print(" #Warning(prepare_fin_ratemt1r_china): invalid stock/stock list",tickers)
2237
- return None
2238
-
2239
- #是否中国股票
2240
- prefix_list=[]
2241
- for t in mticker_list:
2242
- result,prefix,suffix=split_prefix_suffix(t)
2243
- if not (suffix in SUFFIX_LIST_CN):
2244
- print(" #Warning(prepare_fin_ratemt1r_china): not a stock in China",ticker)
2245
- return None
2246
- prefix_list=prefix_list+[prefix]
2247
-
2248
- #检查指标是否支持
2249
- sup_fin_rates,_=cvt_fin_rate('?',prompt=True,printout=False)
2250
- sup_rate_list=list(sup_fin_rates['财务指标代码'])
2251
- if not (rate.lower() in sup_rate_list):
2252
- print(" #Warning(prepare_fin_ratemt1r_china): unsupported financial rate",rate)
2253
- print(" Check supported financial rates? use command rates=cvt_fin_rate('?')")
2254
- return None
2255
- #--------------------------------------------------------------------------
2256
- #逐个检查股票指标,若不存在则抓取,若存在则直接利用,避免重复抓取
2257
- func,name=cvt_fin_rate(rate)
2258
- rdf_list=[]
2259
- if func in ['get_fin_indicator_ak','get_fin_abstract_ak']:
2260
- for t in mticker_list:
2261
- if func == 'get_fin_indicator_ak':
2262
- find=get_fin_indicator_ak(t)
2263
- rdf=find[['ticker',name]]
2264
-
2265
- if func == 'get_fin_abstract_ak':
2266
- fabs=get_fin_abstract_ak(t)
2267
- rdf=fabs[['ticker',name]]
2268
-
2269
- rdf_list=rdf_list+[rdf]
2270
-
2271
- #为了避免重复抓取,将此段独立出来
2272
- if func == 'get_fin_performance_ak':
2273
- fs_dates=cvt_fs_dates(start,end,period_type)
2274
- #合成各个财报日期
2275
- fpfm=pd.DataFrame()
2276
- for enddate in fs_dates:
2277
- tmp=get_fin_performance_akm(mticker_list,enddate)
2278
- try:
2279
- fpfm=fpfm.append(tmp)
2280
- except:
2281
- fpfm=fpfm._append(tmp)
2282
-
2283
- #处理各个rdf进入列表
2284
- for prefix in prefix_list:
2285
- rdf=fpfm[fpfm['ticker'] == prefix]
2286
- rdf2=rdf[['ticker',name,'股票简称','所处行业','同行数量']]
2287
- rdf_list=rdf_list+[rdf2]
2288
-
2289
- rdf_list1=[]
2290
- for rdf in rdf_list:
2291
- #过滤起始日期:
2292
- rdf1=rdf[(rdf.index >= start1) & (rdf.index <= end1)]
2293
-
2294
- #过滤年报类型
2295
- rdf1['month']=rdf1.index.month
2296
- if period_type == 'annual':
2297
- rdf1b=rdf1[rdf1['month'] == 12]
2298
- elif period_type == 'semiannual':
2299
- rdf1b=rdf1[rdf1['month'].isin([6,12])]
2300
- else:
2301
- rdf1b=rdf1
2302
- del rdf1b['month']
2303
-
2304
- rdf_list1=rdf_list1+[rdf1b]
2305
-
2306
- return rdf_list1
2307
-
2308
- if __name__=='__main__':
2309
- tickers=['600519.SS','600606.SS','000002.SZ']
2310
- start='2020-5-1'
2311
- end='2021-11-30'
2312
- df1=prepare_fin_ratemt1r_china(tickers,'ROA',start,end,period_type='all')
2313
- df2=prepare_fin_ratemt1r_china(tickers,'Profit Margin Growth',start,end,period_type='all')
2314
- df3=prepare_fin_ratemt1r_china(tickers,'oper revenue industry share',start,end,period_type='annual')
2315
-
2316
- #==============================================================================
2317
- def cn_codetranslate(ticker):
2318
- """
2319
- 功能:将中国股票代码转换为股票简称
2320
- 注意:既能转换带后缀的股票代码,也能转换不带后缀的股票代码
2321
- """
2322
- result,prefix,suffix=split_prefix_suffix(ticker)
2323
- if suffix in SUFFIX_LIST_CN:
2324
- name=ticker_name(ticker,'stock')
2325
-
2326
- if suffix =='':
2327
- for s in SUFFIX_LIST_CN:
2328
- ticker_try=ticker+'.'+s
2329
- name=ticker_name(ticker_try,'stock')
2330
- print('\b'*99," Looking for the short name of stock",ticker)
2331
- if not (name == ticker_try): break
2332
-
2333
- return name
2334
-
2335
- if __name__=='__main__':
2336
- cn_codetranslate('600519.SS')
2337
- cn_codetranslate('600519')
2338
-
2339
- #==============================================================================
2340
-
2341
- if __name__=='__main__':
2342
- tickers=['600519.SS','000858.SZ','600779.SS',]
2343
- rate='oper revenue industry share'
2344
- start='2021-5-1'
2345
- end='2021-11-30'
2346
- period_type='all'
2347
-
2348
- def print_fin_ratemt1r_china(tickers,rate,start,end,period_type='all'):
2349
- """
2350
- 功能:打印财务指标,多个股票,1个指标,限于中国A股
2351
- 注意:过滤期间,过滤财报类型
2352
- """
2353
- rdf_list=prepare_fin_ratemt1r_china(tickers,rate,start,end,period_type)
2354
-
2355
- _,rate_name=cvt_fin_rate(rate)
2356
- rdf_all=pd.DataFrame()
2357
- #rdf=rdf_list[0]
2358
- for rdf in rdf_list:
2359
- t=rdf['ticker'].values[0]
2360
- df_tmp=rdf[[rate_name]]
2361
- df_tmp.rename(columns={rate_name:t},inplace=True)
2362
- df_tmpt=df_tmp.T
2363
- try:
2364
- rdf_all=rdf_all.append(df_tmpt)
2365
- except:
2366
- rdf_all=rdf_all._append(df_tmpt)
2367
-
2368
- rdf_all['股票代码']=rdf_all.index
2369
- rdf_all['股票简称']=rdf_all['股票代码'].apply(lambda x: cn_codetranslate(x))
2370
-
2371
- cols=list(rdf_all)
2372
- for c in cols:
2373
- try:
2374
- cs=c.strftime("%Y-%m-%d")
2375
- rdf_all[cs]=round(rdf_all[c],2)
2376
- del rdf_all[c]
2377
- except:
2378
- continue
2379
-
2380
- #设置打印对齐
2381
- pd.set_option('display.max_columns', 1000)
2382
- pd.set_option('display.width', 1000)
2383
- pd.set_option('display.max_colwidth', 1000)
2384
- pd.set_option('display.unicode.ambiguous_as_wide', True)
2385
- pd.set_option('display.unicode.east_asian_width', True)
2386
-
2387
- print("\n===== 财务指标对比:"+rate_name+" =====")
2388
- print(rdf_all.to_string(index=False))
2389
-
2390
- import datetime; today=datetime.date.today()
2391
- print("\n*** 数据来源:sina/EM,"+str(today))
2392
-
2393
- return rdf_all
2394
-
2395
- if __name__=='__main__':
2396
- tickers=['600519.SS','000858.SZ','600779.SS',]
2397
- rate='oper revenue industry share'
2398
- start='2021-5-1'
2399
- end='2021-11-30'
2400
-
2401
- df=print_fin_ratemt1r_china(tickers,rate,start,end,period_type='all')
2402
- #==============================================================================
2403
- #==============================================================================
2404
- #==============================================================================
2405
- if __name__ == '__main__':
2406
- tickers=['600606.SS','600519.SS']
2407
- items='Current Ratio'
2408
- start='2020-1-1'
2409
- end='2021-11-30'
2410
- period_type='annual'
2411
-
2412
- datatag=False
2413
- power=0
2414
- zeroline=False
2415
- twinx=False
2416
-
2417
- def compare_history_china(tickers,items,start,end,period_type='annual', \
2418
- datatag=False,power=0,zeroline=False,twinx=False):
2419
- """
2420
- 功能:比较一只股票两个指标或两只股票一个指标的时序数据,绘制折线图
2421
- datatag=False: 不将数值标记在图形旁
2422
- zeroline=False:不绘制水平零线
2423
- twinx=False:单纵轴
2424
- """
2425
- error_flag=False
2426
-
2427
- #检查股票个数
2428
- if isinstance(tickers,str):
2429
- ticker_num=1; ticker1=tickers
2430
- elif isinstance(tickers,list):
2431
- ticker_num=len(tickers)
2432
- if ticker_num >= 1:
2433
- ticker1=tickers[0]
2434
- if ticker_num >= 2:
2435
- ticker2=tickers[1]
2436
- if ticker_num == 0:
2437
- print(" #Error(compare_history_china): no stock code found",tickers)
2438
- error_flag=True
2439
-
2440
- #检查指标个数
2441
- item_num=1
2442
- if isinstance(items,list):
2443
- if len(items) >= 1:
2444
- item1=items[0]
2445
- if len(items) >= 2:
2446
- item2=items[1]
2447
- item_num=2
2448
- if len(items) == 0:
2449
- print(" #Error(compare_history_china): no analytical item found",items)
2450
- error_flag=True
2451
- else:
2452
- item1=items
2453
-
2454
- if error_flag: return None,None
2455
-
2456
- #判断比较模式
2457
- if (ticker_num == 1) and (item_num == 1): mode='T1I1'
2458
- if (ticker_num == 1) and (item_num == 2): mode='T1I2'
2459
- if (ticker_num == 2): mode='T2I1'
2460
-
2461
- #抓取数据
2462
- if mode in ['T1I1','T1I2']:
2463
- rdf_list1=prepare_fin_rate1tmr_china(ticker1,items,start,end,period_type)
2464
- if rdf_list1 is None: error_flag=True
2465
- else:
2466
- for rdf in rdf_list1:
2467
- if rdf is None: error_flg=True
2468
- if len(rdf) == 0: error_flag=True
2469
- if not error_flag:
2470
- df1=rdf_list1[0]
2471
- try: df2=rdf_list1[1]
2472
- except: pass
2473
- if mode in ['T2I1']:
2474
- rdf_list2=prepare_fin_ratemt1r_china(tickers,item1,start,end,period_type)
2475
- if rdf_list2 is None: error_flag=True
2476
- else:
2477
- for rdf in rdf_list2:
2478
- if rdf is None: error_flag=True
2479
- if len(rdf) == 0: error_flag=True
2480
- if not error_flag:
2481
- df1=rdf_list2[0]
2482
- df2=rdf_list2[1]
2483
-
2484
- if error_flag:
2485
- print(" #Error(compare_history_china): info not found for",tickers,"on",items)
2486
- return None,None
2487
-
2488
- #绘图:T1I1,单折线
2489
- import datetime; today=datetime.date.today()
2490
- footnote9="数据来源: sina/EM, "+str(today)
2491
- if mode == 'T1I1':
2492
- _,colname=cvt_fin_rate(item1)
2493
- #collabel=ectranslate(item1)
2494
- collabel=colname
2495
- ylabeltxt=''
2496
- titletxt=ticker_name(ticker1,'stock')+": 财务指标历史"
2497
-
2498
- colmin=round(df1[colname].min(),2)
2499
- colmax=round(df1[colname].max(),2)
2500
- colmean=round(df1[colname].mean(),2)
2501
- footnote=collabel+":"+ \
2502
- str(colmin)+" - "+str(colmax)+ \
2503
- ",均值"+str(colmean)+'\n'+footnote9
2504
- plot_line(df1,colname,collabel,ylabeltxt,titletxt,footnote, \
2505
- datatag=datatag,power=power,zeroline=zeroline,resample_freq='1M')
2506
- return df1,None
2507
-
2508
- #绘图:T1I2,单股票双折线
2509
- if mode == 'T1I2':
2510
- _,colname1=cvt_fin_rate(item1)
2511
- label1=colname1
2512
- _,colname2=cvt_fin_rate(item2)
2513
- label2=colname2
2514
- ylabeltxt=''
2515
- titletxt="财务指标历史"
2516
-
2517
- colmin1=round(df1[colname1].min(),2)
2518
- colmax1=round(df1[colname1].max(),2)
2519
- colmean1=round(df1[colname1].mean(),2)
2520
- colmin2=round(df2[colname2].min(),2)
2521
- colmax2=round(df2[colname2].max(),2)
2522
- colmean2=round(df2[colname2].mean(),2)
2523
- footnote1=label1+":"+ \
2524
- str(colmin1)+" - "+str(colmax1)+",均值"+str(colmean1)
2525
- footnote2=label2+":"+ \
2526
- str(colmin2)+" - "+str(colmax2)+",均值"+str(colmean2)
2527
- footnote=footnote1+'\n'+footnote2+'\n'+footnote9
2528
-
2529
- plot_line2(df1,ticker1,colname1,label1, \
2530
- df2,ticker1,colname2,label2, \
2531
- ylabeltxt,titletxt,footnote, \
2532
- power=power,zeroline=zeroline,twinx=twinx,resample_freq='1M')
2533
- return df1,df2
2534
-
2535
- #绘图:T2I1,双股票双折线,单指标
2536
- if mode == 'T2I1':
2537
- _,colname1=cvt_fin_rate(item1)
2538
- label1=colname1
2539
- colname2=colname1
2540
- label2=label1
2541
- ylabeltxt=''
2542
- titletxt="财务指标历史"
2543
-
2544
- colmin1=round(df1[colname1].min(),2)
2545
- colmax1=round(df1[colname1].max(),2)
2546
- colmean1=round(df1[colname1].mean(),2)
2547
- colmin2=round(df2[colname2].min(),2)
2548
- colmax2=round(df2[colname2].max(),2)
2549
- colmean2=round(df2[colname2].mean(),2)
2550
- footnote1=ticker_name(ticker1,'stock')+":"+ \
2551
- str(colmin1)+" - "+str(colmax1)+",均值"+str(colmean1)
2552
- footnote2=ticker_name(ticker2,'stock')+":"+ \
2553
- str(colmin2)+" - "+str(colmax2)+",均值"+str(colmean2)
2554
- footnote=footnote1+'\n'+footnote2+'\n'+footnote9
2555
-
2556
- plot_line2(df1,ticker1,colname1,label1, \
2557
- df2,ticker2,colname2,label2, \
2558
- ylabeltxt,titletxt,footnote, \
2559
- power=power,zeroline=zeroline,twinx=twinx,resample_freq='1M')
2560
-
2561
- return df1,df2
2562
-
2563
- if __name__ == '__main__':
2564
- tickers=['600606.SS','000002.SZ']
2565
- items=['Current Ratio','Quick Ratio']
2566
- df1,df2=compare_history_china('600606.SS','Current Ratio','2020-1-1','2021-12-13',period_type='all')
2567
- df1,df2=compare_history_china('600606.SS',items,'2020-1-1','2021-12-13',period_type='all')
2568
- df1,df2=compare_history_china(tickers,'Current Ratio','2020-1-1','2021-12-13',period_type='all')
2569
-
2570
- df1,df2=compare_history_china(tickers,'?','2020-1-1','2021-12-13')
2571
-
2572
- rates=['Earnings Industry Share','Oper Revenue Industry Share']
2573
- df1,df2=compare_history_china('600519.SS',rates,'2015-1-1','2021-12-13',period_type='annual')
2574
- df1,df2=compare_history_china('600519.SS',rates,'2015-1-1','2021-12-13',period_type='semiannual')
2575
-
2576
- df1,df2=compare_history_china(['600519.SS','000858.SZ'],'Earnings Industry Share','2015-1-1','2021-12-13',period_type='annual')
2577
- df1,df2=compare_history_china(['600519.SS','000858.SZ'],'Oper Revenue Industry Share','2015-1-1','2021-12-13',period_type='annual')
2578
-
2579
- #==============================================================================
2580
- if __name__ == '__main__':
2581
- tickers=['600519.SS','600606.SS','000002.SZ']
2582
- itemk='ROE'
2583
- endDate='latest'
2584
- datatag=True
2585
- tag_offset=0.01
2586
- graph=True
2587
- axisamp=1.3
2588
-
2589
- def compare_snapshot_china(tickers,itemk,endDate='latest',datatag=True,tag_offset=0.01,graph=True,axisamp=1.3):
2590
- """
2591
- 功能:比较多个股票的快照数据,绘制水平柱状图,仅限中国A股
2592
- itemk需要通过对照表转换为内部的item
2593
- datatag=True: 将数值标记在图形旁
2594
- tag_offset=0.01:标记的数值距离图形的距离,若不理想可以手动调节,可为最大值1%-5%
2595
- """
2596
- error_flag=False
2597
-
2598
- #检查股票代码列表
2599
- if not isinstance(tickers,list):
2600
- print(" #Warning(compare_snapshot_china): need more stock codes in",tickers)
2601
- error_flag=True
2602
- if len(tickers) < 2:
2603
- print(" #Warning(compare_snapshot_china): need more stock codes in",tickers)
2604
- error_flag=True
2605
- if error_flag: return None
2606
-
2607
- #检查指标是否支持
2608
- fin_rates,_=cvt_fin_rate('?',prompt=True,printout=False)
2609
- rate_list=list(fin_rates['财务指标代码'])
2610
- if not (itemk.lower() in rate_list):
2611
- print(" #Warning(compare_snapshot_china): unsupported financial rate",itemk)
2612
- error_flag=True
2613
- if error_flag: return None
2614
-
2615
- #获取最近的报表日期
2616
- if endDate == 'latest':
2617
- import datetime; endDate=datetime.date.today()
2618
- else:
2619
- #检查日期
2620
- valid_date=check_date(endDate)
2621
- if not valid_date:
2622
- error_flag=True
2623
- print(" #Warning(compare_snapshot_china): invalid date",endDate)
2624
- if error_flag: return None
2625
-
2626
- start=date_adjust(endDate, adjust=-365)
2627
- fs_dates=cvt_fs_dates(start,endDate,'all')
2628
- endDate=fs_dates[-1:][0]
2629
-
2630
- #依次获得各个股票的指标
2631
- rdf_list=prepare_fin_ratemt1r_china(tickers,itemk,endDate,endDate,period_type='all')
2632
- #合成
2633
- df=pd.DataFrame(columns=('ticker','item','value','name'))
2634
- for rdf in rdf_list:
2635
- cols=list(rdf)
2636
- t=rdf['ticker'].values[0]
2637
- item=cols[1]
2638
- value=rdf[item].values[0]
2639
- name=ticker_name(t,'stock')
2640
- if name == t:
2641
- name=rdf[cols[2]].values[0]
2642
- row=pd.Series({'ticker':t,'item':item,'value':value,'name':name})
2643
- try:
2644
- df=df.append(row,ignore_index=True)
2645
- except:
2646
- df=df._append(row,ignore_index=True)
2647
-
2648
- if len(df) == 0:
2649
- print(" #Error(compare_snapshot_china): stock info not found in",tickers)
2650
- error_flag=True
2651
- if error_flag: return None
2652
-
2653
- #处理小数点
2654
- try:
2655
- df['value']=round(df['value'],3)
2656
- except:
2657
- pass
2658
- df.sort_values(by='value',ascending=False,inplace=True)
2659
- df['key']=df['name']
2660
- df.set_index('key',inplace=True)
2661
-
2662
- #绘图
2663
- if graph:
2664
- print("...Calculating and drawing graph, please wait ...")
2665
- colname='value'
2666
- titletxt="企业横向对比: 业绩指标快照"
2667
- import datetime; today=datetime.date.today()
2668
- footnote=item+" -->"+ \
2669
- "\n报表截止日期:"+endDate+ \
2670
- "\n数据来源: sina/EM, "+str(today)
2671
- plot_barh(df,colname,titletxt,footnote,datatag=datatag,tag_offset=tag_offset,axisamp=axisamp)
2672
-
2673
- return df
2674
-
2675
- if __name__ == '__main__':
2676
- df=compare_snapshot(tickers,itemk)
2677
-
2678
- #==============================================================================
2679
- if __name__ == '__main__':
2680
- tickers=['600519.SS','600606.SS','000002.SZ']
2681
- endDate='latest'
2682
- graph=True
2683
- axisamp=7
2684
-
2685
- def compare_tax_china(tickers,endDate='latest',datatag=True,tag_offset=0.01,graph=True,axisamp=1.3):
2686
- """
2687
- 功能:比较公司最新的实际所得税率
2688
- """
2689
- error_flag=False
2690
-
2691
- #检查股票代码列表
2692
- if not isinstance(tickers,list):
2693
- print(" #Warning(compare_tax_china): need more stock codes in",tickers)
2694
- error_flag=True
2695
- if len(tickers) < 2:
2696
- print(" #Warning(compare_tax_china): need more stock codes in",tickers)
2697
- error_flag=True
2698
- if error_flag: return None
2699
-
2700
- #获取最近的报表日期
2701
- if endDate == 'latest':
2702
- import datetime; endDate=datetime.date.today()
2703
- else:
2704
- #检查日期
2705
- valid_date=check_date(endDate)
2706
- if not valid_date:
2707
- error_flag=True
2708
- print(" #Warning(compare_tax_china): invalid date",endDate)
2709
- if error_flag: return None
2710
-
2711
- start=date_adjust(endDate, adjust=-365)
2712
- fs_dates=cvt_fs_dates(start,endDate,'all')
2713
- endDate=fs_dates[-1:][0]
2714
-
2715
- #获取实际所得税率
2716
- df=pd.DataFrame(columns=('ticker','name','date','tax rate%'))
2717
- for t in tickers:
2718
- try:
2719
- df0=prepare_hamada_ak(t,endDate,endDate,period_type='all')
2720
- except:
2721
- print(" #Error(compare_tax_china): failed to get financial info for",t)
2722
- continue
2723
- df1=df0.tail(1)
2724
- name=ticker_name(t,'stock')
2725
- reportdate=df1.index[0]
2726
- taxrate=df1['tax rate'][0]
2727
- row=pd.Series({'ticker':t,'name':name,'date':reportdate,'tax rate%':round(taxrate*100,2)})
2728
- try:
2729
- df=df.append(row,ignore_index=True)
2730
- except:
2731
- df=df._append(row,ignore_index=True)
2732
-
2733
- df.sort_values(by='tax rate%',ascending=False,inplace=True)
2734
- df['key']=df['name']
2735
- df.set_index('key',inplace=True)
2736
- #绘图
2737
- if graph:
2738
- print(" Calculating and drawing graph, please wait ...")
2739
- colname='tax rate%'
2740
- titletxt="企业横向对比: 实际所得税率"
2741
- import datetime; today=datetime.date.today()
2742
- itemk="实际所得税率%"
2743
- footnote=ectranslate(itemk)+" -->"+ \
2744
- "\n报表截止日期:"+endDate+ \
2745
- "\n数据来源: sina/EM, "+str(today)
2746
- plot_barh(df,colname,titletxt,footnote,datatag=datatag,tag_offset=tag_offset,axisamp=axisamp)
2747
- return df
2748
-
2749
-
2750
- #==============================================================================
2751
- if __name__ == '__main__':
2752
- ticker='600519.SS'
2753
- endDate='2021-09-30'
2754
-
2755
- def calc_igr_sgr_china(ticker,endDate):
2756
-
2757
- rates=['ROE','ROA','Payout Ratio']
2758
- rdf_list=prepare_fin_rate1tmr_china(ticker,rates,endDate,endDate,period_type='all')
2759
- if rdf_list is None: return None,None
2760
-
2761
- roe=rdf_list[0]['净资产收益率'].values[0]/100
2762
- roa=rdf_list[1]['资产报酬率(%)'].values[0]/100
2763
- try:
2764
- b=1-rdf_list[2]['股息发放率(%)'].values[0]/100
2765
- except:
2766
- b=1-0
2767
-
2768
- igr=round(roa*b/(1-roa*b),4)
2769
- sgr=round(roe*b/(1-roe*b),4)
2770
-
2771
- return igr,sgr
2772
-
2773
-
2774
- #==============================================================================
2775
- if __name__ == '__main__':
2776
- tickers=['600519.SS','600606.SS','000002.SZ']
2777
- endDate='latest'
2778
- graph=True
2779
- axisamp1=1.3
2780
- axisamp2=1.6
2781
-
2782
- def compare_igr_sgr_china(tickers,endDate='latest',graph=True,axisamp1=1.3,axisamp2=1.6):
2783
- """
2784
- 功能:比较公司的IGR和SGR
2785
- """
2786
- error_flag=False
2787
-
2788
- #检查股票代码列表
2789
- if not isinstance(tickers,list):
2790
- print(" #Warning(compare_igr_sgr_china): need more stock codes in",tickers)
2791
- error_flag=True
2792
- if len(tickers) < 2:
2793
- print(" #Warning(compare_igr_sgr_china): need more stock codes in",tickers)
2794
- error_flag=True
2795
- if error_flag: return None
2796
-
2797
- #获取最近的报表日期
2798
- if endDate == 'latest':
2799
- import datetime; endDate=datetime.date.today()
2800
- else:
2801
- #检查日期
2802
- valid_date=check_date(endDate)
2803
- if not valid_date:
2804
- error_flag=True
2805
- print(" #Warning(compare_igr_sgr_china): invalid date",endDate)
2806
- if error_flag: return None
2807
-
2808
- start=date_adjust(endDate, adjust=-365)
2809
- fs_dates=cvt_fs_dates(start,endDate,'all')
2810
- endDate=fs_dates[-1:][0]
2811
-
2812
- #逐个获取公司信息
2813
- df=pd.DataFrame(columns=('ticker','name','date','IGR%','SGR%'))
2814
- for t in tickers:
2815
- try:
2816
- igr,sgr=calc_igr_sgr_china(t,endDate)
2817
- except:
2818
- print(" #Warning(compare_igr_sgr_china): stock info not available for",t)
2819
- continue
2820
- if igr is None or sgr is None:
2821
- print(" #Warning(compare_igr_sgr_china): no stock info found for",t)
2822
- continue
2823
- name=ticker_name(t,'stock')
2824
- row=pd.Series({'ticker':t,'name':name,'IGR%':round(igr*100,2),'SGR%':round(sgr*100,2)})
2825
- try:
2826
- df=df.append(row,ignore_index=True)
2827
- except:
2828
- df=df._append(row,ignore_index=True)
2829
-
2830
- #绘制IGR
2831
- df.sort_values(by='IGR%',ascending=False,inplace=True)
2832
- df['key']=df['name']
2833
- df.set_index('key',inplace=True)
2834
- #绘图
2835
- if graph:
2836
- colname='IGR%'
2837
- titletxt="企业横向对比: 内部增长潜力"
2838
- import datetime; today=datetime.date.today()
2839
- itemk="(不依赖外部融资的)内部增长率(IGR)%"
2840
- footnote=ectranslate(itemk)+" -->"+ \
2841
- "\n报表截止日期:"+endDate+ \
2842
- "\n数据来源: sina/EM, "+str(today)
2843
- plot_barh(df,colname,titletxt,footnote,axisamp=axisamp1)
2844
-
2845
- #绘制SGR
2846
- df.sort_values(by='SGR%',ascending=False,inplace=True)
2847
- df['key']=df['name']
2848
- df.set_index('key',inplace=True)
2849
- #绘图
2850
- if graph:
2851
- print("...Calculating and drawing graph, please wait ...")
2852
- colname='SGR%'
2853
- titletxt="企业横向对比: 可持续增长潜力"
2854
- import datetime; today=datetime.date.today()
2855
- itemk="(不增加财务杠杆的)可持续增长率(SGR)%"
2856
- footnote=ectranslate(itemk)+" -->"+ \
2857
- "\n报表截止日期:"+endDate+ \
2858
- "\n数据来源: sina/EM, "+str(today)
2859
- plot_barh(df,colname,titletxt,footnote,axisamp=axisamp2)
2860
- return df
2861
- #==============================================================================
2862
- if __name__=='__main__':
2863
- ticker="600519.SS"
2864
- ticker="601398.SS"
2865
- ticker='000002.SZ'
2866
-
2867
- fsdate='2022-12-31'
2868
-
2869
- g=dupont_decompose_china(ticker,fsdate)
2870
-
2871
- def dupont_decompose_china(ticker,fsdate,gview=False,facecolor='papayawhip'):
2872
- """
2873
- 功能:杜邦分析分解图
2874
- ticker: 股票代码
2875
- fsdate: 财报日期
2876
- gview: False为嵌入式显示,True为分离式显示
2877
- """
2878
- #检查日期
2879
- result,fspd,_=check_period(fsdate,fsdate)
2880
- if not result:
2881
- print(" #Error(dupont_decompose_china): invalid date",fsdate)
2882
- return None
2883
-
2884
- #获取财报
2885
- fs=get_fin_stmt_ak(ticker)
2886
- if fs is None:
2887
- print(" #Error(dupont_decompose_china): failed to access financial stmts for",ticker)
2888
- return None
2889
-
2890
- fs1=fs[fs.index==fspd]
2891
- if len(fs1)==0:
2892
- print(" #Error(dupont_decompose_china): financial statements not found for",ticker,'@',fsdate)
2893
- return None
2894
-
2895
- #亿元
2896
- yi=100000000
2897
-
2898
- company_name=ticker_name(ticker,'stock')
2899
- # 定义杜邦分解项目变量
2900
-
2901
- roe='【'+company_name+'】\n('+fsdate+')\n'+'净资产收益率'
2902
- try:
2903
- totalOEValue=round(fs1['所有者权益(或股东权益)合计'].values[0] / yi,1)
2904
- except:
2905
- try:
2906
- totalOEValue=round(fs1['股东权益合计'].values[0] / yi,1)
2907
- except:
2908
- totalOEValue=round(fs1['所有者权益合计'].values[0] / yi,1)
2909
-
2910
- roa='总资产净利率'
2911
- em='权益乘数'
2912
- pm='销售净利率'
2913
- tat='总资产周转率'
2914
-
2915
- debtRatio='资产负债率'
2916
- totalLiabValue=round(fs1['负债合计'].values[0] / yi,1)
2917
-
2918
- netProfit='净利润'
2919
- netProfitValue=round(fs1['净利润'].values[0] / yi,1)
2920
- roePct=round(netProfitValue / totalOEValue *100,2)
2921
-
2922
- sales='销售收入'
2923
- try:
2924
- salesValue=round(fs1['营业总收入'].values[0] / yi,1)
2925
- except:
2926
- salesValue=round(fs1['营业收入'].values[0] / yi,1)
2927
-
2928
- pmPct=round(netProfitValue / salesValue *100,2)
2929
-
2930
- totalAssets='资产总额'
2931
- totalAssetsValue=round(fs1['资产总计'].values[0] / yi,1)
2932
- tatValue=round(salesValue / totalAssetsValue *100,2)
2933
- emValue=round(totalAssetsValue / totalOEValue,2)
2934
- debtRatioPct=round(totalLiabValue / totalAssetsValue *100,2)
2935
- roaPct=round(netProfitValue / totalAssetsValue *100,2)
2936
-
2937
- totalCosts='成本费用'
2938
- try:
2939
- totalCostsValue=round(fs1['营业总成本'].values[0] / yi,1)
2940
- except:
2941
- totalCostsValue=round(fs1['营业支出'].values[0] / yi,1)
2942
-
2943
- currentAssets='流动资产'
2944
- try:
2945
- currentAssetsValue=round(fs1['流动资产合计'].values[0] / yi,1)
2946
- except:
2947
- try:
2948
- currentAssetsValue=round((fs1['现金及存放中央银行款项'].values[0] \
2949
- + fs1['存放同业款项'].values[0] \
2950
- + fs1['拆出资金'].values[0] \
2951
- + fs1['贵金属'].values[0] \
2952
- + fs1['交易性金融资产'].values[0] \
2953
- + fs1['衍生金融工具资产'].values[0] \
2954
- + fs1['买入返售金融资产'].values[0] \
2955
- + fs1['应收利息'].values[0] \
2956
- )/ yi,1)
2957
- except:
2958
- currentAssetsValue=round((fs1['货币资金'].values[0] \
2959
- + fs1['拆出资金'].values[0] \
2960
- + fs1['交易性金融资产'].values[0] \
2961
- + fs1['衍生金融资产'].values[0] \
2962
- + fs1['买入返售金融资产'].values[0] \
2963
- + fs1['应收保费'].values[0] \
2964
- + fs1['应收利息'].values[0] \
2965
- + fs1['应收分保账款'].values[0] \
2966
- + fs1['应收分保未到期责任准备金'].values[0] \
2967
- + fs1['应收分保未决赔款准备金'].values[0] \
2968
- + fs1['应收分保寿险责任准备金'].values[0] \
2969
- + fs1['应收分保长期健康险责任准备金'].values[0] \
2970
- )/ yi,1)
2971
-
2972
- LTAssets='非流动资产'
2973
- try:
2974
- LTAssetsValue=round(fs1['非流动资产合计'].values[0] / yi,1)
2975
- except:
2976
- try:
2977
- LTAssetsValue=round((fs1['发放贷款及垫款'].values[0] \
2978
- + fs1['代理业务资产'].values[0] \
2979
- + fs1['可供出售金融资产'].values[0] \
2980
- + fs1['持有至到期投资'].values[0] \
2981
- + fs1['长期股权投资'].values[0] \
2982
- + fs1['应收投资款项'].values[0] \
2983
- + fs1['固定资产合计'].values[0] \
2984
- + fs1['无形资产'].values[0] \
2985
- + fs1['商誉'].values[0] \
2986
- + fs1['递延税款借项'].values[0] \
2987
- + fs1['投资性房地产'].values[0] \
2988
- + fs1['其他资产'].values[0] \
2989
- )/ yi,1)
2990
- except:
2991
- LTAssetsValue=round((fs1['保户质押贷款'].values[0] \
2992
- + fs1['存出资本保证金'].values[0] \
2993
- + fs1['可供出售金融资产'].values[0] \
2994
- + fs1['持有至到期投资'].values[0] \
2995
- + fs1['长期股权投资'].values[0] \
2996
- + fs1['应收款项类投资'].values[0] \
2997
- + fs1['固定资产'].values[0] \
2998
- + fs1['无形资产'].values[0] \
2999
- + fs1['商誉'].values[0] \
3000
- + fs1['独立账户资产'].values[0] \
3001
- + fs1['递延所得税资产'].values[0] \
3002
- + fs1['投资性房地产'].values[0] \
3003
- + fs1['定期存款'].values[0] \
3004
- + fs1['其他资产'].values[0] \
3005
- )/ yi,1)
3006
-
3007
- salesCosts='营业\\n成本'
3008
- try:
3009
- salesCostsValue=round(fs1['营业成本'].values[0] / yi,1)
3010
- except:
3011
- salesCostsValue=round(fs1['营业支出'].values[0] / yi,1)
3012
-
3013
- periodExpenses='期间\\n费用'
3014
- salesExpenses='销售\\n费用'
3015
- try:
3016
- salesExpensesValue=round(fs1['销售费用'].values[0] / yi,1)
3017
- except:
3018
- salesExpensesValue=0.0
3019
-
3020
- mgmtExpenses='管理\\n费用'
3021
- try:
3022
- mgmtExpensesValue=round(fs1['管理费用'].values[0] / yi,1)
3023
- except:
3024
- mgmtExpensesValue=round(fs1['业务及管理费用'].values[0] / yi,1)
3025
-
3026
- rndExpenses='研发\\n费用'
3027
- rndExpensesValue=round(fs1['研发费用'].values[0] / yi,1)
3028
- """
3029
- #是否中国股票
3030
- result,prefix,suffix=split_prefix_suffix(ticker)
3031
- if not (suffix in SUFFIX_LIST_CN):
3032
- print(" #Error(dupont_decompose_china): not a stock in China",ticker)
3033
- return None
3034
-
3035
- #财务报告摘要
3036
- try:
3037
- fsabs = ak.stock_financial_abstract(prefix)
3038
- except:
3039
- print(" #Warning(dupont_decompose_china): financial summary not found for",ticker)
3040
- return None
3041
- """
3042
- financialExpenses='财务\\n费用'
3043
- try:
3044
- financialExpensesValue=round((fs1['财务费用'].values[0])/ yi,1)
3045
- except:
3046
- financialExpensesValue=round(fs1['应付利息'].values[0] / yi,1)
3047
-
3048
- taxExpenses='税金'
3049
- try:
3050
- taxExpensesValue=round((fs1['营业税金及附加'].values[0] + fs1['所得税费用'].values[0]) / yi,1)
3051
- except:
3052
- try:
3053
- taxExpensesValue=round((fs1['营业税金及附加'].values[0] + fs1['减:所得税'].values[0]) / yi,1)
3054
- except:
3055
- taxExpensesValue=round((fs1['营业税金及附加'].values[0] + fs1['减:所得税费用'].values[0]) / yi,1)
3056
-
3057
- """
3058
- otherExpenses='其他支出'
3059
- otherExpensesVaue=round((fs1['开发支出'].values[0] + fs1['减:营业外支出'].values[0]) / yi,1)
3060
- """
3061
- monetaryFunds='货币\\n资金'
3062
- try:
3063
- monetaryFundsValue=round(fs1['货币资金'].values[0] / yi,1)
3064
- except:
3065
- monetaryFundsValue=round(fs1['现金及存放中央银行款项'].values[0] / yi,1)
3066
-
3067
- securityAssets='金融\\n资产'
3068
- try:
3069
- securityAssetsValue=round((fs1['交易性金融资产'].values[0] + \
3070
- fs1['衍生金融资产'].values[0] + \
3071
- fs1['买入返售金融资产'].values[0]) / yi,1)
3072
- except:
3073
- securityAssetsValue=round((fs1['交易性金融资产'].values[0] + \
3074
- fs1['衍生金融工具资产'].values[0] + \
3075
- fs1['买入返售金融资产'].values[0]) / yi,1)
3076
-
3077
- ar_prepaid='应收\\n与\\n预付'
3078
- accountReceivables='应收\\n款项'
3079
- try:
3080
- accountReceivablesValue=round((fs1['应收票据及应收账款'].values[0] + fs1['其他应收款(合计)'].values[0]) / yi,1)
3081
- except:
3082
- try:
3083
- accountReceivablesValue=round((fs1['应收保费'].values[0] + \
3084
- fs1['应收利息'].values[0] + \
3085
- fs1['应收分保账款'].values[0] + \
3086
- fs1['应收分保未到期责任准备金'].values[0] + \
3087
- fs1['应收分保未决赔款准备金'].values[0] + \
3088
- fs1['应收分保寿险责任准备金'].values[0] + \
3089
- fs1['应收分保长期健康险责任准备金'].values[0]) / yi,1)
3090
- except:
3091
- accountReceivablesValue=round((fs1['应收利息'].values[0] + \
3092
- fs1['应收投资款项'].values[0]) / yi,1)
3093
-
3094
- prepaid='预付\\n款项'
3095
- try:
3096
- prepaidValue=round(fs1['预付款项'].values[0] / yi,1)
3097
- except:
3098
- prepaidValue=0.0
3099
-
3100
- inventory='存货'
3101
- try:
3102
- inventoryValue=round(fs1['存货'].values[0] / yi,1)
3103
- except:
3104
- inventoryValue=0.0
3105
-
3106
- otherCurrentAssets='其他\\n流动\\n资产'
3107
- try:
3108
- otherCurrentAssetsValue=round(fs1['其他流动资产'].values[0] / yi,1)
3109
- except:
3110
- otherCurrentAssetsValue=0.0
3111
-
3112
- fixedAssets='固定\\n资产'
3113
- try:
3114
- fixedAssetsValue=round(fs1['固定资产及清理合计'].values[0] / yi,1)
3115
- except:
3116
- try:
3117
- fixedAssetsValue=round(fs1['固定资产合计'].values[0] / yi,1)
3118
- except:
3119
- fixedAssetsValue=round(fs1['固定资产'].values[0] / yi,1)
3120
-
3121
- LTInvestment='长期\\n投资'
3122
- try:
3123
- LTInvestmentValue=round((fs1['发放贷款及垫款'].values[0] + \
3124
- fs1['可供出售金融资产'].values[0] + \
3125
- fs1['持有至到期投资'].values[0] + \
3126
- #fs1['长期应收款'].values[0] + \
3127
- fs1['长期股权投资'].values[0] + \
3128
- fs1['投资性房地产'].values[0] + \
3129
- fs1['在建工程(合计)'].values[0]) / yi,1)
3130
- except:
3131
- try:
3132
- LTInvestmentValue=round((fs1['发放贷款及垫款'].values[0] + \
3133
- fs1['可供出售金融资产'].values[0] + \
3134
- #fs1['持有至到期投资'].values[0] + \
3135
- #fs1['长期应收款'].values[0] + \
3136
- fs1['长期股权投资'].values[0] + \
3137
- fs1['投资性房地产'].values[0]) / yi,1)
3138
- except:
3139
- LTInvestmentValue=round((#fs1['保户质押贷款'].values[0] + \
3140
- fs1['可供出售金融资产'].values[0] + \
3141
- #fs1['持有至到期投资'].values[0] + \
3142
- fs1['长期股权投资'].values[0] + \
3143
- fs1['投资性房地产'].values[0]) / yi,1)
3144
-
3145
- intangibleAssets='无形\\n资产'
3146
- intangibleAssetsValue=round(fs1['无形资产'].values[0] / yi,1)
3147
-
3148
- deferredAssets='递延\\n资产'
3149
- try:
3150
- deferredAssetsValue=round(fs1['递延所得税资产'].values[0] / yi,1)
3151
- except:
3152
- deferredAssetsValue=round(fs1['递延税款借项'].values[0] / yi,1)
3153
-
3154
- goodwill='商誉'
3155
- goodwillValue=round(fs1['商誉'].values[0] / yi,1)
3156
-
3157
- #合成具体的分解项目
3158
- roe=roe+'\n'+str(roePct)+'%'
3159
- roa=roa+'\n'+str(roaPct)+'%'
3160
- em=em+'\n'+str(emValue)
3161
- pm=pm+'\n'+str(pmPct)+'%'
3162
- tat=tat+'\n'+str(tatValue)
3163
-
3164
- netProfit=netProfit+'\n'+str(netProfitValue)
3165
- totalAssets=totalAssets+'\n'+str(totalAssetsValue)
3166
- totalCosts=totalCosts+'\n'+str(totalCostsValue)
3167
- sales=sales+'\n'+str(salesValue)
3168
- currentAssets=currentAssets+'\n'+str(currentAssetsValue)
3169
- LTAssets=LTAssets+'\n'+str(LTAssetsValue)
3170
-
3171
- salesCosts=salesCosts+'\n'+str(salesCostsValue)
3172
- taxExpenses=taxExpenses+'\n'+str(taxExpensesValue)
3173
-
3174
- salesExpenses=salesExpenses+'\n'+str(salesExpensesValue)
3175
- mgmtExpenses=mgmtExpenses+'\n'+str(mgmtExpensesValue)
3176
- financialExpenses=financialExpenses+'\n'+str(financialExpensesValue)
3177
- rndExpenses=rndExpenses+'\n'+str(rndExpensesValue)
3178
-
3179
- monetaryFunds=monetaryFunds+'\n'+str(monetaryFundsValue)
3180
- securityAssets=securityAssets+'\n'+str(securityAssetsValue)
3181
- accountReceivables=accountReceivables+'\n'+str(accountReceivablesValue)
3182
- prepaid=prepaid+'\n'+str(prepaidValue)
3183
- inventory=inventory+'\n'+str(inventoryValue)
3184
-
3185
- fixedAssets=fixedAssets+'\n'+str(fixedAssetsValue)
3186
- LTInvestment=LTInvestment+'\n'+str(LTInvestmentValue)
3187
- intangibleAssets=intangibleAssets+'\n'+str(intangibleAssetsValue)
3188
-
3189
- #下面字段:“序号”、“父单位”、“父单位层级”、“子单位”、“子单位层级”、“父单位持股比例”
3190
- #注意:最后面的空格字段为必须,否则显示顺序不受控
3191
- L=[
3192
- [1, roe, 1, roa, 2, ' '],
3193
- [2, roe, 1, em, 2, ' '],
3194
- [3, roa, 2, pm, 3, ' '],
3195
- [4, roa, 2, tat, 3, ' '],
3196
- [5, pm, 3, netProfit, 4, ' '],
3197
- [6, pm, 3, sales, 4, ' '],
3198
- [7, netProfit, 4, sales, 5, ' '],
3199
- [8, netProfit, 4, totalCosts, 5, ' '],
3200
- [9, totalCosts, 5, salesCosts, 6, ' '],
3201
-
3202
- [10, totalCosts, 5, periodExpenses, 6, ' '],
3203
- [11, periodExpenses, 6, salesExpenses, 7, ' '],
3204
- [12, periodExpenses, 6, mgmtExpenses, 7, ' '],
3205
- [13, periodExpenses, 6, financialExpenses, 7, ' '],
3206
- [14, periodExpenses, 6, rndExpenses, 7, ' '],
3207
-
3208
- [15, totalCosts, 5, taxExpenses, 6, ' '],
3209
-
3210
- [16, tat, 3, sales, 4, ' '],
3211
- [17, tat, 3, totalAssets, 4, ' '],
3212
- [18, totalAssets, 4, currentAssets, 5, ' '],
3213
- [19, totalAssets, 4, LTAssets, 5, ' '],
3214
-
3215
- [20, currentAssets, 5, monetaryFunds, 6, ' '],
3216
- [21, currentAssets, 5, securityAssets, 6, ' '],
3217
-
3218
- [22, currentAssets, 5, ar_prepaid, 6, ' '],
3219
- [23, ar_prepaid, 6, accountReceivables, 7, ' '],
3220
- [24, ar_prepaid, 6, prepaid, 7, ' '],
3221
-
3222
- [25, currentAssets, 5, inventory, 10, ' '],
3223
- #[26, currentAssets, 5, otherCurrentAssets, 11, ' '],
3224
-
3225
- [27, LTAssets, 5, fixedAssets, 6, ' '],
3226
- [28, LTAssets, 5, LTInvestment, 6, ' '],
3227
- [29, LTAssets, 5, intangibleAssets, 6, ' '],
3228
- #[30, LTAssets, 5, deferredAssets, 6, ' '],
3229
- #[31, LTAssets, 5, goodwill, 6, ' '],
3230
-
3231
- ]
3232
-
3233
- dic={}
3234
- father_name_list=[]
3235
- child_name_list=[]
3236
- equity_portion_list=[]
3237
- for i1 in range(len(L)):
3238
-
3239
- M=L[i1]
3240
- father_name=M[1]
3241
- try:
3242
- father_name_list.append(M[1])
3243
- father_layer=M[2]
3244
- child_name=M[3]
3245
- child_name_list.append(M[3])
3246
- child_layer=M[4]
3247
- equity_portion=M[5]
3248
- equity_portion_list.append(M[5])
3249
- except:
3250
- father_name_list._append(M[1])
3251
- father_layer=M[2]
3252
- child_name=M[3]
3253
- child_name_list._append(M[3])
3254
- child_layer=M[4]
3255
- equity_portion=M[5]
3256
- equity_portion_list._append(M[5])
3257
-
3258
- #debug使用
3259
- #print(i1,M,father_name,father_layer,child_name,child_layer,equity_portion)
3260
-
3261
- for x in father_name:
3262
- dic[father_name]=father_layer #生成父单位名称和对应的层级(用字典考虑去重)
3263
-
3264
- for y in child_name:
3265
- dic[child_name]=child_layer #将子单位名称和对应的层级也添加到字典中
3266
-
3267
- name_layer_list = sorted(dic.items(), key=lambda x: x[1]) #对字典按值(value)进行排序(默认由小到大)
3268
-
3269
- u=[]
3270
- for z in name_layer_list:
3271
- company_name=z[0]
3272
- layer=z[1]
3273
- try:
3274
- u.append(z[1])
3275
- except:
3276
- u._append(z[1])
3277
- number_of_layers=max(u) #计算出层数
3278
-
3279
- from graphviz import Digraph
3280
- #按各公司的层数生产分层的节点:
3281
- g=Digraph(name=ticker_name(ticker,'stock')+fsdate)
3282
-
3283
- for key in dic:
3284
- for n in range(number_of_layers+1):
3285
- if dic[key]==n:
3286
- with g.subgraph() as layer_n:
3287
- layer_n.attr(rank='same')
3288
- layer_n.node(name=key,color='blue',shape='box', \
3289
- fontname='Microsoft YaHei',fontsize='11', \
3290
- nodesranksep='0.5',nodesnodesep='0.5')
3291
-
3292
- #生产各节点间的连线:
3293
- for i2 in range(len(L)):
3294
- g.edge(father_name_list[i2],child_name_list[i2],label=equity_portion_list[i2], \
3295
- color='red',fontname='Microsoft Yahei',edgeweight='1.0',fontsize='11')
3296
-
3297
- if gview:
3298
- # 分离式显示,便于多图对比
3299
- g.view()
3300
- else:
3301
- # 嵌入式显示
3302
- display(g)
3303
-
3304
- #打印信息
3305
- if not gview:
3306
- print("\n注:",ticker_name(ticker,'stock'),"\b,金额单位:亿元,财报日期:",fsdate)
3307
- print("1、为避免图示过大,这里未列出所有分解项目")
3308
- print("2、金融机构报表与普通企业结构不同,此处仅为约算")
3309
- print("3、应收款项包括应收账款、应收利息、应收保费以及应收票据等")
3310
- print("4、递延资产为递延所得税资产或借项")
3311
- print("5、税金包括营业税金及附加以及所得税费用")
3312
- print("6、此处金融资产主要为交易性金融资产、衍生金融资产和买入返售金融资产")
3313
- print("7、此处长期投资包括贷款及垫款、可供出售金融资产、持有至到期投资、长期股权投资、投资性房地产等")
3314
- print("8、注意:图中比率和比值均为基于财报期末数值直接计算,并非基于期初期末均值,可能与公告数字存在差异。")
3315
-
3316
- return g
3317
-
3318
- if __name__=='__main__':
3319
- ticker="600519.SS"
3320
- fsdate='2022-12-31'
3321
-
3322
- g=dupont_decompose_china(ticker,fsdate)
3323
-
3324
- #==============================================================================
3325
- if __name__=='__main__':
3326
- tickers=['000002.SZ','601398.SS']
3327
- fsdates=['2022-12-31','2021-12-31']
3328
-
3329
-
3330
- def get_fin_summary_china_original(tickers,fsdates):
3331
- """
3332
- 功能:获得A股财报摘要,进行数据整理
3333
- 输出:
3334
- 1、项目列表:不带(元)、(次)、(%)等后缀括弧
3335
- 2、数量级变换:(元)-->(亿元),并相应修改字段名
3336
-
3337
- 废弃!
3338
- """
3339
-
3340
- import pandas as pd
3341
- df=pd.DataFrame()
3342
- # 获得股票
3343
- for t in tickers:
3344
- _,t1,_=split_prefix_suffix(t)
3345
- dft=ak.stock_financial_abstract(t1)
3346
-
3347
- dft['ticker']=t
3348
-
3349
- if len(df)==0:
3350
- df=dft
3351
- else:
3352
- df=pd.concat([df,dft])
3353
-
3354
- # 遍历
3355
- collist=list(df)
3356
- colremove=['选项','指标','ticker']
3357
- for cr in colremove:
3358
- collist.remove(cr)
3359
- noamtlist=["率","每股","周转","乘数",'/']
3360
- yiyuan=100000000
3361
-
3362
- for index,row in df.iterrows():
3363
- #print(index,row)
3364
- # 金额变为亿元,不含"率"、"每股"、"周转"、"乘数"
3365
-
3366
- if '率' in row['指标']:
3367
- if not ('周转率' in row['指标']):
3368
- df.at[index,'指标']=row['指标']+'(%)'
3369
- for d in collist:
3370
- df.at[index,d] = round(row[d],2)
3371
- continue
3372
-
3373
- if '/' in row['指标']:
3374
- df.at[index,'指标']=row['指标']+'(%)'
3375
- for d in collist:
3376
- df.at[index,d] = round(row[d],2)
3377
- continue
3378
-
3379
- if '每股' in row['指标']:
3380
- df.at[index,d] = round(row[d],2)
3381
- continue
3382
-
3383
- # 元变换为亿元
3384
- for d in collist:
3385
- df.at[index,d] = round(row[d] / yiyuan,1)
3386
- df.at[index,'指标']=row['指标']+'(亿元)'
3387
-
3388
- # 填充nan为零
3389
- df.fillna('-',inplace=True)
3390
-
3391
- # 改变日期格式
3392
- for c in collist:
3393
- try:
3394
- c1=pd.to_datetime(c)
3395
- c2=c1.strftime("%Y-%m-%d")
3396
- except:
3397
- continue
3398
- df.rename(columns={c:c2},inplace=True)
3399
-
3400
- # 过滤财报日期
3401
- fsdates2=[]
3402
- for d in fsdates:
3403
- d1=pd.to_datetime(d)
3404
- d2=d1.strftime("%Y-%m-%d")
3405
- fsdates2=fsdates2 + [d2]
3406
-
3407
- collistnew=colremove+fsdates2
3408
- fsdf=df[collistnew]
3409
-
3410
- return fsdf
3411
-
3412
- #==============================================================================
3413
- if __name__=='__main__':
3414
- astr='存货周转天数'
3415
- substrlist=['乘数','每股','/','周转']
3416
- str_contain_any_substr(astr,substrlist)
3417
-
3418
-
3419
- def str_contain_any_substr(astr,substrlist):
3420
- """
3421
- 功能:判断astr是否含有子串列表substrlist之一
3422
- """
3423
- result=False
3424
- for sub in substrlist:
3425
- if sub in astr:
3426
- result=True
3427
-
3428
- return result
3429
-
3430
- #==============================================================================
3431
- if __name__=='__main__':
3432
- ticker='600305.SS'
3433
- fsdates=['2023-6-30','2022-6-30','2021-6-30']
3434
-
3435
- ticker='002352.SZ'
3436
- fsdates=['2024-12-31','2023-12-31','2022-12-31']
3437
-
3438
- fsdf1=get_fin_summary_1ticker_china(ticker,fsdates)
3439
-
3440
- def get_fin_summary_1ticker_china(ticker,fsdates):
3441
- """
3442
- 功能:获得A股财报摘要,进行数据整理,单一股票
3443
- 输出:
3444
- 1、数量级变换:(元)-->(亿元),并相应修改字段名
3445
- """
3446
-
3447
- import pandas as pd
3448
- # 过滤财报日期
3449
- fsdates2=[]
3450
- for d in fsdates:
3451
- d1=pd.to_datetime(d)
3452
- d2=d1.strftime("%Y-%m-%d")
3453
- fsdates2=fsdates2 + [d2]
3454
- fsdates3=sorted(fsdates2,reverse=True)
3455
-
3456
- yiyuan=float(1e+08)
3457
-
3458
- # 获得股票所有财报数据
3459
- print(" Retrieving financial abstract of",ticker,'... ...')
3460
- _,t1,_=split_prefix_suffix(ticker)
3461
- dft=ak.stock_financial_abstract(t1)
3462
-
3463
- #akshare错误更正:此处的“营业成本”其实为“营业总成本”
3464
- if ("营业成本" in list(dft['指标'])) and not ("营业总成本" in list(dft['指标'])):
3465
- dft.loc[dft['指标']=="营业成本","指标"]="营业总成本"
3466
-
3467
- # 处理金额单位和百分号字段
3468
- # 选项-指标对照表
3469
- option_indicator_df=dft[['选项','指标']]
3470
- option_indicator_df.set_index('指标',inplace=True)
3471
-
3472
- # 变换日期格式为YYYY-MM-DD
3473
- collist=list(dft)
3474
- for c in collist:
3475
- try:
3476
- c1=pd.to_datetime(c)
3477
- c2=c1.strftime("%Y-%m-%d")
3478
- except:
3479
- continue
3480
- dft.rename(columns={c:c2},inplace=True)
3481
-
3482
- # 过滤财报日期
3483
- try:
3484
- dft2=dft[['选项','指标']+fsdates3]
3485
- except:
3486
- print(" #Warning(get_fin_summary_1ticker_china): fin stmt of",fsdates3[0],"unavailable for",ticker+'('+ticker_name(ticker,'stock')+')')
3487
- return None
3488
-
3489
- # 金额变换:元-->亿元,小数位截取
3490
- dft2=dft2.drop(labels=['选项'],axis=1)
3491
- # 去掉重复行,非常重要!!!
3492
- dft2b=dft2.drop_duplicates (keep='first')
3493
-
3494
- dft2b.set_index('指标',inplace=True)
3495
-
3496
- dft2t=dft2b.T
3497
-
3498
- noneamtlist=['乘数','每股','/','周转','率','天数']
3499
- collist2=list(dft2t)
3500
-
3501
- for c in collist2:
3502
-
3503
- if not str_contain_any_substr(c,noneamtlist):
3504
- dft2t[c]=dft2t[c] / yiyuan
3505
-
3506
- if ('/' in c) and ('现金' in c):
3507
- dft2t[c]=dft2t[c] * 100.0
3508
-
3509
- dft2t[c]=dft2t[c].apply(lambda x: round(x,2))
3510
-
3511
- # 标记字段后缀
3512
- dft3=dft2t.T
3513
- dft3['指标2']=dft3.index
3514
- dft3t=dft3.T
3515
- collist3=list(dft3t)
3516
- for c in collist3:
3517
- # 判断顺序不可颠倒
3518
- if str_contain_any_substr(c,['乘数','周转']):
3519
- continue
3520
- if str_contain_any_substr(c,['率']):
3521
- dft3t.rename(columns={c:c+'%'},inplace=True)
3522
- continue
3523
- if str_contain_any_substr(c,['每股']):
3524
- dft3t.rename(columns={c:c+'(元)'},inplace=True)
3525
- continue
3526
- if str_contain_any_substr(c,['/']):
3527
- dft3t.rename(columns={c:c+'(%)'},inplace=True)
3528
- continue
3529
-
3530
- # 其余的字段:元变换为亿元
3531
- dft3t.rename(columns={c:c+'(亿元)'},inplace=True)
3532
-
3533
- # 检查字段改名情况
3534
- collist3t=list(dft3t)
3535
-
3536
- # 回填指标选项类型
3537
- dft4=dft3t.T
3538
- dft5=dft4.reset_index()
3539
- dft5.set_index('指标2',inplace=True)
3540
- dft6=dft5.merge(option_indicator_df,how='left',left_index=True, right_index=True)
3541
-
3542
- dft7=dft6.reset_index(drop=True)
3543
-
3544
- dft7['ticker']=ticker
3545
-
3546
- # 填充nan为零
3547
- dft7.fillna('-',inplace=True)
3548
-
3549
- return dft7
3550
-
3551
- #==============================================================================
3552
- if __name__=='__main__':
3553
- tickers=['000002.SZ','601398.SS']
3554
- fsdates=['2022-12-31','2021-12-31','2020-12-31']
3555
-
3556
- fsdfm=get_fin_summary_china(tickers,fsdates)
3557
-
3558
-
3559
- def get_fin_summary_china(tickers,fsdates):
3560
- """
3561
- 功能:获得A股财报摘要,进行数据整理
3562
- 输出:
3563
- 1、数量级变换:(元)-->(亿元),并相应修改字段名
3564
- """
3565
-
3566
- import pandas as pd
3567
- # 过滤财报日期
3568
- fsdates2=[]
3569
- for d in fsdates:
3570
- d1=pd.to_datetime(d)
3571
- d2=d1.strftime("%Y-%m-%d")
3572
- fsdates2=fsdates2 + [d2]
3573
- fsdates3=list(set(fsdates2))
3574
- fsdates4=sorted(fsdates3,reverse=True)
3575
-
3576
- df=pd.DataFrame()
3577
-
3578
- # 获得多只股票财报
3579
- for t in tickers:
3580
- # 抓取一只股票的财报
3581
- dft=get_fin_summary_1ticker_china(t,fsdates4)
3582
- if dft is None:
3583
- continue
3584
-
3585
- if len(df)==0:
3586
- df=dft
3587
- else:
3588
- df=pd.concat([df,dft])
3589
-
3590
- return df
3591
-
3592
- if __name__=='__main__':
3593
- tickers=['000002.SZ','601398.SS']
3594
- fsdates=['2022-12-31','2021-12-31','2020-12-31']
3595
-
3596
- tickers='000002.SZ'
3597
- fsdates=['2022-12-31','2021-12-31','2020-12-31']
3598
-
3599
- tickers=['000002.SZ','600048.SS','001979.SZ','600325.SS','000069.SZ','600383.SS','600895.SS','601155.SS']
3600
- fsdates='2022-12-31'
3601
-
3602
- tickers=['002373.SZ', '002279.SZ', '002368.SZ', '600410.SS', '603927.SS', '002405.SS']
3603
- fsdates='2023-12-31'
3604
-
3605
-
3606
- def compare_fin_summary_china(tickers,fsdates,facecolor='whitesmoke',font_size='16px'):
3607
- """
3608
- 功能:分类别显示财报摘要中的指标
3609
- """
3610
- DEBUG=False
3611
-
3612
- # 检查股票列表
3613
- if isinstance(tickers,str):
3614
- tickers=[tickers]
3615
- tickers=list(tickers) #强制转换
3616
- if len(tickers)==0:
3617
- print(" #Error(compare_fin_summary_china): need at least one stock in",tickers)
3618
- return None
3619
-
3620
- # 检查财报日期列表
3621
- if isinstance(fsdates,str):
3622
- fsdates=[fsdates]
3623
- if len(fsdates)==0:
3624
- print(" #Error(compare_fin_summary_china): need at least one date in",fsdates)
3625
- return None
3626
-
3627
- for d in fsdates:
3628
- result,_,_=check_period(d,d)
3629
- if not result:
3630
- print(" #Error(compare_fin_summary_china): invalid date",d)
3631
- return None
3632
-
3633
- # 获取财报数据
3634
- print("Searching for financial statements, please wait ...")
3635
- fsdf=get_fin_summary_china(tickers,fsdates)
3636
-
3637
- # 不改变列表顺序去重
3638
- tickerlist=list(fsdf['ticker'])
3639
- tickers_found=sorted(list(set(tickerlist)),key=tickerlist.index)
3640
-
3641
- #optionlist=list(fsdf['选项'])
3642
- #typelist=sorted(list(set(optionlist)),key=optionlist.index)
3643
- typelist=['常用指标','每股指标','营运能力','盈利能力','收益质量','成长能力','财务风险']
3644
- # 手工设定项目显示顺序,使其看起来更合理
3645
- typedict={'常用指标':
3646
- ['营业总收入(亿元)','营业总成本(亿元)',
3647
- '净利润(亿元)','扣非净利润(亿元)','归母净利润(亿元)',
3648
- '毛利率%','销售净利率%','期间费用率%',
3649
- '基本每股收益(元)','总资产报酬率(ROA)%','净资产收益率(ROE)%',
3650
- '经营现金流量净额(亿元)','每股现金流(元)',
3651
- '股东权益合计(净资产)(亿元)','每股净资产(元)',
3652
- '资产负债率%','商誉(亿元)',],
3653
-
3654
- '每股指标':
3655
- ['每股营业总收入(元)',#'每股营业收入(元)',
3656
- '每股息税前利润(元)','基本每股收益(元)','稀释每股收益(元)',
3657
- '每股净资产_最新股数(元)','摊薄每股净资产_期末股数(元)','调整每股净资产_期末股数(元)',
3658
- '每股未分配利润(元)','每股留存收益(元)','每股盈余公积金(元)','每股资本公积金(元)',
3659
- '每股现金流量净额(元)','每股经营现金流(元)','每股企业自由现金流量(元)','每股股东自由现金流量(元)',],
3660
-
3661
- '营运能力':
3662
- ['总资产周转率','总资产周转天数',
3663
- '流动资产周转天数','流动资产周转率',
3664
- '存货周转率','存货周转天数',
3665
- '应收账款周转率','应收账款周转天数','应付账款周转率',],
3666
-
3667
- '盈利能力':
3668
- ['毛利率%','营业利润率%','息税前利润率%','销售净利率%','成本费用利润率%',
3669
- '总资产净利率_平均%','总资产净利率_平均(含少数股东损益)%',
3670
- '总资产报酬率%', '总资本回报率%','息前税后总资产报酬率_平均%',
3671
- '净资产收益率(ROE)%','净资产收益率_平均%','净资产收益率_平均_扣除非经常损益%',
3672
- '摊薄净资产收益率%','摊薄净资产收益率_扣除非经常损益%',
3673
- '投入资本回报率%',],
3674
-
3675
- '收益质量':
3676
- ['销售成本率%','成本费用率%','期间费用率%','所得税/利润总额(%)',
3677
- '经营性现金净流量/营业总收入(%)','经营活动净现金/销售收入(%)',
3678
- '经营活动净现金/归属母公司的净利润(%)',],
3679
-
3680
- '成长能力':
3681
- ['营业总收入(亿元)','营业总收入增长率%',
3682
- '净利润(亿元)','扣非净利润(亿元)',
3683
- '归母净利润(亿元)','归属母公司净利润增长率%',],
3684
-
3685
- '财务风险':
3686
- ['流动比率%','速动比率%','保守速动比率%','现金比率%','资产负债率%',
3687
- '权益乘数','权益乘数(含少数股权的净资产)','产权比率%',],
3688
- }
3689
-
3690
- # 注释
3691
- notesdict={'常用指标': \
3692
- '注:\n'+ \
3693
- '毛利率=毛利润 / 营业(总)收入 ×100% \n'+ \
3694
- '毛利润=营业(总)收入-营业(总)成本 \n'+ \
3695
- '销售净利率=净利润 / 营业(总)收入 ×100% \n'+ \
3696
- '期间费用率=期间费用 / 营业(总)收入 ×100%,期间费用包括管理费用,销售费用和财务费用。 \n', \
3697
-
3698
- '每股指标': \
3699
- '注:\n'+ \
3700
- '稀释每股指标:假设企业所有发行在外的稀释性潜在普通股期间内均转换为普通股,导致普通股股数增加 \n'+ \
3701
- '潜在普通股:赋予其持有者在报告期或以后享有取得普通股权利的金融工具或者其他合同,如可转债、认股权证、股份期权等 \n'+ \
3702
- '摊薄每股指标:使用期末数值计算,而非期初期末均值,其结果更能反应期末情况,可能大于、小于或等于每股指标 \n'+ \
3703
- '调整后每股净资产的意义:考虑了净资产的流动性和变现能力,更加具有谨慎性或稳健性 \n'+ \
3704
- '调整后每股净资产=(年度末股东权益-三年以上的应收款项净额-待摊费用-长期待摊费用)/年度末普通股股份总数 \n'+ \
3705
- '应收款项净额:包括应收帐款、其他应收款、预付帐款、应收股利、应收利息、应收补贴款 \n'+ \
3706
- '留存收益:指企业从历年实现的利润中提取或形成的留存于企业的内部积累,包括盈余公积和未分配利润两类 \n'+ \
3707
- '盈余公积:指从税后利润中提取的、存留于企业内部、具有特定用途的收益积累,包括法定盈余公积和任意盈余公积 \n'+ \
3708
- '盈余公积的用途:可用于企业职工福利设施的支出、弥补亏损、扩大生产经营、转增资本(或股本)或派送新股等 \n'+ \
3709
- '法定盈余公积:指企业按照公司法等法律规定的比例(例如10%)从净利润中提取的盈余公积 \n'+ \
3710
- '任意盈余公积:指企业经股东大会或类似机构批准按照规定的比例从净利润中提取的盈余公积,其提取比例由企业自行确定 \n'+ \
3711
- '未分配利润:指企业实现的净利润经过弥补亏损、提取盈余公积和向投资者分配利润后留存在企业的、历年结存的利润 \n'+ \
3712
- '资本公积金:指由资本原因形成的公积金,如发行股份溢价、资产重估增值、接受捐赠等,用于转增股本,不得用于弥补亏损 \n'+ \
3713
- '自由现金流=企业经营活动现金流-资本性支出,用来衡量实际持有的在不影响企业生存时可回报股东(债权人)的最大现金额 \n'+ \
3714
- '(企业)自由现金流可以理解为归属于股东与债权人的最大现金流,而股东(股权)自由现金流则是归属于股东的最大现金流。 \n', \
3715
-
3716
- '营运能力': \
3717
- '注:\n'+ \
3718
- '指标周转率/周转次数:营业(总)收入 / 指标的起初期末均值。一般来说数值越大越好 \n'+ \
3719
- '指标周转天数:360/周转率(或周转次数)。一般来说数值越小越好 \n'+ \
3720
- '注意:本表指标主要针对非金融行业,部分指标不适用于金融行业。 \n', \
3721
-
3722
- '盈利能力':
3723
- '注:\n'+ \
3724
- '毛利润=营业(总)收入-营业(总)成本 \n'+ \
3725
- '营业利润=毛利润-营业税金及附加-期间费用-资产减值损失+公允价值变动损益+投资损益 \n'+ \
3726
- '营业利润率=营业利润 / 营业(总)收入 x100% \n'+ \
3727
- '营业利润率的意义:不考虑营业外收支和所得税费用时,反映主营业务创造利润能力,比毛利率更能反映盈利能力 \n'+ \
3728
- '成本费用利润率=利润总额 / 成本费用总额 ×100%,反映付出一单位成本可以获取多少利润,即获取利润的效率 \n'+ \
3729
- '利润总额=营业利润+营业外收入-营业外支出 \n'+ \
3730
- '成本费用总额=营业成本+营业税金及附加+期间费用+资产减值损失 \n'+ \
3731
- '总资产净利率:这里默认使用总资产期初期末平均值,=净利润 / 总资产期初期末平均值 \n'+ \
3732
- '总资产净利率_平均(含少数股东损益):即在扣除少数股东损益之前的总资产净利率 \n'+ \
3733
- '少数股东损益:指公司合并报表的子公司中其它非控股股东享有的损益 \n'+ \
3734
- '总资产报酬率=息税前利润 / 总资产期初期末均值 ×100%,即(利润总额+利息支出) / 平均总资产 ×100% \n'+ \
3735
- '总资本回报率(收益率)(Return of Total Capital)=(利润总额+借入资本利息) / 总资本 x100%, \n'+ \
3736
- '总资本回报率(收益率)反映企业总资本的收益能力,从经营者的立场观察,自有资本和借入资本没有区分的必要 \n'+ \
3737
- '在借入资本利息中,除去利息支出、贴现费用、企业借债利息外,还应包括公司债券折价摊销数额在内 \n'+ \
3738
- '息前税后总资产报酬率=息前税后利润 / 总资产期初期末均值 ×100% \n'+ \
3739
- '净资产收益率(ROE)=净利润 / 净资产期初期末均值 ×100% \n'+ \
3740
- '摊薄净资产收益率=净利润 / 净资产期末数值 ×100%,强调期末情况 \n'+ \
3741
- '投入资本回报率(ROIC,Return Of Invested Capital)=息税前利润(EBIT) x (1-税率) / 投入资本 x100% \n'+ \
3742
- '息税前利润(EBIT)=营业收入–营业成本–营业费用+营业外收入支出(或是税前净利+利息费用) \n'+ \
3743
- '投入资本(Invested Capital)=流动资产–流动负债+不动产与厂房设备净额+无形资产及商誉,注意与总资本不同 \n'+ \
3744
- '投入资本回报率的意义:更多地从投资而非财务角度看,每单位投资的资本所能赚取息税前利润的多少。 \n', \
3745
-
3746
- '收益质量':
3747
- '注:\n'+ \
3748
- '销售成本率=营业(总)成本 / 营业(总)收入 x100%,=1-毛利润 \n'+ \
3749
- '成本费用率=(营业(总)成本+期间费用) / 营业(总)收入 x100%,=销售成本率+期间费用率,表示企业的成本费用控制能力 \n'+ \
3750
- '期间费用率=期间费用 / 营业(总)收入 x100% \n',
3751
-
3752
- '成长能力':
3753
- '注:\n'+ \
3754
- '扣费(后)净利润=净利润-非经常性损益,即扣除了偶然的不可持续或不常发生的收益,可持续性强,可大于或小于净利润 \n'+ \
3755
- '归属母公司净利润:简称归母净利润,指合并报表中归属母公司(上市公司)的利润,不包括子公司中非控股股东的利润。 \n',
3756
-
3757
- '财务风险':
3758
- '注:\n'+ \
3759
- '保守速动比率:又称超速动比率,=(现金+短期证券投资+应收账款净额)/流动负债,比速动比能够更好地评价短期偿债能力 \n'+ \
3760
- '应收账款净额:指应收账款和其他应收款减去备抵坏账的净额,实质即为信誉高客户的应收款净额 \n'+ \
3761
- '产权比率(equity ratio)=负债总额 / 所有者权益总额(净资产) x100%,从一个方面说明企业长期偿债能力,越高越弱 \n'+ \
3762
- '说明:本表指标主要针对非金融普通企业,部分指标不完全适用于金融行业。 \n\n'+ \
3763
- '注意:财报比率统计口径可能存在差异(例如采用期末/期初期末均值/期间加权等),但不影响同一指标的可比性。',
3764
- }
3765
-
3766
- #确定表格字体大小
3767
- titile_font_size=font_size
3768
- heading_font_size=data_font_size=str(int(font_size.replace('px',''))-1)+'px'
3769
-
3770
- # 一只股票情形:多日期
3771
- if len(tickers_found) == 1:
3772
- ticker1=tickers[0]
3773
- """
3774
- titletxt="\n===== 上市公司财务报表摘要:"+ticker_name(ticker1)+" ====="
3775
- print(titletxt)
3776
- """
3777
- titletxt=ticker_name(ticker1,'stock')+':'+"财报摘要"
3778
-
3779
- fsdf1=fsdf[fsdf['ticker']==ticker1]
3780
- for ty in typelist:
3781
- dft=fsdf1[fsdf1['选项']==ty]
3782
- #print(list(dft['指标']))
3783
-
3784
- # 自定义排序
3785
- dft["指标"]=dft["指标"].astype('category').cat.set_categories(typedict[ty])
3786
- dft.sort_values(by='指标',inplace=True)
3787
-
3788
- dft.reset_index(drop=True,inplace=True)
3789
- dft.index=dft.index + 1
3790
- dft2=dft.drop(labels=['选项','ticker'],axis=1)
3791
-
3792
- """
3793
- print("\n***",ty+':')
3794
- colalign=['center','left']+['right']*(len(list(dft2)) - 1)
3795
- print(dft2.to_markdown(tablefmt='Simple',index=True,colalign=colalign))
3796
- print(notesdict[ty])
3797
- """
3798
- titletxt1=titletxt+','+ty
3799
-
3800
- if DEBUG:
3801
- if ty == "每股指标":
3802
- print(dft2['指标'])
3803
- display(dft2)
3804
- dft4=dft2.dropna(subset=['指标'])
3805
-
3806
- df_display_CSS(df=dft4,titletxt=titletxt1,footnote=notesdict[ty], \
3807
- facecolor=facecolor,decimals=2, \
3808
- titile_font_size=titile_font_size,heading_font_size=heading_font_size, \
3809
- data_font_size=data_font_size)
3810
-
3811
- # 多只股票情形:单日期
3812
- import pandas as pd
3813
- fsdates2=[]
3814
- for fsd in fsdates:
3815
- fsd2=pd.to_datetime(fsd)
3816
- fsd3=fsd2.strftime("%Y-%m-%d")
3817
- fsdates2=fsdates2+[fsd3]
3818
- fsdates3=sorted(fsdates2,reverse=True)
3819
-
3820
- if len(tickers_found) > 1:
3821
- """
3822
- titletxt="\n===== 上市公司财务报表摘要对比:报表日期"+fsdates3[0]+" ====="
3823
- print('\n'+titletxt)
3824
- """
3825
- #titletxt="财报摘要:报表日"+fsdates3[0]
3826
- titletxt="财报摘要:"+fsdates3[0]
3827
-
3828
- mdf=pd.DataFrame()
3829
- for t in tickers_found:
3830
- dft=fsdf[fsdf['ticker']==t]
3831
-
3832
- try:
3833
- dft2=dft[['选项','指标',fsdates3[0]]]
3834
- dft2.rename(columns={fsdates3[0]:ticker_name(t,'stock')},inplace=True)
3835
- except:
3836
- print(" #Error(compare_fin_summary_china): fin stmt of",t,'not found on',fsdates3[0])
3837
- return None
3838
-
3839
- if len(mdf) == 0:
3840
- mdf=dft2
3841
- else:
3842
- mdf=mdf.merge(dft2,left_on=['选项','指标'], right_on=['选项','指标'])
3843
-
3844
- for ty in typelist:
3845
- dft=mdf[mdf['选项']==ty]
3846
-
3847
- # 自定义排序
3848
- dft["指标"]=dft["指标"].astype('category').cat.set_categories(typedict[ty])
3849
- dft.sort_values(by='指标',inplace=True)
3850
-
3851
- dft.reset_index(drop=True,inplace=True)
3852
- dft.index=dft.index + 1
3853
- dft2=dft.drop(labels=['选项'],axis=1)
3854
- """
3855
- print("\n***",ty+':')
3856
- colalign=['center','left']+['right']*(len(list(dft2)) - 1)
3857
- print(dft2.to_markdown(tablefmt='Simple',index=True,colalign=colalign))
3858
- print(notesdict[ty])
3859
- """
3860
- titletxt1=titletxt+','+ty
3861
- dft4=dft2.dropna(subset=['指标'])
3862
-
3863
- df_display_CSS(df=dft4,titletxt=titletxt1,footnote=notesdict[ty], \
3864
- facecolor=facecolor,decimals=2, \
3865
- titile_font_size=titile_font_size,heading_font_size=heading_font_size, \
3866
- data_font_size=data_font_size)
3867
-
3868
- return dft2
3869
-
3870
- #==============================================================================
3871
- #==============================================================================
3872
- if __name__=='__main__':
3873
- ticker='000002.SZ'
3874
- fsdates=['2022-12-31','2021-12-31','2020-12-31']
3875
-
3876
- fsdf1=get_fin_indicator_1ticker_china(ticker,fsdates)
3877
-
3878
- def get_fin_indicator_1ticker_china(ticker,fsdates):
3879
- """
3880
- 功能:获得A股财报指标,进行数据整理,单一股票
3881
- 输出:
3882
- 1、数量级变换:(元)-->(亿元),并相应修改字段名
3883
- """
3884
-
3885
- import pandas as pd
3886
- # 过滤财报日期
3887
- if isinstance(fsdates,str):
3888
- fsdates=[fsdates]
3889
-
3890
- fsdates2=[]
3891
- for d in fsdates:
3892
- d1=pd.to_datetime(d)
3893
- d2=d1.strftime("%Y-%m-%d")
3894
- fsdates2=fsdates2 + [d2]
3895
- fsdates3=sorted(fsdates2,reverse=True)
3896
- start_year=fsdates3[-1][:4]
3897
-
3898
- # 亿元
3899
- yiyuan=float(1e+08)
3900
-
3901
- # 获得股票所有财报数据
3902
- _,t1,_=split_prefix_suffix(ticker)
3903
- try:
3904
- dft=ak.stock_financial_analysis_indicator(t1,start_year=start_year)
3905
- dft['日期']=dft['日期'].apply(lambda x: x.strftime("%Y-%m-%d"))
3906
- dft.sort_values(by=['日期'],ascending=False,inplace=True)
3907
- except:
3908
- print(" #Error(get_fin_indicator_1ticker_china): no info found for",ticker)
3909
- return None
3910
-
3911
- # 过滤财报日期
3912
- fsdate_field=list(dft)[0]
3913
- dft2=dft[dft[fsdate_field].isin(fsdates3)]
3914
- if len(dft2) < len(fsdates3):
3915
- print(" #Warning(get_fin_indicator_1ticker_china): info of some dates unavailable for",ticker+'('+ticker_name(ticker,'stock')+')')
3916
- if len(dft2) ==0:
3917
- print(" #Error(get_fin_indicator_1ticker_china): no info found for",ticker+'('+ticker_name(ticker,'stock')+') on',fsdates)
3918
- return None
3919
-
3920
- # 去掉重复行
3921
- dft2b=dft2.drop_duplicates (keep='first')
3922
- dft3=dft2b.replace('--',0)
3923
-
3924
- # 金额变换:元-->亿元,小数位截取
3925
- collist3=list(dft3)
3926
- for c in collist3:
3927
- c1=c
3928
- try:
3929
- dft3[c]=dft3[c].astype(float)
3930
- if ('元' in c) and not ('每股' in c):
3931
- dft3[c]=dft3[c] / yiyuan
3932
- c1=c.replace('元','亿元')
3933
- dft3.rename(columns={c:c1},inplace=True)
3934
-
3935
- if ('比重' in c) and not ('%' in c):
3936
- c1=c+'(%)'
3937
- dft3.rename(columns={c:c1},inplace=True)
3938
-
3939
- dft3[c1]=dft3[c1].apply(lambda x: round(x,2))
3940
- except:
3941
- pass
3942
-
3943
- # 检查字段改名情况
3944
- collist3b=list(dft3)
3945
-
3946
- # 回填指标选项类型
3947
- dft4=dft3.set_index(fsdate_field)
3948
- dft4t=dft4.T
3949
- dft5=dft4t.reset_index()
3950
- dft5.index=dft5.index + 1
3951
- dft5.rename(columns={'index':'指标'},inplace=True)
3952
-
3953
- # 去重
3954
- dft5=dft5.drop_duplicates (keep='first')
3955
-
3956
- dft5['ticker']=ticker
3957
-
3958
- # 填充nan为零
3959
- dft5.fillna(0,inplace=True)
3960
- dft5.replace(0,'-',inplace=True)
3961
-
3962
- # 指标分类
3963
- retstrlist=['利润率','毛利率','成本率','净利率','报酬率','比重','股息','收益率','增长率']
3964
- debtstrlist=['流动比','速动比','现金比','倍数','负债','债务','权益','产权','清算']
3965
-
3966
- dft5['选项']=''
3967
- for index,row in dft5.iterrows():
3968
- # 改变顺序要谨慎
3969
- if ('每股' in row['指标']) and (row['选项'] == ''):
3970
- #row['选项']='每股指标'
3971
- dft5.loc[index,'选项']='每股指标'
3972
-
3973
- if ('周转' in row['指标']) and (row['选项'] == ''):
3974
- #row['选项']='营运能力'
3975
- dft5.loc[index,'选项']='营运能力'
3976
-
3977
- if ('应收' in row['指标']) and (row['选项'] == ''):
3978
- #row['选项']='应收账款'
3979
- dft5.loc[index,'选项']='应收账款'
3980
-
3981
- if ('预付' in row['指标']) and (row['选项'] == ''):
3982
- #row['选项']='预付账款'
3983
- dft5.loc[index,'选项']='预付账款'
3984
-
3985
- if str_contain_any_substr(row['指标'],debtstrlist) and (row['选项'] == ''):
3986
- #row['选项']='偿债能力'
3987
- dft5.loc[index,'选项']='偿债能力'
3988
-
3989
- if ('现金' in row['指标']) and (row['选项'] == ''):
3990
- #row['选项']='现金指标'
3991
- dft5.loc[index,'选项']='现金指标'
3992
-
3993
- if ('亿元' in row['指标']) and (row['选项'] == ''):
3994
- #row['选项']='规模指标'
3995
- dft5.loc[index,'选项']='规模指标'
3996
- """
3997
- if ('亿元' in row['指标']) and (row['选项'] == ''):
3998
- #row['选项']='规模指标'
3999
- dft5.loc[index,'选项']='规模指标'
4000
- """
4001
- if str_contain_any_substr(row['指标'],retstrlist) and (row['选项'] == ''):
4002
- #row['选项']='利润回报'
4003
- dft5.loc[index,'选项']='利润回报'
4004
-
4005
- if (row['选项'] == ''):
4006
- #row['选项']='其他指标'
4007
- dft5.loc[index,'选项']='其他指标'
4008
-
4009
- return dft5
4010
-
4011
- #==============================================================================
4012
- if __name__=='__main__':
4013
- tickers=['000002.SZ','601398.SS']
4014
- fsdates=['2022-12-31','2021-12-31','2020-12-31']
4015
-
4016
- fsdfm=get_fin_indicator_china(tickers,fsdates)
4017
-
4018
-
4019
- def get_fin_indicator_china(tickers,fsdates):
4020
- """
4021
- 功能:获得A股财报指标,进行数据整理
4022
- 输出:
4023
- 1、数量级变换:(元)-->(亿元),并相应修改字段名
4024
- """
4025
-
4026
- import pandas as pd
4027
- # 过滤财报日期
4028
- fsdates2=[]
4029
- for d in fsdates:
4030
- d1=pd.to_datetime(d)
4031
- d2=d1.strftime("%Y-%m-%d")
4032
- fsdates2=fsdates2 + [d2]
4033
- fsdates3=list(set(fsdates2))
4034
- fsdates4=sorted(fsdates3,reverse=True)
4035
-
4036
- df=pd.DataFrame()
4037
- # 获得多只股票财报
4038
- for t in tickers:
4039
- # 抓取一只股票的财报
4040
- dft=get_fin_indicator_1ticker_china(t,fsdates4)
4041
- if dft is None:
4042
- continue
4043
-
4044
- if len(df)==0:
4045
- df=dft
4046
- else:
4047
- df=pd.concat([df,dft])
4048
-
4049
- return df
4050
-
4051
- #==============================================================================
4052
-
4053
- if __name__=='__main__':
4054
- tickers=['000002.SZ','601398.SS']
4055
- fsdates=['2022-12-31','2021-12-31','2020-12-31']
4056
-
4057
- tickers=['000002.SZ','600048.SS','001979.SZ','600325.SS','000069.SZ','600383.SS','600895.SS','601155.SS']
4058
- fidf=compare_fin_indicator_china(tickers,fsdates)
4059
-
4060
- tickers='601615.SS'
4061
- fsdates=['2022-12-31',
4062
- '2021-12-31',
4063
- '2020-12-31',
4064
- '2019-12-31',
4065
- '2018-12-31',
4066
- ]
4067
-
4068
- def compare_fin_indicator_china(tickers,fsdates,facecolor='papayawhip',font_size='16px'):
4069
- """
4070
- 功能:分类别显示财报摘要中的指标
4071
- """
4072
-
4073
- # 检查股票列表
4074
- if isinstance(tickers,str):
4075
- tickers=[tickers]
4076
- tickers=list(tickers) #强制转换
4077
- if len(tickers)==0:
4078
- print(" #Error(compare_fin_indicator_china): need at least one stock in",tickers)
4079
- return None
4080
-
4081
- # 检查财报日期列表
4082
- if isinstance(fsdates,str):
4083
- fsdates=[fsdates]
4084
- if len(fsdates)==0:
4085
- print(" #Error(compare_fin_indicator_china): need at least one date in",fsdates)
4086
- return None
4087
-
4088
- for d in fsdates:
4089
- result,_,_=check_period(d,d)
4090
- if not result:
4091
- print(" #Error(compare_fin_indicator_china): invalid date",d)
4092
- return None
4093
-
4094
- # 获取财报数据
4095
- print(" Searching for financial statements, please wait ...")
4096
- fsdf=get_fin_indicator_china(tickers,fsdates)
4097
- if fsdf is None:
4098
- print(" #Error(compare_fin_indicator_china): none record found for above tickers on",fsdates[0])
4099
- print(" Reasons: either wrong tickers or dates, or blocked by data source, try later")
4100
- return None
4101
- if len(fsdf) == 0:
4102
- print(" #Warning(compare_fin_indicator_china): zero recrod found for above tickers on",fsdates[0])
4103
- print(" Reasons: wrong tickers or dates, or blocked by data source, try later")
4104
- return None
4105
-
4106
- # 不改变列表顺序去重
4107
- tickerlist=list(fsdf['ticker'])
4108
- tickers_found=sorted(list(set(tickerlist)),key=tickerlist.index)
4109
-
4110
- #optionlist=list(fsdf['选项'])
4111
- #typelist=sorted(list(set(optionlist)),key=optionlist.index)
4112
- typelist=['规模指标','利润回报','每股指标','营运能力','现金指标','偿债能力','应收账款','预付账款','其他指标']
4113
- """
4114
- # 按照指定的选项顺序排序
4115
- fsdf['选项'] = fsdf['选项'].astype('category')
4116
- fsdf['选项'].cat.reorder_categories(typelist, inplace=True)
4117
- fsdf.sort_values('选项', inplace=True)
4118
- """
4119
- """
4120
- # 列出每个选项的科目
4121
- fsdf1=fsdf[fsdf['ticker']==tickers[0]]
4122
- dictlist={}
4123
- for ty in typelist:
4124
- tmpdf=fsdf1[fsdf1['选项']==ty]
4125
- tmplist=list(tmpdf['指标'])
4126
- dictlist[ty]=tmplist
4127
-
4128
- """
4129
- # 手工设定项目显示顺序,使其看起来更合理
4130
- typedict={'规模指标': [
4131
- '总资产(亿元)',
4132
- '短期股票投资(亿元)','短期债券投资(亿元)','短期其它经营性投资(亿元)',
4133
- '长期股票投资(亿元)','长期债券投资(亿元)','长期其它经营性投资(亿元)',
4134
- '主营业务利润(亿元)','扣除非经常性损益后的净利润(亿元)',
4135
- ],
4136
-
4137
- '利润回报': [
4138
- '销售毛利率(%)','主营业务利润率(%)','营业利润率(%)','销售净利率(%)','成本费用利润率(%)',
4139
- '主营业务成本率(%)','三项费用比重(%)',
4140
- #'主营利润比重(%)','非主营比重(%)','固定资产比重(%)',
4141
- '主营利润比重(%)','固定资产比重(%)',
4142
- '总资产利润率(%)','总资产净利润率(%)','资产报酬率(%)',
4143
- '净资产收益率(%)','加权净资产收益率(%)','净资产报酬率(%)','投资收益率(%)','股本报酬率(%)','股息发放率(%)',
4144
- '总资产增长率(%)','净资产增长率(%)','主营业务收入增长率(%)','净利润增长率(%)',
4145
- ],
4146
-
4147
- '每股指标': [
4148
- '加权每股收益(元)','扣除非经常性损益后的每股收益(元)','摊薄每股收益(元)','每股收益_调整后(元)',
4149
- '每股净资产_调整前(元)','每股净资产_调整后(元)',#'调整后的每股净资产(元)',
4150
- '每股未分配利润(元)','每股资本公积金(元)',
4151
- '每股经营性现金流(元)',
4152
- ],
4153
-
4154
- '营运能力': [
4155
- '总资产周转率(次)','总资产周转天数(天)',
4156
- '应收账款周转率(次)','应收账款周转天数(天)',
4157
- '存货周转率(次)','存货周转天数(天)',
4158
- '流动资产周转率(次)','流动资产周转天数(天)',
4159
- '固定资产周转率(次)',
4160
- '股东权益周转率(次)'
4161
- ],
4162
-
4163
- '现金指标': [
4164
- '现金流量比率(%)',
4165
- '经营现金净流量对销售收入比率(%)',
4166
- '资产的经营现金流量回报率(%)',
4167
- '经营现金净流量与净利润的比率(%)',
4168
- ],
4169
-
4170
- '偿债能力': [
4171
- '流动比率','速动比率','现金比率(%)','利息支付倍数',
4172
- '资产负债率(%)','长期负债比率(%)','股东权益比率(%)','产权比率(%)','负债与所有者权益比率(%)',
4173
- '长期债务与营运资金比率(%)','股东权益与固定资产比率(%)','清算价值比率(%)',
4174
- '经营现金净流量对负债比率(%)'],
4175
-
4176
- '应收账款': [
4177
- '1年以内应收帐款(亿元)','1-2年以内应收帐款(亿元)',
4178
- '2-3年以内应收帐款(亿元)','3年以内应收帐款(亿元)',
4179
- '1年以内其它应收款(亿元)','1-2年以内其它应收款(亿元)',
4180
- '2-3年以内其它应收款(亿元)','3年以内其它应收款(亿元)'],
4181
-
4182
- '预付账款': [
4183
- '1年以内预付货款(亿元)','1-2年以内预付货款(亿元)',
4184
- '2-3年以内预付货款(亿元)','3年以内预付货款(亿元)'],
4185
-
4186
- '其他指标': [
4187
- '长期资产与长期资金比率(%)',
4188
- '资本化比率(%)',
4189
- '固定资产净值率(%)',
4190
- '资本固定化比率(%)']}
4191
-
4192
- # 注释
4193
- notesdict={'规模指标': \
4194
- '注:\n'+ \
4195
- '短期=一年以内,长期=一年以上 \n'+ \
4196
- '非经常性损益=非经常性或偶然活动带来的损益,一般不可持续。 \n', \
4197
-
4198
- '利润回报': \
4199
- '注:\n'+ \
4200
- '销售毛利率=毛利率=毛利润/营业(总)收入*100%,毛利润=营业(总)收入-营业(总)成本 \n'+ \
4201
- '主营业务利润率=主营业务利润/主营业务收入*100%,主营业务利润=主营业务收入-主营业务成本-主营业务税金及附加 \n'+ \
4202
- '营业利润率=营业利润/营业(总)收入*100%,营业利润=主营业务利润+其他业务利润-期间费用+其他损益 \n'+ \
4203
- '其他业务利润=其他业务收入-其它业务支出-其他业务税金及附加,期间费用=销售费用+管理费用+财务费用 \n'+ \
4204
- '其他损益=-资产减值损失+公允价值变动损益(损失为负数)+投资损益(损失为负数)+资产处置损益(损失为负数) \n'+ \
4205
- '利润总额=主营营业利润+非主营营业利润+投资损益+营业外收入支出净额 \n'+ \
4206
- '销售净利率=净利润率=净利润/营业(总)收入*100%,净利润=利润总额-所得税费用 \n'+ \
4207
- '成本费用利润率=利润总额/成本费用总额*100%,成本费用总额=营业(总)成本+营业税金及附加+期间费用+资产减值损失 \n'+ \
4208
- '主营业务收入成本率=主营业务成本/主营业务收入*100% \n'+ \
4209
- '三项费用比重=(管理费用+营业费用+财务费用)/营业(总)收入*100% \n'+ \
4210
- '主营利润比重=主营业务利润/利润总额×100% \n'+ \
4211
- '非主营比重=非主营业务利润/利润总额×100% \n'+ \
4212
- '固定资产比重=固定资产/资产总额*100% \n'+ \
4213
- '总资产利润率=利润总额/资产总额*100%,总资产净利润率(ROA)=净利润/资产总额*100% \n'+ \
4214
- '资产报酬率=息税前利润/资产总额*100%,息税前利润(EBIT)=净利润+所得税费用+利息费用 \n'+ \
4215
- '净资产收益率(ROE)=净利润/净资产*100%,净资产=资产总额-负债总额 \n'+ \
4216
- '加权净资产收益率=净利润/加权平均净资产*100%,加权平均净资产含期间内净资产增减(如发新股、债转股或分红)情况 \n'+ \
4217
- '净资产报酬率=息税前利润/加权平均净资产*100% \n'+ \
4218
- '投资报酬率=投资回报率(ROI)=利润总额/投资总额*100%,投资总额=短期投资+长期投资 \n'+ \
4219
- '股本报酬率=净利润/股东股本总数*100%,相当于股东股本按面值1元计算 \n'+ \
4220
- '股息发放率=股利支付率=股利/净利润*100%,反映公司的股利分配政策,比例高表明公司不需更多的资金进行再投入。 \n', \
4221
-
4222
- '每股指标': \
4223
- '注:\n'+ \
4224
- '每股收益=净利润/流通股数量,其中流通股数量按期初期末均值计算 \n'+ \
4225
- '扣除非经常性损益后的每股收益=扣除非经常性损益后的净利润/流通股数量 \n'+ \
4226
- '加权每股收益=净利润/加权流通股数量,加权流通股数量含期间内流通股数量的变化(增发新股、送股、转增股本或配股等) \n'+ \
4227
- '摊薄每股收益=净利润/期末流通股数量 \n'+ \
4228
- '调整后每股收益=调整后净利润/流通股数量,其中调整后净利润考虑了长期账龄的应收款项成为呆坏账对净利润的影响 \n'+ \
4229
- '稀释后每股收益=净利润/稀释后流通股数量,假设企业的可转债、认股权证、股票期权等期间内均转换为普通股的情形 \n'+ \
4230
- '每股净资产(调整前)=净资产/流通股数量,调整后指标则考虑了其流动性和变现能力,扣除了长期应收款项和(长期)待摊费用 \n'+ \
4231
- '每股未分配利润=未分配利润/流通股数量,未分配利润指净利润经过弥补亏损、提取盈余公积和分红后留存在企业的利润累积 \n'+ \
4232
- '每股资本公积金=资本公积金/流通股数量,资本公积金含发行股份的溢价、资产重估增值、接受捐赠等,仅可用于转增股本。 \n', \
4233
-
4234
- '营运能力':
4235
- '注:\n'+ \
4236
- '指标周转率/周转次数:营业(总)收入/指标的起初期末均值,从企业自身角度来说越大越好,但若对供应商则可能相反 \n'+ \
4237
- '指标周转天数:360/周转率(或周转次数),从企业自身角度来说越小越好,但若涉及供应商则可能相反 \n'+ \
4238
- '注意:本表指标主要针对非金融行业,部分指标不适用于金融行业。 \n', \
4239
-
4240
- '现金指标':
4241
- '注:\n'+ \
4242
- '现金流量比率=经营活动产生的现金净流量/期末流动负债*100%,反映企业短期偿债能力 \n'+ \
4243
- '经营现金净流量对销售收入比率=销售现金比率,与赊销政策有关,若企业有虚假收入,也会使该指标过低 \n'+ \
4244
- '资产的经营现金流量回报率=经营现金流量净额/总资产*100%,体现企业经营活动的收现能力 \n'+ \
4245
- '经营现金净流量与净利润的比率=净现比=经营现金流量净额/净利润*100%。比率越大,企业盈利质量越高。 \n',
4246
-
4247
- '偿债能力':
4248
- '注:\n'+ \
4249
- '流动比率=流动资产/流动负债,反映企业的短期偿债能力,属于宽松指标 \n'+ \
4250
- '速动比率=(流动资产-存货)/流动负债,反映企业的短期偿债能力,属于较严的指标 \n'+ \
4251
- '现金比率=(货币资金+短期有价证券)/流动负债*100%,反映企业的短期偿债能力,属于严厉的指标 \n'+ \
4252
- '利息支付倍数=利息保障倍数=税息前利润/利息费用*100%,衡量偿付借款利息的能力 \n'+ \
4253
- '长期负债比率=资本化比率=长期负债/资产总额*100% \n'+ \
4254
- '股东权益比率=自有资本比率=净资产比率=股东权益/资产总额*100% \n'+ \
4255
- '产权比率=长期负债/所有者权益(股东权益)*100% \n'+ \
4256
- '长期债务与营运资金比率=长期债务/营运资金*100%,反映偿还债务能力,通常长期债务不应超过营运资金 \n'+ \
4257
- '营运资金=流动资产-流动负债 \n'+ \
4258
- '股东权益与固定资产比率=股东权益总额÷固定资产总额×100%,衡量公司财务结构稳定性,越大越稳定 \n'+ \
4259
- '清算价值比率=有形资产/负债总额*100%,反映公司清偿全部债务的能力 \n'+ \
4260
- '经营现金净流量对负债比率=现金流量负债比=经营活动现金流量净额/负债总额*100%,比率越高,财务弹性越好。 \n',
4261
-
4262
- '应收账款':
4263
- '注:\n'+ \
4264
- '使用账龄法对应收账款/其他应收款分类 \n'+ \
4265
- '一般而言,应收款项的账龄越长,成为呆坏账的可能性就越大。 \n',
4266
-
4267
- '预付账款':
4268
- '注:\n'+ \
4269
- '一般而言,预付款项数额越大,企业在供应链中的地位越低 \n'+ \
4270
- '一般而言,预付款项的账龄越长,企业在供应链中的地位越低。 \n',
4271
-
4272
- '其他指标':
4273
- '注:\n'+ \
4274
- '长期资产与长期资金比率=非流动资产/(长期负债+股东权益)*100%,长期资金少,流动负债较多,财务风险较大 \n'+ \
4275
- '资本化比率=长期负债/(长期负债+股东权益)*100%,指标值越小,负债的资本化程度就越低,长期偿债压力就越小 \n'+ \
4276
- '固定资产净值率=(固定资产原值-累计折旧)/固定资产原值*100%,反映企业全部固定资产平均新旧程度 \n'+ \
4277
- '资本固定化比率=非流动资产/净资产*100%,若超过100%说明固定资产资金投入超过自身能力,易造成财务状况恶化。\n',
4278
- }
4279
-
4280
- #确定表格字体大小
4281
- titile_font_size=font_size
4282
- heading_font_size=data_font_size=str(int(font_size.replace('px',''))-1)+'px'
4283
-
4284
- # 标记选项类型
4285
- typedict_keys=list(typedict.keys())
4286
- for index,row in fsdf.iterrows():
4287
- for k in typedict_keys:
4288
- if row['指标'] in typedict[k]:
4289
- fsdf.loc[index,'选项']=k
4290
-
4291
- # 一只股票情形:多日期
4292
- if len(tickers_found) == 1:
4293
- ticker1=tickers[0]
4294
- """
4295
- titletxt="\n===== 上市公司主要财务比率和重要指标:"+ticker_name(ticker1)+" ====="
4296
- print(titletxt)
4297
- """
4298
- titletxt="主要财务比率和指标:"+ticker_name(ticker1,'stock')
4299
-
4300
- fsdf1=fsdf[fsdf['ticker']==ticker1]
4301
- for ty in typelist:
4302
- dft=fsdf1[fsdf1['选项']==ty]
4303
- #dft=fsdf1[fsdf1['选项'].apply(lambda x: x in typedict[ty])]
4304
- #print(list(dft['指标']))
4305
-
4306
- # 自定义排序
4307
- try:
4308
- tmplist=typedict[ty]
4309
- dft2=dft[dft['指标'].isin(tmplist)]
4310
-
4311
- tmplist2=sorted(list(set(tmplist)),key=tmplist.index)
4312
- dft2["指标"]=dft2["指标"].astype('category').cat.set_categories(tmplist2)
4313
- except:
4314
- print("#set_categories error",ty)
4315
- print(typedict[ty])
4316
- pass
4317
-
4318
- dft2.sort_values(by='指标',inplace=True)
4319
-
4320
- dft2.reset_index(drop=True,inplace=True)
4321
- dft2.index=dft2.index + 1
4322
- dft3=dft2.drop(labels=['选项','ticker'],axis=1)
4323
- """
4324
- print("\n***",ty+':')
4325
- colalign=['center','left']+['right']*(len(list(dft3)) - 1)
4326
- print(dft3.to_markdown(tablefmt='Simple',index=True,colalign=colalign))
4327
- print(notesdict[ty])
4328
- """
4329
- """
4330
- 注意:若dft3为空,则会出现错误:list assignment index out of range
4331
- 无论如何修改colalign都没用
4332
- """
4333
- titletxt1=titletxt+','+ty
4334
-
4335
- df_display_CSS(df=dft3,titletxt=titletxt1,footnote=notesdict[ty], \
4336
- facecolor=facecolor,decimals=2, \
4337
- titile_font_size=titile_font_size,heading_font_size=heading_font_size, \
4338
- data_font_size=data_font_size)
4339
-
4340
- return dft3
4341
-
4342
- # 多只股票情形:单日期
4343
- if len(tickers_found) > 1:
4344
-
4345
- import pandas as pd
4346
- fsdates2=[]
4347
- for fsd in fsdates:
4348
- fsd2=pd.to_datetime(fsd)
4349
- fsd3=fsd2.strftime("%Y-%m-%d")
4350
- fsdates2=fsdates2+[fsd3]
4351
- fsdates3=sorted(fsdates2,reverse=True)
4352
-
4353
- mdf=pd.DataFrame()
4354
- for t in tickers_found:
4355
- dft=fsdf[fsdf['ticker']==t]
4356
-
4357
- try:
4358
- dft2=dft[['选项','指标',fsdates3[0]]]
4359
- dft2.rename(columns={fsdates3[0]:ticker_name(t,'stock')},inplace=True)
4360
- except:
4361
- print(" #Error(compare_fin_summary_china): fin stmt of",fsdates3[0],'not found for',t)
4362
- return None
4363
-
4364
- if len(mdf) == 0:
4365
- mdf=dft2
4366
- else:
4367
- mdf=mdf.merge(dft2,how='outer',left_on=['选项','指标'], right_on=['选项','指标'])
4368
- """
4369
- titletxt="\n===== 上市公司财务报表主要比率和重要项目对比:报表日期"+fsdates3[0]+" ====="
4370
- print('\n'+titletxt)
4371
- """
4372
- #titletxt="主要财务比率和指标:报表日"+fsdates3[0]
4373
- titletxt="主要财务比率和指标:"+fsdates3[0]
4374
-
4375
- for ty in typelist:
4376
- dft=mdf[mdf['选项']==ty]
4377
-
4378
- # 自定义排序
4379
- tmplist=typedict[ty]
4380
- dft2=dft[dft['指标'].isin(tmplist)]
4381
-
4382
- tmplist2=sorted(list(set(tmplist)),key=tmplist.index)
4383
- dft2["指标"]=dft2["指标"].astype('category').cat.set_categories(tmplist2)
4384
- dft2.sort_values(by='指标',inplace=True)
4385
-
4386
- dft2.reset_index(drop=True,inplace=True)
4387
- dft2.index=dft2.index + 1
4388
- dft3=dft2.drop(labels=['选项'],axis=1)
4389
- """
4390
- print("\n***",ty+':')
4391
- colalign=['center','left']+['right']*(len(list(dft3))-2)
4392
- print(dft3.to_markdown(tablefmt='Simple',index=True,colalign=colalign))
4393
- print(notesdict[ty])
4394
- """
4395
- titletxt1=titletxt+','+ty
4396
- df_display_CSS(df=dft3,titletxt=titletxt1,footnote=notesdict[ty], \
4397
- facecolor=facecolor,decimals=2, \
4398
- titile_font_size=titile_font_size,heading_font_size=heading_font_size, \
4399
- data_font_size=data_font_size)
4400
-
4401
- return dft3
4402
-
4403
- #==============================================================================
4404
- #==============================================================================
4405
- if __name__ == '__main__':
4406
- fsdf=get_fin_stmt_ak('601398.SS')
4407
- account_entry='资产总计'
4408
-
4409
- fsdf1=fs_entry_begin_china(fsdf,account_entry=account_entry,suffix='_期初')
4410
-
4411
- def fs_entry_begin_china(fsdf,account_entry='资产总计',suffix='_期初'):
4412
- """
4413
- 功能:以上年年报期末科目数值作为本期年报和季报的期初,仅适用于akshare大陆财报!
4414
- """
4415
- import pandas as pd
4416
- import numpy as np
4417
- #获取年报日期
4418
- ar_mm_dd='12-31'
4419
-
4420
- fsdf['asOfDate_pd']=fsdf.index
4421
- fsdf['Date_y4']=fsdf['asOfDate_pd'].apply(lambda x: pd.to_datetime(x).strftime("%Y"))
4422
- fsdf['Date_begin_pd']=fsdf['Date_y4'].apply(lambda x: pd.to_datetime(str(int(x)-1)+'-'+ar_mm_dd))
4423
-
4424
- asOfDate_pd_list=list(fsdf['asOfDate_pd'])
4425
- entry_begin=lambda x: fsdf[fsdf['asOfDate_pd']==x][account_entry].values[0] if x in asOfDate_pd_list else np.nan
4426
- fsdf[account_entry+suffix]=fsdf['Date_begin_pd'].apply(entry_begin)
4427
-
4428
- fsdf.drop(['asOfDate_pd','Date_y4','Date_begin_pd'],axis=1,inplace=True)
4429
-
4430
- return fsdf
4431
- #==============================================================================
4432
- #==============================================================================
4433
- #==============================================================================