Help with the Profit table exploration

Hi there! I hope you're fine.

I'm using the following AFL I found in the forum to explore the Profit table of my equity. I tried my best to remove the ticker column but I couldn't:
1.- First I commented line 88: Only the Ticker Header disappeared
AddColumn(Null, "Ticker", 1);

2.- Then I changed line 141: The ticker name disappeared but the column stayed blank.
AddRow(StrFormat("%s\t%g\t%s%"+fmt+"%%",Name(),y,ar_str,x)); for AddRow(StrFormat("\t%g\t%s%"+fmt+"%%",y,ar_str,x));

Before
imatge

After my modifications
imatge

Could anyone put some light in my darkness, please?

I enclose the code I'm using

EnableTextOutput( 3 ); // enable HTML output into report (Version 5.84 or higher!)
/// HTML Report table AFL code copyright (C)2014 AmiBroker.com
/// Modified (08/2021) by fxshrat@gmail.com (added output to Explorer, loop replacement) 
/// @link https://forum.amibroker.com/t/problem-to-achieve-table-profit-to-csv/27325/6
eq = C;

yr = Year();
mo = Month();

YearChange = yr != Ref( yr, 1 );
MonChange = mo != Ref( mo, 1 );

FirstYr = 0;
LastYr = 0;

////////////////////////////
// SKIP non-trading bars
////////////////////////////
// Removed loop
bi = BarIndex();
fbr = Status("firstbarinrange");
//is_equal = SumSince(fbr,eq==0) == BarsSince(fbr);
is_equal = SumSince(fbr,Nz(eq)==0) == BarsSince(fbr); // cambio fxshrat
startbar = LastValue(ValueWhen(is_equal, bi));
endbar = BarCount - 1;

////////////////////////////
// collect yearly / monthly changes in equity
// into dynamic variables
////////////////////////////

LastYrValue = eq[ startbar ];
LastMoValue = eq[ startbar ];
MaxYrProfit = MinYrProfit = 0;
MaxMoProfit = MinMoProfit = 0;

for ( i = startbar + 1; i <= endbar; i++ )
{
    if ( YearChange[ i ] || i == endbar )
    {
        Chg = 100 * ( -1 + eq[ i ] / LastYrValue );
        VarSet( "ChgYear" + yr[ i ], Chg );

        MaxYrProfit = Max( MaxYrProfit, Chg );
        MinYrProfit = Min( MinYrProfit, Chg );

        if ( FirstYr == 0 )
            FirstYr = yr[ i ];

        LastYr = yr[ i ];

        LastYrValue = eq[ i ];
    }

    if ( MonChange [ i ] || i == endbar )
    {
        mon = mo[ i ];

        Chg = 100 * ( -1 + eq[ i ] / LastMoValue );

        VarSet( "ChgMon" + yr[ i ] + "_" + mon, Chg );
        VarSet( "SumChgMon" + mon, Chg + Nz( VarGet( "SumChgMon" + mon ) ) );
        VarSet( "SumMon" + mon, 1 + Nz( VarGet( "SumMon" + mon ) ) );

        MaxMoProfit = Max( MaxMoProfit, Chg );
        MinMoProfit = Min( MinMoProfit, Chg );

        LastMoValue = eq[ i ];
    }
}

MonthNames = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";

