Having trouble with my own report charts

Hi,
I'm having trouble using the Report metrics to create custom Report Chart AFLs. Here's an example with a single metric, the CAR (%).

With this AFL:


Title = "";
SetGradientFill( colorLime, colorPaleGreen  );
Plot( C, "Equity", ColorBlend( colorPaleGreen, colorBlack ), styleGradient | styleLine , Null, Null, 0, -1 );

// CÁLCULAR "A MANO" LA MÉTRICA CAR(%)
function CalendarDays()
{  
    ddsince1900 = DaysSince1900();
    result = ddsince1900 - ddsince1900[0];
    return result;
}

EQ = C;

bi       = BarIndex();
fbr      = Status( "firstbarinrange" );
lbr      = Status( "lastbarinrange"  );
fbrbi    = LastValue( ValueWhen( fbr, bi ) );
lbrbi    = LastValue( ValueWhen( lbr, bi ) );
cd       = CalendarDays();
Days     = cd[ lbrbi ] - cd[ fbrbi ];
CAR      = 100 * ( ( eq / eq[ fbrbi ] ) ^ ( 365 / Days ) - 1 ); 


// TABLA GRÁFICA
_SECTION_BEGIN("GFX Table Backtest");

cellHeight = 20; 
cellWidth  = 60;

startX = 20;       
startY = 40;       

headerColor = ColorRGB(180, 210, 255);  
valueColor  = colorWhite;              
borderColor = colorWhite;
textColor   = colorBlack;

function Celda(text, col, row, bgColor)
{
    x1 = startX + (col - 1) * cellWidth;
    y1 = startY + (row - 1) * cellHeight;
    x2 = x1 + cellWidth;
    y2 = y1 + cellHeight;

    GfxSelectPen(borderColor, 1);
    GfxSelectSolidBrush(bgColor);
    GfxRectangle(x1, y1, x2, y2);

    GfxSetBkColor(bgColor);
    GfxSetTextColor(textColor);
    GfxSetTextAlign( 6 | 0 );  
    GfxSelectFont("Arial", 10, 700 );
    GfxTextOut(text, x1 + cellWidth/2, y1 + cellHeight/8);
}

// MÉTRICAS DEL BACKTEST 
CAR_lv        = LastValue(CAR);

//    FILA 1 - NOMBRES
Celda("CAR %", 1, 1, headerColor);

//    FILA 2 - VALORES
Celda( StrFormat("%.2f", CAR_lv), 1, 2, valueColor);

_SECTION_END();

I get what I want, but I had to manually calculate the CAR (%).

What I'm trying to do is take advantage of the fact that the Report already provides the CAR (%) so I don't have to calculate it, just get it and use it in my Report Chart.

Screenshot_17

To do this, I'm using this AFL:


Title = "";
SetGradientFill( colorLime, colorPaleGreen  );
Plot( C, "Equity", ColorBlend( colorPaleGreen, colorBlack ), styleGradient | styleLine , Null, Null, 0, -1 );

// OBTENER MÉTRICA CAR(%) DEL REPORT

SetCustomBacktestProc(""); 

if( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 
    bo.Backtest();
    st = bo.GetPerformanceStats(0);

    car_value = st.CAR;
    
    StaticVarSet( "CAR_Value_Stored", st.CAR );
}

CAR      = StaticVarGet( "CAR_Value_Stored" );




// TABLA GRÁFICA
_SECTION_BEGIN("GFX Table Backtest");

cellHeight = 20; 
cellWidth  = 60;

startX = 20;       
startY = 40;       

headerColor = ColorRGB(180, 210, 255);  
valueColor  = colorWhite;              
borderColor = colorWhite;
textColor   = colorBlack;

function Celda(text, col, row, bgColor)
{
    x1 = startX + (col - 1) * cellWidth;
    y1 = startY + (row - 1) * cellHeight;
    x2 = x1 + cellWidth;
    y2 = y1 + cellHeight;

    GfxSelectPen(borderColor, 1);
    GfxSelectSolidBrush(bgColor);
    GfxRectangle(x1, y1, x2, y2);

    GfxSetBkColor(bgColor);
    GfxSetTextColor(textColor);
    GfxSetTextAlign( 6 | 0 );  
    GfxSelectFont("Arial", 10, 700 );
    GfxTextOut(text, x1 + cellWidth/2, y1 + cellHeight/8);
}

// MÉTRICAS DEL BACKTEST 
CAR_lv        = LastValue(CAR);

//    FILA 1 - NOMBRES
Celda("CAR %", 1, 1, headerColor);

//    FILA 2 - VALORES
Celda( StrFormat("%.2f", CAR_lv), 1, 2, valueColor);

_SECTION_END();


Which isn't working.

I know I'm doing it wrong, very wrong and missing something fundamental. I think I'm getting confused and I don't know where to go from here...

Quite obviously this part

if( Status("action") == actionPortfolio ) 

of code DOES NOT get exected at all because it is only executed during backtest (in second phase of backtest).

Report charts ARE NOT run context of backtest, but separately AFTER backtest is completed, when ~~~EQUITY ticker is populated with equity.

Therefore this code section should be put INTO YOUR SYSTEM code so it gets executed, NOT in "report charts".

1 Like

Thanks, Dr. Tomasz.

I think I'm starting to understand. Let's continue with the CAR (%). metric.

I have this AFL to obtain the metric:

SetCustomBacktestProc(""); 

if( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 
    
    bo.Backtest();
    stats = bo.GetPerformanceStats(0);

    car_value = stat.GetValue("CAR");
    
    StaticVarSet( "CAR_Value_Stored", car_value );
}

and I call it from Settings/Custom backtest procedure path:

