Want to compare the S&P500 dividend yield with stock list yield
Author: rmandel00
Creation Date: 2/26/2012 10:52 AM
profile picture

rmandel00

#1
I'm looking for importing the S&P average yield into a strategy and then have as one of the conditions of trade that the yield on the stock list have a higher dividend than the S&P500 average yield. I know how to use an external symbol stock price but not its fundamental data like its dividend yield.
Thanks for your help.
Rich Man
profile picture

Eugene

#2
Fundamental data type is exactly what an external's symbol stock price i.e. DataSeries. Check out Fundamental Data and FundamentalItem Object in the QuickRef.
profile picture

rmandel00

#3
Eugene,
I still don't understand how to do it. Maybe an example would help. I want to import the dividend yield of the S&P500 into the code. Thanks.
Rich MAn
profile picture

Cone

#4
Assuming a Fidelity customer...
Instead of calculating yield with the "cash dividends" fundamental series, which only gives you a rolling 24 quarters of data, I prefer to calculate using the "dividend" per share series, which comes with about 10 years of history.

The YieldAtBar routine tries to calculate the trailing annual yield by adding all the dividend distributions in the last year, and, if there hasn't been at least 4, then it looks back another 3 weeks to compensate for the fact that distributions don't always occur precisely on the same dates each year. It's a copy and paste solution to incorporate in your strategy.


CODE:
Please log in to see this code.
Note: Importing the average S&P 500 yield is another step, and I assume you have that data from somewhere.
profile picture

rmandel00

#5
Cone,
Thanks for the cut and paste code. I'm sure that your code would be more accurate than what I am using. I was using the following code to get the dividend yield on my symbols.

CODE:
Please log in to see this code.

I'm not sure where I can get S&P yield from or how to import into the code. I thought I probably could get it using the SPY (spiders) or .SPX
Thanks,
Rich
profile picture

Cone

#6
Your code based on WealthLab.Rules is accurate (you only need the DataSeries dsRatio = FundamentalsRatio.YieldSeries.. statement), it's just that it calculates it internally a different way (using the "cash dividend" series), it may include large one-time distributions, and you only get 24 quarters. Anyway, after comparing the two, I still have a little tweaking to do with my rendition anyway.

One way to get the S&P 500 yield is to calculate it yourself, export it to an ASCII series, and update it whenever required. (Alternatively an index definition could be made to do this.) Here's the manual way (without the export..)

NOTE! Just click one symbol in the S&P 500 DataSet to run this script!

CODE:
Please log in to see this code.
There are some large steps in the result, but the curve is continuous after the steps.. it looks strange, so it would take some investigation to determine what's going on there.

profile picture

Eugene

#7
QUOTE:
(Alternatively an index definition could be made to do this.)

Problem is to obtain the WealthScript reference (the "this" in FundamentalsRatio.YieldSeries required to build the DataSeries) in an IndexDefinition. It's not evident to me.
profile picture

Cone

#8
Here's another way without using the WealthLab.Rules -

CODE:
Please log in to see this code.
profile picture

Eugene

#9
Okay but how about FundamentalDataSeries not accessible in IndexDefinitions (being part of the same WealthScript namespace)? ;)

Here's another, simpler idea (almost no code required):

YCharts provider has S&P 500 Dividend Yield (as well as some other items for the S&P 500 like Dividends, Reported Earnings, Market Cap, Operating EPS, P/E Ratio, Dividends Per Share, and more)

What's required is a YCharts Pro subscription (~$20 a month) or a YCharts Pro free trial (YCharts Basic accounts do not work) and the provider extension installed.
profile picture

Eugene

#10
With YCharts, it would translate to something like this:
CODE:
Please log in to see this code.
profile picture

Cone

#11
I think that's a winner!
profile picture

Eugene

#12
Well, I stand corrected. Everybody familiar with YCharts knows how often they like to make changes... no more "[yc] sp_500_dividend_yield" item.

The closest available item is now called "S&P 500 Year to year return from dividends %". Let's compare some charts, the YCharts version goes first:



S&P 500 Dividend Yield from multpl.com:



As they appear very close for the data range available at YCharts, the "[yc] sp_500_dividend_yield" translates to...
CODE:
Please log in to see this code.

... but not before I update the YCharts provider next month! Aargh.
profile picture

rmandel00

#13
Thanks for the help guys. I am using Cone's code and trying to export the data to a csv file as follows.
CODE:
Please log in to see this code.


The code outputs the data to the file but the data is a little strange. I am for the moment using the DJ30 as it runs quickly.
First there are 3806 lines with
12/2/1991 0:00

Then up to line 4780 it looks like this:
12/2/1991 0:00 0.490263554
with the dividend rate changing from line to line

Finally there are lines that increment up to the present date starting with:
11/18/2010 0:00 2.634809586

and ending with:
2/27/2012 0:00 2.596087244

When I run the S&P500 list I get a runtime error to the effect
Object refernce not set to an instance of an object
at WL Strategies.MyStrategy.Execute()
at WL.WealthSCript.SetContext(String symbol, Boolean synchornize)
at WL.Bars..ctor(Bars b)

Thanks,
Rich Man
profile picture

Eugene

#14
In a duplicate thread that once existed, RickTg asked:

In the following code (YieldAtBar) provided by Cone [from post #4 @ 2/26/2012 12:17 PM] in one of the discussions, there is an exclusion of one time distributions in excess of 4% of share price. I understand why the exclusion exists. What I do not understand is why was 4% chosen. Why not a larger or smaller percentage? I have searched but I can not find an explanation on the WLP website.

Thank you

Rick

profile picture

Cone

#15
QUOTE:
What I do not understand is why was 4% chosen
Arbitrarily. Special dividends shouldn't be included in a stock's yield, and generally they are large. Other times, they aren't. If you have a more definite way to identify a dividend as "special", it would be better to replace that logic.

For example, Fidelity fundamental data includes that info in their "dividend" FundamentalItem

CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).