Applystop and ExRem() mistake

Hi,

I've a problem with my debug and backtester system with ExRem and ApplyStop functions, because when 1 buy order is closed by any ApplyStop the ExRem function not detect a sell order by ApllyStop, so the next valid buy order isn't put to 1 since sell=1 on other candle.
So I'm losting a lot buy signals on my test.

Anyone know how I can solve it?

I've the picture below with all my buyentries=1 but buy=0 since one sell=1, if I delete ApplyStops the code runs perfect.

Thanks for your support.

2019-01-18_1021

1 Like

I maybe able to point you in the direction since your code is also important.

https://www.amibroker.com/guide/afl/applystop.html

You will get more info if you scroll to the end.
You can use the Equity() function to visualize stops and then check the status of the stop

Depending on kind of the stop various values
are written back to sell/cover array to enable you to distinguish if given signal was generated by regular rule or by stop.

1 - regular exit
2 - max. loss
3 - profit target
4 - trailing
5 - n-bar stop
6 - ruin stop

https://www.amibroker.com/guide/afl/equity.html
Equity(1); should occur after ApplyStop() and also it matters to put after all if you have more than one type of ApplyStop()

1 Like

Thanks @travick for your support, this is my code, I test the equity function and not solve this problem.

SetTradeDelays(1,0,1,0);
Setpositionsize(1, spsShares);

//Vars

//Trading schedule
InitSchedule = 90000;
EndSchedule = 190000;
VolMinZone = 450;
VolMaxEntry = 200;
MaxStopLoss = 25;
MA_21 = MA (C,21);
MA_80 = MA (C,80);
HHZone = H;
LHZone = IIf(C>=O,Close,Open);
Vol_OK = IIf(V>=VolMinZone,1,0);
dn=DateNum();
tn=TimeNum();
MINZoneCandles = 3; 
MINZonePriceMove = 10; 
MINZonePriceEntry = 5;
	
//Zone Conditions
CondZone1 = V > VolMinZone; 
CondZone2 = H-L <= MaxStopLoss; 

//BUY Zone Conditions
BUYCondZone1 = C > O; 
BUYCondZone2 = H-C < C-L AND H-C > O-L; 
BUYZone = CondZone1 AND CondZone2 AND BUYCondZone1 AND BUYCondZone2;
BUYZone = IIf (Ref(BUYZone,-1)==1 OR Ref(BUYZone,-2)==1 OR Ref(BUYZone,-3)==1,0,BUYZone); 

//BUYCondEntry1 = Sum(BUYZone, 150-MINZoneCandles);
//BUYCondEntry1 = Ref(BUYZone, -MINZoneCandles);
LastBUYZoneCandle = BarsSince(BUYZone>0); 

//LastBUYZoneCandle = Iif(Ref(Buy,-1)==1,Ref(LastBUYZoneCandle,-LastBUYZoneCandle)+LastBUYZoneCandle,LastBUYZoneCandle);

BUYCondEntry1 = IIf(HHV(H,-LastBUYZoneCandle+1)-Ref(H,-LastBUYZoneCandle)>=MINZonePriceMove,1,0);

//Entry Conditions
CondEntry1 = V < VolMaxEntry; 
CondEntry2 = tn >= InitSchedule AND tn<=EndSchedule;
CondEntry3 = dn == Ref(dn,-LastBUYZoneCandle); 

//BUY Entry Conditions
BUYCondEntry2 = L <= Ref(HHZone,-LastBUYZoneCandle);
BUYCondEntry3 = L >= IIf(Ref(HHZone,-LastBUYZoneCandle)-Ref(LHZone,-LastBUYZoneCandle)>=MINZonePriceEntry,Ref(LHZone,-LastBUYZoneCandle),Ref(HHZone,-LastBUYZoneCandle)-MINZonePriceEntry); 
BUYCondEntry4 = MA_21 > Ref(MA_21,21);
BUYCondEntry5 = MA_21 > MA_80;

//StopLoss
StopLoss = Ref(L,-LastBUYZoneCandle)-2;

SELLCond1 = V>1.5*VolMinZone;
SELLCond2 = tn>=EndSchedule;
SELLCond3 = MA_21<MA_80;

//BUY Trigger
Buy = BUYCondEntry1 AND BUYCondEntry2 AND BUYCondEntry3 AND BUYCondEntry4 AND BUYCondEntry5 AND CondEntry1 AND CondEntry2 AND CondEntry3;

//SELL Trigger
Sell = SELLCond1 OR SELLCond2;

//Excluye señales anidadas hasta cierre de operacion
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
	
BuyPrice = O;
SellPrice = O;

ApplyStop(stopTypeNBar,stopModeBars,20,1); //Pendiente Optimizar		
ApplyStop(stopTypeLoss,stopModePoint,StopLoss,1);
ApplyStop(stopTypeProfit,stopModePoint,Optimize("Max.Profit",15,5,100,5),1);

Equity(1);

for ( i = 1; i < BarCount-3; i++ )
{
	if(BUYZone[i])
	{
		selectFrom = i;
		selectH = IIf(O[i]>C[i],C[i],H[i]);
		selectL = IIf(O[i]>C[i],L[i],C[i]);;
		selectto = i+50; 
		
		// draw Horizontal lines in foreground
		GfxSetZOrder( 5 );
		GfxSetCoordsMode( 1 );

		GfxSelectPen( colorGreen, 2, 0 );//Draw support resistance line in chart


		GfxMoveTo( selectFrom, selectH  );
		GfxLineTo( selectto, selectH );

		GfxSelectPen( colorGreen, 2, 0 );
		GfxMoveTo( selectFrom, selectL);
		GfxLineTo( selectto, selectL);

		// fill Rectangle in background
		GfxSetZOrder( -5 );
		GfxSetCoordsMode( 1 );
		GfxSelectPen( colorBlue);
		GfxSelectSolidBrush( colorLightYellow ); // fill color
		GfxRectangle ( selectFrom, selectL, selectto, selectH ); 
	}


}

//PLOT BUY/SELL ARROWS
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L, -10);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H, -10);

//plot chart
Plot( Close, "Price", colorBlack, styleCandle );

//chart title
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g ", O, H, L, C ));

_SECTION_BEGIN("MA1");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();

_SECTION_BEGIN("MA2");
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1, 10 );
Plot( MA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style") ); 
_SECTION_END();
1 Like