Back to Community
The Dividend Calculation looks VERY WRONG

I am new to quantopian and recommended by my friend, after writing a very simple buy and hold strategy on a single stock APPLE. I found a very strange problem for the dividend calculation.
OK, here's the strategy attached, spent all the cash 100,000 to buy AAPL at 2014/03/01 and hold it to 2014/09/01, simple enough.
During this period there's a ONLY ONE split happened at 2014/06/09: 1 to 7.
And two dividends event:
1. 2014-05-16: 0.47
2. 2014-08-15: 0.47

OK, now let's look at the calculation result in Daily Positions & Gains, notice that the only contribution could happen to my cash change is DIVIDEND
1. At the very beginning I've bought 1326 shares of AAPL. Cash is -673.9. I understood that quantopian is using SPLIT adjusted price for smooth backtesting, so the shares cal is correct.
2. At 2014-05-16, I've got the dividend cash contributed into my Cash: 3688.64, which means dividend cash at 2014-05-16 is: 4361 This looks strange, since a quick calculation 0.47 * 1326(shares) = 623
3. At 2014-08-15, I've got the second dividend cash and contributed into my Cash: 4311.86, so the dividend distributed at 2014-08-15 is 623.2. If we do a quick calculation, 0.47 * 1326 = 623, so the dividend this time makes sense considering split already happened.

But now things are very wrong: dividend cash before split is larger than dividend cash after split, it's around 7 times larger - which is the same split number.

Conclusion is: it seems that dividend calculation before a split event is doing "*" split factor instead of "/" split factor.

Clone Algorithm
18
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
    context.aapl = symbol('AAPL')
    context.fired = False

# Will be called on every trade event for the securities you specify. 
def handle_data(context, data):
    # Implement your algorithm logic here.

    # data[sid(X)] holds the trade event data for that security.
    # context.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.
    price = data[context.aapl].price
    if not context.fired:
        order_percent(context.aapl,1)
        context.fired = True
        log.info("Fired all money for stocks AAPL")
There was a runtime error.
12 responses

Ah, so they are not split-adjusting historical dividends?

Hi Simon, interesting that I didn't notice the dividend records from Yahoo is adjusted by split. But here's what I get from Nasdaq without split adjusting: http://www.nasdaq.com/symbol/aapl/dividend-history
8/7/2014 Cash 0.47 7/22/2014 8/11/2014 8/14/2014
5/8/2014 Cash 3.29 4/23/2014 5/12/2014 5/15/2014

Let's take a look at the cash dividend distributed at 5/15/2014: 3.29 * 1326 = 4362, which looks correct for the dividend cash calculated in my original post. But let's think about the real case:

  1. On the first day 2014-03-01 of BT, I've put my whole 100000 into market, I can ONLY hold 100000 / 527.76 = 190 shares. And then hold to 5/15/2014 I've got my first dividend cash: 3.29 * 190 = 625.1, it's much less than 4362 in Q's calculation.

  2. Then at 2014/06/09, split happens, I've now got 190 * 7 = 1330 shares.

  3. Finally at 2014/08/15, a new dividend 0.47 happens, I've got 0.47 * 1330 = 625, only the last dividend calculation in Q is correct.

Or... I am wrong about data somewhere?

OK, a simple way to think is:
Before split the dividend is 3.29, after split dividend is becoming 0.47 (got /7). And shares also enlarged by 7 times, so the dividend cash received by these two dividend events should be the SAME instead of 4361 vs 623 if you check in the "Daily Positions & Gains".

I agree with you. I ran into the very same problem in my system, hopefully they'll get it fixed.

Thanks Simon for your reply.

Hi Q guys, will this get fixed? Otherwise all the returns and risk related calculation is NOT ACCURATE.

This helps show the issue:

def handle_data(context, data):  
    for stock in data:  
        PrintPosition(context, data, stock, data[stock])  
    if not context.fired:  
        order_percent(context.aapl,1)  
        context.fired = True

def PrintPosition(context, data, stock, stockData):  
    print(  
        "{0:>15} {1:>10.2f}".format("Positions value", context.account.total_positions_value) +  
        "{0:>15} {1:>8.2f}" .format("Portfolio P&L", context.portfolio.pnl) +  
        "{0:>15} {1:>8.2f}" .format("Portfolio cash", context.portfolio.cash) +  
        "{0:>10} {1:>5}"    .format("Symbol", stock.symbol) +  
        "{0:>15} {1:>8.2f}" .format("Close price", stockData.close_price) +  
        "{0:>15} {1:>8.2f}" .format("Quantity", context.portfolio.positions[stock].amount) +  
        "{0:>15} {1:>8.2f}" .format("Cost basis", context.portfolio.positions[stock].cost_basis))  

I think I just ran into this myself backtesting a strategy on NUGT - quantopian misinterprets a $1 dividend in 2011 as a $50 dividend, since it's since been split 1:50.

That does look like a bug. I'll write it up and look into it. Thank you for reporting it.

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.

This is fixed!

If you re-run the backtest above, you'll find that the total return is lower because the dividend returned is lower (and now correct).

Similarly, there seems to be issues with the dividend values in the Morningstar data.

Is there another, more reliable, source for dividend data?

Here it reports the last dividend per share paid by AAPL to be 3.0, but in reality it was 0.77 according to Yahoo.

Ah ok the FactSet data seems to be accurate.

Loading notebook preview...
Notebook previews are currently unavailable.

Fundamentals can be tricky. The timeframes aren't always obvious especially with the Morningstar data. For the most part, the Mornngstar data is quarterly and represents the numbers from the latest 10Q filing. However, in the case of cash_dividends_paid, the value is for the total trailing twelve months (TTM) dividends. The Factset and Yahoo data are simply the most recent dividend.

In this case, the value 3.0 on 2019-9-3 for AAPL is correct. It represents the sum of the TTM dividends:

8/9/2019    $0.77  
5/10/2019   $0.77  
2/8/2019    $0.73  
11/8/2018   $0.73

It's excellent you are looking at the fields before passively using them. There can of course be issues with the data but, more often, the issue is in what the data represents.

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.

Oh, thank you Dan!