siat 3.10.75__py3-none-any.whl → 3.10.125__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.
- siat/assets_liquidity.py +168 -30
- siat/bond.py +34 -7
- siat/capm_beta.py +34 -9
- siat/common.py +88 -7
- siat/economy2.py +75 -1
- siat/exchange_bond_china.pickle +0 -0
- siat/fama_french.py +201 -12
- siat/financial_statements.py +26 -85
- siat/financials.py +114 -14
- siat/financials_china.py +1 -1
- siat/fund_china.py +6 -5
- siat/future_china.py +8 -2
- siat/holding_risk.py +38 -31
- siat/market_china.py +20 -10
- siat/markowitz2.py +72 -10
- siat/option_china.py +45 -17
- siat/option_pricing.py +3 -0
- siat/other_indexes.py +16 -3
- siat/risk_adjusted_return.py +182 -114
- siat/risk_evaluation.py +252 -20
- siat/sector_china.py +12 -9
- siat/security_price2.py +19 -4
- siat/security_prices.py +222 -23
- siat/security_trend2.py +3 -3
- siat/stock.py +32 -0
- siat/translate.py +18 -9
- siat/var_model_validation.py +59 -0
- {siat-3.10.75.dist-info → siat-3.10.125.dist-info}/METADATA +1 -1
- {siat-3.10.75.dist-info → siat-3.10.125.dist-info}/RECORD +32 -32
- {siat-3.10.75.dist-info → siat-3.10.125.dist-info}/LICENSE +0 -0
- {siat-3.10.75.dist-info → siat-3.10.125.dist-info}/WHEEL +0 -0
- {siat-3.10.75.dist-info → siat-3.10.125.dist-info}/top_level.txt +0 -0
siat/risk_evaluation.py
CHANGED
@@ -243,7 +243,11 @@ def stock_VaR_normal_standard(ticker,shares,today, \
|
|
243
243
|
|
244
244
|
VaR_ratio=abs(VaR/position)
|
245
245
|
|
246
|
+
#from rich import print as rprint
|
247
|
+
import pandas as pd
|
248
|
+
disp_df=pd.DataFrame(columns=['Item','Value'])
|
246
249
|
if printout == True:
|
250
|
+
"""
|
247
251
|
print("\n=== 计算在险价值:标准正态模型 ===")
|
248
252
|
print("持有股票 :",ticker_name(ticker))
|
249
253
|
print("持有股数 :",format(shares,','))
|
@@ -256,7 +260,51 @@ def stock_VaR_normal_standard(ticker,shares,today, \
|
|
256
260
|
import datetime as dt; today=dt.date.today()
|
257
261
|
footnote="*数据来源:新浪/stooq,"+str(today)
|
258
262
|
print(footnote)
|
259
|
-
|
263
|
+
"""
|
264
|
+
titletxt="在险价值:标准正态模型"
|
265
|
+
import datetime as dt; todaydt=dt.date.today()
|
266
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
267
|
+
"""
|
268
|
+
s=pd.Series({'Item':'持有股票','Value':ticker_name(ticker)})
|
269
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
270
|
+
|
271
|
+
s=pd.Series({'Item':'持有股数','Value':format(shares,',')})
|
272
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
273
|
+
|
274
|
+
s=pd.Series({'Item':'持有日期','Value':today})
|
275
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
276
|
+
|
277
|
+
s=pd.Series({'Item':'预计持有天数','Value':future_days})
|
278
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
279
|
+
|
280
|
+
s=pd.Series({'Item':'置信度','Value':str(alpha*100)+'%'})
|
281
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
282
|
+
|
283
|
+
s=pd.Series({'Item':'在险价值VaR','Value':format(round(VaR,2),',')})
|
284
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
285
|
+
|
286
|
+
s=pd.Series({'Item':'VaR比率','Value':str(round(VaR_ratio*100,2))+'%'})
|
287
|
+
disp_df=disp_df._append(s,ignore_index=True)
|
288
|
+
|
289
|
+
df_display_CSS(disp_df,titletxt=titletxt,footnote=footnote,facecolor='papayawhip',decimals=2, \
|
290
|
+
hide_columns=True,
|
291
|
+
first_col_align='left',second_col_align='right', \
|
292
|
+
last_col_align='right',other_col_align='right', \
|
293
|
+
titile_font_size='14px',footnote_font_size='12px')
|
294
|
+
"""
|
295
|
+
|
296
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
297
|
+
'持有股数:':format(shares,','), \
|
298
|
+
'持有日期:':today, \
|
299
|
+
'预计持有天数:':future_days, \
|
300
|
+
'置信度:':str(alpha*100)+'%', \
|
301
|
+
'在险价值VaR金额:':format(round(VaR,2),','), \
|
302
|
+
'在险价值VaR比率:':str(round(VaR_ratio*100,2))+'%'}
|
303
|
+
|
304
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote,
|
305
|
+
#facecolor='whitesmoke',
|
306
|
+
)
|
307
|
+
|
260
308
|
return VaR,VaR_ratio
|
261
309
|
|
262
310
|
if __name__ == '__main__':
|
@@ -300,7 +348,7 @@ def series_VaR_normal_standard(ticker,shares,datelist, \
|
|
300
348
|
print(text1)
|
301
349
|
print(result3)
|
302
350
|
import datetime as dt; today=dt.date.today()
|
303
|
-
print("
|
351
|
+
print("数据来源:新浪/stooq,"+str(today))
|
304
352
|
|
305
353
|
#绘图
|
306
354
|
#VaR金额绘图
|
@@ -574,6 +622,7 @@ def stock_ES_normal_standard(ticker,shares,today, \
|
|
574
622
|
ratio=abs(ES/position)
|
575
623
|
|
576
624
|
if printout == True:
|
625
|
+
"""
|
577
626
|
print("\n=== 计算预期不足ES:标准正态模型 ===")
|
578
627
|
print("持有股票 :",ticker_name(ticker))
|
579
628
|
print("持有股数 :",format(shares,','))
|
@@ -584,7 +633,22 @@ def stock_ES_normal_standard(ticker,shares,today, \
|
|
584
633
|
print("ES比例 : ",round(ratio*100,2),'%',sep='')
|
585
634
|
|
586
635
|
import datetime as dt; today=dt.date.today()
|
587
|
-
print("
|
636
|
+
print("数据来源:新浪/stooq,"+str(today))
|
637
|
+
"""
|
638
|
+
|
639
|
+
titletxt="预期不足:标准正态模型"
|
640
|
+
import datetime as dt; todaydt=dt.date.today()
|
641
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
642
|
+
|
643
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
644
|
+
'持有股数:':format(shares,','), \
|
645
|
+
'持有日期:':today, \
|
646
|
+
'预计持有天数:':future_days, \
|
647
|
+
'置信度:':str(alpha*100)+'%', \
|
648
|
+
'预期不足ES金额:':format(round(ES,2),','), \
|
649
|
+
'预期不足ES比例:':str(round(ratio*100,2))+'%'}
|
650
|
+
|
651
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
588
652
|
|
589
653
|
return ES,ratio
|
590
654
|
|
@@ -627,7 +691,7 @@ def series_ES_normal_standard(ticker,shares,datelist, \
|
|
627
691
|
print(text1)
|
628
692
|
print(result3)
|
629
693
|
import datetime as dt; today=dt.date.today()
|
630
|
-
print("
|
694
|
+
print("数据来源:新浪/stooq,"+str(today))
|
631
695
|
|
632
696
|
#绘图
|
633
697
|
#VaR金额绘图
|
@@ -794,7 +858,7 @@ def plot_rets_histogram(ticker,start,end,num_bins=20):
|
|
794
858
|
plt.plot(bins,y,'r--',label='正态分布',lw=2)
|
795
859
|
plt.ylabel('Frequency')
|
796
860
|
plt.xlabel('Stock return')
|
797
|
-
titletxt="正态性检验:"+ticker_name(ticker)+"
|
861
|
+
titletxt="正态性检验:"+ticker_name(ticker)+"收益率, "+start+"至"+end
|
798
862
|
plt.title(titletxt)
|
799
863
|
plt.legend(loc='best')
|
800
864
|
|
@@ -809,13 +873,15 @@ if __name__ == '__main__':
|
|
809
873
|
plot_rets_histogram('JD','2019-1-1','2019-6-30')
|
810
874
|
|
811
875
|
#==============================================================================
|
812
|
-
def plot_rets_curve(ticker,start,end):
|
876
|
+
def plot_rets_curve(ticker,start='MRY',end='today'):
|
813
877
|
"""
|
814
878
|
功能:绘制收益率分布的曲线,并于相应的正态分布图对照
|
815
879
|
输入:股票代码,开始/结束时间
|
816
880
|
显示:收益率分布的直方图(实线),相应的正态分布图(虚线)
|
817
881
|
x轴为收益率(非百分比),y轴为频度(Frequency)
|
818
882
|
"""
|
883
|
+
start,end=start_end_preprocess(start,end)
|
884
|
+
|
819
885
|
#抓取股价并计算收益率
|
820
886
|
quotes=get_stock_quotes(ticker,start,end)
|
821
887
|
if (quotes is None) or (len(quotes)==0):
|
@@ -842,7 +908,7 @@ def plot_rets_curve(ticker,start,end):
|
|
842
908
|
plt.ylabel('',fontsize=ylabel_txt_size)
|
843
909
|
#plt.xlabel('收益率')
|
844
910
|
plt.legend(loc='best',fontsize=legend_txt_size)
|
845
|
-
titletxt="正态性检验: "+ticker_name(ticker)+"
|
911
|
+
titletxt="正态性检验: "+ticker_name(ticker)+",收益率, "+start+"至"+end
|
846
912
|
plt.title(titletxt,fontsize=title_txt_size)
|
847
913
|
|
848
914
|
import datetime as dt; today=dt.date.today()
|
@@ -858,7 +924,7 @@ if __name__ == '__main__':
|
|
858
924
|
|
859
925
|
|
860
926
|
#===========================================================================
|
861
|
-
def stock_ret_Normality_SW(ticker,
|
927
|
+
def stock_ret_Normality_SW(ticker,start='MRY',end='today',siglevel=0.05):
|
862
928
|
"""
|
863
929
|
功能:测试一个日收益率序列是否符合正态分布,原假设:符合正态分布
|
864
930
|
输入参数:股票代码,开始日期,结束日期
|
@@ -866,6 +932,8 @@ def stock_ret_Normality_SW(ticker,start_date,end_date,siglevel=0.05):
|
|
866
932
|
start_date,end_date均为datetime类型
|
867
933
|
【Shapiro-Wilk正态性检验】原假设:服从正态分布
|
868
934
|
"""
|
935
|
+
start_date,end_date=start_end_preprocess(start,end)
|
936
|
+
|
869
937
|
from scipy import stats
|
870
938
|
|
871
939
|
quotes=get_stock_quotes(ticker,start_date,end_date)
|
@@ -875,6 +943,7 @@ def stock_ret_Normality_SW(ticker,start_date,end_date,siglevel=0.05):
|
|
875
943
|
ret=get_ret_series(quotes)
|
876
944
|
(W,p_value)=stats.shapiro(ret)
|
877
945
|
|
946
|
+
"""
|
878
947
|
print("\n= Shapiro-Wilk正态性检验: 股票收益率 =")
|
879
948
|
print("股票 :",ticker_name(ticker))
|
880
949
|
print("期间 :",start_date,"至",end_date)
|
@@ -887,6 +956,26 @@ def stock_ret_Normality_SW(ticker,start_date,end_date,siglevel=0.05):
|
|
887
956
|
print("结果 : 拒绝原假设, 不符合正态分布")
|
888
957
|
import datetime as dt; today=dt.date.today()
|
889
958
|
print("*数据来源:新浪/stooq,"+str(today))
|
959
|
+
"""
|
960
|
+
|
961
|
+
titletxt="Shapiro-Wilk检验: 收益率"
|
962
|
+
data_dict1={'股票:':ticker_name(ticker), \
|
963
|
+
'期间:':start_date+"至"+end_date, \
|
964
|
+
'原假设:':'符合正态分布', \
|
965
|
+
'W值:':round(W,4), \
|
966
|
+
'p值:':round(p_value,4)}
|
967
|
+
|
968
|
+
if p_value >= siglevel:
|
969
|
+
result="接受原假设, 符合正态分布"
|
970
|
+
else:
|
971
|
+
result="拒绝原假设, 不符合正态分布"
|
972
|
+
data_dict2={'结果:':result}
|
973
|
+
data_dict={**data_dict1,**data_dict2}
|
974
|
+
|
975
|
+
import datetime as dt; todaydt=dt.date.today()
|
976
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
977
|
+
|
978
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
890
979
|
|
891
980
|
return p_value
|
892
981
|
|
@@ -935,7 +1024,7 @@ def stock_VaR_normal_modified(ticker,shares,today, \
|
|
935
1024
|
start=get_start_date(today,pastyears)
|
936
1025
|
p=get_stock_quotes(ticker,start,today)
|
937
1026
|
if (p is None) or (len(p)==0):
|
938
|
-
print("#Error(stock_VaR_normal_modified): no
|
1027
|
+
print(" #Error(stock_VaR_normal_modified): no observation retrieved.")
|
939
1028
|
return None,None
|
940
1029
|
|
941
1030
|
r=get_ret_series(p)
|
@@ -948,6 +1037,7 @@ def stock_VaR_normal_modified(ticker,shares,today, \
|
|
948
1037
|
VaR_ratio=abs(VaR/position)
|
949
1038
|
|
950
1039
|
if printout == True:
|
1040
|
+
"""
|
951
1041
|
print("\n=== 在险价值VaR: 修正正态模型 ===")
|
952
1042
|
print("持有股票 :",ticker_name(ticker))
|
953
1043
|
print("持有股数 :",format(shares,','))
|
@@ -959,7 +1049,21 @@ def stock_VaR_normal_modified(ticker,shares,today, \
|
|
959
1049
|
|
960
1050
|
import datetime as dt; today=dt.date.today()
|
961
1051
|
print("*数据来源:新浪/stooq,"+str(today))
|
962
|
-
|
1052
|
+
"""
|
1053
|
+
titletxt="在险价值:修正正态模型"
|
1054
|
+
import datetime as dt; todaydt=dt.date.today()
|
1055
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
1056
|
+
|
1057
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
1058
|
+
'持有股数:':format(shares,','), \
|
1059
|
+
'持有日期:':today, \
|
1060
|
+
'预计持有天数:':future_days, \
|
1061
|
+
'置信度:':str(alpha*100)+'%', \
|
1062
|
+
'在险价值VaR金额:':format(round(VaR,2),','), \
|
1063
|
+
'在险价值VaR比例:':str(round(VaR_ratio*100,2))+'%'}
|
1064
|
+
|
1065
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1066
|
+
|
963
1067
|
return VaR,VaR_ratio
|
964
1068
|
|
965
1069
|
#==============================================================================
|
@@ -998,7 +1102,7 @@ def series_VaR_normal_modified(ticker,shares,datelist, \
|
|
998
1102
|
print(text1)
|
999
1103
|
print(result3)
|
1000
1104
|
import datetime as dt; today=dt.date.today()
|
1001
|
-
print("
|
1105
|
+
print("数据来源:新浪/stooq,"+str(today))
|
1002
1106
|
|
1003
1107
|
#绘图
|
1004
1108
|
#VaR金额绘图
|
@@ -1144,6 +1248,7 @@ def stock_VaR_historical_1d(ticker,shares,today,alpha=0.99, \
|
|
1144
1248
|
VaR_ratio=abs(VaR_1d/position)
|
1145
1249
|
|
1146
1250
|
if printout == True:
|
1251
|
+
"""
|
1147
1252
|
print("\n=== 计算在险价值VaR:历史模拟方法 ===")
|
1148
1253
|
print("持有股票 :",ticker_name(ticker))
|
1149
1254
|
print("持有股数 :",format(shares,','))
|
@@ -1156,7 +1261,22 @@ def stock_VaR_historical_1d(ticker,shares,today,alpha=0.99, \
|
|
1156
1261
|
|
1157
1262
|
import datetime as dt; today=dt.date.today()
|
1158
1263
|
print("*数据来源:新浪/stooq,"+str(today))
|
1159
|
-
|
1264
|
+
"""
|
1265
|
+
titletxt="在险价值:历史模拟方法"
|
1266
|
+
import datetime as dt; todaydt=dt.date.today()
|
1267
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
1268
|
+
|
1269
|
+
future_days=1
|
1270
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
1271
|
+
'持有股数:':format(shares,','), \
|
1272
|
+
'持有日期:':today, \
|
1273
|
+
'预计持有天数:':future_days, \
|
1274
|
+
'置信度:':str(alpha*100)+'%', \
|
1275
|
+
'在险价值VaR金额:':format(round(VaR_1d,2),','), \
|
1276
|
+
'在险价值VaR比例:':str(round(VaR_ratio*100,2))+'%'}
|
1277
|
+
|
1278
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1279
|
+
|
1160
1280
|
return VaR_1d,VaR_ratio
|
1161
1281
|
|
1162
1282
|
if __name__ == '__main__':
|
@@ -1246,11 +1366,12 @@ def stock_VaR_historical_grouping(ticker,shares,today, \
|
|
1246
1366
|
VaR_ratio=abs(VaR_days/position)
|
1247
1367
|
|
1248
1368
|
if future_days == 1:
|
1249
|
-
modeltxt="
|
1369
|
+
modeltxt="历史排序模拟法"
|
1250
1370
|
else:
|
1251
|
-
modeltxt="
|
1371
|
+
modeltxt="分组历史模拟法"
|
1252
1372
|
|
1253
1373
|
if printout == True:
|
1374
|
+
"""
|
1254
1375
|
print("\n=== 在险价值VaR:"+modeltxt+" ===")
|
1255
1376
|
print("持有股票 :",ticker_name(ticker))
|
1256
1377
|
print("持有股数 :",format(shares,','))
|
@@ -1262,7 +1383,23 @@ def stock_VaR_historical_grouping(ticker,shares,today, \
|
|
1262
1383
|
|
1263
1384
|
import datetime as dt; today=dt.date.today()
|
1264
1385
|
print("*数据来源:新浪/stooq,"+str(today))
|
1265
|
-
|
1386
|
+
"""
|
1387
|
+
|
1388
|
+
titletxt="在险价值:"+modeltxt
|
1389
|
+
import datetime as dt; todaydt=dt.date.today()
|
1390
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
1391
|
+
|
1392
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
1393
|
+
'持有股数:':format(shares,','), \
|
1394
|
+
'持有日期:':today, \
|
1395
|
+
'预计持有天数:':future_days, \
|
1396
|
+
'置信度:':str(alpha*100)+'%', \
|
1397
|
+
'在险价值VaR金额:':format(round(VaR_days,2),','), \
|
1398
|
+
'在险价值VaR比例:':str(round(VaR_ratio*100,2))+'%'}
|
1399
|
+
|
1400
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1401
|
+
|
1402
|
+
|
1266
1403
|
return -abs(VaR_days),VaR_ratio
|
1267
1404
|
|
1268
1405
|
#==============================================================================
|
@@ -1325,6 +1462,7 @@ def stock_VaR_montecarlo(ticker,shares,today,future_days=1,alpha=0.99, \
|
|
1325
1462
|
if abs(VaR_days) > position: VaR_days=-position
|
1326
1463
|
|
1327
1464
|
if printout == True:
|
1465
|
+
"""
|
1328
1466
|
print("\n=== 在险价值VaR:蒙特卡洛模拟法 ===")
|
1329
1467
|
print("持有日期 :",today)
|
1330
1468
|
print("持有股票 :",ticker_name(ticker))
|
@@ -1339,7 +1477,23 @@ def stock_VaR_montecarlo(ticker,shares,today,future_days=1,alpha=0.99, \
|
|
1339
1477
|
|
1340
1478
|
import datetime as dt; today=dt.date.today()
|
1341
1479
|
print("*数据来源:新浪/stooq,"+str(today))
|
1342
|
-
|
1480
|
+
"""
|
1481
|
+
titletxt="在险价值:蒙特卡洛模拟法"
|
1482
|
+
import datetime as dt; todaydt=dt.date.today()
|
1483
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
1484
|
+
|
1485
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
1486
|
+
'持有股数:':format(shares,','), \
|
1487
|
+
'持有头寸:':format(round(position,2),','), \
|
1488
|
+
'持有日期:':today, \
|
1489
|
+
'预计持有天数:':future_days, \
|
1490
|
+
'序列生成方法:':mctype, \
|
1491
|
+
'置信度:':str(alpha*100)+'%', \
|
1492
|
+
'在险价值VaR金额:':format(round(VaR_days,2),','), \
|
1493
|
+
'在险价值VaR比例:':str(round(ratio*100,2))+'%'}
|
1494
|
+
|
1495
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1496
|
+
|
1343
1497
|
return VaR_days,ratio
|
1344
1498
|
|
1345
1499
|
|
@@ -1387,6 +1541,7 @@ def calc_VaR_tlcp(ticker,today,alpha=0.99, \
|
|
1387
1541
|
break
|
1388
1542
|
if stop1==0:
|
1389
1543
|
if printout == True:
|
1544
|
+
"""
|
1390
1545
|
print("\n=== VaR的全损临界点TLCP ===")
|
1391
1546
|
print("持有股票 :",ticker_name(ticker))
|
1392
1547
|
print("持有日期 :",today)
|
@@ -1396,7 +1551,21 @@ def calc_VaR_tlcp(ticker,today,alpha=0.99, \
|
|
1396
1551
|
|
1397
1552
|
print("*注:实际发生全损的概率极小")
|
1398
1553
|
import datetime as dt; today=dt.date.today()
|
1399
|
-
print("
|
1554
|
+
print("数据来源:新浪/stooq,"+str(today))
|
1555
|
+
"""
|
1556
|
+
titletxt="VaR全损临界点"
|
1557
|
+
import datetime as dt; todaydt=dt.date.today()
|
1558
|
+
ft0="*注:实际发生全损的概率极小"
|
1559
|
+
footnote=ft0+'\n'+"数据来源:新浪/stooq,"+str(todaydt)
|
1560
|
+
|
1561
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
1562
|
+
'持有日期:':today, \
|
1563
|
+
'使用的模型:':model, \
|
1564
|
+
'置信度:':str(alpha*100)+'%', \
|
1565
|
+
'全损临界点TLCP天数 >':format(max1,',')}
|
1566
|
+
|
1567
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1568
|
+
|
1400
1569
|
return max1
|
1401
1570
|
|
1402
1571
|
#第2轮搜索,步长100天
|
@@ -1427,13 +1596,29 @@ def calc_VaR_tlcp(ticker,today,alpha=0.99, \
|
|
1427
1596
|
break
|
1428
1597
|
|
1429
1598
|
if printout == True:
|
1599
|
+
"""
|
1430
1600
|
print("\n=== VaR的全损临界点TLCP ===")
|
1431
1601
|
print("持有股票 :",ticker_name(ticker))
|
1432
1602
|
print("持有日期 :",today)
|
1433
1603
|
print("使用的模型:",model)
|
1434
1604
|
print("置信度 : ",alpha*100,'%',sep='')
|
1435
1605
|
print("TLCP天数 : >",format(stop4,','))
|
1436
|
-
|
1606
|
+
"""
|
1607
|
+
|
1608
|
+
titletxt="VaR全损临界点"
|
1609
|
+
import datetime as dt; todaydt=dt.date.today()
|
1610
|
+
ft0="*注:实际发生全损的概率极小"
|
1611
|
+
footnote=ft0+'\n'+"数据来源:新浪/stooq,"+str(todaydt)
|
1612
|
+
|
1613
|
+
data_dict={'持有股票:':ticker_name(ticker), \
|
1614
|
+
'持有日期:':today, \
|
1615
|
+
'使用的模型:':model, \
|
1616
|
+
'置信度:':str(alpha*100)+'%', \
|
1617
|
+
'全损临界点TLCP天数 >':format(stop4,',')}
|
1618
|
+
|
1619
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1620
|
+
|
1621
|
+
|
1437
1622
|
return stop4
|
1438
1623
|
|
1439
1624
|
if __name__ == "__main__":
|
@@ -1499,12 +1684,30 @@ def series_VaR_tlcp(tickerlist,today,alpha=0.99,pastyears=1,model="montecarlo"):
|
|
1499
1684
|
import siat.grafix as g
|
1500
1685
|
g.plot_barh(tlcpdf,'TLCP',titletxt,footnote)
|
1501
1686
|
|
1687
|
+
"""
|
1502
1688
|
print("===== VaR全损临界点(TLCP, 天数) =====")
|
1503
1689
|
print("持有日期 :",today)
|
1504
1690
|
print("使用的模型:",model)
|
1505
1691
|
print("置信度 :",alpha*100,'%',sep='')
|
1506
1692
|
print(tlcpdf)
|
1507
|
-
print("
|
1693
|
+
print("数据来源:新浪/stooq,"+str(today))
|
1694
|
+
"""
|
1695
|
+
|
1696
|
+
titletxt="VaR全损临界点(TLCP, 天数)"
|
1697
|
+
import datetime as dt; todaydt=dt.date.today()
|
1698
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
1699
|
+
|
1700
|
+
data_dict={'持有日期:':today, \
|
1701
|
+
'使用的计算模型:':model, \
|
1702
|
+
'置信度:':str(alpha*100)+'%'}
|
1703
|
+
|
1704
|
+
print2CSS(data_dict,titletxt='',footnote='')
|
1705
|
+
df_display_CSS(tlcpdf,titletxt=titletxt,footnote=footnote,facecolor='papayawhip',decimals=2, \
|
1706
|
+
hide_columns=False,
|
1707
|
+
first_col_align='left',second_col_align='right', \
|
1708
|
+
last_col_align='right',other_col_align='right', \
|
1709
|
+
titile_font_size='14px',heading_font_size='14px', \
|
1710
|
+
data_font_size='14px',footnote_font_size='12px')
|
1508
1711
|
|
1509
1712
|
return tlcpdf
|
1510
1713
|
|
@@ -1689,6 +1892,7 @@ def get_VaR_allmodels(ticker,shares,today, \
|
|
1689
1892
|
VaR4=int(VaR_montecarlo(position,rets,future_days,alpha)+0.5)
|
1690
1893
|
ratio4=abs(round(VaR4/position,4))
|
1691
1894
|
|
1895
|
+
"""
|
1692
1896
|
print("\n==== 不同模型下VaR的计算结果 ====")
|
1693
1897
|
print("持有日期 :",today)
|
1694
1898
|
print("持有股票 :",ticker_name(ticker))
|
@@ -1710,7 +1914,35 @@ def get_VaR_allmodels(ticker,shares,today, \
|
|
1710
1914
|
print("蒙特卡洛模拟法:",round(ratio4*100,2),'%',sep='')
|
1711
1915
|
|
1712
1916
|
import datetime as dt; today=dt.date.today()
|
1713
|
-
print("\n
|
1917
|
+
print("\n数据来源:新浪/stooq,"+str(today))
|
1918
|
+
"""
|
1919
|
+
|
1920
|
+
import datetime as dt; todaydt=dt.date.today()
|
1921
|
+
footnote="数据来源:新浪/stooq,"+str(todaydt)
|
1922
|
+
|
1923
|
+
titletxt="持有资产的状况"
|
1924
|
+
data_dict={'持有日期:':today, \
|
1925
|
+
"持有股票:":ticker_name(ticker), \
|
1926
|
+
"持有股数:":format(shares,','), \
|
1927
|
+
"持有股数:":format(shares,','), \
|
1928
|
+
"持有头寸:":format(round(position,2),','), \
|
1929
|
+
"预计持有天数:":future_days, \
|
1930
|
+
'置信度:':str(alpha*100)+'%'}
|
1931
|
+
print2CSS(data_dict,titletxt=titletxt,footnote='')
|
1932
|
+
|
1933
|
+
titletxt="各模型的VaR金额"
|
1934
|
+
data_dict={"标准正态模型:":format(VaR1,','), \
|
1935
|
+
"修正正态模型:":format(VaR2,','), \
|
1936
|
+
"历史模拟模型:":format(VaR3,','), \
|
1937
|
+
"蒙特卡洛模拟:":format(VaR4,',')}
|
1938
|
+
print2CSS(data_dict,titletxt=titletxt,footnote='')
|
1939
|
+
|
1940
|
+
titletxt="各模型的VaR比例"
|
1941
|
+
data_dict={"标准正态模型:":str(round(ratio1*100,2))+'%', \
|
1942
|
+
"修正正态模型:":str(round(ratio2*100,2))+'%', \
|
1943
|
+
"历史模拟模型:":str(round(ratio3*100,2))+'%', \
|
1944
|
+
"蒙特卡洛模拟:":str(round(ratio4*100,2))+'%'}
|
1945
|
+
print2CSS(data_dict,titletxt=titletxt,footnote=footnote)
|
1714
1946
|
|
1715
1947
|
VaRlist=[VaR1,VaR2,VaR3,VaR4]
|
1716
1948
|
ratiolist=[ratio1,ratio2,ratio3,ratio4]
|
siat/sector_china.py
CHANGED
@@ -264,11 +264,11 @@ def sector_rank_sina(indicator="涨跌幅",category="新浪行业",rank=5):
|
|
264
264
|
def sector_rank_china(ticker="新浪行业",indicator="涨跌幅",rank=10):
|
265
265
|
"""
|
266
266
|
功能:按照比较指标降序排列
|
267
|
-
|
268
|
-
indicator="
|
269
|
-
|
267
|
+
ticker="新浪行业","启明星行业","概念","地域","行业"
|
268
|
+
indicator="涨跌幅",平均价格,公司家数
|
269
|
+
rank:为正数时列出最高的前几名,为负数时列出最后几名
|
270
270
|
|
271
|
-
注意:公司家数字段最大值为100
|
271
|
+
注意:公司家数字段最大值为100,超过100仅显示为100
|
272
272
|
"""
|
273
273
|
comp=indicator
|
274
274
|
indicator=ticker
|
@@ -430,8 +430,9 @@ def sector_detail_sina(sector="new_dlhy",indicator="涨跌幅",rank=5):
|
|
430
430
|
def sector_detail_china(ticker="new_dlhy",indicator="涨跌幅",rank=10):
|
431
431
|
"""
|
432
432
|
功能:按照板块内部股票的比较指标降序排列
|
433
|
-
|
434
|
-
|
433
|
+
ticker:板块代码
|
434
|
+
indicator:默认"涨跌幅",还可选"换手率"、"收盘价"、"市盈率"、"市净率"、"总市值"、"流通市值"
|
435
|
+
rank:为正数时列出最高的前几名,为负数时列出最后几名
|
435
436
|
"""
|
436
437
|
sector=ticker
|
437
438
|
comp=indicator
|
@@ -751,8 +752,10 @@ def sector_position_china(ticker,sector="new_dlhy"):
|
|
751
752
|
def invest_concept_china(num=10,max_sleep=30):
|
752
753
|
"""
|
753
754
|
废弃!
|
754
|
-
|
755
|
+
功能:汇总新浪投资概念股票名单,排行
|
755
756
|
来源网址:http://finance.sina.com.cn/stock/sl/#qmxindustry_1
|
757
|
+
|
758
|
+
注意:网站有反爬虫,循环做不下去!
|
756
759
|
"""
|
757
760
|
print("\nWarning: This function might cause your IP address banned by data source!")
|
758
761
|
print("Searching stocks with investment concepts in China, it may take long time ...")
|
@@ -796,8 +799,8 @@ def invest_concept_china(num=10,max_sleep=30):
|
|
796
799
|
print("...Searching completed.")
|
797
800
|
|
798
801
|
if len(totaldf) == 0:
|
799
|
-
print("#Error(
|
800
|
-
print("Solutions:1) try
|
802
|
+
print(" #Error(sector_concept_china): data source tentatively banned your access:-(")
|
803
|
+
print(" Solutions:1) try a bit later, or 2) switch to another IP address.")
|
801
804
|
return None
|
802
805
|
|
803
806
|
#分组统计
|
siat/security_price2.py
CHANGED
@@ -70,6 +70,10 @@ if __name__=='__main__':
|
|
70
70
|
ticker="XAUUSD"
|
71
71
|
ticker="^NSEI"
|
72
72
|
fromdate='2024-5-1'; todate='2024-5-20'
|
73
|
+
|
74
|
+
ticker="BMW.DE"
|
75
|
+
fromdate='2022-6-1'; todate='2025-6-15'
|
76
|
+
|
73
77
|
ticker_type='auto';source='auto'
|
74
78
|
adjust='';fill=False
|
75
79
|
|
@@ -91,6 +95,10 @@ def get_price_1ticker(ticker,fromdate,todate, \
|
|
91
95
|
简化使用者判断
|
92
96
|
fill:为现有数据开始结束日期之间的工作日填充=True
|
93
97
|
"""
|
98
|
+
#设置雅虎yfinance开关
|
99
|
+
YF=False
|
100
|
+
#设置雅虎pandas_datareader开关,避免在此花费时间
|
101
|
+
PDR_yahoo=False
|
94
102
|
|
95
103
|
#返回值初始状态,确保各种情况下都有返回值
|
96
104
|
df=None
|
@@ -177,13 +185,15 @@ def get_price_1ticker(ticker,fromdate,todate, \
|
|
177
185
|
|
178
186
|
#数据源情形3b:yahoo, yfinance, 需要访问yahoo,直接为复权价
|
179
187
|
if found not in ['Found','Empty']:
|
180
|
-
|
181
|
-
|
188
|
+
if YF:
|
189
|
+
dft=get_price_yf(ticker1,fromdate,todate)
|
190
|
+
found=df_have_data(dft)
|
182
191
|
|
183
192
|
#数据源情形4:yahoo, pandas_datareader,需要访问yahoo,似乎不工作了!
|
184
193
|
if found not in ['Found','Empty']:
|
185
|
-
|
186
|
-
|
194
|
+
if PDR_yahoo:
|
195
|
+
dft=get_prices_yahoo(ticker1,fromdate,todate)
|
196
|
+
found=df_have_data(dft)
|
187
197
|
else:
|
188
198
|
if source in ['yahoo']:
|
189
199
|
print(" #Warning(get_price_1ticker): sorry, yahoo is currently inaccessible")
|
@@ -660,6 +670,11 @@ if __name__=='__main__':
|
|
660
670
|
|
661
671
|
fromdate='2021-1-1'
|
662
672
|
todate='2024-5-30'
|
673
|
+
|
674
|
+
ticker='BMW.DE'
|
675
|
+
fromdate='2024-6-2'
|
676
|
+
todate='2025-6-15'
|
677
|
+
|
663
678
|
adjust=''
|
664
679
|
source='auto'
|
665
680
|
fill=True
|