siat 2.14.1__py3-none-any.whl → 3.0.0__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/markowitz.py CHANGED
@@ -473,7 +473,8 @@ def portfolio_corr(pf_info):
473
473
  sr=stock_return.copy()
474
474
  collist=list(sr)
475
475
  for col in collist:
476
- sr.rename(columns={col:codetranslate(col)},inplace=True)
476
+ #投资组合中名称翻译以债券优先处理,因此几乎没有人把基金作为成分股
477
+ sr.rename(columns={col:ticker_name(col,'bond')},inplace=True)
477
478
 
478
479
  # 计算相关矩阵
479
480
  correlation_matrix = sr.corr()
@@ -1910,7 +1911,7 @@ def portfolio_optimize_strategy(pf_info,ratio='sharpe',simulation=50000,RF=False
1910
1911
  def translate_tickerlist(tickerlist):
1911
1912
  newlist=[]
1912
1913
  for t in tickerlist:
1913
- name=codetranslate(t)
1914
+ name=ticker_name(t,'bond')
1914
1915
  newlist=newlist+[name]
1915
1916
 
1916
1917
  return newlist
@@ -2165,7 +2166,7 @@ def security_correlation(tickers,start,end,info_type='Close'):
2165
2166
 
2166
2167
  collist=list(df)
2167
2168
  for col in collist:
2168
- df.rename(columns={col:codetranslate(col)},inplace=True)
2169
+ df.rename(columns={col:ticker_name(col,'bond')},inplace=True)
2169
2170
  df_coor = df.corr()
2170
2171
 
2171
2172
 
@@ -2265,7 +2266,7 @@ def describe_portfolio(portfolio):
2265
2266
 
2266
2267
  print("*** 投资组合信息:",pname)
2267
2268
  print("\n所在市场:",ectranslate(scope))
2268
- print("市场指数:",codetranslate(mktidx)+'('+mktidx+')')
2269
+ print("市场指数:",ticker_name(mktidx,'bond')+'('+mktidx+')')
2269
2270
  print("成分股及其份额:")
2270
2271
 
2271
2272
  num=len(tickerlist)
@@ -2274,7 +2275,7 @@ def describe_portfolio(portfolio):
2274
2275
  sharelist1=[]
2275
2276
  for t in range(num):
2276
2277
  #seqlist=seqlist+[t+1]
2277
- tickerlist1=tickerlist1+[codetranslate(tickerlist[t])+'('+tickerlist[t]+')']
2278
+ tickerlist1=tickerlist1+[ticker_name(tickerlist[t],'bond')+'('+tickerlist[t]+')']
2278
2279
  sharelist1=sharelist1+[str(round(sharelist[t],2))+'%']
2279
2280
 
2280
2281
  import pandas as pd
siat/option_china.py CHANGED
@@ -1648,7 +1648,7 @@ def index_option_price_china2(option,contract, \
1648
1648
  if graph:
1649
1649
  #获取股指历史价格
1650
1650
  collabel_so='期权价格'
1651
- collabel_si=codetranslate(ua)
1651
+ collabel_si=ticker_name(ua)
1652
1652
  if ('P' in contract) or ('p' in contract):
1653
1653
  direction='Put'
1654
1654
  else:
siat/option_pricing.py CHANGED
@@ -1260,7 +1260,7 @@ def option_chain(ticker,mdate,printout=True):
1260
1260
  today = datetime.date.today()
1261
1261
 
1262
1262
  print("\n===== 期权链的结构 =====")
1263
- print("标的资产:",codetranslate(ticker))
1263
+ print("标的资产:",ticker_name(ticker))
1264
1264
  print("到期日期:",mdate)
1265
1265
  print("看涨期权:",num_call)
1266
1266
  print(" 实值/虚值:",num_call_ITM,'/',num_call_OTM)
@@ -1286,7 +1286,7 @@ def option_chain(ticker,mdate,printout=True):
1286
1286
  ylabeltxt='期权价格('+currency+')'
1287
1287
  titletxt="期权价格与其标的资产行权价格的关系"
1288
1288
  footnote="行权价("+currency+") -->\n"+ \
1289
- "标的资产: "+codetranslate(ticker)+ \
1289
+ "标的资产: "+ticker_name(ticker)+ \
1290
1290
  ", "+"到期日: "+mdate+ \
1291
1291
  "\n数据来源: 雅虎财经, "+str(today)
1292
1292
  plot_2lines(df1,colname1,label1,df2,colname2,label2, \
@@ -1746,7 +1746,7 @@ def stock_trend_by_option(ticker,lastndays=7,cutoff=[1.1,5.0,10.0]):
1746
1746
  colname2='Benchmark'
1747
1747
  label2='等比例线'
1748
1748
  ylabeltxt='比例'
1749
- titletxt="期权链结构中的合约数量: "+codetranslate(ticker)+", 虚值看涨/看跌期权比例"
1749
+ titletxt="期权链结构中的合约数量: "+ticker_name(ticker)+", 虚值看涨/看跌期权比例"
1750
1750
 
1751
1751
  footnote="数据来源:雅虎财经, "+str(today)
1752
1752
  plot_line2(df2,ticker,colname1,label1, \
@@ -1756,7 +1756,7 @@ def stock_trend_by_option(ticker,lastndays=7,cutoff=[1.1,5.0,10.0]):
1756
1756
  #绘图2:OTM Calls vs OTM Puts交易金额比例
1757
1757
  colname1='OTM Amount Call/Put'
1758
1758
  label1='虚值看涨/看跌期权交易金额比例'
1759
- titletxt="期权链结构中的交易金额: "+codetranslate(ticker)+", 虚值看涨/看跌期权比例"
1759
+ titletxt="期权链结构中的交易金额: "+ticker_name(ticker)+", 虚值看涨/看跌期权比例"
1760
1760
 
1761
1761
  footnote="数据来源:雅虎财经, "+str(today)
1762
1762
  plot_line2(df2,ticker,colname1,label1, \
@@ -1770,7 +1770,7 @@ def stock_trend_by_option(ticker,lastndays=7,cutoff=[1.1,5.0,10.0]):
1770
1770
  colname2='Benchmark'
1771
1771
  label2='当前股价'
1772
1772
  ylabeltxt='股价('+currency+')'
1773
- titletxt="基于期权链结构的股价走势估计: "+codetranslate(ticker)
1773
+ titletxt="基于期权链结构的股价走势估计: "+ticker_name(ticker)
1774
1774
 
1775
1775
  footnote="数据来源:雅虎财经, "+str(today)
1776
1776
  plot_line2(df2,ticker,colname1,label1, \
@@ -1781,7 +1781,7 @@ def stock_trend_by_option(ticker,lastndays=7,cutoff=[1.1,5.0,10.0]):
1781
1781
  collist=['Date','Trend','Estimated Price','OTM Volume Call/Put','OTM Amount Call/Put']
1782
1782
  df3=df2[collist]
1783
1783
 
1784
- print("\n ======= 基于期权链结构的股票走势和价格预期: "+codetranslate(ticker)+" =======")
1784
+ print("\n ======= 基于期权链结构的股票走势和价格预期: "+ticker_name(ticker)+" =======")
1785
1785
  #设置打印对齐
1786
1786
  pd.set_option('display.max_columns', 1000)
1787
1787
  pd.set_option('display.width', 1000)
@@ -150,8 +150,8 @@ def print_rar_ratio(regdf,portfolio,ret_mean,ratio_name,ratio):
150
150
  date_end=str(regdf.index[-1].year)+'-'+str(regdf.index[-1].month)+ \
151
151
  '-'+str(regdf.index[-1].day)
152
152
  print("\n===== 风险调整收益率 =====")
153
- print("市场指数:",ectranslate(scope),'\b,',codetranslate(mktidx))
154
- #print("成分股 :",codetranslate(stocklist))
153
+ print("市场指数:",ectranslate(scope),'\b,',ticker_name(mktidx))
154
+ #print("成分股 :",ticker_name(stocklist))
155
155
  #print("持仓权重:",portionlist)
156
156
  print("计算期间:",date_start,"至",date_end)
157
157
  """
@@ -482,12 +482,12 @@ def draw_rar_ratio(rars,portfolio,ratio_name):
482
482
  #plt.plot(rars['Mean(Ret)'],label='Stock(s) return(%)',color='blue',lw=1)
483
483
  plt.axhline(y=0.0,color='black',linestyle=':')
484
484
 
485
- titletxt='风险调整收益的滚动趋势'+'\n'+str(codetranslate(stocklist))
485
+ titletxt='风险调整收益的滚动趋势'+'\n'+str(ticker_name(stocklist))
486
486
  if len(stocklist) > 1:
487
487
  titletxt=titletxt+'\n持仓比例: '+str(portionlist)
488
488
  """
489
489
  if len(stocklist) == 1:
490
- titletxt='风险调整收益的滚动趋势'+'\n('+codetranslate(stocklist)+')'
490
+ titletxt='风险调整收益的滚动趋势'+'\n('+ticker_name(stocklist)+')'
491
491
  """
492
492
  #plt.title(titletxt,fontsize=12,fontweight='bold')
493
493
  plt.title(titletxt,fontsize=12)
@@ -597,9 +597,9 @@ def sharpe_ratio_portfolio(portfolio,start,end,RF=True,printout=True):
597
597
 
598
598
  _,_,tickerlist,sharelist=decompose_portfolio(portfolio)
599
599
  if len(tickerlist)==1:
600
- product=str(codetranslate(tickerlist))
600
+ product=str(ticker_name(tickerlist,'bond'))
601
601
  else:
602
- product=str(codetranslate(tickerlist))+' by '+str(sharelist)
602
+ product=str(ticker_name(tickerlist,'bond'))+' by '+str(sharelist)
603
603
  print("投资组合:",product)
604
604
  print("计算期间:",date_start,"至",date_end,"(可用日期)")
605
605
  print("风险溢价均值(%):",round(ret_rf_mean,4))
@@ -693,9 +693,9 @@ def sortino_ratio_portfolio(portfolio,start,end,RF=True,printout=True):
693
693
 
694
694
  _,_,tickerlist,sharelist=decompose_portfolio(portfolio)
695
695
  if len(tickerlist)==1:
696
- product=str(codetranslate(tickerlist))
696
+ product=str(ticker_name(tickerlist,'bond'))
697
697
  else:
698
- product=str(codetranslate(tickerlist))+' by '+str(sharelist)
698
+ product=str(ticker_name(tickerlist,'bond'))+' by '+str(sharelist)
699
699
  print("投资组合:",product)
700
700
  print("计算期间:",date_start,"至",date_end, \
701
701
  "(可用日期)")
@@ -798,9 +798,9 @@ def jensen_alpha_portfolio(portfolio,start,end,RF=True,printout=True):
798
798
 
799
799
  _,_,tickerlist,sharelist=decompose_portfolio(portfolio)
800
800
  if len(tickerlist)==1:
801
- product=str(codetranslate(tickerlist))
801
+ product=str(ticker_name(tickerlist,'bond'))
802
802
  else:
803
- product=str(codetranslate(tickerlist))+' by '+str(sharelist)
803
+ product=str(ticker_name(tickerlist,'bond'))+' by '+str(sharelist)
804
804
  print("投资组合:",product)
805
805
  print("计算期间:",date_start,"至",date_end,"(可用日期)")
806
806
  print("风险溢价均值(%):",round(ret_rf_mean,4))
@@ -1256,7 +1256,7 @@ def compare_mrar(tickers,rar_name,start,end, \
1256
1256
  continue
1257
1257
  else:
1258
1258
  dft=df_tmp[['RAR']]
1259
- #dft.rename(columns={'RAR':codetranslate(t)},inplace=True)
1259
+ #dft.rename(columns={'RAR':ticker_name(t)},inplace=True)
1260
1260
  dft.rename(columns={'RAR':t},inplace=True)
1261
1261
 
1262
1262
  if len(df)==0:
@@ -1301,7 +1301,7 @@ def compare_mrar(tickers,rar_name,start,end, \
1301
1301
  if graph:
1302
1302
  # 翻译证券名称
1303
1303
  for c in list(df1):
1304
- df1.rename(columns={c:codetranslate(c)},inplace=True)
1304
+ df1.rename(columns={c:ticker_name(c)},inplace=True)
1305
1305
 
1306
1306
  draw_lines(df1,y_label,x_label, \
1307
1307
  axhline_value=axhline_value,axhline_label=axhline_label, \
@@ -1311,7 +1311,7 @@ def compare_mrar(tickers,rar_name,start,end, \
1311
1311
 
1312
1312
  dfcols=list(df)
1313
1313
  for c in dfcols:
1314
- ccn=codetranslate(c)+'('+c+')'
1314
+ ccn=ticker_name(c)+'('+c+')'
1315
1315
  df.rename(columns={c:ccn},inplace=True)
1316
1316
 
1317
1317
  if sortby=='tpw_mean':
@@ -1433,7 +1433,7 @@ def compare_1security_mrar(ticker,rar_names,start,end, \
1433
1433
  import datetime; today = datetime.date.today()
1434
1434
  y_label="风险调整收益指标"
1435
1435
  x_label="数据来源: 综合新浪/stooq/Yahoo,"+str(today)
1436
- title_txt="证券风险调整收益滚动指标:"+codetranslate(tickers[0])
1436
+ title_txt="证券风险调整收益滚动指标:"+ticker_name(tickers[0])
1437
1437
 
1438
1438
  # 是否绘图
1439
1439
  if graph:
@@ -50,7 +50,9 @@ if __name__=='__main__':
50
50
  sortino1=get_rolling_sharpe_sortino(ticker,start,end,rar_name="sortino",ret_type="Monthly Ret%",RF=0)
51
51
  sortino2=get_rolling_sharpe_sortino(ticker,start,end,rar_name="sortino",ret_type="Monthly Ret%",RF=0.01759)
52
52
 
53
- def get_rolling_sharpe_sortino(ticker,start,end,rar_name="sharpe",ret_type="Monthly Ret%",RF=0,source='auto'):
53
+ def get_rolling_sharpe_sortino(ticker,start,end,rar_name="sharpe", \
54
+ ret_type="Monthly Ret%",RF=0,source='auto', \
55
+ ticker_type='auto'):
54
56
  """
55
57
  功能:获取一只股票的夏普比率或索替诺比率,基于给定的滚动收益率类型,在指定期间内
56
58
  支持股票和投资组合
@@ -80,7 +82,9 @@ def get_rolling_sharpe_sortino(ticker,start,end,rar_name="sharpe",ret_type="Mont
80
82
 
81
83
  #抓取股价
82
84
  #pricedf=get_price(ticker,start1,end,source=source)
83
- pricedf=get_price_security(ticker,start1,end,source=source)
85
+ #pricedf=get_price_security(ticker,start1,end,source=source)
86
+ pricedf,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1, \
87
+ todate=end,source=source,ticker_type=ticker_type)
84
88
 
85
89
  #计算收益率和收益率标准差
86
90
  rardf1=calc_daily_return(pricedf)
@@ -148,7 +152,8 @@ if __name__=='__main__':
148
152
  sharpe2=get_expanding_sharpe_sortino(ticker,start,end,rar_name="sharpe",RF=0.01759)
149
153
  sortino2=get_expanding_sharpe_sortino(ticker,start,end,rar_name="sortino",RF=0.01759)
150
154
 
151
- def get_expanding_sharpe_sortino(ticker,start,end,rar_name="sharpe",ret_type="Exp Ret%",RF=0,source='auto'):
155
+ def get_expanding_sharpe_sortino(ticker,start,end,rar_name="sharpe", \
156
+ ret_type="Exp Ret%",RF=0,source='auto',ticker_type='auto'):
152
157
  """
153
158
  功能:获取一只股票的夏普比率或索替诺比率,基于扩展收益率,在指定期间内
154
159
  支持股票和投资组合
@@ -161,7 +166,9 @@ def get_expanding_sharpe_sortino(ticker,start,end,rar_name="sharpe",ret_type="Ex
161
166
 
162
167
  #抓取股价
163
168
  #pricedf=get_price(ticker,start1,end,source=source)
164
- pricedf=get_price_security(ticker,start1,end,source=source)
169
+ #pricedf=get_price_security(ticker,start1,end,source=source)
170
+ pricedf,found=get_price_1ticker_mixed(ticker=ticker,fromdate=start1, \
171
+ todate=end,source=source,ticker_type=ticker_type)
165
172
 
166
173
  #计算收益率和收益率标准差
167
174
  rardf2=calc_expanding_return(pricedf,start)
@@ -236,7 +243,7 @@ if __name__=='__main__':
236
243
 
237
244
  def get_rolling_treynor_alpha(ticker,start,end,rar_name="alpha", \
238
245
  ret_type="Monthly Ret%",RF=0, \
239
- regression_period=365,mktidx='auto',source='auto'):
246
+ regression_period=365,mktidx='auto',source='auto',ticker_type='auto'):
240
247
  """
241
248
  功能:获取一只股票的特雷诺比率或阿尔法指数,基于给定的滚动收益率类型,在指定期间内
242
249
  支持股票和投资组合
@@ -273,7 +280,8 @@ def get_rolling_treynor_alpha(ticker,start,end,rar_name="alpha", \
273
280
 
274
281
  #CAPM回归,计算贝塔系数
275
282
  reg_result,dretdf3=regression_capm(ticker,start2,end,ret_type=ret_type,RF=RF, \
276
- regtrddays=regtrddays,mktidx=mktidx,source=source)
283
+ regtrddays=regtrddays,mktidx=mktidx, \
284
+ source=source,ticker_type=ticker_type)
277
285
 
278
286
  #计算股票和指数的滚动收益率
279
287
  varx=ret_type+'_x'
@@ -340,7 +348,7 @@ if __name__=='__main__':
340
348
 
341
349
  def get_expanding_treynor_alpha(ticker,start,end,rar_name="alpha", \
342
350
  ret_type="Exp Ret%",RF=0, \
343
- regression_period=365,mktidx='auto',source='auto'):
351
+ regression_period=365,mktidx='auto',source='auto',ticker_type='auto'):
344
352
  """
345
353
  功能:获取一只股票的特雷诺比率或阿尔法指数,基于扩展收益率类型,在指定期间内
346
354
  支持股票和投资组合
@@ -358,7 +366,8 @@ def get_expanding_treynor_alpha(ticker,start,end,rar_name="alpha", \
358
366
 
359
367
  #CAPM回归,计算贝塔系数
360
368
  reg_result,dretdf3=regression_capm(ticker,start2,end,ret_type=ret_type,RF=RF, \
361
- regtrddays=regtrddays,mktidx=mktidx,source=source)
369
+ regtrddays=regtrddays,mktidx=mktidx, \
370
+ source=source,ticker_type=ticker_type)
362
371
 
363
372
  #计算股票和指数的扩展收益率
364
373
  varx=ret_type+'_x'
@@ -443,7 +452,7 @@ if __name__=='__main__':
443
452
 
444
453
 
445
454
  def get_rar(ticker,start,end,rar_name="sharpe",ret_type="Monthly Ret%", \
446
- RF=0,regression_period=365,mktidx='auto',source='auto'):
455
+ RF=0,regression_period=365,mktidx='auto',source='auto',ticker_type='auto'):
447
456
  """
448
457
  功能:获取一只股票的收益-风险性价比指标,在指定期间内,支持股票和投资组合
449
458
  支持滚动收益率和扩展收益率
@@ -463,25 +472,27 @@ def get_rar(ticker,start,end,rar_name="sharpe",ret_type="Monthly Ret%", \
463
472
  if ('sharpe' in rar_name_lower) or ('sortino' in rar_name_lower):
464
473
  rardf=get_rolling_sharpe_sortino(ticker=ticker,start=start,end=end, \
465
474
  rar_name=rar_name_lower, \
466
- ret_type=ret_type_title,RF=RF,source=source)
475
+ ret_type=ret_type_title,RF=RF, \
476
+ source=source,ticker_type=ticker_type)
467
477
  elif ('alpha' in rar_name_lower) or ('treynor' in rar_name_lower):
468
478
  rardf=get_rolling_treynor_alpha(ticker=ticker,start=start,end=end, \
469
479
  rar_name=rar_name_lower, \
470
480
  ret_type=ret_type_title,RF=RF, \
471
481
  regression_period=regression_period, \
472
- mktidx=mktidx,source=source)
482
+ mktidx=mktidx,source=source,ticker_type=ticker_type)
473
483
 
474
484
  else:
475
485
  if ('sharpe' in rar_name_lower) or ('sortino' in rar_name_lower):
476
486
  rardf=get_expanding_sharpe_sortino(ticker=ticker,start=start,end=end, \
477
487
  rar_name=rar_name_lower, \
478
- ret_type=ret_type_title,RF=RF,source=source)
488
+ ret_type=ret_type_title,RF=RF, \
489
+ source=source,ticker_type=ticker_type)
479
490
  elif ('alpha' in rar_name_lower) or ('treynor' in rar_name_lower):
480
491
  rardf=get_expanding_treynor_alpha(ticker=ticker,start=start,end=end, \
481
492
  rar_name=rar_name_lower, \
482
493
  ret_type=ret_type_title,RF=RF, \
483
494
  regression_period=regression_period, \
484
- mktidx=mktidx,source=source)
495
+ mktidx=mktidx,source=source,ticker_type=ticker_type)
485
496
 
486
497
  return rardf
487
498
 
@@ -508,7 +519,7 @@ def compare_1ticker_mrar(ticker,start,end,rar=['sharpe','sortino','treynor','alp
508
519
  ret_type="Annual Ret%",RF=0,regression_period=365, \
509
520
  graph=True,axhline_value=0,axhline_label='', \
510
521
  printout=False,sortby='tpw_mean',trailing=7,trend_threshhold=0.01, \
511
- annotate=False,mktidx='auto',source='auto'):
522
+ annotate=False,mktidx='auto',source='auto',ticker_type='auto'):
512
523
  """
513
524
  功能:一只股票,对比其多个rar,支持股票和投资组合
514
525
  """
@@ -534,8 +545,11 @@ def compare_1ticker_mrar(ticker,start,end,rar=['sharpe','sortino','treynor','alp
534
545
  if isinstance(regression_period,list):
535
546
  regression_period=regression_period[0]
536
547
 
537
- tname=ticker_name(ticker)
548
+ tname=ticker_name(ticker,ticker_type)
538
549
  print(" Starting to retrive and calculate different rar for",tname,"\b, please wait ......")
550
+
551
+ #预处理ticker_type
552
+ ticker_type=ticker_type_preprocess_mticker_mixed(ticker,ticker_type)
539
553
 
540
554
  df=pd.DataFrame()
541
555
  for t in rar:
@@ -543,7 +557,7 @@ def compare_1ticker_mrar(ticker,start,end,rar=['sharpe','sortino','treynor','alp
543
557
  with HiddenPrints():
544
558
  df_tmp=get_rar(ticker,start,end,t,ret_type=ret_type, \
545
559
  RF=RF,regression_period=regression_period, \
546
- mktidx=mktidx,source=source)
560
+ mktidx=mktidx,source=source,ticker_type=ticker_type)
547
561
 
548
562
  if df_tmp is None:
549
563
  break
@@ -649,7 +663,7 @@ def compare_mticker_1rar(ticker,start,end,rar='sharpe', \
649
663
  graph=True,axhline_value=0,axhline_label='', \
650
664
  printout=False,sortby='tpw_mean',trailing=7,trend_threshhold=0.01, \
651
665
  annotate=False,mktidx='auto',source='auto', \
652
- style_print=True):
666
+ style_print=True,ticker_type='auto'):
653
667
  """
654
668
  功能:多只股票,对比其同一个rar,支持股票和投资组合
655
669
  """
@@ -677,19 +691,24 @@ def compare_mticker_1rar(ticker,start,end,rar='sharpe', \
677
691
  regression_period=regression_period[0]
678
692
  print(" Starting to retrive and calculate",rar,"\b, please wait ......")
679
693
 
694
+ #预处理ticker_type
695
+ ticker_type_list=ticker_type_preprocess_mticker_mixed(ticker,ticker_type)
696
+
680
697
  df=pd.DataFrame()
681
698
  for t in ticker:
699
+ pos=ticker.index(t)
700
+ tt=ticker_type_list[pos]
682
701
  #关闭print输出
683
702
  with HiddenPrints():
684
703
  df_tmp=get_rar(t,start,end,rar_name=rar,ret_type=ret_type, \
685
704
  RF=RF,regression_period=regression_period, \
686
- mktidx=mktidx,source=source)
705
+ mktidx=mktidx,source=source,ticker_type=tt)
687
706
 
688
707
  if df_tmp is None:
689
708
  break
690
709
  else:
691
710
  dft=df_tmp[[rar]]
692
- dft.rename(columns={rar:ticker_name(t)},inplace=True)
711
+ dft.rename(columns={rar:ticker_name(t,tt)},inplace=True)
693
712
 
694
713
  if len(df)==0: #第一个
695
714
  df=dft
@@ -697,7 +716,7 @@ def compare_mticker_1rar(ticker,start,end,rar='sharpe', \
697
716
  df=pd.merge(df,dft,how='outer',left_index=True,right_index=True)
698
717
 
699
718
  if len(df)==0:
700
- print(" #Error(compare_mticker_1rar): rar data not available for",ticker_name(t),"between",start,end)
719
+ print(" #Error(compare_mticker_1rar): rar data not available for",ticker_name(t,tt),"between",start,end)
701
720
  return None
702
721
 
703
722
  #仅用于绘图和制表
@@ -705,7 +724,7 @@ def compare_mticker_1rar(ticker,start,end,rar='sharpe', \
705
724
  for c in list(df1):
706
725
  if df1[c].max() > axhline_value and df1[c].min() < axhline_value:
707
726
  axhline_label='零线' #显示零线,但不标注图例
708
- #df1.rename(columns={c:codetranslate(c)},inplace=True)
727
+ #df1.rename(columns={c:ticker_name(c)},inplace=True)
709
728
 
710
729
  #共同脚注
711
730
  footnote1=ectranslate(rar)+"基于"+ectranslate(ret_type)+"。"
@@ -785,7 +804,7 @@ def compare_mticker_mrar(ticker,start,end,rar=['sharpe','alpha','sortino','treyn
785
804
  ret_type="Annual Ret%",RF=0,regression_period=365, \
786
805
  graph=True,axhline_value=0,axhline_label='', \
787
806
  printout=True,sortby='tpw_mean',trailing=7,trend_threshhold=0.01, \
788
- annotate=False,mktidx='auto',source='auto'):
807
+ annotate=False,mktidx='auto',source='auto',ticker_type='auto'):
789
808
  """
790
809
  功能:多只股票,多个rar,综合对比和排列。支持股票和投资组合
791
810
  """
@@ -809,7 +828,7 @@ def compare_mticker_mrar(ticker,start,end,rar=['sharpe','alpha','sortino','treyn
809
828
  ret_type,RF,regression_period, \
810
829
  graph,axhline_value,axhline_label, \
811
830
  printout,sortby,trailing,trend_threshhold, \
812
- annotate,mktidx,source,style_print=True)
831
+ annotate,mktidx,source,style_print=True,ticker_type=ticker_type)
813
832
  if df_tmp is None:
814
833
  break
815
834
  else:
@@ -937,7 +956,7 @@ def compare_1ticker_1rar_mret(ticker,start,end,rar='sharpe', \
937
956
  RF=0,regression_period=365, \
938
957
  graph=True,axhline_value=0,axhline_label='', \
939
958
  printout=False,sortby='tpw_mean',trailing=7,trend_threshhold=0.01, \
940
- annotate=False,mktidx='auto',source='auto'):
959
+ annotate=False,mktidx='auto',source='auto',ticker_type='auto'):
941
960
  """
942
961
  功能:一只股票,同一个rar,对比其不同的收益率类型,支持股票和投资组合
943
962
  """
@@ -963,14 +982,15 @@ def compare_1ticker_1rar_mret(ticker,start,end,rar='sharpe', \
963
982
  RF=RF[0]
964
983
  if isinstance(regression_period,list):
965
984
  regression_period=regression_period[0]
966
- print(" Starting to retrive and calculate",rar,"for",ticker_name(ticker),"on different types of return, please wait ......")
985
+ print(" Starting to retrive and calculate",rar,"for",ticker_name(ticker,ticker_type),"on different types of return, please wait ......")
967
986
 
968
987
  df=pd.DataFrame()
969
988
  for t in ret_type:
970
989
  #关闭print输出
971
990
  with HiddenPrints():
972
991
  df_tmp=get_rar(ticker,start,end,rar,ret_type=t, \
973
- RF=RF,regression_period=regression_period,mktidx=mktidx,source=source)
992
+ RF=RF,regression_period=regression_period,mktidx=mktidx, \
993
+ source=source,ticker_type=ticker_type)
974
994
 
975
995
  if df_tmp is None:
976
996
  break
@@ -984,7 +1004,7 @@ def compare_1ticker_1rar_mret(ticker,start,end,rar='sharpe', \
984
1004
  df=pd.merge(df,dft,how='outer',left_index=True,right_index=True)
985
1005
 
986
1006
  if len(df)==0:
987
- print(" #Error(compare_mticker_1rar): rar data not available for",ticker_name(ticker),"between",start,end)
1007
+ print(" #Error(compare_mticker_1rar): rar data not available for",ticker_name(ticker,ticker_type),"between",start,end)
988
1008
  return None
989
1009
 
990
1010
  #仅用于绘图和制表
@@ -1015,7 +1035,7 @@ def compare_1ticker_1rar_mret(ticker,start,end,rar='sharpe', \
1015
1035
  #绘图
1016
1036
  if graph:
1017
1037
 
1018
- title_txt="风险调整收益:"+ticker_name(ticker)
1038
+ title_txt="风险调整收益:"+ticker_name(ticker,ticker_type)
1019
1039
  y_label=ectranslate(rar)
1020
1040
 
1021
1041
  draw_lines(df1,y_label,x_label=footnotex, \
@@ -1036,7 +1056,7 @@ def compare_1ticker_1rar_mret(ticker,start,end,rar='sharpe', \
1036
1056
  elif sortby=='trailing':
1037
1057
  sortby_txt='按推荐标记+短期均值走势降序排列'
1038
1058
 
1039
- title_txt='***** 风险调整收益评估:'+'基于'+ectranslate(rar)+','+ticker_name(ticker)+','+sortby_txt+' *****'
1059
+ title_txt='***** 风险调整收益评估:'+'基于'+ectranslate(rar)+','+ticker_name(ticker,ticker_type)+','+sortby_txt+' *****'
1040
1060
 
1041
1061
  footnote6='期间范围:'+str(start)+'至'+str(end)+";近期范围:近"+str(trailing)+"个交易日。趋势变化率阈值:"+str(trend_threshhold)+"。"
1042
1062
  footnote7="近期趋势和星号为多项因素综合研判,最多五颗星星"
@@ -1077,7 +1097,7 @@ def compare_1ticker_1rar_1ret_mRF(ticker,start,end,rar='sharpe', \
1077
1097
  regression_period=365, \
1078
1098
  graph=True,axhline_value=0,axhline_label='', \
1079
1099
  printout=False,sortby='tpw_mean',trailing=7,trend_threshhold=0.01, \
1080
- annotate=False,mktidx='auto',source='auto'):
1100
+ annotate=False,mktidx='auto',source='auto',ticker_type='auto'):
1081
1101
  """
1082
1102
  功能:一只股票,相同的rar,相同的收益率类型,不同的无风险收益率
1083
1103
  支持股票和投资组合
@@ -1104,14 +1124,15 @@ def compare_1ticker_1rar_1ret_mRF(ticker,start,end,rar='sharpe', \
1104
1124
  RF=[RF]
1105
1125
  if isinstance(regression_period,list):
1106
1126
  regression_period=regression_period[0]
1107
- print(" Starting to retrive and calculate",rar,"for",ticker_name(ticker),"on different RF, please wait ......")
1127
+ print(" Starting to retrive and calculate",rar,"for",ticker_name(ticker,ticker_type),"on different RF, please wait ......")
1108
1128
 
1109
1129
  df=pd.DataFrame()
1110
1130
  for t in RF:
1111
1131
  #关闭print输出
1112
1132
  with HiddenPrints():
1113
1133
  df_tmp=get_rar(ticker,start,end,rar,ret_type, \
1114
- RF=t,regression_period=regression_period,mktidx=mktidx,source=source)
1134
+ RF=t,regression_period=regression_period,mktidx=mktidx, \
1135
+ source=source,ticker_type=ticker_type)
1115
1136
 
1116
1137
  if df_tmp is None:
1117
1138
  break
@@ -1125,7 +1146,7 @@ def compare_1ticker_1rar_1ret_mRF(ticker,start,end,rar='sharpe', \
1125
1146
  df=pd.merge(df,dft,how='outer',left_index=True,right_index=True)
1126
1147
 
1127
1148
  if len(df)==0:
1128
- print(" #Error(compare_mticker_1rar): rar data inaccessible for",ticker_name(ticker),"between",start,end)
1149
+ print(" #Error(compare_mticker_1rar): rar data inaccessible for",ticker_name(ticker,ticker_type),"between",start,end)
1129
1150
  return None
1130
1151
 
1131
1152
  #仅用于绘图和制表
@@ -1153,7 +1174,7 @@ def compare_1ticker_1rar_1ret_mRF(ticker,start,end,rar='sharpe', \
1153
1174
  #绘图
1154
1175
  if graph:
1155
1176
 
1156
- title_txt="风险调整收益:"+ticker_name(ticker)
1177
+ title_txt="风险调整收益:"+ticker_name(ticker,ticker_type)
1157
1178
  y_label=ectranslate(rar)
1158
1179
 
1159
1180
  draw_lines(df1,y_label,x_label=footnotex, \
@@ -1174,7 +1195,7 @@ def compare_1ticker_1rar_1ret_mRF(ticker,start,end,rar='sharpe', \
1174
1195
  elif sortby=='trailing':
1175
1196
  sortby_txt='按推荐标记+短期均值走势降序排列'
1176
1197
 
1177
- title_txt='***** 风险调整收益评估:'+'基于'+ectranslate(rar)+','+ticker_name(ticker)+','+sortby_txt+' *****'
1198
+ title_txt='***** 风险调整收益评估:'+'基于'+ectranslate(rar)+','+ticker_name(ticker,ticker_type)+','+sortby_txt+' *****'
1178
1199
 
1179
1200
  footnote6='期间范围:'+str(start)+'至'+str(end)+";近期范围:近"+str(trailing)+"个交易日。趋势变化率阈值:"+str(trend_threshhold)+"。"
1180
1201
  footnote7="近期趋势和星号为多项因素综合研判,最多五颗星星"
@@ -1228,7 +1249,8 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1228
1249
  regression_period=365, \
1229
1250
  graph=True,axhline_value=0,axhline_label='', \
1230
1251
  printout=False,sortby='tpw_mean',trailing=7,trend_threshhold=0.05, \
1231
- annotate=False,mktidx='auto',source='auto'):
1252
+ annotate=False,mktidx='auto',source='auto',
1253
+ ticker_type='auto'):
1232
1254
  """
1233
1255
  功能:组合情况,可能多只股票,多个rar,多个收益率类型,多个无风险收益率
1234
1256
 
@@ -1255,7 +1277,7 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1255
1277
  ret_type,RF,regression_period, \
1256
1278
  graph,axhline_value,axhline_label, \
1257
1279
  printout,sortby,trailing,trend_threshhold, \
1258
- annotate,mktidx,source)
1280
+ annotate,mktidx,source,ticker_type=ticker_type)
1259
1281
  return df
1260
1282
 
1261
1283
  if rar_num >1: #多个RAR
@@ -1264,7 +1286,7 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1264
1286
  ret_type=ret_type,RF=RF,regression_period=regression_period, \
1265
1287
  graph=graph,axhline_value=axhline_value,axhline_label=axhline_label, \
1266
1288
  printout=printout,sortby=sortby,trailing=trailing,trend_threshhold=trend_threshhold, \
1267
- annotate=annotate,mktidx=mktidx,source=source)
1289
+ annotate=annotate,mktidx=mktidx,source=source,ticker_type=ticker_type)
1268
1290
  return df
1269
1291
  else:
1270
1292
  #实际上是单个证券
@@ -1282,7 +1304,7 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1282
1304
  ret_type,RF,regression_period, \
1283
1305
  graph,axhline_value,axhline_label, \
1284
1306
  printout,sortby,trailing,trend_threshhold, \
1285
- annotate,mktidx,source)
1307
+ annotate,mktidx,source,ticker_type=ticker_type)
1286
1308
  return df
1287
1309
  else:
1288
1310
  #实际上是单个RAR
@@ -1298,7 +1320,7 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1298
1320
  ret_type,RF,regression_period, \
1299
1321
  graph,axhline_value,axhline_label, \
1300
1322
  printout,sortby,trailing,trend_threshhold, \
1301
- annotate,mktidx,source)
1323
+ annotate,mktidx,source,ticker_type=ticker_type)
1302
1324
  return df
1303
1325
  else:
1304
1326
  #实际上是单个收益率类型
@@ -1314,7 +1336,7 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1314
1336
  regression_period, \
1315
1337
  graph,axhline_value,axhline_label, \
1316
1338
  printout,sortby,trailing,trend_threshhold, \
1317
- annotate,mktidx,source)
1339
+ annotate,mktidx,source,ticker_type=ticker_type)
1318
1340
  return df
1319
1341
  else:
1320
1342
  #实际上是单个RF
@@ -1325,7 +1347,7 @@ def compare_rar_security(ticker,start,end,rar='sharpe', \
1325
1347
  ret_type,RF,regression_period, \
1326
1348
  graph,axhline_value,axhline_label, \
1327
1349
  printout,sortby,trailing,trend_threshhold, \
1328
- annotate,mktidx,source)
1350
+ annotate,mktidx,source,ticker_type=ticker_type)
1329
1351
  return df
1330
1352
 
1331
1353