Chart values changing when I click on chart

I think I am missing a really simple concept when trying to chart a the relative performance of ETFs that track the S&P 500 index against the index itself. I have used a response from @fxshrat at this link Fixed Starting Point for Charts and taken the code he has given and tried to adapt it to my purpose. That forum post itself develops further on a Knowledge Base articles from Tomasz: How to add symbol labels to Relative Performance chart

The aim is to choose a reference date and then index all these ETFs to the same value as the $SPX ticker, which is the S&P 500 index. Then from that date forward I can view how the individual ETF has performed relative to the index itself. Here is the code:

if( GetChartID() == 1035 )
	_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

	PlotOHLC( O, H, L, C, "Price", colorwhite, styleCandle|styleNoTitle );
	//	Want to index the price action of the ETFs to a certain date, we will use the first date we have data for all 3 years as a default
	ETFList = ParamStr( "ETFs", "SPY,IVV,VOO" );
	IndexDate = ParamDate( "ETF Index Date", "2010-09-09", format = 2 );
	bi = BarIndex();
	IndexBar = Lookup( bi, IndexDate );
	for( i = 0; ( symbol = StrExtract( ETFList, i ) ) != ""; i++ )	//	This loop basically displays the relative value of the ETF relative to the $SPX from the indexing date we specify
		fc = Foreign( symbol, "C" );
		relP = fc / fc[IndexBar];		
		Plot( relP * Close[IndexBar], symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleLine );

Note: This code is applied to the $SPX ticker which with NorgateData is the index itself.

As soon as I insert this chart to my pane, it gives me the correct values (hand checked with a calculator).

However, as soon as I click on my chart, all the values change:


I cannot understand why the values are correct at the very start, but they change as soon as I click on the chart. If anyone can see what concept I am missing, if you could tell me what I should search for, or provide a link to the topic, I would be very grateful.

First: don't put Param() calls inside if(). They must be called undconditionally.

Second: don't use if( GetChartID() == 1035 ) . Such hard coding is going to bring you just problems. Indicators are always executing in the context of one indicator so such checks are just not needed at all.

Thirdly: if you use lookup function for specific PAST date, you want that data to be actually available. You may need to use SetBarsRequired to ensure that more data than DISPLAYED are used .
You have to carefully read this article:


@Tomasz, thank you the explanation. The concept I was indeed missing was the QuickAFL and the fact that all the bars I required were not being used in the calculation.

Thanks for also pointing out the fundamental errors in my coding, such as putting Param() calls inside if(), and hard coding the chart ID in my afl file. Now that my immediate problem is solved, I will take the time over the coming days to educate myself fully on QuickAFL, chart IDs, etc to better my overall understanding of Amibroker.

Thanks again for taking the time to reply so quickly.

1 Like

Second: don't use if( GetChartID() == 1035 ) . Such hard coding is going to bring you just problems.

The reason I was using the chart ID is that I was trying to use the same afl file to plot different plots on different panes in the same chart sheet. This was my way of choosing what got plotted on each pane. After your response, with a bit of searching in the forum, I see that there are much better ways to do this.

Multiple chart panes with single afl formula

Here @fxshrat has indicated that ParamToggle is a good option to use to select which plots occur on which pane.

Plotting two custom indicators of same AFL into two different panes

Here, @HelixTrader has used ParamList and I also found that served my purposes well.

I understand that you don't need to be told all this information Tomasz (as you obviously already know it), but I am including it merely for completeness, so that someone viewing this post in the future has all the links to relevant information.

Thanks again for picking up the errors in my coding, I had been hard coding ChartIDs into my afl files for years, now I have a proper way to select which charts appear on which pane.

1 Like

I understand the motivation behind this but I am not really a fan of such approach.

1 Like

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