In this AFL, inTRAdeShort is set to 1 when we're short and to 0 when we exit the short (cover).
But the system also has a stop loss, and I want inTRAdeShort to also be set to 0 if I exit through a stop loss.
_SECTION_BEGIN("Agorero Plus");
// Baja Volatilidad ---
CloseNyse = Foreign("$NYA","C");
volat = StDev(log(CloseNyse/Ref(CloseNyse,-1)),10)/StDev(log(CloseNyse/Ref(CloseNyse,-1)),100);//sin anualizar
bajaVolat = volat < 0.8;
// Divergencia en la línea Avance - Descenso ---
Slope_NYSE = LinRegSlope(Foreign("$NYA","C"),12);
lineaAD=MA(Foreign("#NYSEADV","C")-Foreign("#NYSEDEC","C"),20);
Slope_LineaAD = LinRegSlope(lineaAD,12);
divergAD = Slope_NYSE > 0 AND Slope_LineaAD < 0;
// Nuevos mínimos en el NYSE ---
NYSENL52W = Foreign("#NYSELO", "Close");
peligro = Sum(NYSENL52W > 40, 4) >= 4;
// --- LÓGICA DE ENTRADA Y SALIDA ---
Buy=Sell=Short=Cover=0;
ShortSetup = bajaVolat AND divergAD AND peligro;
Short = ShortSetup;
EStocastico = StochK(12,1);
CoverSetup = (LinRegSlope(C,12) < 0 AND LinRegSlope(lineaAD,12) > 0) OR StochK(12,1) < 0.1;
Cover = CoverSetup;
// --- SEÑALES FILTRADAS DEFINITIVAS ---
Short = ExRem(Short, Cover);
Cover = ExRem(Cover, Short);
ApplyStop(stopTypeTrailing, stopModePercent, 6);
// --- ESTADO FINAL DE POSICIÓN ---
inTradeShort = Flip(Short, Cover);
// Printf
printf("bajaVolat = %.0f\n", bajaVolat);
printf("divergAD = %.0f\n", divergAD);
printf("peligro = %.0f\n", peligro);
printf("Short = %.0f\n", Short);
printf("EStocastico = %.2f\n", EStocastico);
printf("Cover = %.0f\n", Cover);
printf("inTradeShort = %.0f\n", inTradeShort);
_SECTION_END();
I'm racking my brain over this; I've seen some loops, but I find them difficult to understand (something I think is called a state machine or something like that).
Could someone please show me a simple way to achieve this without loops?