Moderator comment: @santy when you are copying text that you did not write yourself, always include the link to the source. In this case the text is copied from http://www.amibroker.com/f?applystop Remember text in all amibroker.com pages is subject to copyright. Instead of copying, it is better to just include link alone. Text in user guide will be updated with future releases, copies posted here and there will soon be outdated.
ApplyStop
- apply built-in stop Trading system toolbox
(AFL 1.7)
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 );
|
I studied the above and tried to code for my required condition as below without any success. So please anyone help me with any idea / suggestion.
My required condition:
Once in any trade (either buy or short as per my strategy of 5 & 10 ema crossover, simply ) applystop codes should make me out of the trade for the below mentioned conditions:
- At the begining of any candle if at the end of the previous candle the loss is 10 points
Or - When the trade is in profit whenever profit becomes 25% less than the just previous profit quote