Count Number of Bars Since A Date

@fxshrat that seems to give a similar result to my code from a dozen posts ago (and with a small tweak could be identical). Great minds think alike :grin:?

@QEdges sorry have no time to look at your code for now, but it just doesn't seem that complicated. If its not solved in next couple of days I will give it a go.

image

dt_str = "2000-January-01";
str = DateTimeFormat( "%d%m%Y", _DT( dt_str ) );
OtherWay = DateTimeFormat( "%Y%m%d", _DT( dt_str ) );
Filter = 1;
AddtextColumn( str, "Start Date" );
AddtextColumn( OtherWay, "Start Date otherway" );

image

Though there are probably a few alternatives,

https://www.amibroker.com/guide/afl/datetimeformat.html

1 Like

@QEdges one way to get the first trading date (based on Close > 0) that seems to work for me:

SetBarsRequired( -2 ); // to override any QuickAFL setting
tr = Cum( isTrue( C ) ); 
dt = DateTime();
// without pad & align the index will be zero
firstTradingBarIndex = LastValue( Cum( 1 ) ) - LastValue( tr );
firstTradingDate = dt[firstTradingBarIndex];
Filter = 1;
AddColumn( Close, "Close" );
AddColumn( tr, "Trading", 1 );
AddColumn( firstTradingDate, "First trading date", formatDateTime );
AddColumn( firstTradingBarIndex, "First bar index", 1 );
AddColumn( BarIndex(), "Bar Index", 1 );

To check it, you can apply this exploration also to 1 recent day, both with/without Pad &Align; the resulting first trading dates should be the same.
The first bar index will vary and depends on the symbol used to P&A.

2 Likes

It is much simpler than that

Filter = 1;
dt = DateTime();
firstTradingDate = dt[NullCount(C)];
AddColumn( firstTradingDate, "First trading date", formatDateTime );

And "firstTradingBarIndex" would be just

firstTradingBarIndex = Nullcount(C);
7 Likes

Thank you all for your help. You got me over the finish line. And the "firstTradingDate" along with implementation of NullCount is something I am sure I will use many times. Thanks!

Thank you !
This is excellent for getting equivalent IPO Date.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.