Then I run the backtest and open the Report Chart. Inside the Report chart folder, I have this AFL:


Title = "";
SetGradientFill( colorLime, colorPaleGreen  );
Plot( C, "Equity", ColorBlend( colorPaleGreen, colorBlack ), styleGradient | styleLine , Null, Null, 0, -1 );

// OBTENER MÉTRICA CAR(%) DEL REPORT

CAR      = StaticVarGet( "CAR_Value_Stored" );


// TABLA GRÁFICA
_SECTION_BEGIN("GFX Table Backtest");

cellHeight = 20; 
cellWidth  = 60;

startX = 20;       
startY = 40;       

headerColor = ColorRGB(180, 210, 255);  
valueColor  = colorWhite;              
borderColor = colorWhite;
textColor   = colorBlack;

function Celda(text, col, row, bgColor)
{
    x1 = startX + (col - 1) * cellWidth;
    y1 = startY + (row - 1) * cellHeight;
    x2 = x1 + cellWidth;
    y2 = y1 + cellHeight;

    GfxSelectPen(borderColor, 1);
    GfxSelectSolidBrush(bgColor);
    GfxRectangle(x1, y1, x2, y2);

    GfxSetBkColor(bgColor);
    GfxSetTextColor(textColor);
    GfxSetTextAlign( 6 | 0 );  
    GfxSelectFont("Arial", 10, 700 );
    GfxTextOut(text, x1 + cellWidth/2, y1 + cellHeight/8);
}

// MÉTRICAS DEL BACKTEST 
CAR_lv        = LastValue(CAR);

//    FILA 1 - NOMBRES
Celda("CAR %", 1, 1, headerColor);

//    FILA 2 - VALORES
Celda( StrFormat("%.2f", CAR_lv), 1, 2, valueColor);

_SECTION_END();


But I still can't get the metric value in the Report Chart.

Where am I going wrong?

I did it!!!

Thank you so much for everything.

// Ejecutar el código ÚNICAMENTE en la fase de portafolio
if( Status("action") == actionPortfolio ) 
{ 
    // Obtener el Objeto Backtester (bo)
    bo = GetBacktesterObject(); 
    
    //Ejecutar el backtest por defecto
    // Si no se llama, el motor lo hace automáticamente al final del código. 
    // Para el nivel alto, podemos omitir la llamada explícita a bo.Backtest();
    bo.Backtest();
    
    //Obtener el objeto Stats. El parámetro '0' se usa para el reporte principal (General)
    stats = bo.GetPerformanceStats(0); 
    
    // Acceder a valores específicos, por ejemplo:
	NetProfit = stats.GetValue("NetProfit"); 
	TotalTrades = stats.GetValue("AllQty");
	CAR = stats.GetValue("CAR");
    
    // Calcular métricas personalizadas
    Expectancy = 0;
    if (TotalTrades > 0)
    {
        // NetProfit / TotalTrades
        Expectancy = NetProfit / TotalTrades; // Ejemplo: 15,000 $ / 100 = 150 $
    }
    
    // Añadir la métrica personalizada al reporte
    // Se usa el método AddCustomMetric del Objeto Backtester
    // El primer parámetro es el título que aparecerá en la columna/fila.
    bo.AddCustomMetric("Exp($)", Expectancy);
    
	// Crear Variables Estáticas para luego llamarlas desde los Report Chart
	StaticVarSet("Expectancy", Expectancy);
	StaticVarSet("CAR", CAR);
}
Title = "";
SetGradientFill( colorLime, colorPaleGreen  );
Plot( C, "Equity", ColorBlend( colorPaleGreen, colorBlack ), styleGradient | styleLine , Null, Null, 0, -1 );

// OBTENER MÉTRICA CAR(%) DEL CBT
Expectancy = StaticVarGet( "Expectancy" );
CAR        = StaticVarGet( "CAR" );


// TABLA GRÁFICA
_SECTION_BEGIN("GFX Table Backtest");

cellHeight = 20; 
cellWidth  = 100;

startX = 20;       
startY = 40;       

headerColor = ColorRGB(180, 210, 255);  
valueColor  = colorWhite;              
borderColor = colorWhite;
textColor   = colorBlack;

function Celda(text, col, row, bgColor)
{
    x1 = startX + (col - 1) * cellWidth;
    y1 = startY + (row - 1) * cellHeight;
    x2 = x1 + cellWidth;
    y2 = y1 + cellHeight;

    GfxSelectPen(borderColor, 1);
    GfxSelectSolidBrush(bgColor);
    GfxRectangle(x1, y1, x2, y2);

    GfxSetBkColor(bgColor);
    GfxSetTextColor(textColor);
    GfxSetTextAlign( 6 | 0 );  
    GfxSelectFont("Arial", 10, 700 );
    GfxTextOut(text, x1 + cellWidth/2, y1 + cellHeight/8);
}

// MÉTRICAS DEL BACKTEST 
Expectancy_lv        = LastValue(Expectancy);
CAR_lv               = LastValue(CAR);

//    FILA 1 - NOMBRES
Celda("Expectancy($)", 1, 1, headerColor);
Celda("CAR(%)", 2, 1, headerColor);

//    FILA 2 - VALORES
Celda( StrFormat("%.2f", Expectancy_lv), 1, 2, valueColor);
Celda( StrFormat("%.2f", CAR_lv), 2, 2, valueColor);

_SECTION_END();

I finally understand the two phases of backtesting a bit, and I'm starting to learn something about the high level of CBT. Amibroker's capabilities never cease to amaze me; it really seems like you can do anything with it.

Maybe... I'll even make money in the markets someday!! Hehe :wink:

3 Likes

Seems interesting, congratulation! I think I need to spend some time to understand a little about CBT if time allows.