Fixed starting point for charts

I started by creating a new blank chart then dragged "Relative Performance" from the Basic Charts to create a chart with multiple stocks which all start at 0% then show the relative performance in following bars. I then found the August 2015 knowledge base article showing how to add symbol labels to the chart and saved the AFL.

The zero starting point is always the first displayed bar meaning when I zoom or change the bar period (Daily, Weekly, Monthly etc) or scroll my starting starting point constantly changes.

Sometimes I only want to see the relative performance for the current month, quarter, year to date or some other period starting at a specific time. I have 20 years of history but is there a way I can chart just a subset of it starting at a specific date?

If it makes any difference I'm subscribed to Norgate data and access the data via the Norgate Data Plug-in

There is always a way... in AmiBroker.

Since you talk about this page's code here.... you just have to change view things.

Things I modified are...
From this one

fvb = Status( "firstvisiblebar" );

to this one

Param_dt = ParamDate("Select Date", "2019-01-01", 2);
bi = BarIndex();
fvb = Status( "firstvisiblebar" );
fvb = Nz(Lookup(bi, Param_dt), fvb);

Adding line

bi_cond = bi >= fvb;

To be used within loop (to plot only since start date)... so we get as a whole....

/// @link http://www.amibroker.com/kb/2015/08/29/how-to-add-symbol-labels-to-relative-performance-chart/
/// modification to set start date
/// @link https://forum.amibroker.com/t/fixed-starting-point-for-charts/15488/1
Version(6.20);
_N( TickerList = ParamStr( "Tickers", "^DJI,MSFT,GE" ) );
Param_dt = ParamDate("Select Date", "2019-01-01", 2);
fontsize = Param("Label font size", 10, 4, 30, 1 );
///
bi = BarIndex();
fvb = Status( "firstvisiblebar" );
fvb = Nz(Lookup(bi, Param_dt), fvb);
bi_cond = bi >= fvb;
///
for ( i = 0; ( symbol = StrExtract( Name() + "," + TickerList, i ) ) != ""; i++ ) 
{
    fc = Foreign( symbol, "C" );
    if ( ! IsNull( fc[ 0 ] ) ) 
    {
        relP = 100 * ( fc - fc[ fvb ] ) / fc[ fvb ];
        x = LastValue( bi ) + 1;
        y = LastValue( relP );
        //
        Plot( IIf( bi_cond, relP, Null), symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleLine );
        PlotTextSetFont( symbol, "Arial", fontsize, x, y, GetChartBkColor(), color, -fontsize/2 );
    }
}
///
PlotGrid( 0, colorYellow );
_N(Title = StrFormat( "{{NAME}} - Start Date: %s, Relative Performance [%%]: {{VALUES}}", DateTimeToStr(Param_dt, 1))); 

8

6 Likes

@BoNeZ ,

Two more methods for this,

1. Using TimeFrame Compress/Expand on Charts:

SetChartOptions( 1, chartShowDates );

WatchListNum = 5; // As per "index.txt" located in AmiBroker Database Directory -> Watchlists folder. Count starts from "0".
SymList = CategoryGetSymbols( categoryWatchlist, WatchListNum ); // To retrieve the Tickers from Watchlist # 5. Just add/remove symbols to/from the watchlist.

SelTF = ParamList( "Select Timeframe", "Daily|Weekly|Monthly|Quaterly|Yearly", 0 );
SelPer = ParamList( "Select Period", "Selected|Previous Till Selected|Prev-To-Prev Till Selected", 0 );
LblFntSz = Param( "Label font size", 8, 4, 30, 1 );

err = 0;
NoLabel = 0;
bi = BarIndex();
switch( SelTF ) {
	 case "Daily":
		 if( Interval() <= inDaily ) {
			 switch( SelPer ) {
				 case "Selected":
					 if( Interval() == inDaily ) {
						 err = 1;
						 errMsg = "Select any chart Interval less than Daily Timeframe,\nto view selected day's Relative Performance chart.";
					 }
					 else {
						 BeginBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressOpen ), inDaily, expandFirst ) );
						 EndBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressLast ), inDaily, expandFirst ) );						 
					 }
				 break;
			 
				 case "Previous Till Selected":
					 if( Interval() == inDaily ) {
						 BeginBar = SelectedValue( Ref( bi, -1 ) );
						 EndBar = SelectedValue( bi );
						 NoLabel = 1;
					 }
					 else {
						 BeginBar = SelectedValue( TimeFrameExpand( Ref( TimeFrameCompress( bi, inDaily, compressOpen ), -1 ), inDaily, expandFirst ) );
						 EndBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressLast ), inDaily, expandFirst ) );						 
					 }
				 break;
			 
				 case "Prev-To-Prev Till Selected":
					 if( Interval() == inDaily ) {
						 BeginBar = SelectedValue( Ref( bi, -2 ) );
						 EndBar = SelectedValue( bi );
						 NoLabel = 1;
					 }
					 else {
						 BeginBar = SelectedValue( TimeFrameExpand( Ref( TimeFrameCompress( bi, inDaily, compressOpen ), -2 ), inDaily, expandFirst ) );
						 EndBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressLast ), inDaily, expandFirst ) );
					 }
				 break;
			 }
		 }
		 else {
			 err = 1;
			 errMsg = "Select chart's Interval less than or equal to Daily,\nto view Daily Relative Performance chart";
		 }
	 break;
	 
	 case "Weekly":
		 if( Interval() <= inWeekly ) {
			 switch( SelPer ) {
				 case "Selected":
					 if( Interval() == inWeekly ) {
						 err = 1;
						 errMsg = "Select any chart Interval less than Weekly Timeframe,\nto view selected week's Relative Performance chart.";
					 }
					 else {
						 BeginBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inWeekly, compressOpen ), inWeekly, expandFirst ) );
						 EndBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inWeekly, compressLast ), inWeekly, expandFirst ) );						 
					 }
				 break;
			 
				 case "Previous Till Selected":
					 if( Interval() == inWeekly ) {
						 BeginBar = SelectedValue( Ref( bi, -1 ) );
						 EndBar = SelectedValue( bi );
						 NoLabel = 1;
					 }
					 else {
						 BeginBar = SelectedValue( TimeFrameExpand( Ref( TimeFrameCompress( bi, inWeekly, compressOpen ), -1 ), inWeekly, expandFirst ) );
						 EndBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inWeekly, compressLast ), inWeekly, expandFirst ) );						 
					 }
				 break;
			 
				 case "Prev-To-Prev Till Selected":
					 if( Interval() == inWeekly ) {
						 BeginBar = SelectedValue( Ref( bi, -2 ) );
						 EndBar = SelectedValue( bi );
						 NoLabel = 1;
					 }
					 else {
						 BeginBar = SelectedValue( TimeFrameExpand( Ref( TimeFrameCompress( bi, inWeekly, compressOpen ), -2 ), inWeekly, expandFirst ) );
						 EndBar = SelectedValue( TimeFrameExpand( TimeFrameCompress( bi, inWeekly, compressLast ), inWeekly, expandFirst ) );
					 }
				 break;
			 }
		 }
		 else {
			 err = 1;
			 errMsg = "Select chart's Interval less than or equal to Weekly,\nto view Weekly Relative Performance chart";
		 }
	 break;
	 
	 /*
	 SelTF case for Monthly, Quaterly, Yearly in similar manner
	 */
}

