siat 3.10.125__py3-none-any.whl → 3.10.127__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 (87) hide show
  1. siat/common.py +106 -2
  2. siat/exchange_bond_china.pickle +0 -0
  3. siat/fund_china.pickle +0 -0
  4. siat/stock.py +10 -2
  5. siat/stock_info.pickle +0 -0
  6. {siat-3.10.125.dist-info → siat-3.10.127.dist-info}/METADATA +235 -226
  7. siat-3.10.127.dist-info/RECORD +76 -0
  8. {siat-3.10.125.dist-info → siat-3.10.127.dist-info}/WHEEL +1 -1
  9. {siat-3.10.125.dist-info → siat-3.10.127.dist-info/licenses}/LICENSE +0 -0
  10. {siat-3.10.125.dist-info → siat-3.10.127.dist-info}/top_level.txt +0 -0
  11. siat/__init__ -20240701.py +0 -65
  12. siat/__init__.py.backup_20250214.py +0 -73
  13. siat/alpha_vantage_test.py +0 -24
  14. siat/assets_liquidity_test.py +0 -44
  15. siat/barrons_scraping_test.py +0 -276
  16. siat/beta_adjustment_test.py +0 -77
  17. siat/bond_test.py +0 -142
  18. siat/capm_beta_test.py +0 -49
  19. siat/cmat_commons.py +0 -961
  20. siat/compare_cross_test.py +0 -117
  21. siat/concepts_iwencai.py +0 -86
  22. siat/concepts_kpl.py +0 -93
  23. siat/cryptocurrency_test.py +0 -71
  24. siat/derivative.py +0 -1111
  25. siat/economy-20230125.py +0 -1206
  26. siat/economy_test.py +0 -360
  27. siat/esg_test.py +0 -63
  28. siat/fama_french_test.py +0 -115
  29. siat/financial_statements_test.py +0 -31
  30. siat/financials2 - /321/205/320/231/320/277/321/206/320/254/320/274.py" +0 -341
  31. siat/financials_china2_test.py +0 -67
  32. siat/financials_china2_test2.py +0 -88
  33. siat/financials_china2_test3.py +0 -87
  34. siat/financials_china_test.py +0 -475
  35. siat/financials_china_test2.py +0 -197
  36. siat/financials_china_test2_fin_indicator.py +0 -197
  37. siat/financials_test.py +0 -713
  38. siat/fred_test.py +0 -40
  39. siat/fund_china_test.py +0 -175
  40. siat/fund_test.py +0 -40
  41. siat/future_china_test.py +0 -37
  42. siat/global_index_test.py +0 -66
  43. siat/grafix_test.py +0 -112
  44. siat/holding_risk_test.py +0 -13
  45. siat/local_debug_test.py +0 -100
  46. siat/markowitz2-20240620.py +0 -2614
  47. siat/markowitz_ccb_test.py +0 -37
  48. siat/markowitz_ef_test.py +0 -136
  49. siat/markowitz_old.py +0 -871
  50. siat/markowitz_simple-20230709.py +0 -370
  51. siat/markowitz_test.py +0 -164
  52. siat/markowitz_test2.py +0 -69
  53. siat/ml_cases_example1.py +0 -60
  54. siat/option_china_test.py +0 -447
  55. siat/option_pricing_test.py +0 -81
  56. siat/option_sina_api_test.py +0 -112
  57. siat/proxy_test.py +0 -84
  58. siat/quandl_test.py +0 -39
  59. siat/risk_adjusted_return_test.py +0 -81
  60. siat/risk_evaluation_test.py +0 -96
  61. siat/risk_free_rate_test.py +0 -127
  62. siat/sector_china_test.py +0 -203
  63. siat/security_price.py +0 -831
  64. siat/security_prices_test.py +0 -310
  65. siat/security_trend2-20240620.py +0 -493
  66. siat/setup.py +0 -41
  67. siat/shenwan index history test.py +0 -41
  68. siat/stock_china_test.py +0 -38
  69. siat/stock_info_test.py +0 -189
  70. siat/stock_list_china_test.py +0 -33
  71. siat/stock_technical-20240620.py +0 -2736
  72. siat/stock_test.py +0 -487
  73. siat/temp.py +0 -36
  74. siat/test2_graphviz.py +0 -484
  75. siat/test_graphviz.py +0 -411
  76. siat/test_markowitz_simple.py +0 -198
  77. siat/test_markowitz_simple_revised.py +0 -215
  78. siat/test_markowitz_simple_revised2.py +0 -218
  79. siat/transaction_test.py +0 -436
  80. siat/translate-20230125.py +0 -2107
  81. siat/translate-20230206.py +0 -2109
  82. siat/translate-20230215.py +0 -2158
  83. siat/translate_20240606.py +0 -4206
  84. siat/translate_241003_keep.py +0 -4300
  85. siat/universal_test.py +0 -100
  86. siat/valuation_market_china_test.py +0 -36
  87. siat-3.10.125.dist-info/RECORD +0 -152
