Dividend related strategies
Author: MrIzzy
Creation Date: 10/26/2011 7:40 AM

#### MrIzzy

#1
Is there any way to check a "dividend related strategies"?

1) Buy a stock X days before the ex-dividend date and sell it Y days before the ex-date (where X > Y).
2) Buy a stock X days before the ex-dividend date and sell it K days AFTER the ex-date. Here we also need to factor in the dividend payment into the P&L.

Any help on 1) or 2) would be appreciated!

thanks,
Izzy

#### Cone

#2
Sure you can do this. Assuming the Fidelity data, per share dividends are given by the "dividend" item. Just to show you what's available, here's a small modification of the QuickRef example for FundamentalDataItems:

CODE:
Run it on GRMN, for example, and you get:

CODE:
So, you already know, weeks in advance that GRMN will pay a dividend on 12/13/2011. The only problem is that since you don't have a bar number for that future event, you need to know the number of trading days until 12/13/2011. For that you need a Community.Components method - TradingDaysBetweenDates.

I think that's all you need. Good luck!

#### MrIzzy

#3
Thank you very much indeed. That was very helpful.
WL can do a lot and y'all are very helpful in extracting its capabilities

Izzy

#### MrIzzy

#4
Hi There,

For the second part of the question, suppose I keep the stock until after the ex-dividend date. Can we incorporate the payment of the dividend into the P&L ? We know how much the dividend is, so I was hoping to add it to the P&L (even manually).

Izzy

#5

#### MrIzzy

#6
More on this strategy: I am having problem sizing the positions.

During some periods, we expect more ex-dividend dates than in other periods.
What would be ideas is to be able to count how many ex-dividend days would occur in the next A days (A is a parameter that I set). Let's call this number N.
Assuming, we have access to the cash in the Equity Curve (the one drawn in Dark Green in the Equity Curve chart), call this number C.

We would like to allocate C/N dollars to each trade.

1) How do we get the value of N
2) How do we get to the value of C

Any hints would be appreciated.

#### Eugene

#7
QUOTE:
1) How do we get the value of N
In pseudocode:
CODE:

#### MrIzzy

#8
Perhaps I wasn't being clear in my question.
Suppose I have a list of 100 stocks, how do I get the value of N, how many stocks have their ex-dividend days in the period from now to A days from now?

I think your code measures how many ex-dividend dates one particular stock may have had.

Izzy

#### Eugene

#9
QUOTE:
Assuming, we have access to the cash in the Equity Curve (the one drawn in Dark Green in the Equity Curve chart), call this number C.

Don't assume this. You don't have access to the EquityCurve/CashCurve series unless in a PosSizer/Visualizer or armed with this workaround: WealthScript Techniques | Interacting Dynamically with Portfolio Level Equity
QUOTE:
I think your code measures how many ex-dividend dates one particular stock may have had.

Yes. To get the number for a set of symbols, introduce a new integer variable to count the number of occurences, loop by DataSetSymbols and call SetContext before calling my pseudocode, and don't forget to RestoreContext thereafter.

#### MrIzzy

#10
I could really use a small pseudo-code example for counting N as you described.

Many thanks!

#### MrIzzy

#11
Hello There,

Can someone please post some pseudo-code that counts, N, the number of symbols in a list which will have an ex-dividend in the next A days?

Thanks

#### Cone

#12
Why do you want pseudo-code? (i.e., code that you cannot execute)

Are you a Fidelity customer? (i.e., use Fidelity data)

If so, give this a try... this is just for one symbol, run it on AXP and see the data shown in the PricePane.

CODE:

#### Cone

#13
/* The number of symbols in a list which will have an ex-dividend in the next A days? */

Just another step for this. Run this on one symbol only for the results of the whole DataSet.

CODE:

#### MrIzzy

#14
This is , indeed, exactly what I needed. Thank you!

#### MrIzzy

#15
Hi There,

Does the FundamentalDataItems("dividend") also incluse the relevant Announcement Date? This is the date in which the dividend was announced by the company. So far, I have been able to find the dividend amount and also the ex-dividend date. However, how about the "Announcement Date"?

#### Cone

