RealTest User Guide
RealTest User Guide

 

 

Navigation: RealTest Script Language > Syntax Element Details >

Exit1 - Exit9

 

 

 

 

Category

Strategy Elements

Description

Up to nine more exit orders, each a limit or a stop order with its own exit time, optionally for part of the position (a tranche)

Input

Limit(price) or Stop(price), optionally with an exit time: Limit(price, time), where price is any formula specifying a price per share and time is ThisClose, Intraday (the default), NextOpen, NextClose or (limits only) OpenOnly; and optionally a quantity: Limit(price, time, qty), where qty is a number of shares or Rest (see Tranches below; Limit(price, , qty) keeps the default time)

Notes

Each of Exit1 to Exit9 adds an exit order for the whole position to those of ExitRule, ExitLimit and ExitStop. All of a position's exit orders are alternatives: the first one to fill exits the position, and the others no longer apply. In live trading they are one OCA (one-cancels-all) group.

Use them when a strategy needs more than one exit of the same kind, for example an intraday profit target together with a limit-on-close order. This long strategy exits at a 5% gain during the day, or at the close if the close is at or above the prior close, or at a 10% stop:

    Exit1: Limit(FillPrice * 1.05)
    Exit2: Limit(C, NextClose)
    ExitStop: FillPrice * 0.9

Limit exits at the price or better (a profit target), and Stop exits once the price is touched (a stop loss). The time works as ExitLimitTime and ExitStopTime do for ExitLimit and ExitStop: Intraday is a live order for the next bar, NextOpen exits at the next open if the close reached the price, NextClose exits at the next close if that close reaches a price calculated from the current bar, and ThisClose exits at the current close; an OpenOnly limit is an order in the next opening auction only, cancelled if the open does not reach it. As with ExitLimit, the price is evaluated using the bar prior to the exit bar, except with ThisClose.

When two exits of the same type are reached in the same bar, the earlier fill wins: during the bar, the nearer price (for a long position, the lower limit or the higher stop); at the same open or close, the one listed first, in the order ExitLimit, ExitStop, Exit1 to Exit9. When a limit and a stop are both reached, Ambiguity decides as it does for ExitLimit and ExitStop.

The Reason column of the Trade List shows Exit1 to Exit9 for these exits, unless the price formula starts with a string, which is then the reason. ExitLimitQty and ExitStopQty do not apply: without a quantity, Exit1 to Exit9 exit the whole position (or the whole tranche, see below).

Orders are generated as for an ExitLimit or ExitStop with the same time (see Generated Order Types). When a position has a limit-on-close exit (a NextClose limit), its Intraday limits and stops are good until the exchange's last MOC time (15:40 for US stocks, see ExchangeMap.csv), as next to a market-on-close ExitRule, because a closing order can no longer be cancelled after that. Next to a market-on-close exit, NextClose limits are not placed at all.

The exitid field of an order template names each exit order (ExitRule, ExitLimit, ExitStop, Exit1 to Exit9; see CSV Order Baskets). OrderClerk uses it to tell apart two exit orders of the same order type for one position.

Tranches (scale-out)

A quantity as the third argument makes the exit a tranche: at entry the position is split into one position per exit with a quantity, each holding that many shares, and all of them are live at once, so a move through several targets fills them all on the same bar (with ExitLimitQty only the next target is live). Each tranche has its own exit plus every exit without a quantity -- ExitRule, ExitLimit, ExitStop and any of Exit1 to Exit9 without one -- as alternatives, one OCA group per tranche in live trading. This long strategy sells a third of the position at each of three targets, with one stop for all of it:

    Exit1: Limit(FillPrice * 1.02, Intraday, Shares / 3)
    Exit2: Limit(FillPrice * 1.04, Intraday, Shares / 3)
    Exit3: Limit(FillPrice * 1.06, Intraday, Rest)
    ExitStop: FillPrice * 0.97

The quantity is evaluated once, at entry, with Shares = the whole entry, and rounded as QtyRound rounds a partial exit; the price is evaluated on every bar as usual. The tranches take their shares in exit order until the entry is used up, and a tranche that would get no shares is not created. Rest takes the shares the other tranches leave (only one exit can have it). Without Rest, any shares no tranche takes (34 of 100 with Shares / 3 twice) form a core that only the exits without a quantity close.

Tranche is the number of the exit that defined the current position's tranche (2 for Exit2's), or 0 for the core and for a position without tranches; T.Tranche is the same for a trade. Use it to vary an exit without a quantity per tranche, as in ExitStop: FillPrice * Select(Tranche = 1, 0.98, 0.95), or to let ExitRule close only some tranches.

A tranche set counts as one position for MaxPositions, MaxSameSym, MaxSameCat and S.Positions, until its last tranche has closed, and as one entry. Each tranche exit is a trade of its own in the trade list. Shares, FillValue and FillFraction are those of the whole open set, except inside PositionSum, where each tranche reads its own (so PositionSum(Shares) adds up, and PositionSum(1) counts tranches); FillQty is the whole entry. ExitQty, ExitLimitQty and ExitStopQty cannot be combined with tranches.

In live trading each tranche is an entry order of its own with its own exit orders (its own bracket), because a broker sizes a bracket's exits to its entry order. The order template fields tranche and trancheset mark a tranche's orders (see CSV Order Baskets); OrderClerk keeps one trade per tranche, and its trade list's Tranche and TrancheSet columns (see TLFields) rebuild the tranches as one position. A limit or stop entry can therefore fill for some tranches and not others, and each tranche's entry order pays its own commission, while the backtest enters the set as one order.

Limit and Stop are not reserved words: Data, Library and Parameters items named limit or stop still work, also inside the price, as in Exit1: Stop(stop).

For more information on how the backtest engine works in general, see Backtest Engine Details.

 

 

 

Copyright © 2020-2026 Systematic Solutions, LLC