siat 3.10.132__py3-none-any.whl → 3.10.133__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 (218) hide show
  1. siat/__init__.py +0 -0
  2. siat/allin.py +0 -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 +0 -0
  13. siat/compare_cross.py +0 -0
  14. siat/copyrights.py +0 -0
  15. siat/cryptocurrency.py +0 -0
  16. siat/economy.py +0 -0
  17. siat/economy2.py +0 -0
  18. siat/esg.py +0 -0
  19. siat/event_study.py +0 -0
  20. siat/exchange_bond_china.pickle +0 -0
  21. siat/fama_french.py +0 -0
  22. siat/fin_stmt2_yahoo.py +0 -0
  23. siat/financial_base.py +0 -0
  24. siat/financial_statements.py +0 -0
  25. siat/financials.py +0 -0
  26. siat/financials2.py +0 -0
  27. siat/financials_china.py +0 -0
  28. siat/financials_china2.py +0 -0
  29. siat/fund.py +0 -0
  30. siat/fund_china.pickle +0 -0
  31. siat/fund_china.py +0 -0
  32. siat/future_china.py +0 -0
  33. siat/google_authenticator.py +0 -0
  34. siat/grafix.py +0 -0
  35. siat/holding_risk.py +0 -0
  36. siat/luchy_draw.py +0 -0
  37. siat/market_china.py +0 -0
  38. siat/markowitz.py +0 -0
  39. siat/markowitz2.py +0 -0
  40. siat/markowitz2_20250704.py +0 -0
  41. siat/markowitz2_20250705.py +0 -0
  42. siat/markowitz_simple.py +0 -0
  43. siat/ml_cases.py +0 -0
  44. siat/ml_cases_example.py +0 -0
  45. siat/option_china.py +0 -0
  46. siat/option_pricing.py +0 -0
  47. siat/other_indexes.py +0 -0
  48. siat/risk_adjusted_return.py +0 -0
  49. siat/risk_adjusted_return2.py +0 -0
  50. siat/risk_evaluation.py +0 -0
  51. siat/risk_free_rate.py +0 -0
  52. siat/sector_china.py +0 -0
  53. siat/security_price2.py +0 -0
  54. siat/security_prices.py +40 -2
  55. siat/security_trend.py +0 -0
  56. siat/security_trend2.py +0 -0
  57. siat/stock.py +0 -0
  58. siat/stock_advice_linear.py +0 -0
  59. siat/stock_base.py +0 -0
  60. siat/stock_china.py +0 -0
  61. siat/stock_info.pickle +0 -0
  62. siat/stock_prices_kneighbors.py +0 -0
  63. siat/stock_prices_linear.py +0 -0
  64. siat/stock_profile.py +0 -0
  65. siat/stock_technical.py +0 -0
  66. siat/stooq.py +0 -0
  67. siat/transaction.py +0 -0
  68. siat/translate.py +0 -0
  69. siat/valuation.py +0 -0
  70. siat/valuation_china.py +0 -0
  71. siat/var_model_validation.py +0 -0
  72. siat/yf_name.py +0 -0
  73. {siat-3.10.132.dist-info/licenses → siat-3.10.133.dist-info}/LICENSE +0 -0
  74. {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/METADATA +232 -235
  75. siat-3.10.133.dist-info/RECORD +78 -0
  76. {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/WHEEL +1 -1
  77. {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/top_level.txt +0 -1
  78. build/lib/build/lib/siat/__init__.py +0 -75
  79. build/lib/build/lib/siat/allin.py +0 -137
  80. build/lib/build/lib/siat/assets_liquidity.py +0 -915
  81. build/lib/build/lib/siat/beta_adjustment.py +0 -1058
  82. build/lib/build/lib/siat/beta_adjustment_china.py +0 -548
  83. build/lib/build/lib/siat/blockchain.py +0 -143
  84. build/lib/build/lib/siat/bond.py +0 -2900
  85. build/lib/build/lib/siat/bond_base.py +0 -992
  86. build/lib/build/lib/siat/bond_china.py +0 -100
  87. build/lib/build/lib/siat/bond_zh_sina.py +0 -143
  88. build/lib/build/lib/siat/capm_beta.py +0 -783
  89. build/lib/build/lib/siat/capm_beta2.py +0 -887
  90. build/lib/build/lib/siat/common.py +0 -5360
  91. build/lib/build/lib/siat/compare_cross.py +0 -642
  92. build/lib/build/lib/siat/copyrights.py +0 -18
  93. build/lib/build/lib/siat/cryptocurrency.py +0 -667
  94. build/lib/build/lib/siat/economy.py +0 -1471
  95. build/lib/build/lib/siat/economy2.py +0 -1853
  96. build/lib/build/lib/siat/esg.py +0 -536
  97. build/lib/build/lib/siat/event_study.py +0 -815
  98. build/lib/build/lib/siat/fama_french.py +0 -1521
  99. build/lib/build/lib/siat/fin_stmt2_yahoo.py +0 -982
  100. build/lib/build/lib/siat/financial_base.py +0 -1160
  101. build/lib/build/lib/siat/financial_statements.py +0 -598
  102. build/lib/build/lib/siat/financials.py +0 -2339
  103. build/lib/build/lib/siat/financials2.py +0 -1278
  104. build/lib/build/lib/siat/financials_china.py +0 -4433
  105. build/lib/build/lib/siat/financials_china2.py +0 -2212
  106. build/lib/build/lib/siat/fund.py +0 -629
  107. build/lib/build/lib/siat/fund_china.py +0 -3307
  108. build/lib/build/lib/siat/future_china.py +0 -551
  109. build/lib/build/lib/siat/google_authenticator.py +0 -47
  110. build/lib/build/lib/siat/grafix.py +0 -3636
  111. build/lib/build/lib/siat/holding_risk.py +0 -867
  112. build/lib/build/lib/siat/luchy_draw.py +0 -638
  113. build/lib/build/lib/siat/market_china.py +0 -1168
  114. build/lib/build/lib/siat/markowitz.py +0 -2363
  115. build/lib/build/lib/siat/markowitz2.py +0 -3150
  116. build/lib/build/lib/siat/markowitz2_20250704.py +0 -2969
  117. build/lib/build/lib/siat/markowitz2_20250705.py +0 -3158
  118. build/lib/build/lib/siat/markowitz_simple.py +0 -373
  119. build/lib/build/lib/siat/ml_cases.py +0 -2291
  120. build/lib/build/lib/siat/ml_cases_example.py +0 -60
  121. build/lib/build/lib/siat/option_china.py +0 -3069
  122. build/lib/build/lib/siat/option_pricing.py +0 -1925
  123. build/lib/build/lib/siat/other_indexes.py +0 -409
  124. build/lib/build/lib/siat/risk_adjusted_return.py +0 -1576
  125. build/lib/build/lib/siat/risk_adjusted_return2.py +0 -1900
  126. build/lib/build/lib/siat/risk_evaluation.py +0 -2218
  127. build/lib/build/lib/siat/risk_free_rate.py +0 -351
  128. build/lib/build/lib/siat/sector_china.py +0 -4140
  129. build/lib/build/lib/siat/security_price2.py +0 -727
  130. build/lib/build/lib/siat/security_prices.py +0 -3408
  131. build/lib/build/lib/siat/security_trend.py +0 -402
  132. build/lib/build/lib/siat/security_trend2.py +0 -646
  133. build/lib/build/lib/siat/stock.py +0 -4284
  134. build/lib/build/lib/siat/stock_advice_linear.py +0 -934
  135. build/lib/build/lib/siat/stock_base.py +0 -26
  136. build/lib/build/lib/siat/stock_china.py +0 -2095
  137. build/lib/build/lib/siat/stock_prices_kneighbors.py +0 -910
  138. build/lib/build/lib/siat/stock_prices_linear.py +0 -386
  139. build/lib/build/lib/siat/stock_profile.py +0 -707
  140. build/lib/build/lib/siat/stock_technical.py +0 -3305
  141. build/lib/build/lib/siat/stooq.py +0 -74
  142. build/lib/build/lib/siat/transaction.py +0 -347
  143. build/lib/build/lib/siat/translate.py +0 -5183
  144. build/lib/build/lib/siat/valuation.py +0 -1378
  145. build/lib/build/lib/siat/valuation_china.py +0 -2076
  146. build/lib/build/lib/siat/var_model_validation.py +0 -444
  147. build/lib/build/lib/siat/yf_name.py +0 -811
  148. build/lib/siat/__init__.py +0 -75
  149. build/lib/siat/allin.py +0 -137
  150. build/lib/siat/assets_liquidity.py +0 -915
  151. build/lib/siat/beta_adjustment.py +0 -1058
  152. build/lib/siat/beta_adjustment_china.py +0 -548
  153. build/lib/siat/blockchain.py +0 -143
  154. build/lib/siat/bond.py +0 -2900
  155. build/lib/siat/bond_base.py +0 -992
  156. build/lib/siat/bond_china.py +0 -100
  157. build/lib/siat/bond_zh_sina.py +0 -143
  158. build/lib/siat/capm_beta.py +0 -783
  159. build/lib/siat/capm_beta2.py +0 -887
  160. build/lib/siat/common.py +0 -5360
  161. build/lib/siat/compare_cross.py +0 -642
  162. build/lib/siat/copyrights.py +0 -18
  163. build/lib/siat/cryptocurrency.py +0 -667
  164. build/lib/siat/economy.py +0 -1471
  165. build/lib/siat/economy2.py +0 -1853
  166. build/lib/siat/esg.py +0 -536
  167. build/lib/siat/event_study.py +0 -815
  168. build/lib/siat/fama_french.py +0 -1521
  169. build/lib/siat/fin_stmt2_yahoo.py +0 -982
  170. build/lib/siat/financial_base.py +0 -1160
  171. build/lib/siat/financial_statements.py +0 -598
  172. build/lib/siat/financials.py +0 -2339
  173. build/lib/siat/financials2.py +0 -1278
  174. build/lib/siat/financials_china.py +0 -4433
  175. build/lib/siat/financials_china2.py +0 -2212
  176. build/lib/siat/fund.py +0 -629
  177. build/lib/siat/fund_china.py +0 -3307
  178. build/lib/siat/future_china.py +0 -551
  179. build/lib/siat/google_authenticator.py +0 -47
  180. build/lib/siat/grafix.py +0 -3636
  181. build/lib/siat/holding_risk.py +0 -867
  182. build/lib/siat/luchy_draw.py +0 -638
  183. build/lib/siat/market_china.py +0 -1168
  184. build/lib/siat/markowitz.py +0 -2363
  185. build/lib/siat/markowitz2.py +0 -3150
  186. build/lib/siat/markowitz2_20250704.py +0 -2969
  187. build/lib/siat/markowitz2_20250705.py +0 -3158
  188. build/lib/siat/markowitz_simple.py +0 -373
  189. build/lib/siat/ml_cases.py +0 -2291
  190. build/lib/siat/ml_cases_example.py +0 -60
  191. build/lib/siat/option_china.py +0 -3069
  192. build/lib/siat/option_pricing.py +0 -1925
  193. build/lib/siat/other_indexes.py +0 -409
  194. build/lib/siat/risk_adjusted_return.py +0 -1576
  195. build/lib/siat/risk_adjusted_return2.py +0 -1900
  196. build/lib/siat/risk_evaluation.py +0 -2218
  197. build/lib/siat/risk_free_rate.py +0 -351
  198. build/lib/siat/sector_china.py +0 -4140
  199. build/lib/siat/security_price2.py +0 -727
  200. build/lib/siat/security_prices.py +0 -3408
  201. build/lib/siat/security_trend.py +0 -402
  202. build/lib/siat/security_trend2.py +0 -646
  203. build/lib/siat/stock.py +0 -4284
  204. build/lib/siat/stock_advice_linear.py +0 -934
  205. build/lib/siat/stock_base.py +0 -26
  206. build/lib/siat/stock_china.py +0 -2095
  207. build/lib/siat/stock_prices_kneighbors.py +0 -910
  208. build/lib/siat/stock_prices_linear.py +0 -386
  209. build/lib/siat/stock_profile.py +0 -707
  210. build/lib/siat/stock_technical.py +0 -3305
  211. build/lib/siat/stooq.py +0 -74
  212. build/lib/siat/transaction.py +0 -347
  213. build/lib/siat/translate.py +0 -5183
  214. build/lib/siat/valuation.py +0 -1378
  215. build/lib/siat/valuation_china.py +0 -2076
  216. build/lib/siat/var_model_validation.py +0 -444
  217. build/lib/siat/yf_name.py +0 -811
  218. siat-3.10.132.dist-info/RECORD +0 -218
build/lib/siat/esg.py DELETED
@@ -1,536 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- 本模块功能:提供全球股票ESG信息
4
- 所属工具包:证券投资分析工具SIAT
5
- SIAT:Security Investment Analysis Tool
6
- 创建日期:2019年8月18日
7
- 最新修订日期:2020年7月25日
8
- 作者:王德宏 (WANG Dehong, Peter)
9
- 作者单位:北京外国语大学国际商学院
10
- 版权所有:王德宏
11
- 用途限制:仅限研究与教学使用,不可商用!商用需要额外授权。
12
- 特别声明:作者不对使用本工具进行证券投资导致的任何损益负责!
13
- """
14
- #==============================================================================
15
- #屏蔽所有警告性信息
16
- import warnings; warnings.filterwarnings('ignore')
17
- from siat.common import *
18
- from siat.translate import *
19
- from siat.grafix import *
20
- #==============================================================================
21
- #==============================================================================
22
- #以下使用yfinance数据源
23
- #==============================================================================
24
- def get_esg(stocklist):
25
- """
26
- 功能:根据股票代码列表,抓取企业最新的可持续性发展ESG数据
27
- 输入参数:
28
- stocklist:股票代码列表,例如单个股票["AAPL"], 多只股票["AAPL","MSFT","GOOG"]
29
- 输出参数:
30
- 企业最新的可持续性发展ESG数据,数据框
31
- """
32
-
33
- #引用插件
34
- try:
35
- import yfinance as yf
36
- except:
37
- print("#Error(get_esg), need plugin: yfinance")
38
- print(" Solution: open Anaconda Prompt, type in command \"pip install yfinance\", Enter!")
39
- return None
40
- tickerlist=stocklist.copy()
41
-
42
- #测试数据,使用后请注释掉
43
- """
44
- tickerlist=["PDD","MSFT","BABA","JD","GOOG"]
45
- """
46
-
47
- #处理股票列表中的第一只股票,跳过无数据的项目
48
- skiplist=[]
49
- for t in tickerlist:
50
- tp=yf.Ticker(t)
51
- try:
52
- print("...正在搜索ESG信息:",t,"...",end='')
53
- sst=tp.sustainability
54
- sst.rename(columns={'Value':t},inplace=True)
55
- sstt=sst.T
56
- except: #本项目无数据,进入下一次循环
57
- print("未找到:-(")
58
- skiplist=skiplist+[t]
59
- continue
60
- skiplist=skiplist+[t]
61
- print("完成!")
62
- break
63
-
64
- #仅保留尚未处理的项目
65
- for t in skiplist: tickerlist.remove(t)
66
-
67
- #处理股票列表中的其他股票
68
- for t in tickerlist:
69
- #print("---stock:",t)
70
- tp=yf.Ticker(t)
71
- try:
72
- print("...正在搜索ESG信息:",t,"...",end='')
73
- sst1=tp.sustainability
74
- sst1.rename(columns={'Value':t},inplace=True)
75
- except:
76
- print("未找到:-(")
77
- continue #未抓取到数据
78
- sst1t=sst1.T
79
- try:
80
- sstt=sstt.append([sst1t])
81
- except:
82
- sstt=sstt._append([sst1t])
83
- print("完成!")
84
-
85
- try:
86
- if len(sstt)==0: return None
87
- except:
88
- return None
89
-
90
- #只保留需要的列
91
- sust=sstt[['totalEsg','percentile','esgPerformance','environmentScore', \
92
- 'environmentPercentile','socialScore','socialPercentile', \
93
- 'governanceScore','governancePercentile','peerGroup','peerCount']].copy()
94
- sust.rename(columns={'totalEsg':'ESGscore','percentile':'ESGpercentile', \
95
- 'esgPerformance':'ESGperformance','environmentScore':'EPscore', \
96
- 'environmentPercentile':'EPpercentile','socialScore':'CSRscore', \
97
- 'socialPercentile':'CSRpercentile','governanceScore':'CGscore', \
98
- 'governancePercentile':'CGpercentile', \
99
- 'peerGroup':'Peer Group','peerCount':'Count'},inplace=True)
100
-
101
- return sust
102
-
103
- if __name__ =="__main__":
104
- stocklist=["VIPS","BABA","JD","MSFT","WMT"]
105
- sust=get_esg(stocklist)
106
-
107
-
108
- #==============================================================================
109
- def print_esg(sustainability,option="ESG"):
110
- """
111
- 功能:显示企业的可持续性发展数据
112
- 输入参数:
113
- sustainability:抓取到的企业可持续性数据框
114
- 输出参数:无
115
- """
116
-
117
- if not (option in ['ESG','EP','CSR','CG']):
118
- print("...Error 01(print_sustainability): only ESG/EP/CSR/CG are valid")
119
- return
120
-
121
- import datetime as dt
122
- today=dt.date.today()
123
- s=sustainability.copy()
124
-
125
- #修改评级为中文
126
- s['ESGperformance']=s['ESGperformance'].apply(lambda x: "高" if x=='OUT_PERF' else x)
127
- s['ESGperformance']=s['ESGperformance'].apply(lambda x: "中" if x=='AVG_PERF' else x)
128
-
129
- #列改中文名,并删除含0的无效列
130
- colnames=list(s)
131
- for c in colnames:
132
- if len(s[s[c]==0]) >= 1:
133
- del s[c]; continue
134
- s.rename(columns={c:ectranslate(c)},inplace=True)
135
-
136
- #打印输出对齐
137
- import pandas as pd
138
- pd.set_option('display.unicode.ambiguous_as_wide', True)
139
- pd.set_option('display.unicode.east_asian_width', True)
140
- pd.set_option('display.width', 180) # 设置打印宽度(**重要**)
141
-
142
- #显示分数和分位数
143
- colnames=list(s)
144
- printnames=[]
145
- if option=="ESG":
146
- s=s.sort_values([ectranslate('ESGscore')],ascending=False)
147
- esgnames=[ectranslate('ESGscore'),ectranslate('ESGperformance'), \
148
- ectranslate('ESGpercentile'),ectranslate('Peer Group')]
149
- for i in esgnames:
150
- if i in colnames: printnames=printnames+[i]
151
- print("\n=== 企业发展可持续性:ESG综合风险 ===\n")
152
- print(s[printnames])
153
- print("来源: 雅虎财经,",str(today))
154
-
155
- if option=="EP":
156
- s=s.sort_values([ectranslate('EPscore')],ascending=False)
157
- esgnames=[ectranslate('EPscore'), \
158
- ectranslate('EPpercentile'),ectranslate('Peer Group')]
159
- for i in esgnames:
160
- if i in colnames: printnames=printnames+[i]
161
- print("\n=== 企业发展可持续性:环保风险指数 ===\n")
162
- print(s[printnames])
163
- print("来源: 雅虎财经,",str(today))
164
-
165
- if option=="CSR":
166
- s=s.sort_values([ectranslate('CSRscore')],ascending=False)
167
- esgnames=[ectranslate('CSRscore'), \
168
- ectranslate('CSRpercentile'),ectranslate('Peer Group')]
169
- for i in esgnames:
170
- if i in colnames: printnames=printnames+[i]
171
- print("\n=== 企业发展可持续性:社会责任风险指数 ===\n")
172
- print(s[printnames])
173
- print("来源: 雅虎财经,",str(today))
174
-
175
- if option=="CG":
176
- s=s.sort_values([ectranslate('CGscore')],ascending=False)
177
- esgnames=[ectranslate('CGscore'), \
178
- ectranslate('CGpercentile'),ectranslate('Peer Group')]
179
- for i in esgnames:
180
- if i in colnames: printnames=printnames+[i]
181
- print("\n=== 企业发展可持续性:公司治理风险指数 ===\n")
182
- print(s[printnames])
183
- print("来源: 雅虎财经,",str(today))
184
-
185
- return
186
-
187
- if __name__ =="__main__":
188
- print_esg(sust,option="ESG")
189
- print_esg(sust,option="EP")
190
- print_esg(sust,option="CSR")
191
- print_esg(sust,option="CG")
192
- print_esg(sust,option="ABC")
193
-
194
-
195
-
196
- #==============================================================================
197
- def ploth_esg(sustainability,option="ESG"):
198
- """
199
- 功能:绘制ESG水平直方图
200
- 输入参数:抓取到的企业可持续性数据框sustainability;选项option。
201
- 输出参数:无
202
- """
203
-
204
- if not (option in ['ESG','EP','CSR','CG']):
205
- print("...Error 01(ploth_esg): only ESG/EP/CSR/CG are valid")
206
- return
207
-
208
- s=sustainability.copy()
209
- import datetime as dt; today=dt.date.today()
210
- footnote="注:数值越小,风险越低。来源:雅虎财经,"+str(today)
211
-
212
- #绘制分数和分位数图
213
- if option=="ESG":
214
- #排序
215
- s=s.sort_values(['ESGscore'],ascending=True)
216
- titletxt1="企业发展可持续性:ESG综合风险指数"
217
- plot_barh(s,'ESGscore',titletxt1,footnote)
218
- titletxt2="企业发展可持续性:ESG综合风险行业分位数%"
219
- plot_barh(s,'ESGpercentile',titletxt2,footnote)
220
-
221
- if option=="EP":
222
- s=s.sort_values(['EPscore'],ascending=True)
223
- titletxt="企业发展可持续性:环保风险指数"
224
- plot_barh(s,'EPscore',titletxt,footnote)
225
-
226
- if option=="CSR":
227
- s=s.sort_values(['CSRscore'],ascending=True)
228
- titletxt="企业发展可持续性:社会责任风险指数"
229
- plot_barh(s,'CSRscore',titletxt,footnote)
230
-
231
- if option=="CG":
232
- s=s.sort_values(['CGscore'],ascending=True)
233
- titletxt="企业发展可持续性:公司治理风险指数"
234
- plot_barh(s,'CGscore',titletxt,footnote)
235
-
236
- return
237
-
238
- if __name__ =="__main__":
239
- ploth_esg(sust,option="ESG")
240
- ploth_esg(sust,option="EP")
241
- ploth_esg(sust,option="CSR")
242
- ploth_esg(sust,option="CG")
243
-
244
- #==============================================================================
245
- def esg(stocklist):
246
- """
247
- 功能:抓取、打印和绘图企业的可持续性发展数据,演示用
248
- 输入参数:
249
- stocklist:股票代码列表,例如单个股票["AAPL"], 多只股票["AAPL","MSFT","GOOG"]
250
- 输出参数:
251
- 企业最新的可持续性发展数据,数据框
252
- """
253
-
254
- #抓取数据
255
- try:
256
- sust=get_esg(stocklist)
257
- except:
258
- print("#Error(esg), fail to get ESG data for",stocklist)
259
- return None
260
- if sust is None:
261
- print("#Error(esg), fail to get ESG data for",stocklist)
262
- return None
263
-
264
- #处理小数点
265
- from pandas.api.types import is_numeric_dtype
266
- cols=list(sust)
267
- for c in cols:
268
- if is_numeric_dtype(sust[c]):
269
- sust[c]=round(sust[c],2)
270
-
271
- #打印和绘图ESG
272
- ploth_esg(sust,option="ESG")
273
- print_esg(sust,option="ESG")
274
- #打印和绘图EP
275
- ploth_esg(sust,option="EP")
276
- print_esg(sust,option="EP")
277
- #打印和绘图CSR
278
- ploth_esg(sust,option="CSR")
279
- print_esg(sust,option="CSR")
280
- #打印和绘图CG
281
- ploth_esg(sust,option="CG")
282
- print_esg(sust,option="CG")
283
-
284
- return sust
285
-
286
- if __name__ =="__main__":
287
- stocklist1=["AMZN","EBAY","BABA"]
288
- sust1=esg(stocklist1)
289
- stocklist2=["AMZN","EBAY","BABA","JD","VIPS","WMT"]
290
- sust2=esg(stocklist2)
291
-
292
- #==============================================================================
293
- def portfolio_esg(portfolio):
294
- """
295
- 功能:抓取、打印和绘图投资组合portfolio的可持续性发展数据,演示用
296
- 输入参数:
297
- 企业最新的可持续性发展数据,数据框
298
- """
299
- #解构投资组合
300
- _,_,stocklist,_,ticker_type=decompose_portfolio(portfolio)
301
-
302
- #抓取数据
303
- try:
304
- sust=get_esg(stocklist)
305
- except:
306
- print("#Error(portfolio_esg), fail to get ESG data for",stocklist)
307
- return None
308
- if sust is None:
309
- #print("#Error(portfolio_esg), fail to get ESG data for",stocklist)
310
- return None
311
-
312
- #处理小数点
313
- from pandas.api.types import is_numeric_dtype
314
- cols=list(sust)
315
- for c in cols:
316
- if is_numeric_dtype(sust[c]):
317
- sust[c]=round(sust[c],2)
318
-
319
- #显示结果
320
- print("\n***** Portfolio ESG Risk *****")
321
- print("Portfolio:",stocklist)
322
- #显示各个成分股的ESG分数
323
- sust['Stock']=sust.index
324
- esgdf=sust[['Stock','ESGscore','EPscore','CSRscore','CGscore']]
325
- print(esgdf.to_string(index=False))
326
-
327
- print("\nPortfolio ESG Evaluation:")
328
- #木桶短板:EPScore
329
- esg_ep=esgdf.sort_values(['EPscore'], ascending = True)
330
- p_ep=esg_ep['EPscore'][-1]
331
- p_ep_stock=esg_ep.index[-1]
332
- str_ep=" EP score (from "+str(p_ep_stock)+")"
333
- len_ep=len(str_ep)
334
-
335
- #木桶短板:CSRScore
336
- esg_csr=esgdf.sort_values(['CSRscore'], ascending = True)
337
- p_csr=esg_csr['CSRscore'][-1]
338
- p_csr_stock=esg_csr.index[-1]
339
- str_csr=" CSR score (from "+str(p_csr_stock)+")"
340
- len_csr=len(str_csr)
341
-
342
- #木桶短板:CGScore
343
- esg_cg=esgdf.sort_values(['CGscore'], ascending = True)
344
- p_cg=esg_cg['CGscore'][-1]
345
- p_cg_stock=esg_cg.index[-1]
346
- str_cg=" CG score (from "+str(p_cg_stock)+")"
347
- len_cg=len(str_cg)
348
-
349
- str_esg=" Overall ESG score"
350
- len_esg=len(str_esg)
351
-
352
- #计算对齐冒号中间需要的空格数目
353
- len_max=max(len_ep,len_csr,len_cg,len_esg)
354
- str_ep=str_ep+' '*(len_max-len_ep+1)+':'
355
- str_csr=str_csr+' '*(len_max-len_csr+1)+':'
356
- str_cg=str_cg+' '*(len_max-len_cg+1)+':'
357
- str_esg=str_esg+' '*(len_max-len_esg+1)+':'
358
-
359
- #对齐打印
360
- print(str_ep,p_ep)
361
- print(str_csr,p_csr)
362
- print(str_cg,p_cg)
363
- #计算投资组合的ESG综合风险
364
- p_esg=round(p_ep+p_csr+p_cg,2)
365
- print(str_esg,p_esg)
366
-
367
- import datetime as dt; today=dt.date.today()
368
- footnote="The higher the score, the higher the risk. \
369
- \nSource: Yahoo Finance, "+str(today)
370
- print(footnote)
371
-
372
- return p_esg
373
-
374
- if __name__ =="__main__":
375
- #market={'Market':('China','^HSI')}
376
- market={'Market':('US','^GSPC')}
377
- #stocks={'0939.HK':2,'1398.HK':1,'3988.HK':3}
378
- stocks={'VIPS':3,'JD':2,'BABA':1}
379
- portfolio=dict(market,**stocks)
380
- esg=portfolio_esg(portfolio)
381
- #==============================================================================
382
- #==============================================================================
383
- #==============================================================================
384
- #==============================================================================
385
- #====以下使用yahooquery数据源===================================================
386
- if __name__ =="__main__":
387
- stocklist=["BAC", "TD","PNC"]
388
-
389
- def get_esg2(stocklist):
390
- """
391
- 功能:根据股票代码列表,抓取企业最新的可持续性发展ESG数据
392
- 输入参数:
393
- stocklist:股票代码列表,例如单个股票["AAPL"], 多只股票["AAPL","MSFT","GOOG"]
394
- 输出参数:
395
- 企业最新的可持续性发展ESG数据,数据框
396
- """
397
-
398
- import pandas as pd
399
- collist=['symbol','totalEsg','environmentScore','socialScore','governanceScore']
400
- sust=pd.DataFrame(columns=collist)
401
- for t in stocklist:
402
- try:
403
- info=stock_info(t).T
404
- except:
405
- print("#Error(get_esg2): esg info not available for",t)
406
- continue
407
- if (info is None) or (len(info)==0):
408
- print("#Error(get_esg2): failed to get esg info for",t)
409
- continue
410
- sub=info[collist]
411
- sust=pd.concat([sust,sub])
412
-
413
- newcols=['Stock','ESGscore','EPscore','CSRscore','CGscore']
414
- sust.columns=newcols
415
- """
416
- sust=sust.rename(columns={'symbol':'Stock','totalEsg':'ESGscore', \
417
- 'environmentScore':'EPscore', \
418
- 'socialScore':'CSRscore', \
419
- 'governanceScore':'CGscore'})
420
- """
421
- sust.set_index('Stock',inplace=True)
422
-
423
- return sust
424
-
425
- if __name__ =="__main__":
426
- stocklist=["VIPS","BABA","JD","MSFT","WMT"]
427
- sust=get_esg(stocklist)
428
-
429
- #==============================================================================
430
- if __name__ =="__main__":
431
- market={'Market':('China','^HSI')}
432
- stocks={'0700.HK':3,'9618.HK':2,'9988.HK':1}
433
- portfolio=dict(market,**stocks)
434
-
435
- def portfolio_esg2(portfolio):
436
- """
437
- 功能:抓取、打印和绘图投资组合portfolio的可持续性发展数据,演示用
438
- 输入参数:
439
- 企业最新的可持续性发展数据,数据框
440
- """
441
- #解构投资组合
442
- _,_,stocklist,_,ticker_type=decompose_portfolio(portfolio)
443
-
444
- #抓取数据
445
- try:
446
- sust=get_esg2(stocklist)
447
- except:
448
- print("#Error(portfolio_esg), fail to get ESG data for",stocklist)
449
- return None
450
- if sust is None:
451
- #print("#Error(portfolio_esg), fail to get ESG data for",stocklist)
452
- return None
453
-
454
- #处理小数点
455
- from pandas.api.types import is_numeric_dtype
456
- cols=list(sust)
457
- for c in cols:
458
- if is_numeric_dtype(sust[c]):
459
- sust[c]=round(sust[c],2)
460
-
461
- #显示结果
462
- print("\n***** Portfolio ESG Risk *****")
463
- print("Portfolio:",stocklist)
464
- #显示各个成分股的ESG分数
465
- sust['Stock']=sust.index
466
- esgdf=sust[['Stock','ESGscore','EPscore','CSRscore','CGscore']]
467
- print(esgdf.to_string(index=False))
468
-
469
- print("\nPortfolio ESG Evaluation:")
470
- #木桶短板:EPScore
471
- esg_ep=esgdf.sort_values(['EPscore'], ascending = True)
472
- p_ep=esg_ep['EPscore'][-1]
473
- p_ep_stock=esg_ep.index[-1]
474
- str_ep=" EP score (from "+str(p_ep_stock)+")"
475
- len_ep=len(str_ep)
476
-
477
- #木桶短板:CSRScore
478
- esg_csr=esgdf.sort_values(['CSRscore'], ascending = True)
479
- p_csr=esg_csr['CSRscore'][-1]
480
- p_csr_stock=esg_csr.index[-1]
481
- str_csr=" CSR score (from "+str(p_csr_stock)+")"
482
- len_csr=len(str_csr)
483
-
484
- #木桶短板:CGScore
485
- esg_cg=esgdf.sort_values(['CGscore'], ascending = True)
486
- p_cg=esg_cg['CGscore'][-1]
487
- p_cg_stock=esg_cg.index[-1]
488
- str_cg=" CG score (from "+str(p_cg_stock)+")"
489
- len_cg=len(str_cg)
490
-
491
- str_esg=" Overall ESG score"
492
- len_esg=len(str_esg)
493
-
494
- #计算对齐冒号中间需要的空格数目
495
- len_max=max(len_ep,len_csr,len_cg,len_esg)
496
- str_ep=str_ep+' '*(len_max-len_ep+1)+':'
497
- str_csr=str_csr+' '*(len_max-len_csr+1)+':'
498
- str_cg=str_cg+' '*(len_max-len_cg+1)+':'
499
- str_esg=str_esg+' '*(len_max-len_esg+1)+':'
500
-
501
- #对齐打印
502
- print(str_ep,p_ep)
503
- print(str_csr,p_csr)
504
- print(str_cg,p_cg)
505
- #计算投资组合的ESG综合风险
506
- p_esg=round(p_ep+p_csr+p_cg,2)
507
- print(str_esg,p_esg)
508
-
509
- import datetime as dt; today=dt.date.today()
510
- footnote="The higher the score, the higher the risk. \
511
- \nSource: Yahoo Finance, "+str(today)
512
- print(footnote)
513
-
514
- return p_esg
515
-
516
- if __name__ =="__main__":
517
- #market={'Market':('China','^HSI')}
518
- market={'Market':('US','^GSPC')}
519
- #stocks={'0939.HK':2,'1398.HK':1,'3988.HK':3}
520
- stocks={'VIPS':3,'JD':2,'BABA':1}
521
- portfolio=dict(market,**stocks)
522
- esg=portfolio_esg(portfolio)
523
- #==============================================================================
524
-
525
- #==============================================================================
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-