siat 3.10.25__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 +91 -30
- siat/capm_beta.py +36 -10
- siat/capm_beta2.py +34 -3
- siat/common.py +95 -8
- siat/economy.py +3 -3
- siat/economy2.py +75 -1
- siat/exchange_bond_china.pickle +0 -0
- siat/fama_french.py +292 -12
- siat/financial_statements.py +26 -85
- siat/financials.py +156 -20
- siat/financials_china.py +1 -1
- siat/fund_china.py +52 -22
- siat/future_china.py +53 -59
- siat/grafix.py +218 -155
- siat/holding_risk.py +38 -31
- siat/market_china.py +22 -11
- siat/markowitz2.py +72 -10
- siat/option_china.py +93 -31
- siat/option_pricing.py +16 -6
- siat/other_indexes.py +16 -3
- siat/risk_adjusted_return.py +283 -164
- siat/risk_adjusted_return2.py +3 -1
- siat/risk_evaluation.py +298 -25
- siat/sector_china.py +24 -11
- siat/security_price2.py +19 -4
- siat/security_prices.py +228 -28
- siat/security_trend2.py +14 -14
- siat/stock.py +38 -3
- siat/translate.py +20 -10
- siat/var_model_validation.py +59 -0
- {siat-3.10.25.dist-info → siat-3.10.125.dist-info}/METADATA +1 -1
- {siat-3.10.25.dist-info → siat-3.10.125.dist-info}/RECORD +36 -36
- {siat-3.10.25.dist-info → siat-3.10.125.dist-info}/LICENSE +0 -0
- {siat-3.10.25.dist-info → siat-3.10.125.dist-info}/WHEEL +0 -0
- {siat-3.10.25.dist-info → siat-3.10.125.dist-info}/top_level.txt +0 -0
siat/risk_adjusted_return2.py
CHANGED
@@ -1721,7 +1721,7 @@ if __name__=='__main__':
|
|
1721
1721
|
printout=True)
|
1722
1722
|
|
1723
1723
|
|
1724
|
-
def compare_rar_security(ticker,start,end,
|
1724
|
+
def compare_rar_security(ticker,start,end='today',indicator='sharpe', \
|
1725
1725
|
ret_type="Annual Adj Ret%", \
|
1726
1726
|
RF=0, \
|
1727
1727
|
regression_period=365, \
|
@@ -1741,6 +1741,8 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
|
|
1741
1741
|
|
1742
1742
|
注意:trailing=7,trend_threshhold=0.05,更加贴合视觉效果
|
1743
1743
|
"""
|
1744
|
+
start,end=start_end_preprocess(start,end)
|
1745
|
+
rar=indicator
|
1744
1746
|
|
1745
1747
|
#情形1:多个证券
|
1746
1748
|
if isinstance(ticker,list):
|
siat/risk_evaluation.py
CHANGED
@@ -186,7 +186,27 @@ def stock_VaR(ticker,shares,today,future_days=1,alpha=0.99, \
|
|
186
186
|
get_VaR_allmodels(ticker=ticker,shares=shares,today=today, \
|
187
187
|
future_days=future_days,alpha=alpha,pastyears=pastyears)
|
188
188
|
|
189
|
+
return
|
189
190
|
|
191
|
+
#==============================================================================
|
192
|
+
def security_VaR(ticker,shares,today,future_days=1,alpha=0.99, \
|
193
|
+
pastyears=1,printout=True,random=10000,mctype='random', \
|
194
|
+
model="normal_standard"):
|
195
|
+
"""
|
196
|
+
功能:持有证券的VaR,支持选择四种模型
|
197
|
+
输入参数:证券代码,持有股数,当前日期,未来持有时间(天),置信度,
|
198
|
+
使用历史数据的年数,是否打印结果,模型方法
|
199
|
+
模型方法:标准正态法normal_standard,修正正态法normal_modified,
|
200
|
+
蒙特卡洛模拟法montecarlo,历史排序模拟法historical
|
201
|
+
输出:VaR(负数金额,单位与股价的金额单位相同),VaR比率
|
202
|
+
说明:套壳函数stock_VaR
|
203
|
+
"""
|
204
|
+
stock_VaR(ticker=ticker,shares=shares,today=today,future_days=future_days,alpha=alpha, \
|
205
|
+
pastyears=pastyears,printout=printout,random=random,mctype=mctype, \
|
206
|
+
model=model)
|
207
|
+
|
208
|
+
return
|
209
|
+
|
190
210
|
#==============================================================================
|
191
211
|
if __name__ == '__main__':
|
192
212
|
ticker='BABA'
|
@@ -223,7 +243,11 @@ def stock_VaR_normal_standard(ticker,shares,today, \
|
|
223
243
|
|
224
244
|
VaR_ratio=abs(VaR/position)
|
225
245
|
|
246
|
+
#from rich import print as rprint
|
247
|
+
import pandas as pd
|
248
|
+
disp_df=pd.DataFrame(columns=['Item','Value'])
|
226
249
|
if printout == True:
|
250
|
+
"""
|
227
251
|
print("\n=== 计算在险价值:标准正态模型 ===")
|
228
252
|
print("持有股票 :",ticker_name(ticker))
|
229
253
|
print("持有股数 :",format(shares,','))
|
@@ -236,7 +260,51 @@ def stock_VaR_normal_standard(ticker,shares,today, \
|
|
236
260
|
import datetime as dt; today=dt.date.today()
|
237
261
|
footnote="*数据来源:新浪/stooq,"+str(today)
|
238
262
|
print(footnote)
|
239
|
-
|
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
|
+
|
240
308
|
return VaR,VaR_ratio
|
241
309
|
|
242
310
|
if __name__ == '__main__':
|
@@ -280,7 +348,7 @@ def series_VaR_normal_standard(ticker,shares,datelist, \
|
|
280
348
|
print(text1)
|
281
349
|
print(result3)
|
282
350
|
import datetime as dt; today=dt.date.today()
|
283
|
-
print("
|
351
|
+
print("数据来源:新浪/stooq,"+str(today))
|
284
352
|
|
285
353
|
#绘图
|
286
354
|
#VaR金额绘图
|
@@ -318,7 +386,7 @@ if __name__ == '__main__':
|
|
318
386
|
|
319
387
|
|
320
388
|
#==============================================================================
|
321
|
-
def compare_VaR(
|
389
|
+
def compare_VaR(ticker,shares,datelist, \
|
322
390
|
future_days=1,alpha=0.99,pastyears=1,model='normal_standard'):
|
323
391
|
"""
|
324
392
|
功能:比较多个日期持有一定天数股票资产的VaR高低
|
@@ -328,6 +396,8 @@ def compare_VaR(tickerlist,shares,datelist, \
|
|
328
396
|
输出:无
|
329
397
|
显示:折线图,各个资产的VaR金额和比率对比
|
330
398
|
"""
|
399
|
+
tickerlist=ticker
|
400
|
+
|
331
401
|
modellist=['normal_standard','normal_modified']
|
332
402
|
if not (model in modellist):
|
333
403
|
print(" #Error(compare_VaR): unsupported model",model)
|
@@ -378,7 +448,7 @@ def compare_VaR_normal_standard(tickerlist,shares,datelist, \
|
|
378
448
|
sys.stdout.close()
|
379
449
|
sys.stdout = self._original_stdout
|
380
450
|
|
381
|
-
print("
|
451
|
+
print(" The comparison may take time, please wait ...")
|
382
452
|
markerlist=['.','o','s','*','+','x','1','2']
|
383
453
|
for t in tickerlist:
|
384
454
|
pos=tickerlist.index(t)
|
@@ -497,6 +567,23 @@ def stock_ES(ticker,shares,today,future_days=1,alpha=0.99, \
|
|
497
567
|
pastyears=pastyears,printout=printout)
|
498
568
|
return ES,ratio
|
499
569
|
|
570
|
+
#==============================================================================
|
571
|
+
def security_ES(ticker,shares,today,future_days=1,alpha=0.99, \
|
572
|
+
pastyears=1,printout=True,random=10000,mctype='random', \
|
573
|
+
model="normal_standard"):
|
574
|
+
"""
|
575
|
+
功能:持有证券的预期不足ES,支持选择模型
|
576
|
+
输入参数:证券代码,持有股数,当前日期,未来持有时间(天),置信度,
|
577
|
+
使用历史数据的年数,是否打印结果,模型方法
|
578
|
+
模型方法:默认标准正态法normal_standard
|
579
|
+
说明:套壳函数stock_ES
|
580
|
+
"""
|
581
|
+
ES,ESratio=stock_ES(ticker=ticker,shares=shares,today=today,future_days=future_days,alpha=alpha, \
|
582
|
+
pastyears=pastyears,printout=printout,random=random,mctype=mctype, \
|
583
|
+
model=model)
|
584
|
+
|
585
|
+
return
|
586
|
+
|
500
587
|
#==============================================================================
|
501
588
|
if __name__ == '__main__':
|
502
589
|
ticker='JD'
|
@@ -535,6 +622,7 @@ def stock_ES_normal_standard(ticker,shares,today, \
|
|
535
622
|
ratio=abs(ES/position)
|
536
623
|
|
537
624
|
if printout == True:
|
625
|
+
"""
|
538
626
|
print("\n=== 计算预期不足ES:标准正态模型 ===")
|
539
627
|
print("持有股票 :",ticker_name(ticker))
|
540
628
|
print("持有股数 :",format(shares,','))
|
@@ -545,7 +633,22 @@ def stock_ES_normal_standard(ticker,shares,today, \
|
|
545
633
|
print("ES比例 : ",round(ratio*100,2),'%',sep='')
|
546
634
|
|
547
635
|
import datetime as dt; today=dt.date.today()
|
548
|
-
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)
|
549
652
|
|
550
653
|
return ES,ratio
|
551
654
|
|
@@ -588,7 +691,7 @@ def series_ES_normal_standard(ticker,shares,datelist, \
|
|
588
691
|
print(text1)
|
589
692
|
print(result3)
|
590
693
|
import datetime as dt; today=dt.date.today()
|
591
|
-
print("
|
694
|
+
print("数据来源:新浪/stooq,"+str(today))
|
592
695
|
|
593
696
|
#绘图
|
594
697
|
#VaR金额绘图
|
@@ -618,7 +721,7 @@ if __name__ == '__main__':
|
|
618
721
|
result=series_ES_normal_standard('BABA',10000,datelist,1,0.99)
|
619
722
|
|
620
723
|
#==============================================================================
|
621
|
-
def compare_ES(
|
724
|
+
def compare_ES(ticker,shares,datelist, \
|
622
725
|
future_days=1,alpha=0.99,pastyears=1,model='normal_standard'):
|
623
726
|
"""
|
624
727
|
功能:比较多个日期持有一定天数股票资产的ES高低
|
@@ -628,6 +731,8 @@ def compare_ES(tickerlist,shares,datelist, \
|
|
628
731
|
输出:无
|
629
732
|
显示:折线图,各个资产的ES金额和比率对比
|
630
733
|
"""
|
734
|
+
tickerlist=ticker
|
735
|
+
|
631
736
|
modellist=['normal_standard']
|
632
737
|
if not (model in modellist):
|
633
738
|
print(" #Error(compare_ES): unsupported model",model)
|
@@ -659,7 +764,7 @@ def compare_ES_normal_standard(tickerlist,shares,datelist, \
|
|
659
764
|
sys.stdout.close()
|
660
765
|
sys.stdout = self._original_stdout
|
661
766
|
|
662
|
-
print("
|
767
|
+
print(" The comparison may take time, please wait ...")
|
663
768
|
markerlist=['.','o','s','*','+','x','1','2']
|
664
769
|
for t in tickerlist:
|
665
770
|
pos=tickerlist.index(t)
|
@@ -753,7 +858,7 @@ def plot_rets_histogram(ticker,start,end,num_bins=20):
|
|
753
858
|
plt.plot(bins,y,'r--',label='正态分布',lw=2)
|
754
859
|
plt.ylabel('Frequency')
|
755
860
|
plt.xlabel('Stock return')
|
756
|
-
titletxt="正态性检验:"+ticker_name(ticker)+"
|
861
|
+
titletxt="正态性检验:"+ticker_name(ticker)+"收益率, "+start+"至"+end
|
757
862
|
plt.title(titletxt)
|
758
863
|
plt.legend(loc='best')
|
759
864
|
|
@@ -768,13 +873,15 @@ if __name__ == '__main__':
|
|
768
873
|
plot_rets_histogram('JD','2019-1-1','2019-6-30')
|
769
874
|
|
770
875
|
#==============================================================================
|
771
|
-
def plot_rets_curve(ticker,start,end):
|
876
|
+
def plot_rets_curve(ticker,start='MRY',end='today'):
|
772
877
|
"""
|
773
878
|
功能:绘制收益率分布的曲线,并于相应的正态分布图对照
|
774
879
|
输入:股票代码,开始/结束时间
|
775
880
|
显示:收益率分布的直方图(实线),相应的正态分布图(虚线)
|
776
881
|
x轴为收益率(非百分比),y轴为频度(Frequency)
|
777
882
|
"""
|
883
|
+
start,end=start_end_preprocess(start,end)
|
884
|
+
|
778
885
|
#抓取股价并计算收益率
|
779
886
|
quotes=get_stock_quotes(ticker,start,end)
|
780
887
|
if (quotes is None) or (len(quotes)==0):
|
@@ -801,7 +908,7 @@ def plot_rets_curve(ticker,start,end):
|
|
801
908
|
plt.ylabel('',fontsize=ylabel_txt_size)
|
802
909
|
#plt.xlabel('收益率')
|
803
910
|
plt.legend(loc='best',fontsize=legend_txt_size)
|
804
|
-
titletxt="正态性检验: "+ticker_name(ticker)+"
|
911
|
+
titletxt="正态性检验: "+ticker_name(ticker)+",收益率, "+start+"至"+end
|
805
912
|
plt.title(titletxt,fontsize=title_txt_size)
|
806
913
|
|
807
914
|
import datetime as dt; today=dt.date.today()
|
@@ -817,7 +924,7 @@ if __name__ == '__main__':
|
|
817
924
|
|
818
925
|
|
819
926
|
#===========================================================================
|
820
|
-
def stock_ret_Normality_SW(ticker,
|
927
|
+
def stock_ret_Normality_SW(ticker,start='MRY',end='today',siglevel=0.05):
|
821
928
|
"""
|
822
929
|
功能:测试一个日收益率序列是否符合正态分布,原假设:符合正态分布
|
823
930
|
输入参数:股票代码,开始日期,结束日期
|
@@ -825,6 +932,8 @@ def stock_ret_Normality_SW(ticker,start_date,end_date,siglevel=0.05):
|
|
825
932
|
start_date,end_date均为datetime类型
|
826
933
|
【Shapiro-Wilk正态性检验】原假设:服从正态分布
|
827
934
|
"""
|
935
|
+
start_date,end_date=start_end_preprocess(start,end)
|
936
|
+
|
828
937
|
from scipy import stats
|
829
938
|
|
830
939
|
quotes=get_stock_quotes(ticker,start_date,end_date)
|
@@ -834,6 +943,7 @@ def stock_ret_Normality_SW(ticker,start_date,end_date,siglevel=0.05):
|
|
834
943
|
ret=get_ret_series(quotes)
|
835
944
|
(W,p_value)=stats.shapiro(ret)
|
836
945
|
|
946
|
+
"""
|
837
947
|
print("\n= Shapiro-Wilk正态性检验: 股票收益率 =")
|
838
948
|
print("股票 :",ticker_name(ticker))
|
839
949
|
print("期间 :",start_date,"至",end_date)
|
@@ -846,6 +956,26 @@ def stock_ret_Normality_SW(ticker,start_date,end_date,siglevel=0.05):
|
|
846
956
|
print("结果 : 拒绝原假设, 不符合正态分布")
|
847
957
|
import datetime as dt; today=dt.date.today()
|
848
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)
|
849
979
|
|
850
980
|
return p_value
|
851
981
|
|
@@ -894,7 +1024,7 @@ def stock_VaR_normal_modified(ticker,shares,today, \
|
|
894
1024
|
start=get_start_date(today,pastyears)
|
895
1025
|
p=get_stock_quotes(ticker,start,today)
|
896
1026
|
if (p is None) or (len(p)==0):
|
897
|
-
print("#Error(stock_VaR_normal_modified): no
|
1027
|
+
print(" #Error(stock_VaR_normal_modified): no observation retrieved.")
|
898
1028
|
return None,None
|
899
1029
|
|
900
1030
|
r=get_ret_series(p)
|
@@ -907,6 +1037,7 @@ def stock_VaR_normal_modified(ticker,shares,today, \
|
|
907
1037
|
VaR_ratio=abs(VaR/position)
|
908
1038
|
|
909
1039
|
if printout == True:
|
1040
|
+
"""
|
910
1041
|
print("\n=== 在险价值VaR: 修正正态模型 ===")
|
911
1042
|
print("持有股票 :",ticker_name(ticker))
|
912
1043
|
print("持有股数 :",format(shares,','))
|
@@ -918,7 +1049,21 @@ def stock_VaR_normal_modified(ticker,shares,today, \
|
|
918
1049
|
|
919
1050
|
import datetime as dt; today=dt.date.today()
|
920
1051
|
print("*数据来源:新浪/stooq,"+str(today))
|
921
|
-
|
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
|
+
|
922
1067
|
return VaR,VaR_ratio
|
923
1068
|
|
924
1069
|
#==============================================================================
|
@@ -957,7 +1102,7 @@ def series_VaR_normal_modified(ticker,shares,datelist, \
|
|
957
1102
|
print(text1)
|
958
1103
|
print(result3)
|
959
1104
|
import datetime as dt; today=dt.date.today()
|
960
|
-
print("
|
1105
|
+
print("数据来源:新浪/stooq,"+str(today))
|
961
1106
|
|
962
1107
|
#绘图
|
963
1108
|
#VaR金额绘图
|
@@ -1001,7 +1146,7 @@ def compare_VaR_normal_modified(tickerlist,shares,datelist, \
|
|
1001
1146
|
sys.stdout.close()
|
1002
1147
|
sys.stdout = self._original_stdout
|
1003
1148
|
|
1004
|
-
print("
|
1149
|
+
print(" The comparison may take time, please wait ...")
|
1005
1150
|
markerlist=['.','o','s','*','+','x','1','2']
|
1006
1151
|
for t in tickerlist:
|
1007
1152
|
pos=tickerlist.index(t)
|
@@ -1103,6 +1248,7 @@ def stock_VaR_historical_1d(ticker,shares,today,alpha=0.99, \
|
|
1103
1248
|
VaR_ratio=abs(VaR_1d/position)
|
1104
1249
|
|
1105
1250
|
if printout == True:
|
1251
|
+
"""
|
1106
1252
|
print("\n=== 计算在险价值VaR:历史模拟方法 ===")
|
1107
1253
|
print("持有股票 :",ticker_name(ticker))
|
1108
1254
|
print("持有股数 :",format(shares,','))
|
@@ -1115,7 +1261,22 @@ def stock_VaR_historical_1d(ticker,shares,today,alpha=0.99, \
|
|
1115
1261
|
|
1116
1262
|
import datetime as dt; today=dt.date.today()
|
1117
1263
|
print("*数据来源:新浪/stooq,"+str(today))
|
1118
|
-
|
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
|
+
|
1119
1280
|
return VaR_1d,VaR_ratio
|
1120
1281
|
|
1121
1282
|
if __name__ == '__main__':
|
@@ -1205,11 +1366,12 @@ def stock_VaR_historical_grouping(ticker,shares,today, \
|
|
1205
1366
|
VaR_ratio=abs(VaR_days/position)
|
1206
1367
|
|
1207
1368
|
if future_days == 1:
|
1208
|
-
modeltxt="
|
1369
|
+
modeltxt="历史排序模拟法"
|
1209
1370
|
else:
|
1210
|
-
modeltxt="
|
1371
|
+
modeltxt="分组历史模拟法"
|
1211
1372
|
|
1212
1373
|
if printout == True:
|
1374
|
+
"""
|
1213
1375
|
print("\n=== 在险价值VaR:"+modeltxt+" ===")
|
1214
1376
|
print("持有股票 :",ticker_name(ticker))
|
1215
1377
|
print("持有股数 :",format(shares,','))
|
@@ -1221,7 +1383,23 @@ def stock_VaR_historical_grouping(ticker,shares,today, \
|
|
1221
1383
|
|
1222
1384
|
import datetime as dt; today=dt.date.today()
|
1223
1385
|
print("*数据来源:新浪/stooq,"+str(today))
|
1224
|
-
|
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
|
+
|
1225
1403
|
return -abs(VaR_days),VaR_ratio
|
1226
1404
|
|
1227
1405
|
#==============================================================================
|
@@ -1284,6 +1462,7 @@ def stock_VaR_montecarlo(ticker,shares,today,future_days=1,alpha=0.99, \
|
|
1284
1462
|
if abs(VaR_days) > position: VaR_days=-position
|
1285
1463
|
|
1286
1464
|
if printout == True:
|
1465
|
+
"""
|
1287
1466
|
print("\n=== 在险价值VaR:蒙特卡洛模拟法 ===")
|
1288
1467
|
print("持有日期 :",today)
|
1289
1468
|
print("持有股票 :",ticker_name(ticker))
|
@@ -1298,7 +1477,23 @@ def stock_VaR_montecarlo(ticker,shares,today,future_days=1,alpha=0.99, \
|
|
1298
1477
|
|
1299
1478
|
import datetime as dt; today=dt.date.today()
|
1300
1479
|
print("*数据来源:新浪/stooq,"+str(today))
|
1301
|
-
|
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
|
+
|
1302
1497
|
return VaR_days,ratio
|
1303
1498
|
|
1304
1499
|
|
@@ -1346,6 +1541,7 @@ def calc_VaR_tlcp(ticker,today,alpha=0.99, \
|
|
1346
1541
|
break
|
1347
1542
|
if stop1==0:
|
1348
1543
|
if printout == True:
|
1544
|
+
"""
|
1349
1545
|
print("\n=== VaR的全损临界点TLCP ===")
|
1350
1546
|
print("持有股票 :",ticker_name(ticker))
|
1351
1547
|
print("持有日期 :",today)
|
@@ -1355,7 +1551,21 @@ def calc_VaR_tlcp(ticker,today,alpha=0.99, \
|
|
1355
1551
|
|
1356
1552
|
print("*注:实际发生全损的概率极小")
|
1357
1553
|
import datetime as dt; today=dt.date.today()
|
1358
|
-
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
|
+
|
1359
1569
|
return max1
|
1360
1570
|
|
1361
1571
|
#第2轮搜索,步长100天
|
@@ -1386,13 +1596,29 @@ def calc_VaR_tlcp(ticker,today,alpha=0.99, \
|
|
1386
1596
|
break
|
1387
1597
|
|
1388
1598
|
if printout == True:
|
1599
|
+
"""
|
1389
1600
|
print("\n=== VaR的全损临界点TLCP ===")
|
1390
1601
|
print("持有股票 :",ticker_name(ticker))
|
1391
1602
|
print("持有日期 :",today)
|
1392
1603
|
print("使用的模型:",model)
|
1393
1604
|
print("置信度 : ",alpha*100,'%',sep='')
|
1394
1605
|
print("TLCP天数 : >",format(stop4,','))
|
1395
|
-
|
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
|
+
|
1396
1622
|
return stop4
|
1397
1623
|
|
1398
1624
|
if __name__ == "__main__":
|
@@ -1458,12 +1684,30 @@ def series_VaR_tlcp(tickerlist,today,alpha=0.99,pastyears=1,model="montecarlo"):
|
|
1458
1684
|
import siat.grafix as g
|
1459
1685
|
g.plot_barh(tlcpdf,'TLCP',titletxt,footnote)
|
1460
1686
|
|
1687
|
+
"""
|
1461
1688
|
print("===== VaR全损临界点(TLCP, 天数) =====")
|
1462
1689
|
print("持有日期 :",today)
|
1463
1690
|
print("使用的模型:",model)
|
1464
1691
|
print("置信度 :",alpha*100,'%',sep='')
|
1465
1692
|
print(tlcpdf)
|
1466
|
-
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')
|
1467
1711
|
|
1468
1712
|
return tlcpdf
|
1469
1713
|
|
@@ -1648,6 +1892,7 @@ def get_VaR_allmodels(ticker,shares,today, \
|
|
1648
1892
|
VaR4=int(VaR_montecarlo(position,rets,future_days,alpha)+0.5)
|
1649
1893
|
ratio4=abs(round(VaR4/position,4))
|
1650
1894
|
|
1895
|
+
"""
|
1651
1896
|
print("\n==== 不同模型下VaR的计算结果 ====")
|
1652
1897
|
print("持有日期 :",today)
|
1653
1898
|
print("持有股票 :",ticker_name(ticker))
|
@@ -1669,7 +1914,35 @@ def get_VaR_allmodels(ticker,shares,today, \
|
|
1669
1914
|
print("蒙特卡洛模拟法:",round(ratio4*100,2),'%',sep='')
|
1670
1915
|
|
1671
1916
|
import datetime as dt; today=dt.date.today()
|
1672
|
-
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)
|
1673
1946
|
|
1674
1947
|
VaRlist=[VaR1,VaR2,VaR3,VaR4]
|
1675
1948
|
ratiolist=[ratio1,ratio2,ratio3,ratio4]
|