Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Dividends and algorithm

Hi All -

So the API explains clearly how dividends are handled. Essentially, they are handled realistically by reducing the price of the security and crediting the account cash. So if a security is trading at $9.00 and pays a $2.00 dividend then on the day following the ex-dividend date the security trades at $7.00 and on the payment date, which could be substantially after the ex-dividend date, the cash account would be credited $2.00 if you were long that security.

What about if you were calculating the average price over that two day period of time? For example, would

data[sid].mavg(2)  

return $8.00 if the two days in the average were the ex-dividend date and the day following it? If so, this could produce some false signals with momentum trading. For example, if you were going to sell a security if its price falls below its 5 day moving average, you might get a sell signal after a dividend is paid.

Anyone know if the simple and/or batch transforms take into account dividends?

4 responses

The short answer is that mavg() and similar price measures don't yet know about dividends. You've hit an area of our backtester that still needs some implementation work.

The dividends section of the help doc explains a lot of the design choices we made around dividends. The downside to the design we chose is also mentioned there: dividends can cause discontinuities in the price and brief discontinuities in portfolio value.

The way to mitigate this downside is to feed the dividend information back into the algorithm; if the algo knows that a dividend happened, it can adjust the prices if it thinks it needs to. That's still on our "to do" list. We need to do the same thing for live trading. Right now in live trading and paper trading, the price change is streamed into the algorithm but it doesn't know there was a dividend. I don't know yet if we're going to make that fix for live or backtest first, or do them both at the same time. We'll have a more robust backtester when we do it.

Attached is an algo that you can use to look at the phenomenon. There are 5 signals in the custom signal area. You can get better resolution viewing the data if you click on the legend and toggle. Look at price and mavg together, then turn those off and look at position, cash, and portfolio together.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Ok understood. That is a dangerous caveat then of the backtesting abilities at the moment, because the price only value will show substantial volatility for companies that pay large dividends. I will see if I can work around it - possibly try to import dividend-adjusted values.

Ahhh.... and this is what explains another thing I've been trying to figure out. I was working on an algo that would invest in SPY for a short period of time, while the batch_transform warmed up, and then start trading. To test this, I had it simply go to cash after the warm up period and, seemingly by magic, the cash balance of the account seems to jump by 0.5% around April 28. This must be the result of a dividend.

Yeah, that's it. Ex-date of 3/15, pay date 4/30, as your backtest showed. http://www.nasdaq.com/symbol/spy/dividend-history