ProBacktest/probacktest · proorder

EXITSHORT

EXITSHORT closes an open short position in ProBacktest and ProOrder, either at market or as a pending order. Syntax, worked examples and common gotchas.

Syntax

probuilder
EXITSHORT AT MARKET
probuilder
EXITSHORT x SHARES AT MARKET

Pending variants place the exit at a chosen price instead of executing immediately:

probuilder
EXITSHORT AT price LIMIT
EXITSHORT AT price STOP

x is the number of shares, lots or contracts to buy back. Omitting the quantity closes the whole short position.

How it works

A short position is opened with SELLSHORT and closed by buying the instrument back. EXITSHORT performs that buy-back without ever opening a long: if no short position is open, the instruction has nothing to close and no order is sent. This makes it safer than using BUY for exits, which would open a fresh long position once the short is flat.

The market form executes at the next available price. The pending forms rest at the broker until price reaches the specified level, LIMIT for exits at a favorable price below the current one, STOP for protective exits above it. A quantity turns the instruction into a partial cover, reducing the position while leaving the remainder open, which is the standard way to scale out of a pyramided short.

In ProBacktest the instruction is filled by the simulation engine at the same price logic used for other orders. In live ProOrder the exit is transmitted to the broker like any other order.

Examples

Example 1, MACD-driven short entry and exit (ProBacktest)

probuilder
myMACD = MACD[12,26,9](close)

// Short when the MACD line falls through zero,
// cover when it climbs back above zero
short = myMACD crosses under 0
exit = myMACD crosses over 0

IF NOT SHORTONMARKET AND short THEN
  SELLSHORT 1 LOT AT MARKET
ENDIF

IF SHORTONMARKET AND exit THEN
  EXITSHORT AT MARKET
ENDIF

The canonical round trip from the official reference. The SHORTONMARKET guards make sure entries and exits only fire in the correct position state.

Example 2, Scaling out of a pyramided short (ProOrder)

probuilder
DEFPARAM CumulateOrders = true

// Cover 2 lots once the short is 25 points in profit,
// keep the rest running with a trailing stop
IF SHORTONMARKET AND TRADEPRICE - close >= 25 * pointsize AND COUNTOFSHORTSHARES > 2 THEN
  EXITSHORT 2 SHARES AT MARKET
ENDIF

SET STOP TRAILING 40

The quantity form covers part of the short, banking profit while the remaining lots stay exposed to further downside.

Example 3, Pending limit exit at a profit target (ProOrder)

probuilder
// Rest a buy-back order 30 points below the entry price
IF SHORTONMARKET THEN
  EXITSHORT AT TRADEPRICE - 30 * pointsize LIMIT
ENDIF

Instead of waiting for the strategy code to react, the exit order sits at the broker and fills the moment price trades down to the target level.

Common errors and gotchas

  • No position, no order. EXITSHORT does nothing when the strategy is not short. Logic that relies on it firing unconditionally, for example as a session-end flatten, should verify SHORTONMARKET first or use DEFPARAM FLATAFTER.
  • Using BUY to close a short. BUY reduces a short and then keeps buying into a long position. When the intent is only to cover, EXITSHORT is the correct instruction because it can never reverse the position.
  • Partial exit larger than the position. Requesting more shares than are held simply closes the whole position. Size partial exits from COUNTOFSHORTSHARES to keep the arithmetic honest.
  • Pending exits are re-evaluated each bar. A pending EXITSHORT ... LIMIT placed inside a condition is refreshed on every bar the condition holds and cancelled when it stops holding. Keep the condition true for as long as the order should stay working.
  • SELLSHORT, opens or adds to a short position.
  • SELL, closes all or part of a long position.
  • BUY, opens or adds to a long position.
  • MARKET, executes an order at the current market price.
  • LIMIT, pending order executed at a stated price or better.
  • STOP (pending), pending order triggered when price reaches a level.
  • SHORTONMARKET, true while a short position is open.
  • ONMARKET, true while any position is open.