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

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
profile picture

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:
Please log in to see this code.
Run it on GRMN, for example, and you get:

CODE:
Please log in to see this 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!
profile picture

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
profile picture

MrIzzy

#4
Hi There,

That was very helpful.
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).

Thanks in advance,
Izzy
profile picture

MrIzzy

#5
Please disregard the previous post
profile picture

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.
profile picture

Eugene

#7
QUOTE:
1) How do we get the value of N
In pseudocode:
CODE:
Please log in to see this code.
profile picture

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
profile picture

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.
profile picture

MrIzzy

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

Many thanks!
profile picture

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
profile picture

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:
Please log in to see this code.
profile picture

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:
Please log in to see this code.
profile picture

MrIzzy

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

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"?

Thanks in advance!
profile picture

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.
profile picture

MrIzzy

#17
Thank you for that information!
profile picture

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:
Please log in to see this 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.
profile picture

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.
profile picture

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.
profile picture

WinOpt

#21
Want a dividend reinvestment strategy. Rules:buy and hold. Buy additional shares when dividend is paid. May want to add sell rules later.
profile picture

Eugene

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

thodder

#23
Eugene,

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

thodder

#24
Here's a variation to use payable day instead of ex-dividend...
CODE:
Please log in to see this 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.)
profile picture

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?
profile picture

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.
profile picture

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.
profile picture

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.
profile picture

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).
profile picture

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:
Please log in to see this 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.
profile picture

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:
(1)Can't buy fractional shares
(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:
Please log in to see this code.

profile picture

Eugene

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

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.
profile picture

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:
Please log in to see this code.
profile picture

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.
profile picture

Eugene

#36
Give this a try:

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

innertrader

#37
I am trying to add some dividend logic to an existing strategy. Basically, I want to ignore or delay taking action on a Sell signal generated by my strategy in the early hours of a trading day on which a dividend is paid. This is because I am using Fidelity 3 minute data that does not adjust for dividends, and a Sell signal is likely invalid until there are enough new bars outside the window of the dividend to renormalize the price data.

In looking through the examples above in this post and consulting other WL resources, it appears that the following are true. Please correct if I misunderstand.

1. Dividends are stored separately as Fundamental Data
2. Fundamental Data is essentially a list of Fundamental Data occurrences that can be searched or manipulated
3. You can create a DataSeries of Fundamental Data Items, which I presume can then be used as a condition of a strategy
4. Fundamental Data Items have properties that include Date and Value

If all above is true, I am still unclear how to incorporate as an additional condition within my strategy. I presume I can create and access a DataSeries, e.g.:

CODE:
Please log in to see this code.


But I'm unclear if the dividend value is synced to the first bar of the day only (9:33 AM ET) or to every bar that day. If the former, then I really want to check whether there is a dividend on that day for n number of bars from market open, but there doesn't appear to be a function like:

CODE:
Please log in to see this code.


I could probably set and refer to a variable such as

CODE:
Please log in to see this code.


But I am not sure this is the best practice.

(The fact that the "D" symbol is only attached to the first bar on a chart of the day might be a clue that it is the former. In that case I might be able use something like the code above on the first bar to detect a dividend for that day, and add logic to delay selling for a certain number of bars.)

Assuming I have been clear in my description of what I am looking to do, what's your suggestion of how I should approach?
profile picture

Cone

#38
You don't want to use a sync'd dividend series because that value repeats every bar until the next divident report. Instead you could make a discrete series.

CODE:
Please log in to see this code.

profile picture

Cone

#39
Incidentally, I chose the "dividend" (per share) item because this is current and even has announced dividends that occur in the future!
Fundamental data like "cash dividends" from 10Q reports may not be available for several days after the report and it corresponds with the 10Q date, not the dividend's ex-date.
profile picture

innertrader

#40
Cone,

Thank you for the code and the clarifications. I never would have figured this out on my own. Of course, it works perfectly!

Thanks again,

Robert
profile picture

gbullr

#41
Eugene or Cone:


I have been working with a strategy that trades HYG however upon double checking, the backtest engine does not appear to be including the dividend in the result. To double check I have also tried the strategy with XOM with the same result.



You can see that the trade that started on 1/7/2019 shows a return of 3.77 pct, but that does not include dividends It is just current price/ 82.58 (entry price) -1



Could you suggest what I may be doing wrong?

Thank you very much in advance.


profile picture

Eugene

#42
Your question does not actually concern dividend related strategies (as per topic title) but a Wealth-Lab feature. Anyway, what you should expect when applying dividends in backtetst is documented in the User Guide > Preferences > Backtest Settings > Interest and Dividends. Please find your question answered there in the first bullet-point. (Spoiler: it's correct, that's how it works).
profile picture

gbullr

#43
Where would you like me to ask a follow up question as to how to do this right (meaning including dividends)

Thanks.
profile picture

Eugene

#44
This is already 'done right' and there is no way to include dividends. This is how Wealth-Lab works.
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).