AFL question about 'lookup' function

I am trying to get a date related close for symbols in a short watch list and calculate percent of change from selected to date verses current date.

I’m using the lookup per the example on the Amibroker site, but must be doing something wrong. The code works OK if I put in a fixed dollar value for ‘BuyPrice’ verses trying to look closing price up on particular date

This is the code I’m using. I apparently am using the lookup function incorrectly. Please educate me!!!

Any and all help appreciated! Thank you!

// percent change based on closing price of particular date (2017-05-17 this example)
// verses date exploration (scan) conducted

Filter = 1;

BuyDate = "2017-05-17";

BuyPrice = lookup(Close,_DT (BuyDate),-1); // this line has syntax errors

Change = Close - BuyPrice;

PercentOfChange =  (Change / BuyPrice) * 100;

AddColumn (BuyPrice, "My Buy Price");

AddColumn (Close, "Todays Price");

AddColumn (Change, "Amount of change");

AddColumn (PercentOfChange, "Percentage Change");

It worked fine for me, but as good coding practise, you should avoid reassigning the BuyPrice array with a single value. BuyPrice is a special array that's supposed to be for holding the price at each Buy signal. A better practise is to create a new array with another name based on the value in BuyPrice.

Eg

BuyPrice = Close;
PriceAtBuyDate = Lookup(BuyPrice, _DT(BuyDate), -1); 
Change = Close - PriceAtBuyDate;
PercentOfChange = (Change / PriceAtBuyDate) * 100;
AddColumn (PriceAtBuyDate, "My Buy Price");

Either way it gives the same result.

Hi,

Firstly, thank you for your response, it is much appreciated. However, I feel I led you astray with my OP.

Here is what I am trying to achieve:

I have a watch list with only the tickers SPY, IVE, and EWO. Later when I ‘explore’, I will only use this watch list.

Let’s say the close on 17/05/2017 for each symbol is $25.00 , $32.00, and $50.00 respectfully. I run the ‘exploration’ on 16/06/2017 when the close on 15/06/2017 is $30.00, $40.00, and $65.00 respectfully.

What I’m hoping is that the ‘exploration’ spits out something like this

Ticker Date/Time My Buy Price Todays Price Amount of change Percentage change
SPY 16/06/2017 25.00 30.00 5.00 20
IVE 16/06/2017 32.00 40.00 8.00 25
EWO 16/06/2017 50.00 65.00 15.00 30

Obviously this is only example data, but shows the output I’m looking for.

I’m assuming I could run this exploration on any date and the output would reflect the percentage change between the close on 17/05/2017 (or the date in the ‘BuyDate’ field) and the current price.

Another anomaly I found (or think I found) the code entry for ‘Lookup’ did not turn blue when entered in the AFL editor. Should it be blue??

Again, your help is very much appreciated!

Formatting wasn’t correct but you get the idea!

bexar

In the toolbar you have

</>

button to do proper code formatting. I fixed your posting already but really have better things to do.

I'm still not quite sure what the issue is, it appears to be working just fine:

What version of Amibroker are you using?

Using AB ver 5.0, is this too old for this approach???

Here is my code:

// percent change based on closing price of particular date (2017-05-17 this example)
// verses date exploration (scan) conducted

Filter = 1;

BuyDate = "2017-05-17";

BuyPrice = Close;

PriceAtBuyDate = Lookup(BuyPrice, _DT(BuyDate),-1); 

// Error message for this line:  "Variable 'priceatbuydate' used without having been initialized"

Change = Close - PriceAtBuyDate;

PercentOfChange =  (Change / PriceAtBuyDate) * 100;

AddColumn (PriceAtBuyDate, "My Buy Price");

AddColumn (Close, "Todays Price");

AddColumn (Change, "Amount of change");

AddColumn (PercentOfChange, "Percentage Change");

Will not syntax check, get error message saying “Variable ‘priceatbuydate’ used without having
been initialized”

Again, many thanks for your patience and help!

Sorry, I apologise and will strive to improve with future code postings. Thank you for the heads up!!

bexar

5.00 is ancient and not supported AT ALL.

Lookup is NOT available in such ancient versions.

Minimum supported version is 6.00

1 Like

Thank you for the information, much appreciated!

Apology for reviving an old thread, but I am unable to resolve simple issue i am experiencing

running the lookup function from example with dates modified I am returning Empty Values
which it should not , see image. where selected value is at 1/3/2017 have close value of
5275.97.

any kind soul willing to help ?
AMIBROKER PROFESSIONAL 64 BIT 6.20.1

//InputDate = "2011-04-05";
//InputDate = "1/3/2017";
InputDate = "2017-01-03";
myStr = Name() +" Close value at (or before) " + InputDate + " is " + NumToStr(Lookup( Close,_DT( InputDate ), -1 ));
======== output
2017-01-03
COMPOSITE Close value at (or before) 2017-01-03 is {EMPTY}

Debug session has ended.

image