Almost... 
I think there is something not compatible with the explorers that I run.
I put the if like that to try to avoid execute the exploration for each symbol.
if ( StrToUpper(Name()) == nm )
#include <MyExplorations_Equity.afl>
//// @link https://forum.amibroker.com/t/efficiency-in-the-automation-of-tasks/29623/8
/// Batch (portfolio backtest):
/// Load apx
/// Backtest
/// Run exploration 1
/// Export to file
/// Scan to CategoryAdd/Remove
/// Run exploration 2
/// Export to file
// Put functions to include file
procedure RunExploration1(nm) {
global Filter;
Filter = 0;
if ( StrToUpper(Name()) == nm ){
#include <MyExplorations_Equity.afl>
}
}
procedure RunExploration2(nm) {
global Filter;
Filter = 0;
if ( StrToUpper(Name()) == nm ){
#include <MyExplorations_TableProfit.afl>
}
}
procedure RunExploration(nm) {
global Filter;
// based on AddRow
if ( nm == "~EQUITY1" ) {
RunExploration1(nm);
}
// based on AddColumn only
if ( nm == "~EQUITY2" ) {
RunExploration2(nm);
}
}
watchlist_number = GetOption("FilterIncludeWatchlist");
is_scan = Status( "action" ) == actionScan;
// batch step - Backtest
if ( NOT StrFind(Name(), "~") AND NOT is_scan ) {
/*
Backtest code
*/
period = 20; // number of averaging periods
m = MA( Close, period ); // simple moving average
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
Short = Cover = 0;
}
// custom equity symbol(s) creation
SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest();
AddToComposite( bo.EquityArray, "~Equity1", "X",atcFlagDeleteValues | atcFlagEnableInPortfolio );
AddToComposite( bo.EquityArray, "~Equity2", "X",atcFlagDeleteValues | atcFlagEnableInPortfolio );
CategoryAddSymbol("~Equity1", categoryWatchlist, watchlist_number);
CategoryRemoveSymbol("~Equity2", categoryWatchlist, watchlist_number);
}
// Batch step - Scan
if ( is_scan AND Status("stocknum") == 0 ) {
CategoryRemoveSymbol("~Equity1", categoryWatchlist, watchlist_number);
CategoryAddSymbol("~Equity2", categoryWatchlist, watchlist_number);
}
// Exploration batch step(s)
sym_list = CategoryGetSymbols(categoryWatchlist, watchlist_number);// all upper case
sym_num = StrCount(sym_list, ",");
last_sym = StrExtract(sym_list, sym_num);
_TRACE(last_sym);
RunExploration(last_sym);
#include <MyExplorations_Equity.afl>
fbr = Status("firstbarinrange");// flag first bar of any analysis range
firstClose = LastValue(ValueWhen(Ref(fbr,-2), Close));// firstClose - not an array b
Filter = 1;
//eqname = "~~~EQUITY";
eqname=ParamStr("Ticker","~~~XXXX");
if( Name() != eqname ) SetForeign( eqname );
eq = Close;
Cash = Low;
dr = eq - Highest(eq);
// BENCHMARK
benchmark="^SPX";
SetForeign(benchmark);
eq2 = Close;
Cash2 = Low;
dr2 = eq2 - Highest(eq2);
fbr2 = Status("firstbarinrange");// flag first bar of any analysis range
firstClose2 = LastValue(ValueWhen(Ref(fbr,-1), Close));// firstClose - not an array b
RestorePriceArrays();
InitialEquity= firstClose;
InitialEquity2=firstClose2;
SetOption( "NoDefaultColumns", True );
AddColumn( DateTime(), "Date/Time", formatDateTime);
AddColumn(firstClose, "firstClose");
AddColumn(eq, "Portfolio Equity");
AddColumn(ROC(C,1), "Portfolio Equity PTC");
AddColumn(cash, "Cash");
AddColumn(dr, "Drawdown");
//AddColumn((eq/cash)*100, "Drawdown %"); // nueva
AddColumn((dr/Highest(eq))*100, "Drawdown %"); // nueva
AddColumn((eq/100000-1)*100, "Rentabilidad %"); // nueva
AddTextColumn(Name(),"Symbol");
AddColumn(eq2, "Benchmark");
AddColumn(ROC(eq2,1), "Benchmark PTC");
AddColumn(cash2, "Benchmark Cash");
AddColumn(dr2, "Benchmark Drawdown");
AddColumn((dr2/Highest(eq2))*100, "Benchmark Drawdown %"); // nueva
AddColumn((eq2/InitialEquity2-1)*100, "Rentabilidad Benchmark %"); // nueva
AddTextColumn(benchmark,"Benchmark Symbol");
#include <MyExplorations_TableProfit.afl>
/*
MonthlyTable_01.afl
Exploration code to produce a month-by-month tabular presentation of percentage changes
in a value.
In this code the value is designated as "eq" (for equity), but is simply set to the
price Close for demonstration purposes.
Original code provided by "Mike", here:
http://finance.groups.yahoo.com/group/amibroker/message/125846
Extra comments, _TRACE, and some changes by Progster
*/
// Calculation of the value for which the tabular display of %Chg is desired.
// Change this to whatever calculation you wish.
//SetForeign("~~~BenderDiario2022_SinDividendo");
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";
MonthNames = "Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Set,Oct,Nov,Dic";
if ( Name() != "~~~EQUITY" AND Name() != "~~~OSEQUITY" )
{
printf( "For accurate results switch to ~~~EQUITY symbol<br>" );
}
////////////////////////////
// Main program
////////////////////////////
SetOption("NoDefaultColumns", 1);
Filter = 0;
SetSortColumns( -1 );
printf( "<table border='1' bordercolor='#000000' cellspacing='0' cellpadding='3'style='border-collapse:collapse;'>\n" );
printf( "<tr bgcolor='#eeffee' >\n" );
Header = "Año," + MonthNames + ",Anual%%";
for ( Col = 0; ( Colname = StrExtract( Header, Col ) ) != ""; Col++ )
{
printf( "<td><b>" + Colname + "</b></td>" );
}
printf( "</tr>\n" );
//AddColumn(Null, "Ticker", 1);
AddColumn(Null, "Año", 1);
for ( m = 1; m <= 12; m++ )
AddColumn(Null, StrExtract(MonthNames,m-1), 1.2);
AddColumn(Null, "Anual%", 1.2);
fmt = ".2f";
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> </td>" );
printf( "</tr></table>\n" );
If you try to create the two includes / explorers and run it only on the current symbol example over. ~Equity1 you will see that it works fine, but inside the AFL that we are trying to set up its behavior is completely different and I don't know why 
Thank you very much