Questions on using the Stop Loss in the ApplyStop function

Hello,

I know this seems like a stupid question...

I'm having some difficulties with the Applystop function. I'm trying to incorporate Stop Loss into my code instead of using the "Stops" tab in the Settings window. This is also because I want to use a Stop based on the ATR indicator.

I tried to use the Applystop function but I must be doing somethig wrong. So I created a simple MA crossover code with the exact same Applystop parameters I had on my original code to test it. And indeed it's not working, as it only exits a position if a contrary signal is found - hence it keeps inverting buy and sell positions instead of using a trailig StopLoss based on the ATR, as intended.

Could anyone please help?

W=EMA(C,20);
X=EMA(C,50);

Buy = Ref(W,-1)<Ref(X,-1) AND Ref(W,0)>Ref(X,0);
Sell = Ref(W,-1)>Ref(X,-1) AND Ref(W,0)<Ref(X,0);
Short = 0;
Cover = 0;

ApplyStop(stopTypeTrailing , stopModePoint, ATR(14), 1);

Thank you.

Best
Claudio

@claudio

try to use the code without the sell condition and see what happens:

W=EMA(C,20);
X=EMA(C,50);

Buy = Ref(W,-1)<Ref(X,-1) AND Ref(W,0)>Ref(X,0);
Sell = 0;
Short = 0;
Cover = 0;

ApplyStop(stopTypeTrailing , stopModePoint, ATR(14), True, True);

Note that I also modified the ApplyStop call adding one extra True parameter.
From the ApplyStop() documentation re the last parameter used:

volatile -
decides if amount (or distance) (3rd parameter) is sampled at the trade entry and remains fixed during the trade (Volatile = FALSE - old behaviour) or if can vary during the trade (Volatile = TRUE) (allows single line Chandelier exit implementation)

I'm not sure that this is what you want but maybe you should test it and report back.

Otherwise, you could also try to adapt this code "snippet" (that comes by default with your AmiBroker installation). Just invoke the "Insert Snippet" from the formula code editor (the popup menu is invoked using a short click on the right mouse button).

period = 22; // ATR period
multiplier = 3; // ATR multiplier
ApplyStop(stopTypeTrailing, stopModePoint, multiplier*ATR( period ), True, True );
Buy = 1; // replace with your buy rule
Sell = 0; // replace with your sell rule

StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100;

trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
 {

   if( trailstop == 0 AND Buy[ i ] ) 
    { 
       trailstop = High[ i ] * stoplevel;
    }
    else Buy[ i ] = 0; // remove excess buy signals

   if( trailstop > 0 AND Low[ i ] < trailstop )
    {
       Sell[ i ] = 1;
       SellPrice[ i ] = trailstop;
       trailstop = 0;
    }

   if( trailstop > 0 )
    {   
       trailstop = Max( High[ i ] * stoplevel, trailstop );
       trailARRAY[ i ] = trailstop;
    }

}

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorRed );
2 Likes

In the above code only stopTypeTrailing of ApplyStop function is used .

Can we add along with it the stopTypestoploss of ApplyStop function for 5 fixed points, in case the trade goes into loss, please.

Yes: you can include multiple types of stops in your formula.

Here you'll find an article that explains the Stops priority in the default backtest procedure in AmiBroker.

If you want more control, version 6.0 introduced the SetStopPrecedence().I

2 Likes