#16
No, just the ex-dividend date. Going forward, if you're consistent about updating, you can record the announcment dates yourself. But if the idea is for backtesting, then you'll need another source for that.

#### MrIzzy

#17
Thank you for that information!

#### thodder

#18
Is there any chance that declaration date (announcement date) could be added to the dividend item in the future? I'd like to trade prior to ex-dividend date; however, looking at a future ex-dividend item before the declaration date is peeking. We really should have declaration date available for back testing.

CODE:

The code above shows that dividend items have class DataProviders.Common.FundamentalItemDividend.

QUOTE:
Date Dividend Type
11/26/2003 0.25 WealthLab.DataProviders.Common.FundamentalItemDividend
11/29/2004 0.25 WealthLab.DataProviders.Common.FundamentalItemDividend
11/29/2005 0.25 WealthLab.DataProviders.Common.FundamentalItemDividend
11/29/2006 0.5 WealthLab.DataProviders.Common.FundamentalItemDividend
8/13/2007 0.75 WealthLab.DataProviders.Common.FundamentalItemDividend
11/26/2008 0.75 WealthLab.DataProviders.Common.FundamentalItemDividend
11/27/2009 0.75 WealthLab.DataProviders.Common.FundamentalItemDividend
4/13/2010 1.5 WealthLab.DataProviders.Common.FundamentalItemDividend
4/13/2010 0.001 WealthLab.DataProviders.Common.FundamentalItemDividend
6/13/2011 0.8 WealthLab.DataProviders.Common.FundamentalItemDividend
9/13/2011 0.4 WealthLab.DataProviders.Common.FundamentalItemDividend
12/13/2011 0.4 WealthLab.DataProviders.Common.FundamentalItemDividend
3/13/2012 0.4 WealthLab.DataProviders.Common.FundamentalItemDividend

If there was a public property on this class for declaration date, then we could cast to it and use it. It just seems like critical information that would be good for any strategy trading around dividend dates.

#### Eugene

#19
I see your line of thinking, but there's really no need in casting etc. Via the .SetDetail method of the FundamentalItem class, a FundamentalItem can be assigned as many custom properties as required.

You'd obtain these time series in WealthScript Strategies using the .GetDetail method as documented in the QuickRef and illustrated below:

Analyst Ratings provider

As I don't work with WLP and the Fidelity data, I have no idea if the announcement dates are already being provided or not. You can prove or dispove that yourself, and if it's not available, please call your Fidelity rep and/or create a new support ticket.

#### thodder

#20
Thanks Eugene,

I wasn't aware of the GetDetail method; however, it doesn't appear to provide any useful information in this case. There is one key/value combination in it, but I'm not sure what it is used for. It is basically the same for every dividend FundamentalItem -- "DPC"/"CD.R".

I may create a suppport ticket on this to see if we can get some of the dates associated with a dividend included in the details. I'll have to work around it for now, but it may be helpful in the future.

#21

#22
CODE:

#### thodder

#23
Eugene,

This will reinvest dividends the day after it went ex-dividend. Shouldn't reinvesting wait until the payable date?

#### thodder

#24
Here's a variation to use payable day instead of ex-dividend...
CODE:

I'm still not clear on what position sizer would work with this. Raw will give the days for the trade, but shares won't reflect the dividend distributed. It seems like we should be using 'WealthScript Override (SetShareSize)' option for Position Size and do some calculations in the script to figure out the proper shares to trade.

(Revised: Code fixed to handle payable dates not on trading days plus multiple dividends paid on a single day.)

#### Eugene

#25
Tim,

Your solution is more finished than my approximation but it seems that in addition to the Fidelity fundamental data, it requires the MS123 Extra Fundamental/News providers library installed and the fundamental items collected in order to get "[ms] dividend", right?

#### thodder

#26
Eugene,

You're correct. You need the extra library and collect the Morningstar dividend history for this to work properly. I'm guessing you'll get an exception if you didn't set that up prior to running the script.

One thing I've noticed is that the Morningstar dividend history seems to lag on providing the latest dividend. I've looked at the Morningstar web site a few times, and that seems to be the source of the lag (not the provider library). Fidelity's dividend will show up before Morningstar's. An example is ABT that went ex-dividend on 7/11/2012. Chart annotations show Fidelity's dividend info, but Morningstar's dividend data isn't shown yet. This is true for the chart annotations and reviewing the history in script. I just use the annotations as an example since it is easy to see without writing a script.

