Hello everyone,
I'd been beating myself up against the AFL for a while and couldn't figure out why the explorer said one thing and the backtest did another.
This is the AFL:
_SECTION_BEGIN( "Sistema RSC Mansfield Regular" );
// TIPO DE SISTEMA [SIEMPRE]
SetBacktestMode( backtestRegular );
// TEMPORALIDAD [SIEMPRE]
// Semanal
// PARÁMETROS DE ENTRADA [OPCIONAL]
Periodo_RSC = Param( "Periodo RSC",52,1,500,1 );
Simbolo_RSC = ParamStr( "Simbolo RSC", "$SPX" );
Periodo_WMA = Param("Periodo WMA",52,1,500,1);
// BACKTESTER SETTINGS [SIEMPRE]
SetOption("CommissionAmount", 0.00);
SetOption("InitialEquity",10000);
SetTradeDelays(0,0,0,0);
SetOption("AllowSameBarExit", True);
PosQty = Optimize("PosQty", 4,1,20,1);
SetOption("MaxOpenPositions", PosQty);
SetOption("AllowPositionShrinking",True);
// TAMAÑO DE LA POSICIÓN [SIEMPRE]
SetPositionSize(100/PosQty,spsPercentOfEquity);
// CÁLCULO INDICADORES [SIEMPRE]
Cociente = C/Foreign(Simbolo_RSC,"C");
Countr = Sum(Cociente,Periodo_RSC);
BasePrice = Countr/Periodo_RSC;
RSC = ((Cociente/BasePrice)-1)*10;
// REGLAS DEL SISTEMA [SIEMPRE]
Condicion_Compra = RSC > 1.75;
Condicion_Venta = RSC < 1.75;
// Condiciones de Compra
Buy = Condicion_Compra;
BuyPrice = Close;
// Condiciones de Venta
Sell = Condicion_Venta;
SellPrice = Close;
/* Desempate
Si se dan más señales que huecos tenemos en cartera, los ordenaremos y en este caso, elegiremos los que tengan un RSC mayor
Esto se consigue con la variable PositionScore.
Le decimos que si se da la condicion de compra, ponga el valor de RSC en la variable Score, sino que ponga 0
El PositionScore ordena los valores en valor absoluto, por lo que un RSC de -3 lo va a poner por delante de 2
Por ello, añadimos un valor de 1000 al Score, así un RSC de -3+1000=997 lo pondrá por debajo de un 2+1000=1002
Si el Score da 0, no abrirá ninguna operación y si hubiera una abierta, la cerraría
*/
Score = IIf( Buy == 1, RSC, 0 );
PositionScore = 1000 + Score;
// STOPLOSS [OPCIONAL]
SetOption( "ActivateStopsImmediately", False );
ApplyStop(stopTypeLoss,stopModePercent,8,ExitAtStop=1,Volatile = False,ReEntryDelay=0 );
//ELIMINAR SEÑALES EXTRA
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
// EXPLORADOR [OPCIONAL]
ExploreFilter = ParamToggle("ExploreFilter", "LastBarInTest|All",1);
if (ExploreFilter)
Filter = 1;
else
Filter = Status("LastBarInTest");
// Ordenar en Explorador
if (Status("actionex") ==actionExplore)
{
SetSortColumns(2,-3);
//columnas explorador
AddColumn(RSC, "RSC MANSFIELD", 3.2);
AddColumn(PositionScore, "PositionScore", 3.2);
AddTextColumn(Name(),"Ticker",1.2, IIf(RSC>1.75, colorGreen,colorRed));
AddTextColumn(FullName(), "Nombre",1.2,IIf(RSC>1.75,colorGreen,colorRed));
}
_SECTION_END();
This is the explorer's result:
Clearly, I was supposed to pick the top four stocks with the highest score, but it picked two others that were lower:
I didn't know what to do anymore, so I thought of commenting on the Exrem. That was the solution.
After reading several forum posts, I saw that Tomasz already made it very clear in this thread:
https://forum.amibroker.com/t/what-are-some-possible-ways-for-exrem-to-affect-backtest-results/17464/3?u=yonsi72
But forgetful people like me...hit the same stone repeatedly!