Return value of specific date

/* Hello seasoned AB users. I have used the below code in a prior program that seems to work fine but in my new program is returns the Close price for the first bar only and I can't seem to figure why that is. More the point, how should this code be amended to return the price for the Chosen Date.
You assistance is appreciated. Thanks Keith */

//CHOSEN DATE
ChosenDate = 20171215; // my chosen End date 
barNumber = LastValue( ValueWhen(DateNum() == ChosenDate , BarIndex(), 1) );
Lastprice = Close[barNumber];
printf("Lastprice =  " +LastPrice);

hello
just read again the function datenum() and you will find your mistake :sunglasses:

below are 2+1 examples:

//CHOSEN DATE  "03/12/2018"
ChosenDate = 1181203; // my chosen End date 
barNumber = LastValue( ValueWhen(DateNum() == ChosenDate , BarIndex(), 1) );
Lastprice = Close[barNumber];
printf("Lastprice =  " +LastPrice);
// Return the Close of the specific date "03/12/2018"
CloseOfChosenDate = ValueWhen(DateNum() == 1181203 , Close, 1) ;
printf("\n CloseOfChosenDate =  " +CloseOfChosenDate);

LCloseOfChosenDate = LastValue(ValueWhen(DateNum() == 1181203 , Close, 1)) ;
printf("\n LAST CloseOfChosenDate =  " +LCloseOfChosenDate);

3).

// here we use DateTime() to compare
strDate    =    StrToDateTime("03/12/2018");

ClosePrice = ValueWhen(DateTime()==strDate,C,1); 

// Return the Close of the specific date "03/12/2018"
printf("\n ClosePrice =  "+ ClosePrice );
2 Likes

'Thanks so much for your assistance. I have chosen to option 3
Regards Keith

1 Like