Finding bar index of a specific date

I am trying to find the index of a specific date.

Assume given date is 11/22/2021.
I convert this to 1211122 (Amibroker format)
My code is:
DT = Datenum();
event_DT = 1211122;
Bars = BarsSince(DT == event_DT);

When I print the value of Bars, it is EMPTY

What am I doing wrong? What are other options?

Thanks
Ara

Equality check == requires exact match, if no data with that particular DateNum value was found in currently selected symbol then you would get empty

Tomasz, thanks for the quick response.

The statement Bars = BarsSince(DT == event_DT); started working reliably after I added SetBarsRequired( 1000000, 0 ); to my program.

Still have similar issues though with aonther statement

Below is a complete program with comments to clarify issues and printout from DebugView

//File: Test - Mark Event on chart
// Dec 17, 2021
//
//Purpose of this program is to mark a chart on a given date
// - For this exercice, the marking is a simple vertical bar on date of event


	SetBarsRequired( 1000000, 0 );  
	//
	//plot simple price chart
	plotOHLC(O,H,L,C,"",colorblack,stylecandle);
	//
    DT			= datenum();
    bi			= barindex();
    last_date	= lastvalue( DT);
//
	//Event date selected for debug purposes
    event_date	= 1211122;								
    _TRACE( "Trades-1A - event_Date: " + writeval( event_date, 1.0 ) + "  type: " + typeof(event_Date));
// Find index for this date
//
    Bars		= BarsSince( DT == event_Date );			// THIS LINE WORKED INTERMITTENTLY UNTIL I ADED "SetBarsRequired( 1000000, 0 )" STATEMENT - NOW IS OK
    index		= barcount - Bars - 1;
    _TRACE( "Trades-1B - Barcount " + writeval( Barcount, 1.0 ) + "  Index " + writeval( Index, 1.0 ) + "  event date " + writeval( event_Date, 1.0 ) + "  Last DT " + writeval(last_date, 1.0 ) + "  Bars " + writeval( Bars, 1.0 ) );
//
//
//  PROBLEM AREA BELOW
//  I could use either of the 2 statement below - but both produce {EMPTY} field for plotbar
    Plotbar		= Valuewhen( bi == index, 1, 0 );			// PROBLEM
	Plotbar		= Valuewhen(DT == event_Date,1,0);			// PROBLEM

	plot(Plotbar,"",colorgreen,stylehistogram|styleownscale,0,1);
//

// Diagnostic loop around event date
// The dates and index numbers all seem OK, so program should work
    for( i = barcount - 20; i < barcount - 15; i++ )
    {
        _TRACE( "Trades2 - debug loop - datenum " + writeval( DT[i], 1.0 ) + "  event date " + writeval( event_Date, 1.0 ) + "  bi "  + writeval( bi[i], 1.0 ) + "  index "  + writeval( index, 1.0 ) + "  Plot " + writeval(plotbar,1.0) );
    }type or paste code here

Debug View printout
image

You need to carefully read the Knowledge Base:

Tomasz, I appreciate the speed that quick AFL provides and certainly prefer using it, but obviously there is a problem with my code that is independent of Quick AFL, even though using all bars solved problem with line of code using BarsSince().

Having similar problem with lines using Valuewhen().

I would appreciate if you would look at the problem areas of the code (lines 29 and 30). Either one would work for what I need to do, but both result in {EMPTY} field.

The Debug View printout clearly shows that the condition in the problem statements is being met, so something else is going on ,,,

Thanks
Ara

No, ValueWhen does not have any "period" parameter and because of that it can not "know" how many bars it requires. BarsSince is pretty much the same. You don't pass "period" to it. Depening on DATA BarsSince may require as little as 1 bar and as much as ALL bars. You don't know that from the formula. It is 100% data dependent. For that reason QuickAFL also does not know and can't estimate IN ADVANCE how many bars you would need (because it solely depends on DATA not the function itself).
Therefore as described in QuickAFL article, it is upto YOU to decide how many bars you want to include in calculations. If you want ALL bars, you have to tell it via SetBarsRequired( sbrAll );

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