/*
Rest is similar to the KB "How to add symbol labels to Relative Performance chart" apart from few modifications as requested.
(Link https://www.amibroker.com/kb/2015/08/29/how-to-add-symbol-labels-to-relative-performance-chart/)
*/
if( err ) {
	 SetChartBkColor( ColorRGB( 50, 0, 0 ) );
	 Title = errMsg;
}
else {
	 PosX = ( BeginBar + 9 * EndBar ) / 10;
	 for( i = 0; ( symbol = StrExtract( SymList, i ) ) != ""; i++ ) {
		 fc = Foreign( symbol, "C" );
		 if( ! IsNull( fc[ 0 ] ) ) {
			 RelP = IIf( bi	>= BeginBar && bi <= EndBar, 100 * ( fc - fc[ BeginBar ] ) / fc[ BeginBar ], Null );		 
			 Plot( RelP, symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleNoLabel );
			 if( !NoLabel )
				 PlotTextSetFont( symbol, "Arial", LblFntSz, PosX, RelP[ PosX ], GetChartBkColor(), color, -LblFntSz / 2 );
		 }
	 }
	 _N( Title = StrFormat( "%s %s Relative Performance %% in {{INTERVAL}} interval\n{{VALUES}}", SelPer, SelTF ) );
}

/*_______________________________________________________________________________________________________________________
|This code is for AmiBroker Formula Language (AFL) learning (non-commercial) purposes only. Please do not copy this code|
|(or any other version of it) and paste it over on other forums or anywhere else on the Internet or in any other form	|
|without the AmiBroker Forum owner's consent (https://forum.amibroker.com/).                                            |
_______________________________________________________________________________________________________________________*/
//https://forum.amibroker.com/t/fixed-starting-point-for-charts/15488/3?u=cougar

1


2. Using Scatter (X-Y) charts in Exploration:

The last section of this guide introduces XYChartSetAxis and XYChartAddPoint. Also, @ beppe and @ fxshrat has also provided similar examples in this forum.

ChName = "Rel. Performance";
XYChartSetAxis( ChName, "", "[%]", styleLine );

bi = BarIndex();
bir = Status( "BarInRange" );
fbir = LastValue( ValueWhen( Status( "FirstBarInRange" ), bi ) );
lbir = LastValue( ValueWhen( Status( "LastBarInRange" ), bi ) );

RelP = 100 * ( C - C[ fbir ] ) / C[ fbir ];
color = colorLightOrange + ( ( 2 * Status( "StockNum" ) ) % 15 );
for( j = 0; j < BarCount; j++ ) if( bir[ j ] ) XYChartAddPoint( ChName, "", j, RelP[ j ], color, color );

XYChartAddPoint( ChName, Name(), lbir, RelP[ lbir ], color, color );
XYChartAddPoint( ChName, "", Null, Null );

/*_______________________________________________________________________________________________________________________
|This code is for AmiBroker Formula Language (AFL) learning (non-commercial) purposes only. Please do not copy this code|
|(or any other version of it) and paste it over on other forums or anywhere else on the Internet or in any other form	|
|without the AmiBroker Forum owner's consent (https://forum.amibroker.com/).                                            |
_______________________________________________________________________________________________________________________*/
//https://forum.amibroker.com/t/fixed-starting-point-for-charts/15488/3?u=cougar

5 Likes

Thanks @fxshrat and @Cougar

Amibroker is great and made better by people like you helping.

I've looked at the solutions and for now will use the one from @fxshrat which is based on the sample code I'm already using.

I like the idea of using a scatter (X-Y) charts in exploration but not all the stocks I'm selecting have the same index number for my start and end dates meaning they don't all start and end at the same point on the chart. This is really noticeable when I compare a stock with 900 bars of history with a stock with 5000 bars.

When I pick Australian banks I get the following chart, Most have been around longer than I have data for but one has only traded since 2009 and another since 2016

image

I've thought of ways to fix this but want the date on the x axis rather than the index number.

1 Like