请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  源码分享 帖子:3135801 新帖:22

准确有效计算A股股息率

adasda发表于:8 月 13 日 20:00回复(1)
import datetime
import pandas as pd
from jqdata import *
from jqfactor import get_factor_values
def get_dividend_rate(code_list,date=get_trade_days(start_date=None, end_date=datetime.datetime.now().date(), count=2)[-1]):
    '''date参数需传入datetime.date,默认为最新交易日'''
    if date < datetime.datetime.now().date():
        q_date=date
    else:
        q_date=get_trade_days(start_date=None, end_date=datetime.datetime.now().date(), count=2)[0]
    if type(code_list)!=list:
        code_list=code_list.split()
    ##计算各报告期的分红
    f=finance.STK_XR_XD
    q1=query(f.code,f.company_name,f.report_date,f.bonus_amount_rmb).filter(finance.STK_XR_XD.code.in_(code_list)).order_by(finance.STK_XR_XD.report_date.desc())
    df = finance.run_query(q1)
    df=df.fillna(0)
    #df['report_date']=df['report_date'].apply(lambda x:datetime.datetime.combine(x, datetime.time(0)))
    df.set_index(pd.to_datetime(df.report_date), inplace=True)#字符串到datetime
    df=df.groupby('code').resample('Y').sum()
    df=df.reset_index()
    df.columns=['code','year','bonus_amount_rmb']
    ##计算股票分红使用的报告期
    y1=str(q_date.year-2)
    y2=str(q_date.year-1)
    q2 = query(
            balance.statDate,
            balance.code,
            balance.pubDate,
        ).filter(
            balance.code.in_(code_list)
        )
    ret=get_fundamentals(q2,statDate=y1)
    ret2=get_fundamentals(q2, statDate=y2)
    x=ret.append(ret2)
    x.pubDate=pd.to_datetime(x.pubDate)
    x.set_index(pd.to_datetime(x.pubDate), inplace=True)#
    pub_df=x.groupby('code').max()
    pub_df.drop('statDate',axis=1,inplace=True)
    pub_df['year']=pub_df.applymap(lambda x: pd.Timestamp(y1+'-12-31') if q_date<=x.date() else pd.Timestamp(y2+'-12-31'))
    pub_df=pub_df.reset_index()
    ##计算市值数据
    q3 = query(
                valuation.day,
                valuation.code,
                valuation.market_cap,
            ).filter(
                balance.code.in_(code_list)
            )
    cap=get_fundamentals(q3,q_date)
    ##连接各表并计算股息率
    mix=pd.merge(df,pub_df)
    div_df=pd.merge(mix,cap)
    div_df['dividend_rate']=div_df.bonus_amount_rmb/div_df.market_cap/10000
    div_df.set_index('code', inplace=True)
    return div_df.dividend_rate
stocks = get_index_stocks('000300.XSHG')
HS300_divid=get_dividend_rate(stocks,datetime.date(2018,4,21))
HS300_divid
code
000001.XSHE    0.011982
000002.XSHE    0.026295
000063.XSHE    0.000000
000069.XSHE    0.012774
000100.XSHE    0.022453
000157.XSHE    0.048309
000166.XSHE    0.018936
000333.XSHE    0.023346
000338.XSHE    0.049080
000402.XSHE    0.032967
000408.XSHE    0.000000
000413.XSHE    0.009211
000415.XSHE    0.008562
000423.XSHE    0.015228
000425.XSHE    0.003788
000538.XSHE    0.015890
000553.XSHE    0.004330
000568.XSHE    0.020665
000596.XSHE    0.009860
000625.XSHE    0.042395
000627.XSHE    0.002862
000629.XSHE    0.000000
000630.XSHE    0.007782
000651.XSHE    0.037618
000656.XSHE    0.046642
000661.XSHE    0.004393
000671.XSHE    0.006897
000703.XSHE    0.004822
000709.XSHE    0.030581
000725.XSHE    0.006698
                 ...   
601808.XSHG    0.005282
601818.XSHG    0.045477
601828.XSHG    0.020189
601857.XSHG    0.017016
601877.XSHG    0.017097
601878.XSHG    0.008361
601881.XSHG    0.012000
601888.XSHG    0.009335
601898.XSHG    0.011081
601899.XSHG    0.021378
601901.XSHG    0.001739
601919.XSHG    0.000000
601933.XSHG    0.016413
601939.XSHG    0.039484
601985.XSHG    0.017638
601988.XSHG    0.046561
601989.XSHG    0.000000
601992.XSHG    0.010000
601997.XSHG    0.018233
601998.XSHG    0.040909
603019.XSHG    0.001745
603156.XSHG    0.031390
603160.XSHG    0.003806
603260.XSHG    0.007064
603288.XSHG    0.014134
603799.XSHG    0.000000
603833.XSHG    0.007374
603858.XSHG    0.033886
603986.XSHG    0.001914
603993.XSHG    0.009909
Name: dividend_rate, Length: 287, dtype: float64

全部回复

0/140

量化课程

    移动端课程