Hello All, 

  I am playing around with the different data normalization modes for dividends and noticed something. When using DataNormalizationMode.TotalReturn I noticed that the price is not adjusted until a couple of days after the dividend. Just curious what the logic behind this is? 

Using the following code to test. I am switching between Raw handling and Total Return. 

import numpy as np

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
'''Basic template algorithm simply initializes the date range and cash'''

def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

self.ticker = 'MSFT'
self.raw_handling = True

self.SetStartDate(2018,2,8) #Set Start Date
self.SetEndDate(2018,2,20) #Set End Date
self.SetCash(10000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddEquity(self.ticker, Resolution.Daily)

# Set Dividend Handling Method
# https://www.quantconnect.com/forum/discussion/508/update-dividends-splits-and-custom-price-normalization/p1
if self.raw_handling:
self.Securities[self.ticker].SetDataNormalizationMode(DataNormalizationMode.Raw)
else:
self.Securities[self.ticker].SetDataNormalizationMode(DataNormalizationMode.TotalReturn)


def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if not self.Portfolio.Invested:
self.SetHoldings(self.ticker, 1)

for kvp in data.Dividends: # update this to Dividends dictionary
div_ticker = kvp.Key
div_distribution = kvp.Value.Distribution
div_total_value = div_distribution * self.Portfolio[self.ticker].Quantity
self.Log("{0} >> DIVIDEND >> {1} - ${2} - ${3}".format(self.Time, div_ticker, div_distribution, div_total_value))

self.Log("{0} >> SUMMARY >> {1} | Port Cash: {2} | Port Value: {3} | Holdings: {4} | Price {5}".format(self.Time,
self.ticker, self.Portfolio.Cash, self.Portfolio.TotalPortfolioValue, self.Portfolio[self.ticker].Quantity, self.Portfolio[self.ticker].Price))

The final results are correct from what I can see. I just noticed that the price is not adjusted the day after. Instead it is adjusted 2 days after. 

RAW Log

2018-02-08 00:00:00 Launching analysis for 524f7e92d0fe02169f8772aff24da261 with LEAN Engine v2.4.0.0.4710
2018-02-08 00:00:00 2018-02-08 00:00:00 >> SUMMARY >> MSFT | Port Cash: 10000.0 | Port Value: 10000.00000 | Holdings: 0 | Price 89.5800
2018-02-09 00:00:00 2018-02-09 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 9475.08000 | Holdings: 111 | Price 84.9900
2018-02-10 00:00:00 2018-02-10 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 9825.84000 | Holdings: 111 | Price 88.1500
2018-02-13 00:00:00 2018-02-13 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 9936.84000 | Holdings: 111 | Price 89.1500
2018-02-14 00:00:00 2018-02-14 00:00:00 >> DIVIDEND >> MSFT - $0.42 - $46.62
2018-02-14 00:00:00 2018-02-14 00:00:00 >> SUMMARY >> MSFT | Port Cash: 87.810 | Port Value: 10053.39000 | Holdings: 111 | Price 89.7800
2018-02-15 00:00:00 2018-02-15 00:00:00 >> SUMMARY >> MSFT | Port Cash: 87.810 | Port Value: 10053.39000 | Holdings: 111 | Price 89.7800
2018-02-16 00:00:00 2018-02-16 00:00:00 >> SUMMARY >> MSFT | Port Cash: 87.810 | Port Value: 10379.73000 | Holdings: 111 | Price 92.7200
2018-02-17 00:00:00 2018-02-17 00:00:00 >> SUMMARY >> MSFT | Port Cash: 87.810 | Port Value: 10292.04000 | Holdings: 111 | Price 91.9300
2018-02-21 00:00:00 2018-02-21 00:00:00 >> SUMMARY >> MSFT | Port Cash: 87.810 | Port Value: 10379.73000 | Holdings: 111 | Price 92.7200
2018-02-21 00:00:00 Algorithm Id:(524f7e92d0fe02169f8772aff24da261) completed in 1.09 seconds at 0k data points per second. Processing total of 11 data points.

Total Return Log

2018-02-08 00:00:00 Launching analysis for 1245afb9bd9840408d99861cd4c6661c with LEAN Engine v2.4.0.0.4710
2018-02-08 00:00:00 2018-02-08 00:00:00 >> SUMMARY >> MSFT | Port Cash: 10000.0 | Port Value: 10000.00000 | Holdings: 0 | Price 89.5800
2018-02-09 00:00:00 2018-02-09 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 9475.08000 | Holdings: 111 | Price 84.9900
2018-02-10 00:00:00 2018-02-10 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 9825.84000 | Holdings: 111 | Price 88.1500
2018-02-13 00:00:00 2018-02-13 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 9936.84000 | Holdings: 111 | Price 89.1500
2018-02-14 00:00:00 2018-02-14 00:00:00 >> DIVIDEND >> MSFT - $0.42 - $46.62
2018-02-14 00:00:00 2018-02-14 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 10006.77000 | Holdings: 111 | Price 89.7800
2018-02-15 00:00:00 2018-02-15 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 10006.77000 | Holdings: 111 | Price 89.7800
2018-02-16 00:00:00 2018-02-16 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 10379.73000 | Holdings: 111 | Price 93.1400
2018-02-17 00:00:00 2018-02-17 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 10292.04000 | Holdings: 111 | Price 92.3500
2018-02-21 00:00:00 2018-02-21 00:00:00 >> SUMMARY >> MSFT | Port Cash: 41.190 | Port Value: 10379.73000 | Holdings: 111 | Price 93.1400
2018-02-21 00:00:00 Algorithm Id:(1245afb9bd9840408d99861cd4c6661c) completed in 1.11 seconds at 0k data points per second. Processing total

As we can see the distribution happens on the 14th of Feb. Our cash balance is immediately updated in Raw mode as expected. However, in the total return log, the price remains the same as the Raw log until the 16th of Feb.  

BONUS semi-related question

Is there a way to account for ex-div dates? For example in this example, I probably should not have received the dividend as it would have gone ex-div before the test start date.