JoinQuant 近来新增了国泰安的数据,其中包含分红数据,看到有用户获取股息率的需求,现在小编便教大家使用分红数据计算股息率。
股息率的计算公式:
股息率=每股分红(税前)股票当前价×100%股息率=每股分红(税前)股票当前价×100%
股息率=每股分红(税前)股票当前价×100%
股息率 = \frac{每股分红(税前)}{股票当前价}\times 100\%小编定义了 dividend_yield_ratio 函数,可以取到股息率(报告期),并跟Wind进行了对比,结果是一样的。
现在你可以拷贝走直接放在自己的策略中调用,详情参见下面分享的研究模块:
获取股息率¶
JoinQuant 近来新增了国泰安的数据,其中包含分红数据,看到有用户获取股息率的需求,现在小编便教大家使用分红数据计算股息率。
股息率的计算公式:
$$ 股息率 = \frac{每股分红(税前)}{股票当前价}\times 100\% $$
下面定义了 dividend_yield_ratio 函数,可以取到股息率(报告期),具体用法见函数说明:
def dividend_yield_ratio(stock, start_date=None, count=None, date=None, end_date=None, paymentdate=2015, no_data_return=NaN, skip_paused=False):''' stock: 股票代码 start_date: (start_date 与 count二选一)获取股息率开始日期 count: (start_date 与 count二选一)指定获取之前几个交易日 end_date: 获取股息率结束日期 paymentdate: 报表年度, 默认报告年底2015年 no_data_return: 选填 NaN 或 0,决定没有股息率的股票返回的数据是 NaN 或 0 skip_paused: 跳过停牌,默认不跳过 '''def get_div(df, paymentdate):import pandas as pddf.PAYMENTDATE = pd.to_datetime(df.PAYMENTDATE)try:if len(df)>0:div = df[[x.year == (int(paymentdate)+1) for x in df.PAYMENTDATE]]['DIVIDENTBT'].iloc[-1]return float(div)else:return no_data_returnexcept:return no_data_return# 导入jqdata库from jqdata import gta# 获取股票六位代码symbol = stock[:6]# 获取df = gta.run_query(query(gta.STK_MKT_DIVIDENT.SYMBOL,gta.STK_MKT_DIVIDENT.PAYMENTDATE,gta.STK_MKT_DIVIDENT.DIVIDENTBT,).filter(gta.STK_MKT_DIVIDENT.SYMBOL.in_([symbol])).order_by(gta.STK_MKT_DIVIDENT.PAYMENTDATE))div = get_div(df, paymentdate)# 获取股票最近几天的价格price = get_price(stock, start_date=start_date, count=count, end_date=end_date, \ fields=['close'], frequency='daily', fq=None)# 计算股息率price['close'] = float(div)/price['close']*100# 将股息率精确到小数点后4位price.close = price.close.round(4)# 返回结果return price
以平安银行('000001.XSHE')为例,讲解一下计算方法:
security = '000001.XSHE'count = 3starttime = '2016-07-19'endtime = '2016-07-21'paymentdate = 2015test_dyr = dividend_yield_ratio(stock=security, start_date=None, count=count, end_date=endtime,\paymentdate=paymentdate, no_data_return=0)test_dyr
close | |
---|---|
2016-07-19 | 1.7057 |
2016-07-20 | 1.7076 |
2016-07-21 | 1.7019 |
国农科技('000004.XSHE')如下:
security = '000004.XSHE'count = 3starttime = '2016-07-19'endtime = '2016-07-21'paymentdate = 2015test_dyr_0 = dividend_yield_ratio(stock=security, start_date=None, count=count, end_date=endtime,\paymentdate=paymentdate, no_data_return=0)test_dyr_0
close | |
---|---|
2016-07-19 | 0 |
2016-07-20 | 0 |
2016-07-21 | 0 |
也可指定返回值为 NaN
,只需指定 no_data_return=NaN
, 方法如下:
test_dyr_nan = dividend_yield_ratio(stock=security, start_date=None, count=count, end_date=endtime,\paymentdate=paymentdate, no_data_return=NaN)test_dyr_nan
close | |
---|---|
2016-07-19 | NaN |
2016-07-20 | NaN |
2016-07-21 | NaN |
也许你会怀疑小编计算的方法对不对,请看下图,内容截自Wind:
股息率(报告期),7月19-7月21日,报告年度2015
再来一张报告年度为 2014 年的进行测试一下:
股息率(报告期),7月19-7月21日,报告年度2014
平安银行:¶
security = '000001.XSHE'count = 3starttime = '2016-07-19'endtime = '2016-07-21'paymentdate = 2014test_dyr = dividend_yield_ratio(stock=security, start_date=None, count=count, end_date=endtime,\paymentdate=paymentdate, no_data_return=0)test_dyr
close | |
---|---|
2016-07-19 | 1.9398 |
2016-07-20 | 1.9420 |
2016-07-21 | 1.9355 |
万科A:¶
security = '000002.XSHE'count = 3starttime = '2016-07-19'endtime = '2016-07-21'paymentdate = 2014test_dyr = dividend_yield_ratio(stock=security, start_date=None, count=count, end_date=endtime,\paymentdate=paymentdate, no_data_return=0)test_dyr
close | |
---|---|
2016-07-19 | 2.9223 |
2016-07-20 | 2.9274 |
2016-07-21 | 2.9377 |
测试结果是一样的,小伙伴们可以放心的使用了!