Home › Documentation › Building strategies
Block 5

Exit conditions

On top of the TP/SL you can define indicator conditions that close the position. They are optional; if you define none, the system uses TP/SL alone.

Exit priority within a bar

1
Intrabar TP / SL — top priority

Checked against the bar's High and Low. If it fires, it closes immediately. It takes priority over any indicator signal.

2
Indicator signal at the close

If the TP/SL did not fire, the exit conditions are evaluated at the close. If they all hold at the same time (AND logic), it closes at the open of the next bar. With MaxAnd ≥ 2 it also generates AND combinations of exit conditions, just as on the entry.

⏱️

BarsSinceEntry — exiting on time

BarsSinceEntry counts the bars elapsed since the entry (0 on the entry bar, 1 on the next one…). It is not a price indicator: its value depends on the open trade, which is why it can only be used in EXIT conditions. It lets you close on time, something neither the TP/SL nor the indicators do.

Pure time exit
exit: BarsSinceEntry = 3

Closes on the 3rd bar after entering, whatever happens (if the TP/SL has not fired first).

Combined (do not exit too early)
exit: BarsSinceEntry > 5 AND
    Close crosses below SMA(Close,20)

It only closes on the crossover once more than 5 bars have gone by (it avoids premature exits). Remember: exits combine with AND.

Where to use it: in the Strategy Lab (indicator palette → it shows up as a fixed Exit, with an N range and operators), through AQL (BarsSinceEntry = 3) or in the Editor. In the Strategy Lab, turning BarsSinceEntry on forces the sweep onto the CPU (the GPU cannot compute it as a series). Export: EasyLanguage uses its native BarsSinceEntry function; MQL5 derives it from the position's open time; ProRealTime keeps a bar counter while in a trade.

🎚️

Exit Engine — trailing, break-even and time-stop

Almost everyone optimizes when to get in, but the real money is in managing the exit. Taking an edge that already works and tuning only its exit usually improves the result without touching the entry. These three tools are switched on in the Strategy Lab (TP/SL block), they coexist with the TP/SL and the indicator exit conditions, and they force the sweep onto the CPU (the GPU kernel does not know about them).

📈 Trailing (dynamic stop)

Instead of a fixed SL, the stop chases the favorable price: it rises with the high (on longs) keeping the SL distance. It captures trend and protects what you have made. Checkbox Dynamic stop, in the TP/SL tab of the Lab, under the Stop Loss.

Starts with… With a value above 0, the trailing only starts when the trade has that much in profit (in the SL unit); until then, the fixed stop. This way it does not choke trades in their first bars. In AQL: stoploss: trailing 1.5% after 2%. Also in AQ Genesis (Settings, exit criteria).

🛡️ Break-even

Once the trade is far enough ahead (the trigger), the SL jumps to «entry + offset». With offset 0 it goes to breakeven (zero risk); with offset > 0 it locks in that gain. From then on it cannot lose.

⏱️ Time-stop

Closes the position after N bars in the trade (at the next open). The direct-field equivalent of BarsSinceEntry, handy for sweeping.

⏹️ Close at the end of the session

No trade stays overnight. At the end of the session: it closes at the close price of the session's last bar. At a time: at the close of the bar where the data's clock reaches that time (or on the last bar, if the session ends earlier that day). Either way, nothing new opens until the next session. Fridays only: just the weekend. The session comes from the data (the daily gap), with no hours to type and in any time zone: on CME, with data in Chicago time, the session runs from 17:00 to 16:00 the next day, so a trade can enter one day and exit the next without staying out overnight. No effect on daily bars, and the window says so. Runs on the GPU. In AQL: SessionExit: end, SessionExit: 15:55, and friday for Fridays only. On export: the platform's own session close where there is one (TradeStation, NinjaTrader, TradingView) and, in MetaTrader 5, ProRealTime and AmiBroker, the time of the session's last bar, worked out from your data.

📅 Daily limits

Max. trades per day: at most that many entries per day; with 1, only the first signal of the day. Daily stop and daily target, in money per contract and with costs: when what was closed that day loses or gains that amount, nothing else is opened until the next day. The open position keeps its exits; a pending order is cancelled. The day is the session of the data, the same one the session close uses (in forex, from Sunday to Friday, each date counts as one day). 0 = no limit. No effect on daily bars. It is in the Lab and in Genesis, and runs on the GPU. In AQL: DayLimits: trades 2 loss 500 profit 1000 (any of the three). They are exported to all six platforms. In MetaTrader 5, with the real history of the day. In NinjaTrader and TradingView, with their own session change. In AmiBroker, just like the backtest. In TradeStation and ProRealTime, with a per-session counter. In all of them the money is the platform's (with the commission you have set) and is meant for 1 contract. In AmiBroker, set your commission in aqLCom.

⚖️ Target in R (risk/reward)

The Take Profit as multiples of what you risk: with 2R the target sits at twice the stop distance. R is measured with the initial stop: if the trailing or the break-even move the stop later, the target does not move. Without a Stop Loss there is no R and the trade exits with no target. With the SL in ATR, the one of the bar before the entry.

Where: in the Lab, type R (times the SL) in the Take Profit (fixed or as a range: 1 to 3 tries 1R, 2R and 3R). In AQ Genesis, in Configure, «Take Profit · range» in R: the genetic search tries steps of one half. In AQL: takeprofit: 2R. On export, R is written already converted to the SL unit (2R with a 1.5 % SL = a 3 % TP), which is exactly the same calculation; with the SL in ATR, the code warns that the platform will use the current bar's ATR.

📏 TP or SL at an indicator level

The target or the stop is the value of an indicator (a moving average, a band, the low of N bars, a pivot…) and it moves with it bar by bar: on each bar it takes the value of the last closed bar, nothing from the future (for pivots and previous-period levels, the one already known at the open). A stop at the 10-bar low, a target at the 20-bar average.

If on the entry bar the level is already on the other side of the entry, that bar does not count; from the next one, if the bar opens on the other side, it exits at the open, like any gap. With the SL at a level there is no distance, so there is no trailing or break-even; a TP in R does work: R is the distance from the entry to the level on the entry bar.

Where: in the TP/SL tab of the Lab, type Indicator level, writing the indicator as in AQL (Lowest(Low,10)). It runs on the GPU. In AQL: stoploss: at Lowest(Low, 10), takeprofit: at SMA(Close, 20). It is exported to all six platforms; in TradeStation, MetaTrader 5, ProRealTime and NinjaTrader the level applies from the bar after the entry bar (the code says so), and in TradingView and AmiBroker, from the entry bar, like AniQuant.

🎯 TP and SL active from

The fill (default): the target and the stop are placed on entry, like an OCO order, and can trigger on the entry bar itself. The next bar: they are placed at the close of the entry bar and work from the next one, like TradeStation's «next bar» orders; the trade lasts at least that bar (on daily data, the night) and, if the next bar already opens beyond the level, it exits at the open. It lets you reproduce strategies written that way: an entry that buys sharp drops can earn much more by keeping the overnight rebound than by collecting the target that same afternoon. In the Lab it is in Settings, «How to exit»; in Genesis, next to the session close, with a third option: both, taking turns by round. Runs on the GPU. In AQL: ExitsFrom: nextbar (or fill). On export, each platform does it its own way so it gives the same as AniQuant: in TradeStation, from the fill it uses its built-in orders (SetProfitTarget and SetStopLoss), which already work on the entry bar.

How break-even works, step by step
  • 1. You enter. The SL sits in its normal place.
  • 2.Price moves your way and reaches the trigger («once ahead by X»).
  • 3.The SL moves to Entry + offset and never drops below it again.
  • 4.If price turns around, you exit at breakeven (or with the offset's gain), never at the original loss.

The trigger and the offset use the Stop Loss unit (%, amount or ATR). Activation is checked at each bar's close (using High/Low), without looking into the future.

Sweepable by range (in the Genetic mode)

The break-even trigger and the time-stop can be swept over a range (Fixed / Range pills). The sweep applies in Genetic mode, which adds them to the genome and evolves them; in exhaustive/random the first value is used. The break-even offset stays fixed.

The name shows the exit

Every variant in the ranking states its management in the name, next to the TP/SL:

RSI<30 AND MA50 (L) [TP:2% SL:1%]
    [BE:3% Time:20]

Empty if break-even and time-stop are off.

Persistence: the trailing, the break-even and the time-stop are saved with the strategy in the Portfolio and travel to Strategy Management, Multi-TF and every export (see the Export section).

⚙️

Optimize the exits only

The real money is usually in managing the exit. With this you take a strategy you already like, freeze its entry and launch a sweep that varies the exit only — so you can see which exit management extracts the most from that same edge, without rediscovering the entry.

How to launch it

From three places, on one specific strategy:

  • →The Strategy Lab ranking: right click → ⚙ Optimize the exits only…
  • →Portfolio: Optimize exits button.
  • →Strategy Management: Optimize exits button.
What it does under the hood
  • ·The entry is frozen as a fixed AQL Code block, with Max AND = 1 → it is neither combined nor touched.
  • ·If the strategy reads another market or another timeframe (Close of "$VIX" < 20, RSI(Close,14) @D1 > 50), those conditions arrive inside the fixed AQL Code, visible and editable. The sweep then runs on the CPU.
  • ·The exits are left free: set TP/SL, break-even or time-stop to Range, add closing indicators…
  • ·It opens with the strategy's TP/SL preloaded as a starting point.
  • ·You choose the mode: exhaustive (guaranteed optimum) or genetic (for large spaces).

The resulting ranking shows the same entry with different exit managements, sorted by performance. Note: if the entry uses an OR of groups, it is exported as one alternative per branch (an approximation); pure AND entries are exact.

Optimize the exits only menu and the Lab with the entry frozen
«Optimize the exits only»: the entry arrives as a fixed AQL Code block (Max AND = 1).
Strategy Lab TP/SL block with trailing, break-even and time-stop
The Strategy Lab's TP/SL block with Trailing, Break-even and Time-stop on separate lines.
Ranking showing the exit suffix in the name
Every variant shows its management in the name: [BE:3% Time:20].
Try it yourself

AniQuant can be tried free for 14 days, with every module and no card. If you have the book, 30.

← Previous
Entry conditions: thresholds and AND combinations
Next →
How strategy optimization works
More in Building strategies