Problem using ApplyStop() to trigger Max Stop Loss

Hi -
I am not sure why the following code is not triggering ApplyStop() function for max stop loss. I appreciate much any suggestions.

rsiLength = 2;
buySignal = Cross(RSI(rsiLength), 10);
buyLimitPrice = ValueWhen(buySignal, Close) - (0.1 * ATR(10));

Buy = Ref(buySignal, -1);
Buy = Hold(Buy,2) AND (Low < buyLimitPrice);   //Buy = Buy AND (Low < buyLimitPrice);
BuyPrice = Min(Open, buyLimitPrice);  

sellSignal = Cross(90, RSI(rsiLength));
Sell = sellSignal;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

// Stops
buySignalBarLow = Ref( ValueWhen(buySignal, Low), -1 ); // use entry bar low
maxLossStopPoints = BuyPrice - buySignalBarLow - 0.1*ATR(10);

ApplyStop(stopTypeLoss, stopModePoint, maxLossStopPoints, ExitAtStop=2);
ApplyStop(stopTypeProfit,stopModePoint, 2*ATR(10), ExitAtStop=1);

Following image shows the Max Stop Loss is not applied.
ApplyStopsProblem

Your code is wrong. It should be:

ApplyStop(stopTypeLoss, stopModePoint, 0.1*ATR(10), 2);

Hi @Tomasz - Thanks. I am trying to use signal bar low ( with small additional distance) as my stop loss instead of purely based on ATR.

Used following as an example on how to do it -
http://www.amibroker.com/kb/2014/10/17/using-price-levels-with-applystop-function/

I will try again to see what is the difference between my code and above example.

Regards,
Durga

Hi @Tomasz - I see same issue even when I use the below code fragment

ApplyStop(stopTypeLoss, stopModePoint, 0.2*ATR(10), 2);

Regards,
Durga

Hi @Tomasz - It turns out the code and trades are correct but the chart is showing incorrect sell arrows. Not sure why though. I am not explicitly plotting the signals and instead rely on the option in chart parameters to show trade arrows.

Below is the chart (with incorrectly plotted signals) and corresponding trades in the back test results.

Chart:
ApplyStopsProblem

Trades:
ApplyStopsProblemTrades

My guess is the chart is using RSI signal exit in the code as the signal to plot instead of actual sell signal that is triggered by stop loss. Don't know why.

Regards,
Durga

In order to plot Applystop shapes you have to evaluate stops via Equity function!

Read comments
https://www.amibroker.com/guide/afl/equity.html

It is also mentioned at ApplyStop link
https://www.amibroker.com/guide/afl/applystop.html

1 Like

@durga_public - you need to read more docs. There are THREE different kind of arrows that show different things.

You displayed “RAW” Arrows that are just raw, unprocessed signals that you put into Buy and Sell variables. They are RAW, do NOT include any processing (so they also don’t include any ANY stops)

You should have used Show current trade arrows instead.

https://www.amibroker.com/guide/m_aaresult.html
and
http://www.amibroker.com/kb/2006/08/09/how-to-display-arrows-for-trades-generated-in-backtest/

1 Like

Hi @Tomasz - Thanks. I searched docs to figure issue with my code but has not realized there are different types of Arrow signals and should look for docs on that.

Regards,
Durga

Hi @fxshrat - Thank you for the links. Couple comments -

  • The first link says the equity function is available only for backward compatibility.

  • I noticed that even if I put plots in each of my strategies, when I click the rows in the backtester window, the plots show up in bottom panels of the chart. It means, I have to recreate my default chart layout in each strategy and resize panels to see the trade markers in the context of my chart settings.

@Tomasz solution is perfect for me as I have to just right click and say “show current trades”. They will show up in the context of my default chart layout and I do not have to plot in each strategy.

Regards,
Durga

@durga,

Yes, for majority of things Equity function is obsolete since existence of Portfolio equity.
But that does not mean it is not required anymore.

If you have single security systems and you want to plot Applystops via PlotShapes then it is required to use Equity() function to make stops appear via PlotShapes.