Mainly I'm wondering what position sizer would be appropriate for a dividend reinvestment strategy like this. At the moment it appears the best solution is to use 'WealthScript Override (SetShareSize)' and size the position within the script.

#### Eugene

#27
Tim,

SetShareSize is a way to go but you could pass any extra information required for the position sizing decision via Position.Tag (or Bars.Tag for sizing an Alert) and let the custom PosSizer do the job.

#### thodder

#28
Eugene,

Good idea on the PosSizer. I assume there isn't a public one to handle it, so I'd have to write my own custom one. I believe the way it would work is the script would still have to signal the trade on the appropriate day (ex-dividend or payable day) and then the PosSizer would determine the number of shares to trade. I haven't written a PosSizer, but I don't believe they can initiate trades -- only resize them.

I'd probably go with the SetShareSize idea unless I have multiple scripts I'd like to experiment with. It should be simpler to keep all the code in one script rather than split it between script and PosSizer library. I can use parameters to get values I would normally pass to a PosSizer. It's still a good idea if more dividend strategies are developed.

#### Eugene

#29
SetShareSize will be a bottleneck in portfolio testing. The script has to signal the trade at all times - your understanding about PosSizers being able only to size is correct - and then the PosSizer applies a different sizing depending on trade type (regular or dividend reinvestment) based on a tag (be it an EntrySignal name or a .Tag property).

#### thodder

#30
I get what you mean by a bottleneck in portfolio testing. SetShareSize will never know the value of the portfolio. At the most it can only calculate the value of the current security position -- not all value of all the securities if this is a run against a DataSet. Once we start overriding PosSizers we appear to be in raw mode trading.

I was actually thinking it would be nice to have an IPosSizer interface. A class could be derived from this interface and assigned to Postion.Tag. Code within a PosSizer could allow individual positions to override the share size of a single position as follows:
CODE:

We'd need to pass something to GetShareSize. At a minimum the ActivePositions prior to this date, but possibly some other portfolio information if we are in portfolio mode. At least I think this would have helped in this case where we want the initial buy and hold position share size to be determined by the PosSizer, and then we want to override the share size on subsequent positions when we are doing dividend reinvesting.

Just some thoughts I had while trying to determine how to implement PosSizer(s) for this type of strategy.

#### WinOpt

#31
Here is a modification to thodder's code (dated 7/28). This program attempts to simulate a DRIP (Dividend Reinvestment Program). Rules:
(1) Buy an initial number of shares
(2)When div is paid, use all the cash to buy additional shares
(3) Buy & hold (no sells)
The features of this modification:
(1)The initial number of shares is a program constant(NetShares)
(2)When the div is paid, NewShares is calculated.
(3)SetShareSize() is used to set the share size to the NewShares value.
(4)NetShares is incremented by NewShares to keep a running total.
"WealthScript Override (SetShareSize) must be selected.
Minor problems remain:
(2)NewShares is truncated, not rounded when used in SetShareSize.
These problems are small enough. I believe the program as it now stands gives a reasonable approximation to a DRIP investment.
CODE:

#### Eugene

#32
Hint: you could upload the strategy to make it available to Wealth-Lab users.

#### RoseyF

#33
I recently downloaded MS123 Extra Fundamental/News providers library and retrieved the dividend data from Morningstar. I do not have any coding experience, but I was hoping you could provide an example of a code for buying a stock at the close before a dividend pay date and selling at open on day of pay date.
Thanks.

#### Eugene

#34
This example uses payable date instead of ex-dividend date and will only work on symbols for which the Morningstar extended dividend data is available:

CODE:

#### RoseyF

#35
Hi Eugene--Thanks for the help. The code is close to what I was looking for, but not exactly. The code seems to be buying at the close the day of pay date instead of the close the day before pay date. I was also looking to sell the next day on open, which would be the open the day of pay date. The code seems to be selling two days later.
Thanks.

#36
Give this a try:

CODE: