 # Is it possible to get 5-year average Dividend Yield using zipline in QuantRocket?

I am considering to purchase quantrocket software. But I have a problem as follows.
I have a rebalance strategy , which rebalances a portfolio every week.
On the rebalance day, for each stock, I need it's average Dividend Yield within last 5 years.That means we need its every day's Dividend Yield within last 5 years, so that we can compute this 5-year average.
I wonder if it's possible to get this 5-year average Dividend Yield using zipline in QuantRocket?

In Sharadar's fundationmental dataset, I see DIVYIELD (Dividend Yield measures the ratio between a company's [DPS] and its [PRICE]),
I guess we can get 5-year average Dividend Yield through this factor in zipline. And I also want to get the quantiles of Dividend Yield within last 5 years, is it possible using Sharadar's fundationmental dataset to get it?
Could anyone assure this?

This can be done in Pipeline using the `period_offset` parameter:

``````from zipline.pipeline.data.sharadar import Fundamentals

divyield_0 = Fundamentals.slice(dimension="ARY", period_offset=0).DIVYIELD.latest
divyield_1 = Fundamentals.slice(dimension="ARY", period_offset=-1).DIVYIELD.latest
divyield_2 = Fundamentals.slice(dimension="ARY", period_offset=-2).DIVYIELD.latest
divyield_3 = Fundamentals.slice(dimension="ARY", period_offset=-3).DIVYIELD.latest
divyield_4 = Fundamentals.slice(dimension="ARY", period_offset=-4).DIVYIELD.latest

divyield_avg = (divyield_0 + divyield_1 + divyield_2 + divyield_3 + divyield_4) / 5

divyield_avg_quantiles = divyield_avg.quantiles(5)
``````

In QuantRocket version 2.9 (not yet released) this will become even easier with some new built-in factors for working with periodic data like Sharadar fundamentals:

``````from zipline.pipeline.periodic import PeriodicAverage

divyield = Fundamentals.slice('ARY').DIVYIELD
divyield_avg = PeriodicAverage(divyield, window_length=5)
...
``````

Thank you very much @Brian. Maybe I didn't express my scenario clearly.
By saying 5-year average Dividend Yield, it doesn't mean the average of just 5 data points. to put it another way, for example, I need average Dividend Yield for last 450 days. Then it means we need 450 data points to compute this average. I wonder how to get it in zipline backtesting enviroment (not research enviroment)?

By definition, DIVYIELD (Dividend Yield measures the ratio between a company's [DPS] and its [PRICE]). It varies every day, because the price varies every day.

1 After some study, I guess the DIVYIELD doesn't change every day, it is based on the price of date DATAKEY. am I right? and how to get this DATAKEY in zipline?

2 So, in order to get everyday dividendRatio in zipline pipline, I do the folloing:

``````DIVYIELD = sharadar.Fundamentals.slice(dimension='ART', period_offset=0).DIVYIELD.latest

close = USEquityPricing.close.latest

dividendRatio = DPS / close
pipe = Pipeline(
columns={
'DIVYIELD': DIVYIELD,
'DPS': DPS,
'close': close,
'dividendRatio':dividendRatio,
},

)
res = run_pipeline(pipe, start_date='2016-01-04', end_date='2023-02-23', bundle='usstock-1d-bundle')
print("There are %d assets in this universe." % len(res))

``````

I can get dividendRatio for each day now. Is this solution correct?
next step, I will use this dividendRatios to compute e.g, 400 day average.

This is the right idea but it will yield incorrect results when stock splits have occurred because the fundamental fields like DPS are fully split-adjusted while the bundle data is point-in-time split-adjusted, that is, only adjusted for splits that have occurred up to the current end date of the running Pipeline. This distinction is explained in the docs.

A better solution that avoids this problem is to collect the history db (usstock or sharadar) and import the close price into Pipeline as a custom database. The history databases are fully split-adjusted, like the fundamental data, so you can combine them without a problem:

``````from zipline.pipeline.data.db import Database, Column

Close = Column(float)

``````

good.
So, is the following the correct method to compute dynamic PB(Price to Book Value) ?

``````    class FullyAdjustedEquityPricing(Database):
Close = Column(float)