ProBacktest/probacktest · proorder

MARKET

MARKET in ProBacktest and ProOrder executes BUY and SELL orders at the current market price. Orders fill at the open of the next bar without a price limit.

Syntax

probuilder
BUY quantity SHARES AT MARKET
SELL quantity SHARES AT MARKET

How it works

Every order statement ends with an order type. AT MARKET means the order carries no price condition: it executes at whatever price the market offers at the moment of execution. The alternatives, LIMIT and STOP, create pending orders that wait for a specific price level instead.

Because strategy code runs once per bar at the close, a market order generated on one bar is transmitted and filled at the open of the following bar. ProBacktest models this by filling at the next bar's open price. In live ProOrder the order reaches the broker at that moment and fills at the best available price, which can differ from the open print in fast or thin markets.

A market order trades certainty of execution for uncertainty of price. It suits signals where being in the trade matters more than the exact level, such as trend-following entries or forced exits. When the entry level itself is part of the edge, a pending LIMIT or STOP order expresses that intent better.

AT MARKET combines with all order verbs: BUY and SELLSHORT to open positions, SELL and EXITSHORT to close them.

Examples

Example 1, Immediate purchase of ten shares (ProBacktest)

probuilder
// enter when price closes above the 50-bar average
IF NOT OnMarket AND close > Average[50](close) THEN
  BUY 10 SHARES AT MARKET
ENDIF

When flat and the condition holds at the bar close, an order for ten shares is sent and fills at the open of the next bar at the prevailing price.

Example 2, Forced exit of a short at market (ProOrder)

probuilder
// cover the short as soon as momentum turns positive
exitSignal = RSI[14](close) crosses under 30

IF ShortOnMarket AND exitSignal THEN
  EXITSHORT AT MARKET
ENDIF

The entire short position is closed at market. No quantity is stated, so the default applies and the whole position is covered.

Example 3, Moving average cross with both directions (ProOrder)

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

IF fastMA crosses over slowMA THEN
  IF ShortOnMarket THEN
    EXITSHORT AT MARKET
  ENDIF
  BUY 1 CONTRACT AT MARKET
ENDIF

IF fastMA crosses under slowMA THEN
  IF LongOnMarket THEN
    SELL AT MARKET
  ENDIF
  SELLSHORT 1 CONTRACT AT MARKET
ENDIF

A stop-and-reverse system. Each cross closes any opposite position and opens a new one, all with market orders so the strategy is always positioned with the cross.

Common errors and gotchas

  • Fills happen at the next bar open. The condition is evaluated on the bar close, but the fill price is the next bar's open. Backtest arithmetic that assumes entry at the signal bar's close overstates results whenever bars gap.
  • No price guarantee. Market orders guarantee execution, not price. Live fills can slip beyond the backtested open price in volatile conditions, so live results degrade relative to ProBacktest, which assumes clean fills.
  • SELL and EXITSHORT are not interchangeable. SELL AT MARKET closes long positions and EXITSHORT AT MARKET closes shorts. Using the wrong verb for the open side leaves the position untouched.
  • MARKET is not a standalone statement. The keyword only exists as part of an order statement after AT. Writing MARKET on its own line does not compile.
  • BUY, opens or adds to a long position.
  • SELL, closes some or all of a long position.
  • SELLSHORT, opens or adds to a short position.
  • EXITSHORT, closes some or all of a short position.
  • LIMIT, pending order that fills at a set price or better.
  • STOP, pending order that triggers once price reaches a level.
  • ONMARKET, true while any position is open.
  • LONGONMARKET, true while a long position is open.
  • SHORTONMARKET, true while a short position is open.