ProBacktest/probacktest · proorder

SHORTONMARKET

SHORTONMARKET in ProBacktest and ProOrder is true while a short position is open. Use it to guard short entries and exits, with worked examples.

Syntax

probuilder
ShortOnMarket

How it works

SHORTONMARKET returns true while the strategy holds an open short position and false when it does not. It is read as a plain boolean, so it slots directly into an IF condition, usually negated with NOT to allow a new short only when none is open.

It reports on short positions only. For long positions use LONGONMARKET, and to test whether any position of either side is open use ONMARKET. Combining SHORTONMARKET with a signal condition is the standard way to control when a short entry or a short exit is allowed.

Examples

Example 1, Guarding a short entry (ProBacktest)

probuilder
// Sell short when MACD crosses below zero, but only if no short is open
myMACD = MACD[12,26,9](close)
short = myMACD CROSSES UNDER 0
IF NOT ShortOnMarket AND short THEN
    SELLSHORT 1 CONTRACT AT MARKET
ENDIF

This is the pattern from the official reference. NOT ShortOnMarket stops the strategy from stacking a second short while one is already running.

Example 2, Managing an open short (ProOrder)

probuilder
DEFPARAM CumulateOrders = false
// Close the short when price reclaims the 20-period average
IF ShortOnMarket AND close CROSSES OVER Average[20](close) THEN
    EXITSHORT AT MARKET
ENDIF

The exit block runs only while a short is open, so the CROSSES OVER condition is checked only when it is relevant.

Example 3, One side at a time (ProOrder)

probuilder
DEFPARAM CumulateOrders = false
// Allow a long only when neither side is open
IF NOT ShortOnMarket AND NOT LongOnMarket AND close CROSSES OVER Average[50](close) THEN
    BUY 1 CONTRACT AT MARKET
ENDIF

Testing both SHORTONMARKET and LONGONMARKET keeps the strategy flat before a new entry. NOT OnMarket expresses the same guard more compactly.

Common errors and gotchas

  • Short only. SHORTONMARKET ignores long positions. A strategy that trades both sides needs LONGONMARKET as well, or ONMARKET for the combined state.
  • State, not a count. It reports only whether a short is open, not its size. For share or contract quantities use COUNTOFSHORTSHARES or COUNTOFPOSITION.
  • Same-bar timing. A short opened and closed within a single bar may not leave SHORTONMARKET true the way a multi-bar position would. To detect a fill on the current bar, use ShortTriggered.
  • Forgetting the guard. Omitting NOT ShortOnMarket on an entry lets a persistent signal add repeated shorts, especially with CumulateOrders enabled.