function GenProfitTableHTMLAndExplorer( )
{
    printf( "<table border='1' bordercolor='#000000' cellspacing='0' cellpadding='3'style='border-collapse:collapse;'>\n" );
    printf( "<tr bgcolor='#eeffee' >\n" );

    Header = "Year," + MonthNames + ",Yr%%";

    for ( Col = 0; ( Colname = StrExtract( Header, Col ) ) != ""; Col++ )
    {
        printf( "<td><b>" + Colname + "</b></td>" );
    }

    printf( "</tr>\n" );

    AddColumn(Null, "Ticker", 1);
    AddColumn(Null, "Year", 1);
    for ( m = 1; m <= 12; m++ )
        AddColumn(Null, StrExtract(MonthNames,m-1), 1.2);
    AddColumn(Null, "Yr%", 1.2);
	
    fmt = ".1f";

    for ( y = FirstYr; y <= LastYr; y++ )
    {
        //Color =  ColorRGB( IIf( row == 0 || col == 0 || col == 13, 220, 255 ), 255, IIf( row % 2, 255, 220 ) );

        // new row
        if ( y % 2 )
            printf( "<tr bgcolor='#ffffff'>\n<td bgcolor='#eeffff'>" );
        else
            printf( "<tr bgcolor='#ffffee'>\n<td bgcolor='#eeffee'>" );

        printf( "<b>%g</b></td>", y );

        ar_str = "";
        for ( m = 1; m <= 12; m++ )
        {
            Chg = VarGet( "ChgMon" + y + "_" + m );

            if ( NOT IsNull( Chg ) )
            {
                if ( Chg >= 0 )
                    printf( "<td nowrap>%"+fmt+"%%</td>", Chg );
                else
                    printf( "<td nowrap><font color='880000'>%"+fmt+"%%</font></td>", Chg );
                
                ar_str += StrFormat("%"+fmt+"%%\t", Chg);
            }
            else {
                printf( "<td></td>" );
                ar_str += "\t";
            }
        }

        if ( y % 2 )
            //printf( "<td nowrap bgcolor='#eeffff'>" );
        //else
            printf( "<td nowrap bgcolor='#eeffee'>" );

        x = VarGet( "ChgYear" + y );

        if ( x >= 0 )
            printf( "<b>%"+fmt+"%%</b></td>", x );
        else
            printf( "<font color='880000'><b>%"+fmt+"%%</b></font></td>", x );
        
        x_str = WriteIf(IsNull(x), "N/A", StrFormat("%"+fmt+"%%",x));         	// fxshrat
		AddRow(StrFormat("%s\t%g\t%s%"+fmt+"%%",Name(),y,ar_str,x));   			// fxshrat
        
		//AddRow(StrFormat("\t%g\t%s%"+fmt+"%%",y,ar_str,x));  

        printf( "</tr>\n" ); // end row
    }


    printf( "<tr bgcolor='#eeffee' >\n" ); // new row
    printf( "<td><b>Avg</b></td>" );

    ar_str = "";
    for ( m = 1; m <= 12; m++ )
    {
        x = Nz( VarGet( "SumChgMon" + m ) / VarGet( "SumMon" + m ) );

        if ( x >= 0 )
            printf( "<td nowrap><b>%"+fmt+"%%</b></td>", x );
        else
            printf( "<td nowrap><font color='880000'><b>%"+fmt+"%%</b></font></td>", x );
        
        ar_str += StrFormat("%"+fmt+"%%\t", x);
    }
    //AddRow(Name()+"\tAverage:\t"+ar_str);

    printf( "<td>&nbsp;</td>" );
    printf( "</tr></table>\n" );

}

///////////////////////////
// This function checks if currently selected symbol
// is portfolio equity
//////////////////////////
function CheckSymbol()
{
    if ( Name() != "~~~EQUITY" AND Name() != "~~~OSEQUITY" )
    {
        printf( "For accurate results switch to ~~~EQUITY symbol<br>" );
    }
}

CheckSymbol();

////////////////////////////
// Main program
////////////////////////////
SetOption("NoDefaultColumns", 1);
Filter = 0;
SetSortColumns( -2 ); 

GenProfitTableHTMLAndExplorer();

It is not enough to comment column and remove ticker name from original code.
In addition you need to remove \t column separator.

AddRow(StrFormat("%g\t%s%"+fmt+"%%",y,ar_str,x));

Entire code ->

EnableTextOutput( 3 ); // enable HTML output into report (Version 5.84 or higher!)
/// HTML Report table AFL code copyright (C)2014 AmiBroker.com
/// Modified (08/2021) by fxshrat@gmail.com (added output to Explorer, loop replacement) 
/// @link https://forum.amibroker.com/t/problem-to-achieve-table-profit-to-csv/27325/6
eq = C;

yr = Year();
mo = Month();

YearChange = yr != Ref( yr, 1 );
MonChange = mo != Ref( mo, 1 );

FirstYr = 0;
LastYr = 0;

////////////////////////////
// SKIP non-trading bars
////////////////////////////
// Removed loop
bi = BarIndex();
fbr = Status("firstbarinrange");
//is_equal = SumSince(fbr,eq==0) == BarsSince(fbr);
is_equal = SumSince(fbr,Nz(eq)==0) == BarsSince(fbr); // cambio fxshrat
startbar = LastValue(ValueWhen(is_equal, bi));
endbar = BarCount - 1;

////////////////////////////
// collect yearly / monthly changes in equity
// into dynamic variables
////////////////////////////

LastYrValue = eq[ startbar ];
LastMoValue = eq[ startbar ];
MaxYrProfit = MinYrProfit = 0;
MaxMoProfit = MinMoProfit = 0;

for ( i = startbar + 1; i <= endbar; i++ )
{
    if ( YearChange[ i ] || i == endbar )
    {
        Chg = 100 * ( -1 + eq[ i ] / LastYrValue );
        VarSet( "ChgYear" + yr[ i ], Chg );

        MaxYrProfit = Max( MaxYrProfit, Chg );
        MinYrProfit = Min( MinYrProfit, Chg );

        if ( FirstYr == 0 )
            FirstYr = yr[ i ];

        LastYr = yr[ i ];

        LastYrValue = eq[ i ];
    }

    if ( MonChange [ i ] || i == endbar )
    {
        mon = mo[ i ];

        Chg = 100 * ( -1 + eq[ i ] / LastMoValue );

        VarSet( "ChgMon" + yr[ i ] + "_" + mon, Chg );
        VarSet( "SumChgMon" + mon, Chg + Nz( VarGet( "SumChgMon" + mon ) ) );
        VarSet( "SumMon" + mon, 1 + Nz( VarGet( "SumMon" + mon ) ) );

        MaxMoProfit = Max( MaxMoProfit, Chg );
        MinMoProfit = Min( MinMoProfit, Chg );

        LastMoValue = eq[ i ];
    }
}