@@ -1,215 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- #==============================================================================
4
- %matplotlib inline
5
-
6
- # 小函数,使得过程逻辑更加清晰
7
- def gen_weights(n):
8
- """
9
- 产生投资组合各个成分股的随机权重
10
- """
11
- import numpy as np
12
- w=np.random.rand(n)
13
- return w /sum(w)
14
-
15
- def port_ret(w,r_annual):
16
- """
17
- 计算投资组合的年化收益率
18
- """
19
- import numpy as np
20
- return np.sum(w*r_annual)
21
-
22
- def port_std(w,log_r):
23
- """
24
- 计算投资组合的年化标准差
25
- """
26
- import numpy as np
27
- return np.sqrt((w.dot(log_r.cov()*250).dot(w.T)))
28
-
29
-
30
- def gen_ports(n,times,log_r,r_annual):
31
- #生成若干投资组合的收益和风险
32
- for _ in range(times):#生成不同的组合
33
- w=gen_weights(n)#每次生成不同的权重
34
- yield (port_std(w,log_r),port_ret(w,r_annual),w)#计算风险和期望收益 以及组合的权重情况
35
-
36
- #------------------------------------------------------------------------------
37
-
38
- if __name__=='__main__':
39
- components = {
40
- 'AAPL':'苹果',
41
- 'AMZN':'亚马逊',
42
- 'GOOGL':'谷歌',
43
- 'BABA':'阿里巴巴'
44
- }
45
-
46
- start='2016-1-1'
47
- end='2017-12-31'
48
-
49
- rf=0.015
50
- simulation=25000
51
- trend=True
52
- ef=True
53
- MSR=True
54
- CML=True
55
-
56
- markowitz_simple(components,start,end)
57
-
58
- def markowitz_simple(components,start,end,rf=0.015,simulation=25000, \
59
- trend=True,ef=True,MSR=True,CML=False):
60
- """
61
- 马科维茨有效投资投资边界的基本思想
62
- 通过对资产组合当中不同资产的配置情况进行调整,达到在既定风险水平下的收益最大化,
63
- 或者既定收益水平下的风险最小化。
64
- """
65
-
66
- from datetime import date
67
- import pandas_datareader.data as web
68
- import matplotlib.pyplot as plt
69
- import numpy as np
70
- import seaborn as sns
71
- import warnings
72
- warnings.filterwarnings("ignore")
73
-
74
- # 步骤1:获取股价
75
- tickers=list(components)
76
- stock_data=get_prices(tickers,start,end)['Close']
77
- stock_data.rename(columns=components,inplace=True)
78
- stock_data=stock_data.iloc[::-1]
79
-
80
- #画出收盘价走势图
81
- if trend:
82
- sns.set_style("whitegrid")#横坐标有标线,纵坐标没有标线,背景白色
83
- sns.set_style("darkgrid") #默认,横纵坐标都有标线,组成一个一个格子,背景稍微深色
84
- sns.set_style("dark")#背景稍微深色,没有标线线
85
- sns.set_style("white")#背景白色,没有标线线
86
- sns.set_style("ticks")#xy轴都有非常短的小刻度
87
- sns.despine(offset=30,left=True)#去掉上边和右边的轴线,offset=30表示距离轴线(x轴)的距离,left=True表示左边的轴保留
88
- sns.set(font='SimHei',rc={'figure.figsize':(10,6)})# 图片大小和中文字体设置
89
-
90
- # 图形展示
91
- # stock_data.iloc[0]含有字样Symbols
92
- (stock_data/stock_data.iloc[0]).plot()
93
-
94
- #------------------------------------------------------------------------------
95
- # 步骤2:计算股票的收益率和风险,假定每年有250个交易日
96
- # 收益率
97
- R=stock_data/stock_data.shift(1)-1
98
-
99
- # 对数收益率
100
- log_r=np.log(stock_data/stock_data.shift(1))
101
-
102
- # 年化收益率
103
- r_annual=np.exp(log_r.mean()*250)-1
104
-
105
- # 风险
106
- std = np.sqrt(log_r.var() * 250)#假设协方差为0
107
-
108
- #------------------------------------------------------------------------------
109
- # 步骤3:投资组合的收益和风险
110
-
111
- n=len(list(tickers))
112
- w=gen_weights(n)
113
- #list(zip(r_annual.index,w))
114
-
115
- import pandas as pd
116
- # 投资组合模拟次数
117
- df=pd.DataFrame(gen_ports(n,simulation,log_r,r_annual),columns=["std","ret","w"])
118
-
119
- #------------------------------------------------------------------------------
120
-
121
- # 步骤4:画出投资有效边界,假设无风险利率为rf
122
- # 引入夏普比率
123
- df['sharpe'] = (df['ret'] - rf) / df['std']#定义夏普比率
124
- fig, ax = plt.subplots()
125
- df.plot.scatter('std', 'ret', c='sharpe',s=30, alpha=0.3, cmap='cool',marker='o', ax=ax)
126
- plt.style.use('ggplot')
127
- plt.rcParams['axes.unicode_minus'] = False# 显示负号
128
-
129
- #list(zip(r_annual.index, df.loc[df.sharpe.idxmax()].w))
130
-
131
-
132
- import scipy.optimize as opt
133
- frontier=pd.DataFrame(columns=['std','ret'])
134
-
135
- # std的范围:0.16,0.25
136
- std_min=round(df['std'].min(),2)
137
- std_max=round(df['std'].max(),2)
138
- #for std in np.linspace(0.16,0.25):
139
- for std in np.linspace(std_min,std_max):
140
- res=opt.minimize(lambda x:-port_ret(x,r_annual),
141
- x0=((1/n),)*n,
142
- method='SLSQP',
143
- bounds=((0,1),)*n,
144
- constraints=[
145
- {"fun":lambda x:port_std(x,log_r)-std,"type":"eq"},
146
- {"fun":lambda x:(np.sum(x)-1),"type":"eq"}
147
- ])
148
- if res.success:
149
- frontier=frontier.append({"std":std,"ret":-res.fun},ignore_index=True)
150
-
151
- if ef:
152
- frontier.plot('std','ret',lw=3,c='blue',ax=ax)
153
- fig
154
-
155
- #------------------------------------------------------------------------------
156
- # 步骤5:计算最优资产配置情况
157
-
158
- res=opt.minimize(lambda x:-((port_ret(x,r_annual)-0.03)/port_std(x,log_r)),
159
- x0=((1/n),)*n,
160
- method='SLSQP',
161
- bounds=((0,1),)*n,
162
- constraints={"fun":lambda x:(np.sum(x)-1), "type":"eq"})
163
-
164
- print(res.x.round(3))
165
-
166
- if MSR:
167
- ax.scatter(port_std(res.x,log_r),port_ret(res.x,r_annual),marker="*",c="black",s=300)
168
- fig
169
-
170
- #------------------------------------------------------------------------------
171
- # 步骤6:绘制资本市场线CML=Capital Market Line
172
- if CML:
173
- ax.plot((0,.27),(.03,-res.fun*.27+.03))
174
- fig
175
-
176
- """
177
- 在上图的所示资本市场线上,星号左边表示将资本用于投资一部分无风险资产和一部分风险资产组合,
178
- 而在星号处代表将所有的资本都用于投资风险资产组合,
179
- 星号右边意味着借入无风险资产并投资于风险资产组合,可以在相同的风险水平下获得更高的收益。
180
-
181
- """
182
-
183
- return
184
-
185
-
186
- #------------------------------------------------------------------------------
187
- #------------------------------------------------------------------------------
188
- #------------------------------------------------------------------------------
189
- #------------------------------------------------------------------------------
190
- #------------------------------------------------------------------------------
191
- #------------------------------------------------------------------------------
192
- #------------------------------------------------------------------------------
193
- #------------------------------------------------------------------------------
194
- #------------------------------------------------------------------------------
195
- #------------------------------------------------------------------------------
196
- #------------------------------------------------------------------------------
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
@@ -1,218 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
-
5
- #------------------------------------------------------------------------------
6
- # 全局引用,函数中无需再import
7
- from datetime import date
8
- import pandas_datareader.data as web
9
- import matplotlib.pyplot as plt
10
- import numpy as np
11
- import seaborn as sns
12
- import warnings
13
- warnings.filterwarnings("ignore")
14
- %matplotlib inline
15
-
16
- #------------------------------------------------------------------------------
17
- if __name__=='__main__':
18
- components = {
19
- 'AAPL':'苹果',
20
- 'AMZN':'亚马逊',
21
- 'GOOGL':'谷歌',
22
- 'BABA':'阿里巴巴'
23
- }
24
-
25
- start='2016-1-1'
26
- end='2017-12-31'
27
-
28
- risk_free=0.015
29
- simulation=25000
30
- price_trend=True
31
- feasible_set=True
32
- efficient_frontier=True
33
- MSR=True
34
- CML=True
35
-
36
-
37
- """
38
- 马科维茨有效投资投资边界的基本思想
39
- 通过对资产组合当中不同资产的配置情况进行调整,达到在既定风险水平下的收益最大化,
40
- 或者既定收益水平下的风险最小化。
41
- """
42
-
43
- #------------------------------------------------------------------------------
44
-
45
- #获取股票数据
46
- tickers=list(components)
47
- stock_data=get_prices(tickers,start,end)['Close']
48
- stock_data.rename(columns=components,inplace=True)
49
-
50
- stock_data=stock_data.iloc[::-1]
51
-
52
- #画出收盘价走势图
53
- if price_trend:
54
- print("\n Illustrating price trend ...")
55
-
56
- sns.set_style("whitegrid")#横坐标有标线,纵坐标没有标线,背景白色
57
- sns.set_style("darkgrid") #默认,横纵坐标都有标线,组成一个一个格子,背景稍微深色
58
- sns.set_style("dark")#背景稍微深色,没有标线线
59
- sns.set_style("white")#背景白色,没有标线线
60
- sns.set_style("ticks")#xy轴都有非常短的小刻度
61
- sns.despine(offset=30,left=True)#去掉上边和右边的轴线,offset=30表示距离轴线(x轴)的距离,left=True表示左边的轴保留
62
- sns.set(font='SimHei',rc={'figure.figsize':(10,6)})# 图片大小和中文字体设置
63
-
64
- # 图形展示
65
- (stock_data/stock_data.iloc[0]).plot()
66
-
67
- #------------------------------------------------------------------------------
68
- # 计算收益率和风险
69
- # 收益率
70
- R=stock_data/stock_data.shift(1)-1
71
-
72
- # 对数收益率
73
- log_r=np.log(stock_data/stock_data.shift(1))
74
-
75
- # 年化收益率
76
- r_annual=np.exp(log_r.mean()*250)-1
77
-
78
- # 风险
79
- std = np.sqrt(log_r.var() * 250)#假设协方差为0
80
-
81
- #------------------------------------------------------------------------------
82
- # 投资组合的收益和风险
83
- def gen_weights(n):
84
- #投资组合的权重
85
- import numpy as np
86
- w=np.random.rand(n)
87
- return w /sum(w)
88
-
89
- n=len(list(tickers))
90
- w=gen_weights(n)
91
- #list(zip(r_annual.index,w))
92
-
93
- def port_ret(w,r_annual):
94
- #投资组合收益
95
- import numpy as np
96
- return np.sum(w*r_annual)
97
- #port_ret(w,r_annual)
98
-
99
- def port_std(w,log_r):
100
- #投资组合的风险
101
- import numpy as np
102
- return np.sqrt((w.dot(log_r.cov()*250).dot(w.T)))
103
- #port_std(w,log_r)
104
-
105
- def gen_ports(times,n,log_r,r_annual):
106
- #若干投资组合的收益和风险
107
- import pandas as pd
108
- dft=pd.DataFrame(columns=["std","ret","w"])
109
- for _ in range(times):#生成不同的组合
110
- w=gen_weights(n)#每次生成不同的权重
111
- dft=dft.append({"std":port_std(w,log_r),"ret":-port_ret(w,r_annual),"w":w},ignore_index=True)
112
- #yield (port_std(w,log_r),port_ret(w,r_annual),w)#计算风险和期望收益 以及组合的权重情况
113
- return dft
114
-
115
- # 投资组合模拟次数
116
- print("\n Generating portfolio feasible set ...")
117
- df=gen_ports(simulation,n,log_r,r_annual)
118
- std_min=df['std'].min()
119
- std_max=df['std'].max()
120
- #df.head()
121
-
122
- #------------------------------------------------------------------------------
123
-
124
- # 画出投资组合可行集
125
- df['sharpe'] = (df['ret'] - risk_free) / df['std']#定义夏普比率
126
- sharpe_max=df['sharpe'].max()
127
-
128
- if feasible_set:
129
- fig, ax = plt.subplots()
130
- df.plot.scatter('std','ret',c='sharpe',s=30,alpha=0.3,cmap='cool',marker='o',ax=ax)
131
- plt.style.use('ggplot')
132
- plt.rcParams['axes.unicode_minus'] = False# 显示负号
133
-
134
- #list(zip(r_annual.index, df.loc[df.sharpe.idxmax()].w))
135
-
136
- # 画出投资组合有效集/有效边界
137
- import scipy.optimize as opt
138
- frontier=pd.DataFrame(columns=['std','ret'])
139
-
140
- print("\n Calculating portfolio efficient frontier ...")
141
- # std的范围:0.16,0.25
142
- #for std in np.linspace(0.16,0.25):
143
- for std in np.linspace(std_min,std_max):
144
- res=opt.minimize(lambda x:port_ret(x,r_annual),
145
- x0=((1/n),)*n,
146
- method='SLSQP',
147
- bounds=((0,1),)*n,
148
- constraints=[
149
- {"fun":lambda x:port_std(x,log_r)-std,"type":"eq"},
150
- {"fun":lambda x:(np.sum(x)-1),"type":"eq"}
151
- ])
152
- if res.success:
153
- frontier=frontier.append({"std":std,"ret":-res.fun},ignore_index=True)
154
-
155
- if efficient_frontier:
156
- frontier.plot('std','ret',lw=3,c='blue',ax=ax)
157
- fig
158
-
159
- #------------------------------------------------------------------------------
160
- # 计算最优资产配置情况
161
- std_msr=df[df['sharpe']==sharpe_max]['std'].values[0]
162
- ret_msr=df[df['sharpe']==sharpe_max]['ret'].values[0]
163
- w_msr=df[df['sharpe']==sharpe_max]['w'].values[0]
164
- res=opt.minimize(lambda x:-((port_ret(x,r_annual)-0.03)/port_std(x,log_r)),
165
- x0=((1/n),)*n,
166
- method='SLSQP',
167
- bounds=((0,1),)*n,
168
- constraints={"fun":lambda x:(np.sum(x)-1), "type":"eq"})
169
-
170
- res.x.round(3)
171
-
172
- if MSR:
173
- #ax.scatter(port_std(res.x,log_r),-port_ret(res.x,r_annual),marker="*",c="black",s=300)
174
- ax.scatter(std_msr,ret_msr,marker="*",c="black",s=300)
175
- fig
176
-
177
- #------------------------------------------------------------------------------
178
- # 绘制资本市场线CML=Capital Market Line
179
- if CML:
180
- ax.plot((0,.27),(.03,-res.fun*.27+.03))
181
- fig
182
-
183
- """
184
- 在上图的所示资本市场线上,星号左边表示将资本用于投资一部分无风险资产和一部分风险资产组合,
185
- 而在星号处代表将所有的资本都用于投资风险资产组合,
186
- 星号右边意味着借入无风险资产并投资于风险资产组合,可以在相同的风险水平下获得更高的收益。
187
-
188
- """
189
- #------------------------------------------------------------------------------
190
- #------------------------------------------------------------------------------
191
- #------------------------------------------------------------------------------
192
- #------------------------------------------------------------------------------
193
- #------------------------------------------------------------------------------
194
- #------------------------------------------------------------------------------
195
- #------------------------------------------------------------------------------
196
- #------------------------------------------------------------------------------
197
- #------------------------------------------------------------------------------
198
- #------------------------------------------------------------------------------
199
- #------------------------------------------------------------------------------
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-