siat 3.0.4__py3-none-any.whl → 3.0.15__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/allin.py +2 -1
- siat/capm_beta2.py +51 -22
- siat/common.py +30 -3
- siat/fama_french.py +2 -0
- siat/financials_china.py +28 -21
- siat/financials_china2.py +199 -106
- siat/grafix.py +109 -45
- siat/markowitz.py +25 -14
- siat/markowitz2.py +2446 -0
- siat/risk_adjusted_return2.py +169 -85
- siat/security_trend2.py +39 -25
- siat/stock.py +127 -47
- siat/stock_china.py +1 -1
- siat/stock_technical.py +33 -12
- siat/valuation.py +34 -7
- {siat-3.0.4.dist-info → siat-3.0.15.dist-info}/METADATA +1 -1
- {siat-3.0.4.dist-info → siat-3.0.15.dist-info}/RECORD +19 -18
- {siat-3.0.4.dist-info → siat-3.0.15.dist-info}/WHEEL +1 -1
- {siat-3.0.4.dist-info → siat-3.0.15.dist-info}/top_level.txt +0 -0
siat/grafix.py
CHANGED
@@ -101,7 +101,8 @@ def plot_line(df0,colname,collabel,ylabeltxt,titletxt,footnote,datatag=False, \
|
|
101
101
|
power=0,zeroline=False,average_value=False, \
|
102
102
|
resample_freq='H',loc='best', \
|
103
103
|
date_range=False,date_freq=False,date_fmt='%Y-%m-%d', \
|
104
|
-
mark_top=True,mark_bottom=True,mark_end=True
|
104
|
+
mark_top=True,mark_bottom=True,mark_end=True, \
|
105
|
+
facecolor='whitesmoke'):
|
105
106
|
"""
|
106
107
|
功能:绘制折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
107
108
|
假定:数据表有索引,且已经按照索引排序
|
@@ -267,7 +268,11 @@ def plot_line(df0,colname,collabel,ylabeltxt,titletxt,footnote,datatag=False, \
|
|
267
268
|
plt.legend(loc=loc,fontsize=legend_txt_size)
|
268
269
|
|
269
270
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
270
|
-
|
271
|
+
try:
|
272
|
+
plt.gca().set_facecolor(facecolor) #设置画布背景颜色
|
273
|
+
except:
|
274
|
+
print(" #Warning(plot_line): color",facecolor,"is unsupported, changed to default setting")
|
275
|
+
plt.gca().set_facecolor("whitesmoke")
|
271
276
|
|
272
277
|
if '基金' in titletxt and '收盘价' in ylabeltxt:
|
273
278
|
ylabeltxt=ylabeltxt.replace('收盘价','单位净值')
|
@@ -300,7 +305,7 @@ def plot_line2(df1,ticker1,colname1,label1, \
|
|
300
305
|
power=0,datatag1=False,datatag2=False,yscalemax=5, \
|
301
306
|
zeroline=False,twinx=False,yline=999,xline=999, \
|
302
307
|
resample_freq='H',loc1='best',loc2='best', \
|
303
|
-
color1='red',color2='blue'):
|
308
|
+
color1='red',color2='blue',facecolor='whitesmoke'):
|
304
309
|
"""
|
305
310
|
功能:绘制两个证券的折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
306
311
|
假定:数据表有索引,且已经按照索引排序
|
@@ -322,18 +327,18 @@ def plot_line2(df1,ticker1,colname1,label1, \
|
|
322
327
|
|
323
328
|
if not twinx:
|
324
329
|
plot_line2_coaxial(df1,ticker1,colname1,label1, \
|
325
|
-
|
330
|
+
df2,ticker2,colname2,label2, \
|
326
331
|
ylabeltxt,titletxt,footnote,power,datatag1,datatag2,zeroline, \
|
327
332
|
yline,xline,resample_freq=resample_freq, \
|
328
|
-
|
329
|
-
|
333
|
+
loc1=loc1,loc2=loc2, \
|
334
|
+
color1=color1,color2=color2,facecolor=facecolor)
|
330
335
|
else:
|
331
336
|
plot_line2_twinx(df1,ticker1,colname1,label1, \
|
332
337
|
df2,ticker2,colname2,label2, \
|
333
338
|
titletxt,footnote,power,datatag1,datatag2, \
|
334
339
|
resample_freq=resample_freq, \
|
335
|
-
|
336
|
-
|
340
|
+
loc1=loc1,loc2=loc2, \
|
341
|
+
color1=color1,color2=color2,facecolor=facecolor)
|
337
342
|
return
|
338
343
|
|
339
344
|
|
@@ -345,7 +350,7 @@ def plot2_line2(df1,ticker1,colname1,label1, \
|
|
345
350
|
zeroline=False,twinx=False,yline=999,xline=999, \
|
346
351
|
resample_freq='H',loc1='best',loc2='best', \
|
347
352
|
date_range=False,date_freq=False,date_fmt='%Y-%m-%d', \
|
348
|
-
color1='red',color2='blue'):
|
353
|
+
color1='red',color2='blue',facecolor='whitesmoke'):
|
349
354
|
"""
|
350
355
|
功能:绘制两个证券的折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
351
356
|
假定:数据表有索引,且已经按照索引排序
|
@@ -378,7 +383,7 @@ def plot2_line2(df1,ticker1,colname1,label1, \
|
|
378
383
|
yline,xline,resample_freq=resample_freq, \
|
379
384
|
loc1=loc1,loc2=loc2, \
|
380
385
|
date_range=date_range,date_freq=date_freq,date_fmt=date_fmt, \
|
381
|
-
color1=color1,color2=color2)
|
386
|
+
color1=color1,color2=color2,facecolor=facecolor)
|
382
387
|
else:
|
383
388
|
plot_line2_twinx2(df1,ticker1,colname1,label1, \
|
384
389
|
df2,ticker2,colname2,label2, \
|
@@ -386,7 +391,7 @@ def plot2_line2(df1,ticker1,colname1,label1, \
|
|
386
391
|
resample_freq=resample_freq, \
|
387
392
|
loc1=loc1,loc2=loc2, \
|
388
393
|
date_range=date_range,date_freq=date_freq,date_fmt=date_fmt, \
|
389
|
-
color1=color1,color2=color2)
|
394
|
+
color1=color1,color2=color2,facecolor=facecolor)
|
390
395
|
return
|
391
396
|
|
392
397
|
|
@@ -399,7 +404,7 @@ def plot_line2_coaxial(df01,ticker1,colname1,label1, \
|
|
399
404
|
power=0,datatag1=False,datatag2=False,zeroline=False, \
|
400
405
|
yline=999,xline=999,resample_freq='H', \
|
401
406
|
loc1='best',loc2='best', \
|
402
|
-
color1='red',color2='blue', \
|
407
|
+
color1='red',color2='blue',facecolor='whitesmoke', \
|
403
408
|
ticker_type='auto'):
|
404
409
|
"""
|
405
410
|
功能:绘制两个证券的折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
@@ -526,7 +531,11 @@ def plot_line2_coaxial(df01,ticker1,colname1,label1, \
|
|
526
531
|
# 同轴绘图时,loc1/loc2未用上!
|
527
532
|
plt.legend(loc=loc1,fontsize=legend_txt_size)
|
528
533
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
529
|
-
|
534
|
+
try:
|
535
|
+
plt.gca().set_facecolor(facecolor)
|
536
|
+
except:
|
537
|
+
print(" #Warning(plot_line2_coaxial): color",facecolor,"is unsupported, changed to default setting")
|
538
|
+
plt.gca().set_facecolor("whitesmoke")
|
530
539
|
|
531
540
|
plt.ylabel(ylabeltxt,fontsize=ylabel_txt_size)
|
532
541
|
plt.xlabel(footnote,fontsize=xlabel_txt_size)
|
@@ -562,7 +571,8 @@ def plot_line2_coaxial2(df01,ticker1,colname1,label1, \
|
|
562
571
|
yline=999,xline=999,resample_freq='H', \
|
563
572
|
loc1='best',loc2='best', \
|
564
573
|
date_range=False,date_freq=False,date_fmt='%Y-%m-%d', \
|
565
|
-
color1='red',color2='blue',
|
574
|
+
color1='red',color2='blue',facecolor='whitesmoke', \
|
575
|
+
ticker_type='auto'):
|
566
576
|
"""
|
567
577
|
功能:绘制两个证券的折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
568
578
|
假定:数据表有索引,且已经按照索引排序
|
@@ -710,8 +720,12 @@ def plot_line2_coaxial2(df01,ticker1,colname1,label1, \
|
|
710
720
|
# 同轴绘图时,loc1/loc2未用上!
|
711
721
|
plt.legend(loc=loc1,fontsize=legend_txt_size)
|
712
722
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
713
|
-
|
714
|
-
|
723
|
+
try:
|
724
|
+
plt.gca().set_facecolor(facecolor)
|
725
|
+
except:
|
726
|
+
print(" #Warning(plot_line2_coaxial2): color",facecolor,"is unsupported, changed to default setting")
|
727
|
+
plt.gca().set_facecolor("whitesmoke")
|
728
|
+
|
715
729
|
plt.ylabel(ylabeltxt,fontsize=ylabel_txt_size)
|
716
730
|
plt.xlabel(footnote,fontsize=xlabel_txt_size)
|
717
731
|
plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
|
@@ -723,9 +737,10 @@ def plot_line2_coaxial2(df01,ticker1,colname1,label1, \
|
|
723
737
|
#==============================================================================
|
724
738
|
def plot_line2_twinx(df01,ticker1,colname1,label1, \
|
725
739
|
df02,ticker2,colname2,label2, \
|
726
|
-
|
727
|
-
|
728
|
-
|
740
|
+
titletxt,footnote,power=0,datatag1=False,datatag2=False, \
|
741
|
+
resample_freq='H',loc1='upper left',loc2='lower left', \
|
742
|
+
color1='red',color2='blue',facecolor='whitesmoke', \
|
743
|
+
ticker_type='auto'):
|
729
744
|
"""
|
730
745
|
功能:绘制两个证券的折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
731
746
|
假定:数据表有索引,且已经按照索引排序
|
@@ -757,6 +772,11 @@ def plot_line2_twinx(df01,ticker1,colname1,label1, \
|
|
757
772
|
#证券1:绘制折线图,双坐标轴
|
758
773
|
fig = plt.figure()
|
759
774
|
ax = fig.add_subplot(111)
|
775
|
+
try:
|
776
|
+
fig.gca().set_facecolor(facecolor)
|
777
|
+
except:
|
778
|
+
print(" #Warning(plot_line2_twinx): color",facecolor,"is unsupported, changed to default setting")
|
779
|
+
fig.gca().set_facecolor("whitesmoke")
|
760
780
|
|
761
781
|
if ticker1 == '':
|
762
782
|
label1txt=label1
|
@@ -859,8 +879,7 @@ def plot_line2_twinx(df01,ticker1,colname1,label1, \
|
|
859
879
|
|
860
880
|
#自动优化x轴标签
|
861
881
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
862
|
-
|
863
|
-
|
882
|
+
|
864
883
|
plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
|
865
884
|
plt.show()
|
866
885
|
|
@@ -884,11 +903,12 @@ if __name__ =="__main__":
|
|
884
903
|
|
885
904
|
#==============================================================================
|
886
905
|
def plot_line2_twinx2(df01,ticker1,colname1,label1, \
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
906
|
+
df02,ticker2,colname2,label2, \
|
907
|
+
titletxt,footnote,power=0,datatag1=False,datatag2=False, \
|
908
|
+
resample_freq='H',loc1='upper left',loc2='lower left', \
|
909
|
+
date_range=False,date_freq=False,date_fmt='%Y-%m-%d', \
|
910
|
+
color1='red',color2='blue',facecolor='whitesmoke', \
|
911
|
+
ticker_type='auto'):
|
892
912
|
"""
|
893
913
|
功能:绘制两个证券的折线图。如果power=0不绘制趋势图,否则绘制多项式趋势图
|
894
914
|
假定:数据表有索引,且已经按照索引排序
|
@@ -919,8 +939,14 @@ def plot_line2_twinx2(df01,ticker1,colname1,label1, \
|
|
919
939
|
|
920
940
|
#证券1:绘制折线图,双坐标轴
|
921
941
|
fig = plt.figure()
|
942
|
+
try:
|
943
|
+
fig.gca().set_facecolor(facecolor)
|
944
|
+
except:
|
945
|
+
print(" #Warning(plot_line2_twinx2): color",facecolor,"is unsupported, changed to default setting")
|
946
|
+
fig.gca().set_facecolor("whitesmoke")
|
947
|
+
|
922
948
|
ax = fig.add_subplot(111)
|
923
|
-
|
949
|
+
|
924
950
|
if ticker1 == '':
|
925
951
|
label1txt=label1
|
926
952
|
else:
|
@@ -1049,8 +1075,7 @@ def plot_line2_twinx2(df01,ticker1,colname1,label1, \
|
|
1049
1075
|
#格式化时间轴标注
|
1050
1076
|
#plt.gca().xaxis.set_major_formatter(mdate.DateFormatter('%y-%m-%d'))
|
1051
1077
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
1052
|
-
|
1053
|
-
|
1078
|
+
|
1054
1079
|
plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
|
1055
1080
|
plt.show()
|
1056
1081
|
|
@@ -1060,7 +1085,8 @@ def plot_line2_twinx2(df01,ticker1,colname1,label1, \
|
|
1060
1085
|
def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
1061
1086
|
data_label=True,resample_freq='H',smooth=True,linewidth=1.5, \
|
1062
1087
|
loc='best',annotate=False,annotate_value=False,plus_sign=False, \
|
1063
|
-
mark_top=False,mark_bottom=False,mark_end=False,
|
1088
|
+
mark_top=False,mark_bottom=False,mark_end=False, \
|
1089
|
+
ticker_type='auto',facecolor='whitesmoke'):
|
1064
1090
|
"""
|
1065
1091
|
函数功能:根据df的内容绘制折线图
|
1066
1092
|
输入参数:
|
@@ -1077,6 +1103,13 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1077
1103
|
无返回数据
|
1078
1104
|
注意:需要日期类型作为df索引
|
1079
1105
|
"""
|
1106
|
+
DEBUG=False
|
1107
|
+
|
1108
|
+
if DEBUG:
|
1109
|
+
print("annotate=",annotate,"annotate_value=",annotate_value)
|
1110
|
+
print("mark_top=",mark_top,"mark_bottom=",mark_bottom)
|
1111
|
+
print(df0)
|
1112
|
+
|
1080
1113
|
#空值判断
|
1081
1114
|
if len(df0) ==0:
|
1082
1115
|
print (" #Warning(draw_lines): no data to plot.")
|
@@ -1168,6 +1201,7 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1168
1201
|
last_line_color = lines[-1].get_color()
|
1169
1202
|
|
1170
1203
|
if annotate:
|
1204
|
+
mark_end=False
|
1171
1205
|
df_end=dfg.tail(1)
|
1172
1206
|
# df_end[c]必须为数值类型,否则可能出错
|
1173
1207
|
y_end = df_end[c].min() # 末端的y坐标
|
@@ -1236,7 +1270,6 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1236
1270
|
xytext=(x_end, y_end),fontsize=annotate_size,
|
1237
1271
|
color=last_line_color)
|
1238
1272
|
|
1239
|
-
|
1240
1273
|
#绘制水平辅助线
|
1241
1274
|
if axhline_label !="":
|
1242
1275
|
if '零线' in axhline_label:
|
@@ -1253,9 +1286,14 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1253
1286
|
plt.xlabel(x_label_t,fontweight='bold',fontsize=xlabel_txt_size)
|
1254
1287
|
#图示标题
|
1255
1288
|
plt.title(title_txt,fontweight='bold',fontsize=title_txt_size)
|
1256
|
-
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
1257
|
-
plt.gca().set_facecolor('whitesmoke')
|
1258
1289
|
|
1290
|
+
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
1291
|
+
try:
|
1292
|
+
plt.gca().set_facecolor(facecolor)
|
1293
|
+
except:
|
1294
|
+
print(" #Warning(draw_lines): color",facecolor,"is unsupported, changed to default setting")
|
1295
|
+
plt.gca().set_facecolor("whitesmoke")
|
1296
|
+
|
1259
1297
|
# 若不绘制annotate,则绘制图例
|
1260
1298
|
if not annotate:
|
1261
1299
|
plt.legend(loc=loc,fontsize=legend_txt_size)
|
@@ -1282,7 +1320,8 @@ def draw_lines2(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1282
1320
|
date_range=False,date_freq=False,date_fmt='%Y-%m-%d', \
|
1283
1321
|
colorlist=[],lslist=[],lwlist=[], \
|
1284
1322
|
band_area='',loc='best',annotate=False,annotate_value=False, \
|
1285
|
-
mark_top=False,mark_bottom=False,mark_end=False
|
1323
|
+
mark_top=False,mark_bottom=False,mark_end=False, \
|
1324
|
+
facecolor='whitesmoke'):
|
1286
1325
|
"""
|
1287
1326
|
函数功能:根据df的内容绘制折线图
|
1288
1327
|
输入参数:
|
@@ -1447,8 +1486,12 @@ def draw_lines2(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1447
1486
|
#图示标题
|
1448
1487
|
plt.title(title_txt,fontweight='bold',fontsize=title_txt_size)
|
1449
1488
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
1450
|
-
|
1451
|
-
|
1489
|
+
try:
|
1490
|
+
plt.gca().set_facecolor(facecolor)
|
1491
|
+
except:
|
1492
|
+
print(" #Warning(draw_lines2): color",facecolor,"is unsupported, changed to default setting")
|
1493
|
+
plt.gca().set_facecolor("whitesmoke")
|
1494
|
+
|
1452
1495
|
if not annotate:
|
1453
1496
|
plt.legend(loc=loc,fontsize=legend_txt_size)
|
1454
1497
|
|
@@ -1462,7 +1505,8 @@ def draw_lines2(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
|
|
1462
1505
|
#==============================================================================
|
1463
1506
|
def plot_barh(df,colname,titletxt,footnote,datatag=True, \
|
1464
1507
|
colors=['r','g','b','c','m','y','aquamarine','dodgerblue', \
|
1465
|
-
'deepskyblue','silver'],tag_offset=0.01,axisamp=1.3
|
1508
|
+
'deepskyblue','silver'],tag_offset=0.01,axisamp=1.3, \
|
1509
|
+
facecolor='whitesmoke'):
|
1466
1510
|
"""
|
1467
1511
|
功能:绘制水平单值柱状图,并可标注数据标签。
|
1468
1512
|
输入:数据集df;列名colname;标题titletxt;脚注footnote;
|
@@ -1517,7 +1561,12 @@ def plot_barh(df,colname,titletxt,footnote,datatag=True, \
|
|
1517
1561
|
yticknames=list(df.index)
|
1518
1562
|
plt.yticks(df.index,yticknames)
|
1519
1563
|
|
1520
|
-
|
1564
|
+
try:
|
1565
|
+
plt.gca().set_facecolor(facecolor)
|
1566
|
+
except:
|
1567
|
+
print(" #Warning(plot_barh): color",facecolor,"is unsupported, changed to default setting")
|
1568
|
+
plt.gca().set_facecolor("whitesmoke")
|
1569
|
+
|
1521
1570
|
plt.show(); plt.close()
|
1522
1571
|
|
1523
1572
|
return
|
@@ -1531,7 +1580,7 @@ if __name__=='__main__':
|
|
1531
1580
|
titletxt="This is a title"
|
1532
1581
|
footnote="This is a footnote"
|
1533
1582
|
|
1534
|
-
def plot_barh2(df,colname,titletxt,footnote):
|
1583
|
+
def plot_barh2(df,colname,titletxt,footnote,facecolor='whitesmoke'):
|
1535
1584
|
"""
|
1536
1585
|
功能:绘制水平单值柱状图,并在外侧标注数据标签。
|
1537
1586
|
输入:数据集df;列名colname;标题titletxt;脚注footnote;
|
@@ -1571,6 +1620,12 @@ def plot_barh2(df,colname,titletxt,footnote):
|
|
1571
1620
|
'yanchor': 'top'},
|
1572
1621
|
)
|
1573
1622
|
|
1623
|
+
try:
|
1624
|
+
fig.gca().set_facecolor(facecolor)
|
1625
|
+
except:
|
1626
|
+
print(" #Warning(plot_barh2): color",facecolor,"is unsupported, changed to default setting")
|
1627
|
+
fig.gca().set_facecolor("whitesmoke")
|
1628
|
+
|
1574
1629
|
fig.show()
|
1575
1630
|
|
1576
1631
|
return
|
@@ -1584,7 +1639,8 @@ if __name__=='__main__':
|
|
1584
1639
|
def plot_2lines(df01,colname1,label1, \
|
1585
1640
|
df02,colname2,label2, \
|
1586
1641
|
ylabeltxt,titletxt,footnote,hline=0,vline=0,resample_freq='H', \
|
1587
|
-
date_range=False,date_freq=False,date_fmt='%Y-%m-%d'
|
1642
|
+
date_range=False,date_freq=False,date_fmt='%Y-%m-%d', \
|
1643
|
+
facecolor='whitesmoke'):
|
1588
1644
|
"""
|
1589
1645
|
功能:绘制两个证券的折线图。如果hline=0不绘制水平虚线,vline=0不绘制垂直虚线
|
1590
1646
|
假定:数据表有日期索引,且已经按照索引排序
|
@@ -1673,8 +1729,12 @@ def plot_2lines(df01,colname1,label1, \
|
|
1673
1729
|
plt.legend(loc='best',fontsize=legend_txt_size)
|
1674
1730
|
|
1675
1731
|
plt.gcf().autofmt_xdate() # 优化标注(自动倾斜)
|
1676
|
-
|
1677
|
-
|
1732
|
+
try:
|
1733
|
+
plt.gca().set_facecolor(facecolor)
|
1734
|
+
except:
|
1735
|
+
print(" #Warning(plot_2lines): color",facecolor,"is unsupported, changed to default setting")
|
1736
|
+
plt.gca().set_facecolor("whitesmoke")
|
1737
|
+
|
1678
1738
|
plt.show()
|
1679
1739
|
|
1680
1740
|
return
|
@@ -1847,7 +1907,7 @@ if __name__=='__main__':
|
|
1847
1907
|
sd=1
|
1848
1908
|
obs_num=100
|
1849
1909
|
|
1850
|
-
def plot_norm(mu,sd,graph='pdf',obs_num=100):
|
1910
|
+
def plot_norm(mu,sd,graph='pdf',obs_num=100,facecolor='whitesmoke'):
|
1851
1911
|
"""
|
1852
1912
|
绘制正态分布图形
|
1853
1913
|
mu:均值
|
@@ -1914,8 +1974,12 @@ def plot_norm(mu,sd,graph='pdf',obs_num=100):
|
|
1914
1974
|
#plt.grid() #网格
|
1915
1975
|
plt.legend(loc='best',fontsize=legend_txt_size)
|
1916
1976
|
|
1917
|
-
|
1918
|
-
|
1977
|
+
try:
|
1978
|
+
plt.gca().set_facecolor(facecolor)
|
1979
|
+
except:
|
1980
|
+
print(" #Warning(plot_norm): color",facecolor,"is unsupported, changed to default setting")
|
1981
|
+
plt.gca().set_facecolor("whitesmoke")
|
1982
|
+
|
1919
1983
|
plt.show() #显示图形
|
1920
1984
|
|
1921
1985
|
return
|
siat/markowitz.py
CHANGED
@@ -225,10 +225,13 @@ def portfolio_hpr(portfolio,thedate,pastyears=1, \
|
|
225
225
|
|
226
226
|
#==============================================================================
|
227
227
|
if __name__=='__main__':
|
228
|
+
Market={'Market':('US','^GSPC')}
|
228
229
|
Market={'Market':('US','^GSPC','我的组合001')}
|
229
230
|
Stocks1={'AAPL':.3,'MSFT':.15,'AMZN':.15,'FB':.01,'GOOG':.01}
|
230
231
|
Stocks2={'XOM':.02,'JNJ':.02,'JPM':.01,'TSLA':.3,'SBUX':.03}
|
231
232
|
portfolio=dict(Market,**Stocks1,**Stocks2)
|
233
|
+
|
234
|
+
ticker_name(portfolio)
|
232
235
|
|
233
236
|
thedate='2023-2-17'
|
234
237
|
pastyears=1
|
@@ -241,7 +244,7 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
|
|
241
244
|
rate_period='1Y',rate_type='shibor',RF=False, \
|
242
245
|
printout=True,graph=True):
|
243
246
|
"""
|
244
|
-
|
247
|
+
功能:绘制投资组合的累计收益率趋势图,并与等权和期间内交易额加权组合比较
|
245
248
|
注意:中国部分历史区段的treasury历史可能无法取得;
|
246
249
|
无论是shibor还是treasury的近期利率均可能空缺,只能以最近期的数值填补
|
247
250
|
"""
|
@@ -384,7 +387,7 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
|
|
384
387
|
StockReturns['Portfolio_EW']=stock_return.mul(portfolio_weights_ew,axis=1).sum(axis=1)
|
385
388
|
#..........................................................................
|
386
389
|
|
387
|
-
#
|
390
|
+
# 创建交易额加权组合:按照成交金额计算期间内交易额均值
|
388
391
|
tamount=prices['Close']*prices['Volume']
|
389
392
|
tamountlist=tamount.mean(axis=0) #求列的均值
|
390
393
|
tamountlist_array = np.array(tamountlist)
|
@@ -397,7 +400,7 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
|
|
397
400
|
if lang == 'Chinese':
|
398
401
|
title_txt="投资组合策略:业绩对比"
|
399
402
|
Portfolio_EW_txt="等权重策略"
|
400
|
-
Portfolio_LW_txt="
|
403
|
+
Portfolio_LW_txt="交易额加权策略"
|
401
404
|
else:
|
402
405
|
title_txt="Investment Portfolio Strategies: Performance Comparison"
|
403
406
|
Portfolio_EW_txt="Equal-weight"
|
@@ -443,7 +446,7 @@ if __name__=='__main__':
|
|
443
446
|
def portfolio_expret(portfolio,today,pastyears=1, \
|
444
447
|
rate_period='1Y',rate_type='shibor',RF=False,printout=True,graph=True):
|
445
448
|
"""
|
446
|
-
|
449
|
+
功能:绘制投资组合的持有期收益率趋势图,并与等权和期间内交易额加权组合比较
|
447
450
|
套壳原来的portfolio_cumret函数,以维持兼容性
|
448
451
|
expret: expanding return,以维持与前述章节名词的一致性
|
449
452
|
hpr: holding period return, 持有(期)收益率
|
@@ -994,22 +997,26 @@ def portfolio_es(pf_info,simulation=50000):
|
|
994
997
|
|
995
998
|
import datetime as dt; stoday=dt.date.today()
|
996
999
|
lang = check_language()
|
997
|
-
if lang == 'Chinese':
|
1000
|
+
if lang == 'Chinese':
|
1001
|
+
if pname == '': pname='投资组合'
|
1002
|
+
|
998
1003
|
plt.colorbar(label='收益率/标准差')
|
999
|
-
plt.title("
|
1004
|
+
plt.title(pname+": 马科维茨可行(有效)集",fontsize=title_txt_size)
|
1000
1005
|
plt.ylabel("年化收益率",fontsize=ylabel_txt_size)
|
1001
1006
|
|
1002
1007
|
footnote1="年化收益率标准差-->"
|
1003
|
-
footnote2="\n\n
|
1008
|
+
footnote2="\n\n基于给定的成份证券构造"+str(simulation)+"个投资组合"
|
1004
1009
|
footnote3="\n观察期间:"+hstart+"至"+hend
|
1005
1010
|
footnote4="\n来源: Sina/EM/stooq, "+str(stoday)
|
1006
1011
|
else:
|
1012
|
+
if pname == '': pname='Investment Portfolio'
|
1013
|
+
|
1007
1014
|
plt.colorbar(label='Return/Std')
|
1008
|
-
plt.title("
|
1015
|
+
plt.title(pname+": Efficient Set",fontsize=title_txt_size)
|
1009
1016
|
plt.ylabel("Annualized Return",fontsize=ylabel_txt_size)
|
1010
1017
|
|
1011
1018
|
footnote1="Annualized Std -->\n\n"
|
1012
|
-
footnote2="Based on
|
1019
|
+
footnote2="Based on given component securities, constructed "+str(simulation)+" portfolios\n"
|
1013
1020
|
footnote3="Period of observation: "+hstart+" to "+hend
|
1014
1021
|
footnote4="\nSource: sina/eastmoney/stooq, "+str(stoday)
|
1015
1022
|
|
@@ -1465,21 +1472,25 @@ def RandomPortfolios_plot(RandomPortfolios,col_x,col_y,colorbartxt,title_ext, \
|
|
1465
1472
|
|
1466
1473
|
lang = check_language()
|
1467
1474
|
if lang == 'Chinese':
|
1468
|
-
|
1475
|
+
if pname == '': pname='投资组合'
|
1476
|
+
|
1477
|
+
plt.title(pname+": 马科维茨有效(可行)集,基于"+title_ext,fontsize=title_txt_size)
|
1469
1478
|
plt.ylabel(ylabeltxt,fontsize=ylabel_txt_size)
|
1470
1479
|
|
1471
1480
|
import datetime as dt; stoday=dt.date.today()
|
1472
1481
|
footnote1=x_axis_name+" -->\n\n"
|
1473
|
-
footnote2="
|
1482
|
+
footnote2="基于设定的成份证券构造"+str(simulation)+"个投资组合"
|
1474
1483
|
footnote3="\n观察期间:"+hstart+"至"+hend
|
1475
1484
|
footnote4="\n来源: Sina/EM/stooq/FRED, "+str(stoday)
|
1476
1485
|
else:
|
1477
|
-
|
1486
|
+
if pname == '': pname='Investment Portfolio'
|
1487
|
+
|
1488
|
+
plt.title(pname+": Efficient Set, Based on "+title_ext,fontsize=title_txt_size)
|
1478
1489
|
plt.ylabel(ylabeltxt,fontsize=ylabel_txt_size)
|
1479
1490
|
|
1480
1491
|
import datetime as dt; stoday=dt.date.today()
|
1481
1492
|
footnote1=x_axis_name+" -->\n\n"
|
1482
|
-
footnote2="Based on
|
1493
|
+
footnote2="Based on given component securities, constructed "+str(simulation)+" portfolios"
|
1483
1494
|
footnote3="\nPeriod of observation: "+hstart+" to "+hend
|
1484
1495
|
footnote4="\nSource: sina/eastmoney/stooq/FRED, "+str(stoday)
|
1485
1496
|
|
@@ -1514,7 +1525,7 @@ def cvt_portfolio_name(pname,portfolio_returns):
|
|
1514
1525
|
|
1515
1526
|
lang=check_language()
|
1516
1527
|
if lang == "Chinese":
|
1517
|
-
pclist=[pname,'等权重组合','
|
1528
|
+
pclist=[pname,'等权重组合','交易额加权组合','MSR组合','GMVS组合','MSO组合','GML组合', \
|
1518
1529
|
'MAR组合','GMBA组合', 'MTR组合','GMBT组合']
|
1519
1530
|
else:
|
1520
1531
|
pclist=[pname,'Equal-weight','Amount-weight','MSR','GMVS','MSO','GML', \
|