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/capm_beta.py CHANGED
@@ -128,7 +128,7 @@ def prepare_capm_data(stkcd,mktidx,start,end):
128
128
  return R
129
129
 
130
130
  #==============================================================================
131
- def capm_beta(ticker,mktidx,start,end):
131
+ def capm_beta(ticker,mktidx,start,end='today'):
132
132
  """
133
133
  函数功能:计算一只股票的静态CAPM模型贝塔系数
134
134
  输入参数:
@@ -140,6 +140,7 @@ def capm_beta(ticker,mktidx,start,end):
140
140
  显示CAPM市场模型回归的alpha, beta, 以及显著性和拟合优度
141
141
  无返回数据
142
142
  """
143
+ start,end=start_end_preprocess(start,end)
143
144
 
144
145
  #读取股价并准备好收益率数据
145
146
  R=prepare_capm_data(ticker,mktidx,start,end)
@@ -315,22 +316,36 @@ def capm_beta_yearly(ticker,mktidx,yearlist):
315
316
  betas=betas._append(row,ignore_index=True)
316
317
  #print(year,round(beta,4),round(r_value**2,3),round(p_value,4))
317
318
 
319
+ betas['Significance']=betas['p-value'].apply(lambda x: '***' if x<0.01 \
320
+ else '**' if x<0.05 else '*' if x<0.1 else '')
321
+
318
322
  #设置打印标题与数据对齐
323
+ """
319
324
  pd.set_option('display.unicode.ambiguous_as_wide', True)
320
325
  pd.set_option('display.unicode.east_asian_width', True)
321
326
  pd.set_option('display.width', 180) # 设置打印宽度(**重要**)
322
327
 
323
- print("\n ===== 股票的年度贝塔系数:",ticker_name(ticker),"=====")
328
+ print("\n ===== 分年度静态贝塔系数:",ticker_name(ticker),"=====")
324
329
  print(betas.to_string(index=False))
325
- import datetime as dt; today=dt.date.today()
326
- print(" 数据来源:新浪/stooq/fred,基于"+ticker_name(mktidx)+','+str(today))
327
-
330
+ import datetime as dt; todaydt=dt.date.today()
331
+ print("数据来源:新浪/stooq/fred,基于"+ticker_name(mktidx)+','+str(dttoday))
332
+ print('') #空一行
333
+ """
334
+ titletxt="分年度静态贝塔系数: "+ticker_name(ticker)
335
+ import datetime as dt; todaydt=dt.date.today()
336
+ footnote="数据来源:新浪/stooq/fred,基于"+ticker_name(mktidx)+', '+str(todaydt)
337
+ df_display_CSS(betas,titletxt=titletxt,footnote=footnote,facecolor='papayawhip', \
338
+ decimals=4, \
339
+ first_col_align='center',second_col_align='center', \
340
+ last_col_align='center',other_col_align='center')
341
+
342
+ #准备绘图
328
343
  betas.set_index('Year',inplace=True)
329
344
 
330
345
  #绘图:年度贝塔系数趋势
331
346
  df=pd.DataFrame(betas['Beta'])
332
- title="股票的年度贝塔系数趋势: "+ticker_name(ticker)
333
- foot="\n数据来源:新浪/stooq/fred,基于"+ticker_name(mktidx)+','+str(today)
347
+ title="分年度静态贝塔系数: "+ticker_name(ticker)
348
+ foot="\n数据来源:新浪/stooq/fred,基于"+ticker_name(mktidx)+','+str(todaydt)
334
349
  plot_trend(title,foot,df,power=3,axhline_value=1,axhline_label='市场风险线')
335
350
 
336
351
  return betas
@@ -484,13 +499,24 @@ def capm_beta_portfolio_yearly(tickerlist,sharelist,mktidx,yearlist):
484
499
  betas=betas.append(row,ignore_index=True)
485
500
  except:
486
501
  betas=betas._append(row,ignore_index=True)
