ProBacktest/probacktest · proorder

LIMIT

LIMIT in ProBacktest and ProOrder places a pending order that fills at a stated price or better. Syntax with BUY, SELL, and SELLSHORT, examples, gotchas.

Syntax

probuilder
BUY quantity CONTRACT AT price LIMIT
probuilder
SELLSHORT quantity CONTRACT AT price LIMIT
probuilder
SELL AT price LIMIT
EXITSHORT AT price LIMIT

Parameters

NameTypeDefaultDescription
pricenumeric expressionnoneThe limit level. The order executes at this price or better, lower for a buy, higher for a sell.

How it works

LIMIT is one of the three execution modes of the order instructions, alongside MARKET for immediate execution and STOP for pending stop orders. A limit order states the worst acceptable price. For entries, BUY ... LIMIT rests below the current market and fills on a dip to the level, and SELLSHORT ... LIMIT rests above it and fills on a rally. For exits, SELL ... LIMIT and EXITSHORT ... LIMIT are the standard profit-taking orders, closing the position once price reaches a favourable level.

The direction relative to the market matters. A buy limit placed above the current price is already marketable and executes immediately, behaving like a market order. The pending behaviour only arises when the level sits on the passive side, below the market for buys, above it for sells.

Pending orders in ProBacktest and ProOrder have a one-bar lifetime. An unfilled limit order is cancelled at the end of the next bar, so a strategy that wants the order working for longer must resubmit it on every bar while the entry or exit rule holds. Submitting a new pending order in the same direction replaces the previous one, which also makes it possible to slide the level bar by bar.

Examples

Example 1, Buying a dip after a MACD signal (ProBacktest)

probuilder
myMACD = MACD[12,26,9](close)
long = myMACD crosses over 0
IF long THEN
    // rest a buy order 10 price units below the close
    BUY 1 LOT AT close-10 LIMIT
ENDIF

When the MACD line crosses above zero, a limit order is placed 10 price units under the closing price. The trade only opens if price dips to that level within the next bar, improving the entry at the cost of possibly missing the move. This is the example from the official reference.

Example 2, Shorting into a rally at a fixed offset (ProOrder)

probuilder
// fade signal: RSI overbought
overbought = RSI[14](close) > 70

IF NOT onmarket AND overbought THEN
  // sell short if price rallies 20 points above the current close
  SELLSHORT 1 CONTRACT AT close + 20 * pointsize LIMIT
  SET STOP PLOSS 40
ENDIF

The short entry rests above the market, so the position opens only on a further push upward, at a better price than an immediate market sale.

Example 3, Profit-taking exit resubmitted every bar (ProOrder)

probuilder
// keep a resting take-profit 100 points above the entry price
IF longonmarket THEN
  SELL AT tradeprice + 100 * pointsize LIMIT
ENDIF

Because pending orders expire after one bar, the exit is resubmitted on every bar while the long is open, keeping a permanent profit target 100 points above the fill price.

Common errors and gotchas

  • One-bar lifetime. An unfilled limit order is cancelled after the next bar. Code that places the order once inside an entry block and never repeats it usually trades far less than intended. Resubmit while the condition holds, as in example 3.
  • Marketable limits execute at once. A buy limit above the current price, or a sell limit below it, fills immediately. A sign error in the offset silently converts a patient entry into an instant one.
  • LIMIT is not STOP. Buy limits rest below the market, buy stops above it. Breakout entries need STOP, pullback entries need LIMIT. Mixing them up reverses the logic of the strategy.
  • Backtest fills are optimistic. ProBacktest fills a limit as soon as price touches the level. Live, a touched level with no trade-through can leave the order unfilled, so backtests overstate the fill rate of tight limit entries.
  • BUY, opens a long position.
  • SELL, closes a long position.
  • SELLSHORT, opens a short position.
  • EXITSHORT, closes a short position.
  • MARKET, immediate execution at the current price.
  • STOP (pending), pending order triggered when price crosses a level.
  • ONMARKET, true while any position is open.
  • TRADEPRICE, the entry price of the current position.