If you want to plot Applystop() via PlotShapes() function you have to evaluate stops via Equity() function.
Also check again in comments section of ApplyStop function reference of users guide:

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

So it means:

Standard sell value is 1 so it is equal to

PlotShapes( (Sell==1)*shapeSmallDownTriangle, colorRed, 0, H, -15);

If you have a stoploss and profit target via Applystop then it is for example

SLstop  = Sell == 2;// max loss
TGTstop = Sell == 3;// profit target

PlotShapes( SLstop*shapeSmallDownTriangle, colorOrange, 0, H, -15 );

PlotShapes( TGTstop*shapeDownArrow, colorAqua, 0, H, -15 );

That way you can give different colors and different shapes for each exit via plotshapes contrary to clicking plotting trade arrows via backtester where all exits have same color and same shape.

So here is full example using your code from first post.

/// @link http://forum.amibroker.com/t/problem-using-applystop-to-trigger-max-stop-loss/1966/7

rsiLength = 2;
buySignal = Cross(RSI(rsiLength), 10);
buyLimitPrice = ValueWhen(buySignal, Close) - (0.1 * ATR(10));

Buy = Ref(buySignal, -1);
Buy = Hold(Buy,2) AND (Low < buyLimitPrice);   //Buy = Buy AND (Low < buyLimitPrice);
BuyPrice = Min(Open, buyLimitPrice);  

sellSignal = Cross(90, RSI(rsiLength));
Sell = sellSignal;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

Short = Cover = 0;

// Stops
buySignalBarLow = Ref( ValueWhen(buySignal, Low), -1 ); // use entry bar low
maxLossStopPoints = BuyPrice - buySignalBarLow - 0.1*ATR(10);

ApplyStop(stopTypeLoss, stopModePoint, maxLossStopPoints, ExitAtStop=2);
ApplyStop(stopTypeProfit,stopModePoint, 2*ATR(10), ExitAtStop=1);


// Sample code for chart pane to plot shapes
if( Status( "action" ) == actionIndicator ) {
    SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

    //_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
	
    Plot( C, "Price", colorDefault, styleCandle );// comment this line if you have price plot already (i.e. if overlaying on a price chart)

    Equity( 1, 0 ); // evaluate stops, all quotes
    
    PlotShapes( Buy*shapeSmallUpTriangle, colorGreen, 0, L, -15);
    
	/*
	1 - regular exit
	2 - max. loss
	3 - profit target
	4 - trailing
	5 - n-bar stop
	6 - ruin stop
	*/
	
	regSell = Sell == 1;

	PlotShapes( regSell*shapeSmallDownTriangle, colorRed, 0, H, -15);

	SLstop  = Sell == 2;// max loss
	TGTstop = Sell == 3;// profit target

	PlotShapes( SLstop*shapeDownArrow, colorOrange, 0, H, -15 );
	PlotShapes( TGTstop*shapeDownArrow, colorAqua, 0, H, -15 );
}

Here is sample picture using upper code applied on SPY.
So read shape is standard stop. Orange colored shape is SL. And aqua colored shape is profit target stop.

1

End of first part


This is your picture for others to understand
2017-09-04_9-24-40

Solution is very simple.

  1. Close the bottom pane
  2. Save my upper plotshapes sample code to AFL file
  3. Instead of double clicking that system AFL from "Charts" window first left click the top price pane (the one of your picture)
  4. Then right click my upper system AFL sample code (the one being saved to Charts window before) and choose "Overlay" in context menu.
  5. The system code will be overlayed on top pane’s price. It is a copy of your originally saved one.

If you want to remove that system code from top pane again do following:

  1. Right click the Chart title of the top pane (the one with price)

  2. in the appearing context menu you will see the name of your system code (and before that one you will see Delete…) So click that specific deletion line and the copy of your system code will be removed again from your top chart pane only but not from Charts window (since it is just a copy). So don't panic. :slight_smile:

End.

4 Likes

Hi @fxshrat - Thanks very much. I will try as you suggested utilizing the example code you provided.

Regards,
Durga