487
-
488
- print("\n",betas)
502
+
503
+ betas['Significance']=betas['p-value'].apply(lambda x: '***' if x<0.01 \
504
+ else '**' if x<0.05 else '*' if x<0.1 else '')
505
+ #print("\n",betas)
506
+
507
+ titletxt="投资组合的静态年度贝塔系数"
508
+ import datetime as dt; todaydt=dt.date.today()
509
+ footnote="数据来源:新浪/stooq/fred,基于"+ticker_name(mktidx)+', '+str(todaydt)
510
+ df_display_CSS(betas,titletxt=titletxt,footnote=footnote,facecolor='papayawhip', \
511
+ decimals=4, \
512
+ first_col_align='center',second_col_align='center', \
513
+ last_col_align='center',other_col_align='center')
514
+
489
515
  betas.set_index('Year',inplace=True)
490
516
 
491
517
  #绘图:年度贝塔系数变化
492
518
  df=pd.DataFrame(betas['Beta'])
493
- title="投资组合的年度贝塔系数"+ \
519
+ title="投资组合的静态年度贝塔系数"+ \
494
520
  "\n成分股: "+str(ticker_name(tickerlist,'bond'))+"\n持仓权重: "+str(sharelist)
495
521
  import datetime; today = datetime.date.today()
496
522
  foot="数据来源: 新浪/stooq/fred,"+str(today)
siat/capm_beta2.py CHANGED
@@ -451,7 +451,7 @@ def compare_mticker_1beta(ticker,start,end, \
451
451
  annotate=annotate,annotate_value=annotate, \
452
452
  mark_top=mark_top,mark_bottom=mark_bottom, \
453
453
  mark_start=mark_start,mark_end=mark_end, \
454
- facecolor=facecolor,loc=loc)
454
+ facecolor=facecolor,loc=loc,precision=4)
455
455
 
456
456
  return df
457
457
 
@@ -590,7 +590,8 @@ def compare_1ticker_mRF(ticker,start,end, \
590
590
  annotate=annotate,annotate_value=annotate, \
591
591
  mark_top=mark_top,mark_bottom=mark_bottom, \
592
592
  mark_start=mark_start,mark_end=mark_end, \
593
- facecolor=facecolor,loc=loc)
593
+ facecolor=facecolor,loc=loc, \
594
+ precision=4)
594
595
 
595
596
  return df
596
597
 
@@ -723,7 +724,7 @@ def compare_1ticker_mregression_period(ticker,start,end, \
723
724
  annotate=annotate,annotate_value=annotate, \
724
725
  mark_top=mark_top,mark_bottom=mark_bottom, \
725
726
  mark_start=mark_start,mark_end=mark_end, \
726
- facecolor=facecolor,loc=loc)
727
+ facecolor=facecolor,loc=loc,precision=4)
727
728
 
728
729
  return df
729
730
 
@@ -749,6 +750,36 @@ if __name__=='__main__':
749
750
 
750
751
  betas=compare_beta_security(ticker,start,end,RF)
751
752
 
