Reference(Foreign("DIA","C"),-2)

I am doing something incorrectly and can not figure out what.

I am doing an Exploration with different time frames and periods and want to do a % change ratio between ticker and a reference symbol such as "DIA". I have no problem using the reference function for the "base" ticker, but when I try to reference a past bar for my foreign ticker, i.e. -"DIA", I get the correct value for the previous bar, but asking for bars further back than 1 bar yields the same value as for immediate past bar. Please see example below.

Ref(Foreign("DIA","C"),-1);// OK, yields correct value
Ref(Foreign("DIA","C"),-2);// Yields same value as -1 bar
Ref(Foreign("DIA","C"),-3);// Yields same value as -1 bar

Any assistance as to what I am doing wrong, will be much appreciated.

There's nothing wrong with the logic of that code as far as I can tell, so your fault must be elsewhere.

Ref1 = Ref(Foreign("DIA","C"),-1);
Ref2 = Ref(Foreign("DIA","C"),-2);
Ref3 = Ref(Foreign("DIA","C"),-3);

Filter = 1;
AddColumn(Ref1, "Ref1");
AddColumn(Ref2, "Ref2");
AddColumn(Ref3, "Ref3");

Yields...

image

...as expected.

Thanks. I think my next step will be to delete AB and reinstall, as I am out of ideas and the reference worked fine for you.

Reinstall is not magical cure. AmiBroker does not "disintegrate". The problem is elsewhere - in the source DATA.

Your next step should be

a) rewriting your formula NOT to call same function over and over again (see below)
b) adding column that shows SOURCE data (directly from Foreign)
c) debug using Exploration as instructed here How do I debug my formula?

data = Foreign( "DIA", "C" ); // call it ONCE !

Ref1 = Ref( data,-1);
Ref2 = Ref( data,-2);
Ref3 = Ref( data,-3);

Filter = 1;
AddColumn( data, "Data" ); // first LOOK AT THE SOURCE table !
AddColumn(Ref1, "Ref1");
AddColumn(Ref2, "Ref2");
AddColumn(Ref3, "Ref3");

The reason is likely that your DIA data is misaligned and there are data missing for some dates, so Foreign pads the missing bars, see comment to Foreign function http://www.amibroker.com/f?foreign

Tomasz,

Thanks for your reply. My problem was a result of calling the same function over and over again, your item “a” below. I had previously checked the data via a chart and the data window. Lesson learned – again, thanks.

Harry