ApplyStop
- apply built-in stop

Trading system toolbox
(AmiBroker 3.70)


SYNTAX ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1 )
RETURNS Nothing
FUNCTION controls built-in stops from the formula level (allows optimization of stops)

Parameters:

type =
0 = stopTypeLoss - maximum loss stop,
1 = stopTypeProfit - profit target stop,
2 = stopTypeTrailing - trailing stop,
3 = stopTypeNBar - N-bar stop

mode =
0 - disable stop (stopModeDisable),
1 - amount in percent (stopModePercent), or number of bars for N-bar stop (stopModeBars),
2 - amount in points (stopModePoint);
3 - amount in percent of profit (risk)

amount =
percent/point loss/profit trigger/risk amount.
This could be a number (static stop level) or an array (dynamic stop level)


ExitAtStop

ExitAtStop = 0 - means check stops using only trade price and exit at regular trade price(1)
(if you are trading on close it means that only close price will be checked for exits and exit will be done at close price)
ExitAtStop = 1 - check High-Low prices and exit intraday on price equal to stop level on the same bar when stop was triggered
ExitAtStop = 2 - check High-Low prices but exit NEXT BAR on regular trade price.

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)(2)

ReEntryDelay -
how many bars to wait till entering the same stock is allowed.

ValidFrom -
defines first bar since entry when stop can generate an exit. 0 means from the very beginning

ValidTo -
defines last bar since entry when stop can generate an exit. -1 means "infinite". By default stops are valid all the time (0/-1).

ValidFrom/ValidTo can be used to create stops that get actived/deactivated in different times. This setting is independent for each stop type. It also works in conjunction with SetOption("HoldMinBars", x ). HoldMinBars affects BOTH regular exits and stops, preventing ALL kind of exits during defined period. ValidFrom/ValidTo works on each stop separately and does not affect regular exits.

Note on using stops:
Scenario 1:
you trade on next bar OPEN and want to exit intraday on stop price

Correct settings:
ActivateStopsImmediately turned ON
ExitAtStop = 1
Trade delays set to one
Trade price set to open

Scenario 2:
you trade on today's close and want to exit intraday on stop price
Correct settings:
ActivateStopsImmediately turned OFF
ExitAtStop = 1
Trade delays set to zero
Trade price set to close

Scenario 3:
you trade on next day OPEN and want to exit by stop on OPEN price when PREVIOUS day H-L range hits stop
Correct settings:

ExitAtStop = 2 (NEW)
Trade delays set to one
Trade price set to open

  • a) (if you want to have stops executed AFTER regular signals, so cash from stopped out positions is NOT available to enter trades the same day)
    ActivateStopsImmediately turned ON

  • b) (if you want to have stops executed BEFORE regular signals, so cash from stopped out positions IS available to enter new trades the same day)
    ActivateStopsImmediately turned OFF


Scenario 4:
you trade on today's close and want to exit only when today's close price hits the stop level

Correct settings:
ActivateStopsImmediately turned OFF
ExitAtStop = 0
Trade delays set to zero
Trade price set to close

LIMITATIONS:

  • (1) ExitAtStop = 0 uses SellPrice/CoverPrice variables in backtestRegular mode only, in other modes it uses trade prices from the Settings dialog (not overridable via SellPrice/CoverPrice)

  • (2) Volatile stops (Volatile=True) work only in backtestRegular mode
EXAMPLE /* max loss stop optimization */

ApplyStop(stopTypeLoss,
         
stopModePercent,
         
Optimize( "max. loss stop level", 10, 2, 30, 1 ),
         
True );

/* single-line implementation of Chandelier exit */

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

/* N-bar stop */
ApplyStop( stopTypeNBar, stopModeBars, 5 );

SEE ALSO

Comments:

Herman van den Bergen
psytek [at] magma.ca
2003-02-23 09:53:51
If you are trading at the Close with zero delays be sure to unmark "Activate Stops Immediately" in Settings.
Corey Saxe
csaxe [at] nwi.net
2003-03-01 23:33:13
For visual conformation of ApplyStop function, add the following lines below your ApplyStop formula in Indicator Builder:

Equity(1); // THIS EVALUATES STOPS

Plot(Sell==4,"ApplyStop Sell",colorRed,1|styleOwnScale);
Plot(Cover==4,"ApplyStop Cover",colorGreen,1|styleOwnScale);
Tomasz Janeczko
tj --at-- amibroker.com
2004-08-28 03:14:12
If two or more different stops are triggered on the VERY SAME bar then they are evaluated in this fixed order:

Fixed Ruin stop (loosing 99.96% of the starting capital)
Max. loss stop
Profit target stop
Trailing stop
N-bar stop
Graham Kavanagh
gkavanagh [at] e-wire.net.au
2004-09-30 21:55:42
from equity comments

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
Tomasz Janeczko
tj --at-- amibroker.com
2005-03-01 17:10:39
ExitAtStop has a new meaning for N-BAR stop type.
If ExitAtStop = 0 then N-bar stop has the lowest priority (so if for example profit target stop is hit on the same bar then profit target is evaluated first)
If ExitAtStop = 1 then N-bar stop has highest priority and it is evaluated before all other stops.
The same effect is obtained by checking "Has priority" box in AA Settings window.
Tomasz Janeczko
tj-/nospam/ [at] amibroker.com
2006-01-13 11:41:32
ApplyStop function is designed to be used to simulate
stop orders placed at the exchange or simulated by the brokerage

Please read this how such stops operate:
http://www.interactivebrokers.com/en/trading/orders/stop.php?ib_entity=uk
http://www.interactivebrokers.com/en/trading/orders/trailingStops.php?ib_entity=uk

References:

The ApplyStop function is used in the following formulas in AFL on-line library:

More information:

See updated/extended version on-line.