FirstVisiblebar starts from 0 on x-axis

Hello,

I am a newbie here, learning AFL, and have difficulty in modifying following program from the Foreign() function example so that I can set the number of visible bars. I like to display certain number of bars like 100, 200,... and so on, instead of a number of bars from Status("firstvisiblebar").

If I use a number instead of Status("firstvisiblebar"), the chart of each ticker doesn't start from 0 on y-axis as the attached chart. I like to know how to fix this.

Any help would be appreciated!!

TickerList = ParamStr( "Tickers", "SPY,MSFT,GE" );
numBars = Param("Period", 100, 20, 300, 10);
// numBars = Status("firstvisiblebar"); // return bar number or bar index of first visible bar // Original
Plot(100 * (C - C[numBars-1]) / C[numBars-1], Name(), colorYellow ); // Plot performance of the Current Ticker

for (i = 0; (symbol = StrExtract(TickerList, i)) != ""; i++)
{
    fc = Foreign(symbol, "C");

    if (!IsNull(fc[0]))
    {
        Plot(100 * (fc - fc[numBars-1]) / fc[numBars-1], symbol, colorRed + ((2 * i) % 15), styleLine);
    }
}

startPoint = 0;
startPoint[numBars - 1] = 1;
Plot(startPoint, "", colorWhite, styleHistogram | styleOwnScale, 0, 1); // plot white vertical line
PlotGrid( 0, colorYellow );

Foreign

@ryank I am not certain if this is what you are looking for, but you appear to want to plot the Relative Performance chart for only a specific number of bars (why not just adjust your chart window size?). If you plan on doing some comparisons why not do this in an Exploration?

Regardless, here is my attempt to produce a chart that seem to me to be what you are looking for.

TickerList = ParamStr( "Tickers", "SPY,MSFT,GE" );
numBars = Param( "Period", 100, 20, 300, 10 );

LastBar = Status( "lastvisiblebar" );

bi = BarIndex();
StartBar = LastBar - numBars;

for( i = 0; ( symbol = StrExtract( TickerList, i ) ) != ""; i++ )
{
    fc = Foreign( symbol, "C" );

    if( !IsNull( fc[0] ) )
    {
        Plot( IIf( bi > StartBar, 100 * ( fc - fc[numBars - 1] ) / fc[numBars - 1], Null ), symbol, colorRed + ( ( 2 * i ) % 15 ), styleLine );
    }
}

Plot( IIf( bi >= StartBar, 100 * ( C - C[numBars - 1] ) / C[numBars - 1], Null ), Name(), colorYellow ); // Plot performance of the Current Ticker

VerticalLine = 1;

Plot( IIf( bi == StartBar, VerticalLine, Null ), "", colorWhite, styleHistogram | styleOwnScale, 0, 1, 0 ,0, 4 ); // plot white vertical line
PlotGrid( 0, colorYellow );

image

image

@ryank Sorry I just noticed I must have done something wrong, the relative performances are not starting at the zero level. Sorry, I gave it a shot.:cry:

One last shot,

TickerList = ParamStr( "Tickers", "SPY,MSFT,GE" );
numBars = Param( "Period", 100, 20, 300, 10 );

LastBar = Status( "lastvisiblebar" );

bi = BarIndex();
StartBar = LastBar - numBars;

for( i = 0; ( symbol = StrExtract( TickerList, i ) ) != ""; i++ )
{
    fc = Foreign( symbol, "C" );

    if( !IsNull( fc[0] ) )
    {
        Plot( IIf( bi > StartBar, 100 * ( fc - fc[StartBar ] ) / fc[StartBar], Null ), symbol, colorRed + ( ( 2 * i ) % 15 ), styleLine );
    }
}

Plot( IIf( bi >= StartBar, 100 * ( C - C[StartBar] ) / C[StartBar], Null ), Name(), colorYellow ); // Plot performance of the Current Ticker

VerticalLine = 1;

Plot( IIf( bi == StartBar, VerticalLine, Null ), "", colorWhite, styleHistogram | styleOwnScale, 0, 1, 0 ,0, 4 ); // plot white vertical line
PlotGrid( 0, colorYellow );

image

1 Like

Thanks for your help, really appreciate it.

I encountered error while editing my posting and could not change it. What I wanted was exactly as you guessed in your first reply. I want to adjust my chart window size with a lookback period parameter so that status(“firstvisiblebar”) == lookback period.