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.