ProBacktest/probacktest · proorder

BUY

BUY in ProBacktest and ProOrder opens a long position. Covers quantity units, AT MARKET, LIMIT and STOP order types, plus common sizing and entry mistakes.

Syntax

probuilder
BUY x SHARES AT MARKET
probuilder
BUY x CONTRACT AT price LIMIT
probuilder
BUY x CONTRACT AT price STOP

Parameters

NameTypeDefaultDescription
xnumberrequiredOrder quantity, interpreted through the unit that follows.
unitSHARES, CONTRACT, LOT, CASHrequiredSizing unit. CASH states an amount of account currency instead of a number of units.
order typeAT MARKET, AT price LIMIT, AT price STOPrequiredImmediate execution at the current price, or a pending order at a stated level.

How it works

BUY places a long entry order. With AT MARKET, the order fills at the next available price. With AT price LIMIT, a pending order waits to buy at the stated level or better, below the current price. With AT price STOP, a pending order buys once price rises through the stated level, the usual form for breakout entries.

The quantity unit must match the instrument: SHARES for stocks, CONTRACT for futures and CFDs, LOT for forex, and CASH to size the order as an amount of account currency. Position accumulation is governed by CumulateOrders. When it is enabled, which is the default, repeated BUY orders stack into a larger position; with DEFPARAM CumulateOrders = false, a new BUY while already long is ignored.

Entries are normally wrapped in a position-state guard such as IF NOT LongOnMarket THEN or IF NOT OnMarket THEN, so orders fire only when the strategy intends to hold a new position.

Examples

Example 1, Conditional market entry (ProBacktest)

probuilder
// Enter long at market when flat and the entry conditions hold
IF NOT LongOnMarket AND LongConditions THEN
    BUY 1 SHARES AT MARKET
ENDIF

One share is bought at the current market price, but only when no long position is already open. This is the example from the official reference.

Example 2, Breakout entry with a pending stop order (ProOrder)

probuilder
DEFPARAM CumulateOrders = false

// Buy a break of the 20-bar high
entryLevel = Highest[20](high)

IF NOT onmarket THEN
  BUY 1 CONTRACT AT entryLevel STOP
ENDIF

The pending stop order rests above the market and fills only if price trades up through the previous 20-bar high.

Example 3, Cash-sized entry with a trend filter (ProOrder)

probuilder
DEFPARAM CumulateOrders = false

// Size the position as 5000 units of account currency
IF NOT onmarket AND close > Average[50](close) THEN
  BUY 5000 CASH AT MARKET
ENDIF

// Exit when price loses the average
IF longonmarket AND close < Average[50](close) THEN
  SELL AT MARKET
ENDIF

CASH sizing keeps exposure constant in currency terms across instruments with different prices, instead of fixing a unit count.

Common errors and gotchas

  • Unintended pyramiding. CumulateOrders is enabled by default, so an entry condition that stays true for several bars stacks a new BUY on each of them. Either set DEFPARAM CumulateOrders = false or guard the entry with NOT LongOnMarket.
  • Pending orders expire after one bar. A LIMIT or STOP order that does not fill on the next bar is cancelled. Persistent pending entries must be resubmitted each bar while the setup remains valid.
  • Wrong unit for the instrument. SHARES on a forex pair or LOT on a stock does not size the order as intended. Match the unit to the market, or use CASH for a currency-denominated size.
  • BUY as a short exit. Closing a short position is the job of EXITSHORT. A BUY sized larger than the open short reverses the position rather than just closing it, which is rarely the intended behavior.
  • SELL, closes 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.
  • CONTRACT, quantity unit for futures and CFDs.
  • SHARES, quantity unit for stocks.
  • LOT, quantity unit for forex.
  • CASH, sizes an order in account currency.
  • ONMARKET, true while any position is open.
  • CumulateOrders, controls whether repeated entries stack.