ProBacktest/probacktest · proorder

LONGONMARKET

LONGONMARKET in ProBacktest and ProOrder returns true while a long position is open. Used to guard entries, exits, and stop management in strategy code.

Syntax

probuilder
LongOnMarket

How it works

LONGONMARKET takes no arguments and returns true when the strategy currently holds a long position, regardless of its size. It returns false when the strategy is flat and also when it is short; the short-side counterpart is SHORTONMARKET, and ONMARKET covers both directions at once.

The value reflects filled orders only. A pending limit or stop entry that has not executed leaves LONGONMARKET false until the fill happens. With CumulateOrders enabled, several stacked long entries still read as a single true value; use COUNTOFPOSITION to inspect the position size.

The two standard patterns are IF NOT LongOnMarket THEN ... ENDIF to prevent duplicate long entries, and IF LongOnMarket THEN ... ENDIF to make exit or stop-management code run only while a long trade is live.

Examples

Example 1, Entry guard against duplicate longs (ProBacktest)

probuilder
// Open a long only when no long position is already held
IF NOT LongOnMarket THEN
    BUY 1 LOT AT MARKET TomorrowOpen
ENDIF

Without the guard, the strategy would submit a new buy order on every bar. This is the example from the official reference.

Example 2, Moving average system with entry and exit guards (ProOrder)

probuilder
DEFPARAM CumulateOrders = false

fastMA = Average[20](close)
slowMA = Average[50](close)

// Enter long on a bullish cross, only when flat
IF NOT LongOnMarket AND fastMA CROSSES OVER slowMA THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

// Exit the long on a bearish cross
IF LongOnMarket AND fastMA CROSSES UNDER slowMA THEN
  SELL AT MARKET
ENDIF

Each block is gated on the position state, so entries fire only from flat and exits only while long.

Example 3, Stop management scoped to open longs (ProOrder)

probuilder
// Promote the stop to breakeven after 15 points of open profit
IF LongOnMarket THEN
  IF close - tradeprice >= 15 * pointsize THEN
    SET STOP BREAKEVEN
  ENDIF
ENDIF

Position-management instructions such as SET STOP BREAKEVEN require an open position, so the LONGONMARKET guard prevents runtime errors while flat.

Common errors and gotchas

  • Assuming false means flat. LONGONMARKET is false both when the strategy is flat and when it is short. Code that should run only when no position of any kind is open must test NOT OnMarket instead.
  • Pending orders do not count. Between placing a limit or stop entry and its fill, LONGONMARKET stays false, so an unguarded entry block can queue additional orders. LongTriggered helps detect the bar where a long entry executed.
  • No size information. The flag says nothing about how many contracts are held. Pyramiding logic that stacks entries under CumulateOrders needs COUNTOFPOSITION to cap exposure.
  • Wrong scope. Pasting strategy snippets into an indicator brings LONGONMARKET along, and the indicator then fails to compile. Position state exists only in ProBacktest and ProOrder.
  • ONMARKET, true while any position is open, long or short.
  • SHORTONMARKET, true while a short position is open.
  • MARKET, executes orders at the current market price.
  • BUY, opens a long position.
  • SELL, closes a long position.
  • LongTriggered, signals that a long entry order was executed.
  • COUNTOFPOSITION, the size of the open position.
  • TRADEPRICE, the execution price of a past trade.