How to Get the Close Price of a Particular Date

This should be a simple question but seems complicated for me. I would like to get closing price of my watchlist as of a particular date and 1 week and 3/6 months ago.

Below is my code using lookup to get the price of particular. My logic is get the relevant dates and lookup the price of the relevant dates. My Amibroker is 6.40. Have I missed any critical functions? Would anyone give me some direction or guidance.

type o//Get price of particular date and weeks and months before the particular date
//Calculate the price changes of those period

//Set the relevant Dates, 1 Week; 3 Months and 6 Months from the Input Daate

InputDate = "10-03-2022";
Date1wBack = DateTimeAdd (_DT(InputDate), -1, inWeekly);
Date3mBack = DateTimeAdd (_DT(InputDate), -3, inMonthly);
Date6mBack = DateTimeAdd (_DT(InputDate), -6, inMonthly);

//Lookup Price of the Particular Dates. If no price for a particular date, take price of the nearest predecesor

PriceInputDate = Lookup(C, _DT(InputDate), -1);
Price1wBack = Lookup (C, (Date1wBack), -1);
Price3mBack = Lookup (C, (Date3mBack), -1);
Price6mBack = Lookup (C, (Date6mBack), -1);

//Explore the information

AddTextColumn (Name(), "Ticker", 1);
AddTextColumn (DateTimeFormat("%d/%m/%y", _DT(InputDate)), "Date");
AddColumn (PriceInputDate, "Close Price");
AddTextColumn (DateTimeFormat("%d/%m/%y", Date1wBack), "1 Week");
AddColumn (Price1wBack, "Close Price");
AddTextColumn (DateTimeFormat("%d/%m/%y", Date3mBack), "3 months");
AddColumn (Price3mBack, "Close Price");
AddTextColumn (DateTimeFormat("%d/%m/%y", Date6mBack), "6 months");
AddColumn (Price6mBack, "Close Price");

r paste code here
type or paste code here
1 Like

In the Filter, I select the my WatchList or S&P 100 and from Dates Range 1 Jan 2021 to 14 Mar 2022. No results from running Explore or Scan. Even I changed the InputDate to 1 Mar 2022, it also shows no result. There must something wrong with the concept of date in the form of String or Numeric, which I do not understand. There is no error in the codes. Any indication of my errors?

You get no results because Filter variable is missing. It is essential variable!

Important read below:

1 Like

Why do it this way ? You will have problems with weekends anyway. That is why you are ok with using the nearest predecessor.

Why not just write:

Price1wBack = ref(C, -5);
Price3mBack = ref(C, -66);
Price6mBack = ref(C, -132);

It is not be the exact date.... but your version will also not be exact because of weekends and holiday's.

(Do check the code if this is written correctly by the way, I am very bad at afl so might be wrong but it will give you the idea)

I have not included Filter in AFL Editor just for the flexible to apply it all various Watchlists respectively, To run Exploration, I have select Watchlist Filter, such as S&P500, but still has no result. Do I need to apply any other setting before I run the Exploration?

Thanks Henri. Your suggested coding is the most popular one. The Lookup mode -1 is to get the nearest predecesor, which will handle the problem of trading holidays.

I have verified the codes. No error shown. That makes me wonder what's wrong with my codes or steps on running Exploration.

Dates are always complicated in all kind of programming. But I didn't find any useful resources in the Guide or Knowledge Base.

As @fxshrat wrote, you NEED to add a filter line to your code!!!
(Please, read entirely and carefully the section of the user guide he linked above).

It is simple as:

//Explore the information
filter = 1; /* all symbols and quotes accepted */
// rest of your code

Please re-read post #2 plus page of given link!

I don't understand why (some) newbies have to start discussion and question everything that has been advised by documentation (by original developer) or experienced AB users?

Filter setting of Analysis "Apply to:" is not equal to AFL Filter variable!
Once again to get Exploration result list you have to add Filter line to your exploration code.

1 Like

@fxshrat @bepe Thanks for your help. It clarified my issues. I am not aware the difference of Filter in ALF Editor and the Analysis Filter settings. As a Newbie, Filter is Filter, not sure it makes such differences AFL. That is the different between the Newbie and experienced users. Thanks again.

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