ProBacktest/probacktest · proorder

SELL

SELL in ProBacktest and ProOrder closes an open long position. SELL exits longs while SELLSHORT opens shorts. Syntax, order types, and examples explained.

Syntax

probuilder
SELL AT MARKET
probuilder
SELL x CONTRACT AT MARKET
probuilder
SELL AT price STOP

How it works

SELL is the long-side exit instruction. Written without a quantity, SELL AT MARKET closes the entire open long position at the next available price. With a quantity, such as SELL 1 CONTRACT AT MARKET, it closes only part of a larger position, which is how scaling out is expressed.

Pending forms are also available: SELL AT price LIMIT exits at a stated level or better above the market, a profit-taking order, and SELL AT price STOP exits once price falls through a level, a manual stop order. As with all pending orders in ProBacktest and ProOrder, an unfilled order is cancelled after one bar and must be resubmitted while the exit rule remains valid.

The distinction with SELLSHORT matters. SELL only ever reduces or closes a long position; when the strategy is flat or short, a SELL order has no effect. SELLSHORT opens a new short position, and EXITSHORT closes it. The four order instructions pair up as BUY with SELL for the long side, and SELLSHORT with EXITSHORT for the short side.

Examples

Example 1, Rule-based exit from a long position (ProBacktest)

probuilder
// Close the long at market when the exit conditions are met
IF LongOnMarket AND ExitConditions THEN
    SELL AT MARKET
ENDIF

The position closes at the current market price once the exit rule fires while a long trade is open. This is the example from the official reference.

Example 2, Scaling out half the position after a gain (ProOrder)

probuilder
DEFPARAM CumulateOrders = false

// Enter with two contracts when flat
IF NOT onmarket AND close CROSSES OVER Average[50](close) THEN
  BUY 2 CONTRACT AT MARKET
ENDIF

// Take half off after a 2 percent gain from entry
IF longonmarket AND close >= tradeprice * 1.02 AND countofposition = 2 THEN
  SELL 1 CONTRACT AT MARKET
ENDIF

// Close the remainder when the trend turns
IF longonmarket AND close CROSSES UNDER Average[50](close) THEN
  SELL AT MARKET
ENDIF

A quantity-specified SELL closes part of the position, while the final unquantified SELL flattens whatever remains.

Example 3, Pending stop exit below entry (ProOrder)

probuilder
// Maintain a resting exit 50 points below the entry price
IF longonmarket THEN
  SELL AT tradeprice - 50 * pointsize STOP
ENDIF

The pending stop order is resubmitted on every bar while the long is open, keeping a hard exit 50 points under the fill price.

Common errors and gotchas

  • SELL is not SELLSHORT. SELL closes longs; SELLSHORT opens shorts. A strategy meant to go short that issues SELL while flat does nothing, and the error is silent because the order is simply ignored.
  • No long to close. When the strategy is flat or short, SELL has no effect. Guard exit blocks with IF LongOnMarket THEN so the intent of the code stays explicit.
  • Pending exits expire after one bar. A resting SELL ... STOP or SELL ... LIMIT order is cancelled if unfilled after the next bar. Resubmit it on every bar while the position is open, as in example 3.
  • Partial quantity mismatch. A quantity-specified SELL larger than the open position just closes the position; it does not reverse into a short. Check size with COUNTOFPOSITION before scaling out fixed amounts.
  • BUY, opens a long position.
  • SELLSHORT, opens a short position.
  • EXITSHORT, closes a short position.
  • MARKET, immediate execution at the current price.
  • LIMIT, pending order at a stated price or better.
  • STOP, pending order triggered when price crosses a level.
  • LONGONMARKET, true while a long position is open.
  • ONMARKET, true while any position is open.
  • TRADEPRICE, the execution price of a past trade.