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
BUY x SHARES AT MARKETBUY x CONTRACT AT price LIMITBUY x CONTRACT AT price STOPParameters
| Name | Type | Default | Description |
|---|---|---|---|
x | number | required | Order quantity, interpreted through the unit that follows. |
| unit | SHARES, CONTRACT, LOT, CASH | required | Sizing unit. CASH states an amount of account currency instead of a number of units. |
| order type | AT MARKET, AT price LIMIT, AT price STOP | required | Immediate 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)
// Enter long at market when flat and the entry conditions hold
IF NOT LongOnMarket AND LongConditions THEN
BUY 1 SHARES AT MARKET
ENDIFOne 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)
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
ENDIFThe 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)
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
ENDIFCASH sizing keeps exposure constant in currency terms across instruments with different prices, instead of fixing a unit count.
Common errors and gotchas
- Unintended pyramiding.
CumulateOrdersis enabled by default, so an entry condition that stays true for several bars stacks a new BUY on each of them. Either setDEFPARAM CumulateOrders = falseor guard the entry withNOT LongOnMarket. - Pending orders expire after one bar. A
LIMITorSTOPorder 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.
SHARESon a forex pair orLOTon a stock does not size the order as intended. Match the unit to the market, or useCASHfor 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.
Related instructions
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.
