首页>文档>Python>【Python 入门策略(三)】股息率策略

【Python 入门策略(三)】股息率策略

“股息率”策略

扇贝可以游走,存款可以消失,编不出理由?别怕,还有商誉减值和应收账款计提。 由于上市公司财技花样繁多,因此如果要抛开财务报表选择一个指标来衡量一家公司的好坏。 那么股息率是一个不会让你失望的选择!

因为现金才能给人安全感!

一个好的投资策略,最重要的应该包含两点:

  1. 选股
  2. 仓位

股息率指标天然的完成了这两点,这个策略长这样:

  1. 选出股息大于6%,营收增长率大于5%的股票加入股票池(未来可将6%改为分级A最高隐含收益率)
  2. 股票大于10支就等比例满仓,股票小于10支,就每支股票10%剩下的买固定收益(仓位控制)

最后呢,由于股息发放的时间不是固定的,我们用一个 dataframe 更新和维护股息信息,这样如果今年还没有发放股息,需要用去年的股息来覆盖 先用 get_fundamentals 拿到今年至今为止的股息率情况,存起来。

 fundamental_df = get_fundamentals(
        query(
            fundamentals.eod_derivative_indicator.dividend_yield,
            fundamentals.financial_indicator.inc_operating_revenue,
            fundamentals.eod_derivative_indicator.market_cap
        ).filter(
            fundamentals.financial_indicator.inc_operating_revenue >5
        ).filter(
            fundamentals.eod_derivative_indicator.dividend_yield > 4.5
        )

        .order_by(
            fundamentals.eod_derivative_indicator.dividend_yield .desc()
        ).limit(
            num_stocks
        )
    )

接下来创建或者维护 一个 dataframe,计算每家家公司的股息,这样如果到了第二年还没有发放股息,去年的股息就被存下来了,如果发放了就被更新了。

 li=list(fundamental_df.columns.values)
    if context.flag:
        dividend = []
        for stock in li:
          di = float(fundamental_df[stock]['dividend_yield'])/100
          di = di*float(fundamental_df[stock]['market_cap'])
          dividend.append([di])
        div=np.array(dividend)
        context.df = pd.DataFrame(div.T,index=['dividend'],columns=li)
        logger.info(context.df)
        context.flag = False
    else:
        li2 = list(context.df.columns.values)
        for stock in li:
            if stock not in li2:
                di = float(fundamental_df[stock]['dividend_yield'])/100
                di = di*float(fundamental_df[stock]['market_cap'])
                context.df.insert(0,stock,[di])
            else:
                di = float(fundamental_df[stock]['dividend_yield'])/100
                di = di*float(fundamental_df[stock]['market_cap'])
                context.df[stock]['dividend']=di
    #logger.info(context.df)

    stocks=context.df.columns.values

最后拿到 dataframe 里的股票数据,重新拿到市值并按 股息/市值 计算股息率 并排序。

fundamental_df = get_fundamentals(
    query(
        fundamentals.eod_derivative_indicator.market_cap,fundamentals.financial_indicator.inc_operating_revenue
    ).filter(
        fundamentals.financial_indicator.inc_operating_revenue >5
    ).filter(
        fundamentals.income_statement.stockcode.in_(stocks)
    )
)
stocks=fundamental_df.columns.values

dividend_yield=[]
for stock in stocks:
    rate=context.df[stock]['dividend']/float(fundamental_df[stock]['market_cap'])
    dividend_yield.append(rate)

df = pd.DataFrame(dividend_yield,index=stocks,columns=['dividend_yield'])
df=df[df['dividend_yield']>0.06]
logger.info(df)

context.fundamental_df = fundamental_df

 context.stocks = df.T.columns.values

这样我们就做到了用去年保存下来的股息来代替今年可能是空白的股息了,不会错过任何一个对的人哦不是对的股票!收益图:

【Python 入门策略(三)】股息率策略

源代码如下:

# 可以自己import我们平台支持的第三方python模块,比如pandas、numpy等。
import pandas as pd
import numpy as np
import datetime
import math
import talib
CAP=0
OBSERVATION = 40
SMA5 = 5
SMA10=10
# 在这个方法中编写任何的初始化逻辑。context对象将会在你的算法策略的任何方法之间做传递。
def init(context):
    context.fja_list=['150283.XSHE','150249.XSHE','502007.XSHG','150259.XSHE','150217.XSHE','150245.XSHE','502049.XSHG','150241.XSHE','150231.XSHE','150257.XSHE','150169.XSHE','150177.XSHE','150243.XSHE','150329.XSHE','150051.XSHE','150179.XSHE','150186.XSHE','150255.XSHE','150171.XSHE','150315.XSHE','150227.XSHE','150018.XSHE','150237.XSHE','150235.XSHE','150279.XSHE','150305.XSHE','150269.XSHE','150181.XSHE','502004.XSHG','150229.XSHE','150173.XSHE','150277.XSHE','150200.XSHE','150209.XSHE','150194.XSHE','150273.XSHE','150184.XSHE','150205.XSHE','150309.XSHE','150275.XSHE']

    context.cur_stock=''
    update_universe(context.fja_list)
    scheduler.run_daily(rebalance)
    context.flag=True


# 你选择的证券的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新
def handle_bar(context, bar_dict):

    pass

def before_trading(context):




    num_stocks = 20

    #删选股票
    fundamental_df = get_fundamentals(
        query(
            fundamentals.eod_derivative_indicator.dividend_yield,
            fundamentals.financial_indicator.inc_operating_revenue,
            fundamentals.eod_derivative_indicator.market_cap
        ).filter(
            fundamentals.financial_indicator.inc_operating_revenue >5
        ).filter(
            fundamentals.eod_derivative_indicator.dividend_yield > 4.5
        )

        .order_by(
            fundamentals.eod_derivative_indicator.dividend_yield .desc()
        ).limit(
            num_stocks
        )
    )

    li=list(fundamental_df.columns.values)
    if context.flag:
        dividend = []
        for stock in li:
          di = float(fundamental_df[stock]['dividend_yield'])/100
          di = di*float(fundamental_df[stock]['market_cap'])
          dividend.append([di])
        div=np.array(dividend)
        context.df = pd.DataFrame(div.T,index=['dividend'],columns=li)
        logger.info(context.df)
        context.flag = False
    else:
        li2 = list(context.df.columns.values)
        for stock in li:
            if stock not in li2:
                di = float(fundamental_df[stock]['dividend_yield'])/100
                di = di*float(fundamental_df[stock]['market_cap'])
                
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