AddColumn using date from Analysis window Filter Setting

//The below code returns the last bar closing price for a ticker and writes it to the Analysis Window using the heading “Last Bar Close”
LastPrice = Close[ EndValue( BarIndex() ) ];
AddColumn(LastPrice,"Last Bar Close ",1.3,textColor = colorblack, bkgndColor = colorGreen );
//How can this code be re-written to return the bar closing price for a chosen date. The chosen d would be that chosen in the Analysis Window “From/To Dates” section?
//Thanks Keith

You don't have to do so much.

LastPrice = LastValue ( C );

This would suffice.

To find the close at a specifc date, use ValueWhen()
https://www.amibroker.com/guide/afl/valuewhen.html

Also use code tags and proper formatting when you post.

Applying code tags is mandatory in this forum!
How to use this site

This is now the nth time that you ignore it despite having been informed several time before.


Use Lookup function to get an element at chosen date

dtstr = "2018-10-01"; // your chosen date

lc = Lookup( C, _DT(dtstr), 0 );	
							
Filter = Status( "lastbarinrange" );

AddColumn(C,"Last Bar Close ", 1.3 );
AddColumn(lc,"Close at " + dtstr, 1.3 );

Alternativally you may use ParamDate

dtstr = "2018-10-01" ; // your chosen default date

pdt = DateTimeConvert(2, ParamDate( "Chosen Date", dtstr));
lc = Lookup( C, pdt, 0 );								

Filter = Status( "lastbarinrange" );

AddColumn(C,"Last Bar Close ", 1.3 );
AddColumn(lc,"Close at " + dtstr, 1.3 );
2 Likes

FWIW the second code's last AddColumn rather should be this one

AddColumn(lc, "Close at " + DateTimeToStr(pdt), 1.3);

since that 2nd one uses ParamDate.
As I just copied the first code snippet I forgot to adjust the column header's date output.

The OP won't respond anyway so who cares... I was just babbling a little bit to myself... So nevermind....

1 Like

The OP won't respond anyway so who cares... I was just babbling a little bit to myself... So nevermind....

I (and I suspect many others) benefit greatly from your posts. I really appreciate the effort you and various others go to in this forum.

You don't need ParamDate() or specify date in the code. Status("lastbarinrange") is all you need. Two lines are perfectly enough

Filter = Status("lastbarinrange");
AddColumn(C,"Close price" );  

This will display Close price at the last bar of selected range. Date column (automatic) will show the corresponding date. There is no need for any more code than just 2 lines above.

If for some reason you want to know the datetime of that particular bar, you can do:

SelectedValue( DateTime() );

If you want it as string you can do:

DateTimeToStr( SelectedValue( DateTime() ) );

SelectedValue() in Analysis window refers to last bar of selected range. You can use SelectedValue() function to get value of any array at the last bar of selected range in Analysis.

No doubt about it, that is definitely true if someone wants to output chosen date values only. But if someone wants to output value(s) at e.g last bar of array or at last bar of range together with value(s) at another chosen date then one would need to specify another date. No?

If it is only last bar of array to be compared with another chosen date then in that case one can use LastValue() function together with previous post's suggestion.

// output value(s) at last bar of array together 
// with value(s) at some chosen date (of "to-date" of "from-to range")
//
dt = DateTime();

Filter = Status("lastbarinrange");
AddColumn(LastValue(C)," Last Close price at " + DateTimeToStr(LastValue(dt)), 1.3 );  
AddColumn(C, "Chosen Close price at " + DateTimeToStr(SelectedValue(dt)), 1.3  );  

But for cases of output of value(s) at some last bar of date range (which also includes last bar of array) together with a chosen date then we are back at e.g LookUp.

// output value(s) at last bar of some range (All Quotes, From-To range,..) 
// together with value(s) at some chosen date to be looked up
//
dtstr_chosen = "2018-10-01"; // your chosen EOD date
//dtstr_chosen = "2018-10-01 00:00:00"; // your chosen datetime if using iEOD data

lc = Lookup(C, _DT(dtstr_chosen), 0);		

dt = DateTime();					

Filter = Status( "lastbarinrange" );
AddColumn(C,"Last Close price at " + DateTimeToStr(SelectedValue(dt)), 1.3 );
AddColumn(lc,"Chosen Close price at " + dtstr_chosen, 1.3 );

Or....?

Sure but your code answers different need and not what original poster asked for. Original poster specifically asked for end date of selected period:

So SelectedValue() is the way to go for what OP asked.

The following code works perfectly. Thank you Tomasz

Filter = Status("lastbarinrange");
AddColumn(C,"Close price" );