MonthNames = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";

function GenProfitTableHTMLAndExplorer( )
{
    printf( "<table border='1' bordercolor='#000000' cellspacing='0' cellpadding='3'style='border-collapse:collapse;'>\n" );
    printf( "<tr bgcolor='#eeffee' >\n" );

    Header = "Year," + MonthNames + ",Yr%%";

    for ( Col = 0; ( Colname = StrExtract( Header, Col ) ) != ""; Col++ )
    {
        printf( "<td><b>" + Colname + "</b></td>" );
    }

    printf( "</tr>\n" );

    //AddColumn(Null, "Ticker", 1); // no ticker column
    AddColumn(Null, "Year", 1);
    for ( m = 1; m <= 12; m++ )
        AddColumn(Null, StrExtract(MonthNames,m-1), 1.2);
    AddColumn(Null, "Yr%", 1.2);
	
    fmt = ".1f";

    for ( y = FirstYr; y <= LastYr; y++ )
    {
        //Color =  ColorRGB( IIf( row == 0 || col == 0 || col == 13, 220, 255 ), 255, IIf( row % 2, 255, 220 ) );

        // new row
        if ( y % 2 )
            printf( "<tr bgcolor='#ffffff'>\n<td bgcolor='#eeffff'>" );
        else
            printf( "<tr bgcolor='#ffffee'>\n<td bgcolor='#eeffee'>" );

        printf( "<b>%g</b></td>", y );

        ar_str = "";
        for ( m = 1; m <= 12; m++ )
        {
            Chg = VarGet( "ChgMon" + y + "_" + m );

            if ( NOT IsNull( Chg ) )
            {
                if ( Chg >= 0 )
                    printf( "<td nowrap>%"+fmt+"%%</td>", Chg );
                else
                    printf( "<td nowrap><font color='880000'>%"+fmt+"%%</font></td>", Chg );
                
                ar_str += StrFormat("%"+fmt+"%%\t", Chg);
            }
            else {
                printf( "<td></td>" );
                ar_str += "\t";
            }
        }

        if ( y % 2 )
            //printf( "<td nowrap bgcolor='#eeffff'>" );
        //else
            printf( "<td nowrap bgcolor='#eeffee'>" );

        x = VarGet( "ChgYear" + y );

        if ( x >= 0 )
            printf( "<b>%"+fmt+"%%</b></td>", x );
        else
            printf( "<font color='880000'><b>%"+fmt+"%%</b></font></td>", x );
        
        x_str = WriteIf(IsNull(x), "N/A", StrFormat("%"+fmt+"%%",x));         	// fxshrat
		//AddRow(StrFormat("%s\t%g\t%s%"+fmt+"%%",Name(),y,ar_str,x));   			// fxshrat        
		AddRow(StrFormat("%g\t%s%"+fmt+"%%",y,ar_str,x));  // no ticker column

        printf( "</tr>\n" ); // end row
    }


    printf( "<tr bgcolor='#eeffee' >\n" ); // new row
    printf( "<td><b>Avg</b></td>" );

    ar_str = "";
    for ( m = 1; m <= 12; m++ )
    {
        x = Nz( VarGet( "SumChgMon" + m ) / VarGet( "SumMon" + m ) );

        if ( x >= 0 )
            printf( "<td nowrap><b>%"+fmt+"%%</b></td>", x );
        else
            printf( "<td nowrap><font color='880000'><b>%"+fmt+"%%</b></font></td>", x );
        
        ar_str += StrFormat("%"+fmt+"%%\t", x);
    }
    //AddRow(Name()+"\tAverage:\t"+ar_str);

    printf( "<td>&nbsp;</td>" );
    printf( "</tr></table>\n" );

}

///////////////////////////
// This function checks if currently selected symbol
// is portfolio equity
//////////////////////////
function CheckSymbol()
{
    if ( Name() != "~~~EQUITY" AND Name() != "~~~OSEQUITY" )
    {
        printf( "For accurate results switch to ~~~EQUITY symbol<br>" );
    }
}

CheckSymbol();

////////////////////////////
// Main program
////////////////////////////
SetOption("NoDefaultColumns", 1);
Filter = 0;
SetSortColumns( -2 ); 

GenProfitTableHTMLAndExplorer();
4 Likes

Thank you very much @fxshrat :smiling_face:

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