Count Number of Days (Bars) Until Ex-Dividend

I am working on code to explore for dividend stocks. One of the pieces of information I want to return is the number of bars until the Ex-Dividend date (looking forward). I use Norgate, and have been able to get the ex-dividend date into my analysis window when running an explore, however am running into difficulty counting the bars until Ex-Dividend.

I tried to convert the calculation of the barsbetween to a string as you will see in the code, thinking that was part of the problem

Here is the code I have developed so far (I am learning but still generate messy code):

#include_once "Formulas\Norgate Data\Norgate Data"

//Exploration for Dividend Stocks

//Calculate Dividend Yield + Prepare to Display % in Explore
DividendYield = NorgateDividendYieldTimeSeries();
PercentText = NumToStr( DividendYield , 1.2 ) + "%";

//Determine Ex-Dividend Date
ExD = GetFnData( "ExDividendDate" );

//Count Number of Days Between Now and Ex-Dividend Date
StartDate = Now(3); 
EndDate = ExD;

BarsBetween = EndDate - StartDate;
BarsBetweenText = NumToStr( BarsBetween, 1.0 ) + " Days";

//Only Want Tickers Where Yield > 2.5%
GoodYield = DividendYield > 2.5;

//Exploration Columns
//Filter = Status("lastbarinrange");
Filter = GoodYield AND 1;

AddColumn( C , "Close" );
AddColumn( ExD , "Ex-Dividend Date" , formatDateTime );
AddTextColumn( PercentText , "Dividend Yield" );
AddTextColumn( BarsBetweenText , "Day Until Ex-Dividend" );

As you can see the "Days Until Ex-Dividend" is giving me some weird results, and I can figure out why? Any help will be appreciated.



You should use function

DateTimeDiff(EndDate, Now(5))

This function returns difference in seconds. Function arguments must be in DateTime format, so instead of Now (3), you must use Now (5).

@Jeremy, please use forum search first. Topic about "strange/weird/...." ExDividendDate exists already. What I mean... please use existing threads instead of creating new one.

And Now(3) does not return DateTime as ExDividendDate field of GetFnData does but Now(3) does return (max.) 7 digits long date number (as DateNum() array function does) not including any time information as DateTime does. So DateTime and DateNum are two completely different kind of things that should not be mixed together.

So you should replace StartDate as well as BarsBetween to

//Determine Ex-Dividend Date
ExD = GetFnData( "ExDividendDate" );

//Count Number of Days Between Now and Ex-Dividend Date
StartDate = Now(5); 
EndDate = ExD;

/// DateTimeDiff returns seconds so divide by inDaily to get number of days. 
/// Note: DateTimeDiff may become more than 7 significant digits
/// if dividend date is far away. So also read here 
/// @link
BarsBetween = DateTimeDiff(StartDate, EndDate) / inDaily;// number of days

/// if you want to return negative number then 
//BarsBetween = DateTimeDiff(EndDate, StartDate) / inDaily;// number of days

If you want to get number of bars (instead of days) then you may replace inDaily by Max(1,Interval()).


Thank you for your help - that works. This code is somewhat limited as it is capped by the data that is loaded as future ex-dividend dates. That's ok though as I can run it nightly to see what gets added to the list.

Thanks again!