753
+ def compare_beta(ticker,start,end, \
754
+ adjust='qfq', \
755
+ RF=0,regression_period=365, \
756
+ attention_value='',attention_value_area='', \
757
+ attention_point='',attention_point_area='', \
758
+ band_area='', \
759
+ graph=True,facecolor='whitesmoke', \
760
+ annotate=False,annotate_value=False, \
761
+ mark_high=False,mark_low=False, \
762
+ mark_start=False,mark_end=False, \
763
+ mktidx='auto',source='auto', \
764
+ ticker_type='auto',loc="best"):
765
+ """
766
+ 功能:组合情况,可能多只股票、多个投资组合或投资组合与股票的混合,多个无风险收益率
767
+
768
+ """
769
+ df=compare_beta_security(ticker=ticker,start=start,end=end, \
770
+ adjust=adjust, \
771
+ RF=RF,regression_period=regression_period, \
772
+ attention_value=attention_value,attention_value_area=attention_value_area, \
773
+ attention_point=attention_point,attention_point_area=attention_point_area, \
774
+ band_area=band_area, \
775
+ graph=graph,facecolor=facecolor, \
776
+ annotate=annotate,annotate_value=annotate_value, \
777
+ mark_top=mark_high,mark_bottom=mark_low, \
778
+ mark_start=mark_start,mark_end=mark_end, \
779
+ mktidx=mktidx,source=source, \
780
+ ticker_type=ticker_type,loc=loc)
781
+
782
+
752
783
  def compare_beta_security(ticker,start,end, \
753
784
  adjust='qfq', \
754
785
  RF=0,regression_period=365, \
siat/common.py CHANGED
@@ -21,6 +21,27 @@ import pandas as pd
21
21
  SUFFIX_LIST_CN=['SS','SZ','BJ','SW','SH']
22
22
  SUFFIX_LIST_HK=['HK']
23
23
  #==============================================================================
24
+
25
+ if __name__=='__main__':
26
+ ticker='000858.SZ'
27
+ ticker='AAPL'
28
+
29
+ security_in_China(ticker)
30
+
31
+ def security_in_China(ticker):
32
+ """
33
+ 功能:判断证券代码,是否在中国市场
34
+ """
35
+
36
+ tlist=ticker.split('.')
37
+ if len(tlist) == 1: return False
38
+ else:
39
+ if tlist[1] in SUFFIX_LIST_CN:
40
+ return True
41
+ else:
42
+ return False
43
+
44
+ #==============================================================================
24
45
  #设置全局语言环境
25
46
  import pickle
26
47
 
@@ -523,7 +544,7 @@ def portfolio_name(portfolio):
523
544
  name=portfolio[keylist[0]][2]
524
545
  except:
525
546
  #name="PF1"
526
- e=text_lang("投资组合","Investment Portfolio")
547
+ name=text_lang("投资组合","Portfolio")
527
548
 
528
549
  return name
529
550
 
@@ -912,6 +933,10 @@ def regparms(results):
912
933
  df_pvalues=pd.DataFrame(pvalues)
913
934
  df_pvalues.columns=['p_values']
914
935
 
936
+ #取rsquared_adj值:单个数值,非数组
937
+ rsquared=results.rsquared
938
+ rsquared_adj=results.rsquared_adj
939
+
915
940
  #生成星星
916
941
  df_pvalues['sig']=df_pvalues['p_values'].apply(lambda x:sigstars(x))
917
942
 
@@ -920,8 +945,10 @@ def regparms(results):
920
945
  how='inner',left_index=True,right_index=True)
921
946
  parms2=pd.merge(parms1,df_pvalues, \
922
947
  how='inner',left_index=True,right_index=True)
923
-
948
+
949
+
924
950
  return parms2
951
+
925
952
  #==============================================================================
926
953
  if __name__=='__main__':
927
954
  txt='QDII-指数'
@@ -2509,7 +2536,7 @@ def list2str(alist):
2509
2536
 
2510
2537
  #==============================================================================
2511
2538
  # FUNCTION TO REMOVE TIMEZONE
2512
- def remove_timezone(dt):
2539
+ def remove_timezone_dt(dt):
2513
2540
 
2514
2541
  # HERE `dt` is a python datetime
2515
2542
  # object that used .replace() method
@@ -2517,7 +2544,7 @@ def remove_timezone(dt):
2517
2544
  #==============================================================================
2518
2545
  def remove_df_index_timezone(df):
2519
2546
  df['timestamp']=df.index
2520
- df['timestamp'] = df['timestamp'].apply(remove_timezone)
2547
+ df['timestamp'] = df['timestamp'].apply(remove_timezone_dt)
2521
2548
  df.index=df['timestamp']
2522
2549
  del df['timestamp']
2523
2550
 
@@ -3760,6 +3787,7 @@ if __name__=='__main__':
3760
3787
  df_display_CSS(df,titletxt,footnote,facecolor,decimals)
3761
3788
 
