Customize Title Chart

Hi,

Is it possible to get the symbols arranged in Vertical format at top left end arranged in descending order of relative performance? With my limited knowledge I tried this " \n {{VALUES}} " In Title Format.

But how to get each Value in NewLine and arranged in Descending order of Relative Perforamce?

Source Code:
https://www.amibroker.com/kb/2015/08/29/how-to-add-symbol-labels-to-relative-performance-chart/

_SECTION_BEGIN("Relative Performance 4");
WatchlistName = ParamList("WatchList", "List-1,List-2");
_TRACE(WatchlistName);
wlnumber = CategoryFind(WatchlistName, categoryWatchlist ); 
tickerlist = CategoryGetSymbols( categoryWatchlist, wlnumber );
_TRACE("WL number " + wlnumber);
_TRACE("Ticker list" + tickerlist);

fontsize = Param("Label font size", 10, 4, 30, 1 );
fvb = Status( "firstvisiblebar" );

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

    if( ! IsNull( fc[ 0 ] ) )
    {
        relP = 100 * ( fc - fc[ fvb ] ) / fc[ fvb ];
        Plot( relP , symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleNoLabel  );

        x = LastValue( BarIndex() ) + 1;
        y = LastValue( relP );

        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))); 
SetChartOptions(0, chartWrapTitle);
_SECTION_END();

Modified By:


Credits: @fxshrat @beppe

Modify

Plot( relP, symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleNoLabel  );

to

Plot( relP, "\n"+symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleNoLabel  );

This is not arranging them in descending order (but just per separate line).
For descending order there is more code modification required than that.
E.g. storing values to matrix and then applying MxSort().

1 Like

@fxshrat

Sir, Thank you for the help. Regarding Descending order I came across one of your posts. Could you help me what variables need to be changed in the code?

Source Code:Question regarding sorting arrays

/// @link https://forum.amibroker.com/t/question-regarding-sorting-arrays/10235/6
/// @link https://forum.amibroker.com/t/question-regarding-sorting-arrays/10235/9
//********************************** SORTING ***************************
 // I can add any number of array comma separated here
columnnames = "Open,Close";

bi = BarIndex();
fvb = FirstVisibleValue(bi);
lvb = LastVisibleValue(bi);
barnum = lvb-fvb+1;
selectedColIndex = SelectedValue(bi) - fvb;

printf("visible barnum: %g, BarCount: %g\n", barnum, BarCount);

MatrixRows = StrCount(columnnames, ",")+1;
MatrixColumns = barnum + 1;// 1 extra column for storing row idx

z = Matrix(Max(1, MatrixRows), Max(2, MatrixColumns), 0);
 
rownum = MxGetSize(z, 0);
colnum = MxGetSize(z, 1);

last_column = colnum-1;
last_bi = last_column-1;// last idx of 1-dim array

for (row=0;row < rownum; row++) {
	//Assign the arrays to each row..
	arr = VarGet(StrExtract(columnnames, row));
	z = MxSetBlock(z, row, row, 0, last_bi, arr);
	// this last column is used to store counter like a unique row Id, 
	//so if sorted i can reach out to correct column when displaying
	z[row][last_column] = row;
}

"\nBefore sorting";
MxToString(z);

//finally do a descending order sort
z = mxSortRows( z, False, selectedColIndex );

"After sorting";
MxToString(z);

//************************************** END SORTING ****************************
for (row = 0; row < rownum; row++) {
	printf( "%s:%g (column:%g)\n", StrExtract(columnnames, z[row][last_column]), z[row][selectedColIndex], selectedColIndex+1);
}

Copy and paste and changing code lines here and there is not a solution.

_SECTION_BEGIN("Relative Performance_modified");
/// Code source:
/// @link https://www.amibroker.com/kb/2015/08/29/how-to-add-symbol-labels-to-relative-performance-chart/
/// Added sorting in Chart title and Exploration output:
/// @link https://forum.amibroker.com/t/customize-title-chart/22313/4
/// by AmiBroker.com and fxshrat@gmail.com
WatchlistName = ParamStr("Insert WatchList Name", "List 0");
wlnumber = CategoryFind(WatchlistName, cat = categoryWatchlist); 
tickerlist = Name() + "," + CategoryGetSymbols(cat, wlnumber);
fontsize = Param("Label font size", 10, 4, 30, 1 );
bi = BarIndex();
fbr = Status("firstbarinrange");
Filter = Status("barinrange");
explore = Status("action") == actionExplore;
fvb = IIf(explore, LastValue(ValueWhen(fbr,bi)), Status("firstvisiblebar"));
mat = Matrix(StrCount(tickerlist, ",")+1, 2);
rownum = MxGetSize(mat, 0);
for ( i = 0; ( symbol = StrExtract(TickerList, i ) ) != ""; i++ ) {
	fc = Foreign( symbol, "C" ); 
	idx = Max(NullCount(fc), fvb);
	if (! IsNull(fc[ idx ])) {
		relP = 100 * (fc - fc[ idx ]) / fc[ idx ]; 		
		mat[i][0] = SelectedValue(relP); 
		mat[i][1] = i; 		
		x = LastValue(bi) + 1;
		y = LastValue(relP);
		VarSet("color"+i, colorLightOrange + ((2*i)%15));      
		AddColumn(relP, "RS-"+symbol, 1.2, color = Varget("color"+i));     
		Plot(relP, "\n"+symbol, color, styleNoLabel | styleNoTitle);        
		PlotTextSetFont(symbol, "Arial", fontsize, x, y, GetChartBkColor(), color, -fontsize/2);
	}
}
//
PlotGrid( 0, colorYellow );
Param_dt = FirstVisibleValue(DateTime());
_N(Title = StrFormat( "WL(%g): %s, {{NAME}} - Start Date: %s, Relative Performance [%%]: {{VALUES}}", 
						wlnumber, WatchlistName, DateTimeToStr(Param_dt, 1))); 
SetChartOptions(0, chartWrapTitle);
//
mat = MxSortRows(mat, False, 0);
//_TRACE(MxToString(mat));
for ( i = 0; i < rownum; i++ ) {
	val = mat[i][0];	n = mat[i][1];
	if (! IsNull(val)) {		
		sym = StrExtract(tickerlist, n);
		color = EncodeColor(Nz(Varget("color"+n)));
		Title += StrFormat("\n%s %s: %g", color,sym,val);		
	}
}
_SECTION_END();

14

14

6 Likes

Thank you so much for the help. I agree that Copy and paste and changing code lines is not a solution but I am not from programming background and it takes time to overcome the learning curve.

I appreciate the kind support you extend voluntarily. I thank you sincerely for your time and help.

Modified the below line to plot the values in decimals out of interest.

Title += StrFormat("\n%s %s = %.2f", color,sym,val);

Source of Learning;
http://www.amibroker.com/kb/2015/02/13/customizing-chart-titles/

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