3762
3789
  def df_display_CSS(df,titletxt='',footnote='',facecolor='papayawhip',decimals=2, \
3790
+ hide_columns=False,
3763
3791
  first_col_align='left',second_col_align='right', \
3764
3792
  last_col_align='right',other_col_align='right', \
3765
3793
  titile_font_size='16px',heading_font_size='15px', \
@@ -3788,7 +3816,10 @@ def df_display_CSS(df,titletxt='',footnote='',facecolor='papayawhip',decimals=2,
3788
3816
  facecolor_default='papayawhip'
3789
3817
 
3790
3818
  #不显示索引列,注意style1已经不是DaraFrame了
3791
- style1=df.style.hide()
3819
+ style1=df.style.hide()
3820
+
3821
+ if hide_columns:
3822
+ style1=df.style.hide(axis='index').hide(axis='columns')
3792
3823
 
3793
3824
  #设置数值字段的千分位符号,同时设置数值字段的小数点精度
3794
3825
  style2=style1.format(precision=decimals,thousands=',',na_rep='-')
@@ -3966,10 +3997,21 @@ def df_index_timezone_remove(df):
3966
3997
  功能:去掉df索引日期中的时区信息,避免日期过滤时出错
3967
3998
  注意:从雅虎财经获取的数据中日期索引项很可能带有时区
3968
3999
  """
4000
+ DEBUG=False
4001
+
4002
+ # 检查是否因为处理时区而丢失了数据
4003
+ if DEBUG:
4004
+ print(f"BEFORE processing timezone, counts={len(df)}, from {df.index[0]} to {df.index[-1]}")
4005
+
3969
4006
  import pandas as pd
3970
- df.index = pd.to_datetime(df.index)
4007
+
4008
+ #可能无法处理某些复杂的时区情况
4009
+ df.index = pd.to_datetime(df.index,utc=True)
3971
4010
  df.index = df.index.tz_localize(None)
3972
-
4011
+
4012
+ if DEBUG:
4013
+ print(f"AFTER processing timezone, counts={len(df)}, from {df.index[0]} to {df.index[-1]}")
4014
+
3973
4015
  return df
3974
4016
  #==============================================================================
3975
4017
 
@@ -4918,7 +4960,13 @@ async def jupyter2pdf2(notebook_dir, notebook_file):
4918
4960
 
4919
4961
  import os
4920
4962
  from nbconvert import HTMLExporter
4921
- from playwright.async_api import async_playwright
4963
+
4964
+ try:
4965
+ from playwright.async_api import async_playwright
4966
+ except:
4967
+ print(" #Warning(jupyter2pdf2): playwright seems not fully installed yet")
4968
+ print(" [Solution] execute the command before re-run: playwright install")
4969
+ return
4922
4970
 
4923
4971
  html_file = ""
4924
4972
 
@@ -5101,5 +5149,44 @@ def is_A_share(ticker):
5101
5149
  return False
5102
5150
 
5103
5151
 
5152
+ #==============================================================================
5153
+
5154
+ def print2CSS(data_dict, \
5155
+ titletxt='',footnote='',facecolor='papayawhip',decimals=2, \
5156
+ hide_columns=True,
5157
+ first_col_align='left',second_col_align='right', \
5158
+ last_col_align='right',other_col_align='right', \
5159
+ titile_font_size='14px',heading_font_size='14px', \
5160
+ data_font_size='14px',footnote_font_size='11px'):
5161
+ """
5162
+ 功能:将字典中的数据转化为df,使用CSS形式实现整齐输出
5163
+ """
5164
+ import pandas as pd
5165
+ disp_df=pd.DataFrame(columns=['Item','Value'])
5166
+
5167
+ keys=list(data_dict.keys())
5168
+ for key in keys:
5169
+ value=data_dict[key]
5170
+
5171
+ s=pd.Series({'Item':key,'Value':value})
5172
+ disp_df=disp_df._append(s,ignore_index=True)
5173
+
5174
+ df_display_CSS(disp_df,titletxt=titletxt,footnote=footnote, \
5175
+ facecolor=facecolor,decimals=decimals, \
5176
+ hide_columns=hide_columns,
5177
+ first_col_align=first_col_align,second_col_align=second_col_align, \
5178
+ last_col_align=last_col_align,other_col_align=other_col_align, \
5179
+ titile_font_size=titile_font_size,heading_font_size=heading_font_size, \
5180
+ data_font_size=data_font_size,footnote_font_size=footnote_font_size)
5181
+
5182
+ return
5183
+
5184
+ #==============================================================================
5104
5185
  #==============================================================================
5105
5186
  #==============================================================================
5187
+ #==============================================================================
5188
+ #==============================================================================
5189
+ #==============================================================================
5190
+ #==============================================================================
5191
+ #==============================================================================
5192
+
siat/economy.py CHANGED
@@ -1435,8 +1435,8 @@ def macro_trend(ticker,indicator,start='L10Y',end='today', \
1435
1435
  if 'YoY PPI' in indicator:
1436
1436
  attention_value=0
1437
1437
 
1438
- df=compare_economy(tickers=ticker,measures=indicator, \
1439
- fromdate=fromdate,todate=todate, \
1438
+ df=compare_economy(ticker=ticker,indicator=indicator, \
1439
+ start=fromdate,end=todate, \
1440
1440
  power=power,twinx=twinx, \
1441
1441
  yline=attention_value, \
1442
1442
  loc1=loc1,loc2=loc2,facecolor=facecolor)
@@ -1451,7 +1451,7 @@ def macro_trend(ticker,indicator,start='L10Y',end='today', \
1451
1451
  if 'YoY PPI' in indicator:
1452
1452
  attention_value=0
1453
1453
 
1454
- df=economy_trend(start=fromdate,end=todate,scope=ticker,factor=indicator, \
1454
+ df=economy_trend0(start=fromdate,end=todate,ticker=ticker,indicator=indicator, \
1455
1455
  datatag=datatag,power=power, \
1456
1456
  zeroline=zeroline,yline=attention_value,facecolor=facecolor)
1457
1457
  # 计算增长率
siat/economy2.py CHANGED
@@ -1367,6 +1367,68 @@ def economic_translate(indicator):
1367
1367
  'GDP per capita, PPP (constant 2021 international $)',
1368
1368
  'GDP per capita, PPP (constant 2021 international $)'],
1369
1369
 
1370
+ # NY.GNP.MKTP:国民总收入GNI总量=======================================
1371
+ ['NY.GNP.MKTP.CD','GNI(美元现价)',
1372
+ 'GNI (current US$)',
1373
+ 'GNI (current US$)'],
1374
+
1375
+ ['NY.GNP.MKTP.CN','GNI(本币现价)',
1376
+ 'GNI (current LCU)',
1377
+ 'GNI (current LCU)'],
1378
+
1379
+ ['NY.GNP.MKTP.CN.AD','GNI(统计口径调整后,本币现价)',
1380
+ 'GNI: linked series (current LCU)',
1381
+ 'GNI: linked series (current LCU)'],
1382
+
1383
+ ['NY.GNP.MKTP.KD','GNI(2015美元不变价格)',
1384
+ 'GNI (constant 2015 US$)',
1385
+ 'GNI (constant 2015 US$)'],
1386
+
1387
+ ['NY.GNP.MKTP.KD.ZG','GNI年增速%(2015美元不变价格)',
1388
+ 'GNI growth (annual %, constant 2015 US$)',
1389
+ 'GNI growth (annual %, constant 2015 US$)'],
1390
+
1391
+ ['NY.GNP.MKTP.KN','GNI(本币不变价格)',
1392
+ 'GNI (constant LCU)',
1393
+ 'GNI (constant LCU)'],
1394
+
1395
+ ['NY.GNP.MKTP.PP.CD','GNI(购买力平价,国际美元现价)',
1396
+ 'GNI(PPP, current international $)',
1397
+ 'GNI(PPP, current international $)'],
1398
+
1399
+ ['NY.GNP.MKTP.PP.KD','GNI(购买力平价,2021国际美元不变价格)',
1400
+ 'GNI(PPP, constant 2021 international $)',
1401
+ 'GNI(PPP, constant 2021 international $)'],
1402
+
1403
+ # NY.GNP.PCAP:GNI人均=======================================
1404
+ ['NY.GNP.PCAP.CD','人均GNI(美元现价)',
1405
+ 'GNI per capita (current US$)',
1406
+ 'GNI per capita (current US$)'],
1407
+
1408
+ ['NY.GNP.PCAP.CN','人均GNI(本币现价)',
1409
+ 'GNI per capita (current LCU)',
1410
+ 'GNI per capita (current LCU)'],
1411
+
1412
+ ['NY.GNP.PCAP.KD','人均GNI(2015美元不变价格)',
1413
+ 'GNI per capita (constant 2015 US$)',
1414
+ 'GNI per capita (constant 2015 US$)'],
1415
+
1416
+ ['NY.GNP.PCAP.KD.ZG','人均GNI年增速%(2015美元不变价格)',
1417
+ 'GNI per capita growth (annual %, constant 2015 US$)',
1418
+ 'GNI per capita growth (annual %, constant 2015 US$)'],
1419
+
1420
+ ['NY.GNP.PCAP.KN','人均GNI(本币不变价格)',
1421
+ 'GNI per capita (constant LCU)',
1422
+ 'GNI per capita (constant LCU)'],
1423
+
1424
+ ['NY.GNP.PCAP.PP.CD','人均GNI(购买力平价,国际美元现价)',
1425
+ 'GNI per capita, PPP (current international $)',
1426
+ 'GNI per capita, PPP (current international $)'],
1427
+
1428
+ ['NY.GNP.PCAP.PP.KD','人均GNI(购买力平价,2021国际美元不变价格)',
1429
+ 'GNI per capita, PPP (constant 2021 international $)',
1430
+ 'GNI per capita, PPP (constant 2021 international $)'],
1431
+
1370
1432
  #######################################################################
1371
1433
  #“International $”(国际美元)是一种标准化的货币单位,用于消除汇率差异,
1372
1434
  #使不同国家的经济指标(如 GDP)在购买力平价(PPP)基础上更具可比性。
@@ -1387,6 +1449,10 @@ def economic_translate(indicator):
1387
1449
  #######################################################################
1388
1450
 
1389
1451
  # GFDD.DM:证券市场-股票、权益与债券=====================================
1452
+ ['CM.MKT.LCAP.GD.ZS','国内上市公司市值占GDP%',
1453
+ 'Market cap of domestic listed companies to GDP (%)',
1454
+ 'Market cap of domestic listed companies to GDP (%)'],
1455
+
1390
1456
  ['GFDD.DM.01','股票市场总市值占GDP%',
1391
1457
  'Stock market capitalization to GDP (%)',
1392
1458
  'Stock market capitalization to GDP (%)'],
@@ -1494,7 +1560,15 @@ def economic_translate(indicator):
1494
1560
  ['FM.LBL.BMNY.CN','广义货币(本币现价)',
1495
1561
  'Broad money (current LCU)',
1496
1562
  'Broad money (current LCU)'],
1497
-
1563
+
1564
+ ['FM.LBL.BMNY.ZG','广义货币年增速%',
1565
+ 'Broad money growth (annual %)',
1566
+ 'Broad money growth (annual %)'],
1567
+
1568
+ ['FM.LBL.MQMY.XD','货币流通速度(GDP/M2)',
1569
+ 'Income velocity of money (GDP/M2)',
1570
+ 'Income velocity of money (GDP/M2)'],
1571
+
1498
1572
  ['FM.LBL.BMNY.GD.ZS','广义货币(占GDP%)',
1499
1573
  'Broad money (% of GDP)',
1500
1574
  'Broad money (% of GDP)'],
Binary file