ProBacktest/probacktest · proorder

TRADEPRICE

TRADEPRICE in ProBacktest and ProOrder returns the execution price of a past trade. TRADEPRICE(1) is the most recent. Syntax, examples, and gotchas covered.

Syntax

probuilder
TRADEPRICE
probuilder
TRADEPRICE(N)

Parameters

NameTypeDefaultDescription
Ninteger1Index of the order, counted backwards from the most recent execution. TRADEPRICE(1) is the latest order, TRADEPRICE(2) the one before it.

How it works

Every filled order in a strategy, entries and exits alike, is recorded with its execution price. TRADEPRICE reads that history: with no argument or with N = 1 it returns the most recent execution price, and higher values of N step further back.

The most common use is measuring how far price has moved since entry, for example close - tradeprice >= 15 * pointsize as a trigger to tighten a stop. In pyramiding strategies under CumulateOrders, TRADEPRICE(1) marks the last add-on, so new entries can be spaced by a minimum distance from it. TRADEINDEX is the companion function that returns the bar index of the same order.

TRADEPRICE indexes individual orders, not the aggregate position. When several entries have been cumulated into one position, each keeps its own trade price, while POSITIONPRICE returns the weighted average price of the open position.

Examples

Example 1, Pyramiding with distance and bar spacing (ProBacktest)

probuilder
myMACD = MACD[12,26,9](close)
long = myMACD crosses over 0
exit = myMACD crosses under 0

// Initial entry on the MACD signal
IF NOT LongOnMarket AND long THEN
    BUY 1 CONTRACTS AT MARKET
ENDIF

If LongOnMarket AND exit THEN
    SELL AT MARKET
ENDIF

// Add a contract once price is 10 points above the last fill
// and at least 5 bars have passed since it
IF BARINDEX - TRADEINDEX(1) > 5 AND Close - TRADEPRICE(1) > 10 AND LongOnMarket THEN
    BUY 1 CONTRACTS AT MARKET
ENDIF

// Trail the whole position
SET STOP %TRAILING 1.5

TRADEPRICE(1) and TRADEINDEX(1) space the add-on entries by price and by time. This is the example from the official reference.

Example 2, Breakeven promotion measured from entry (ProOrder)

probuilder
// Move the stop to entry after 20 points of open profit
IF longonmarket THEN
  IF close - tradeprice >= 20 * pointsize THEN
    SET STOP BREAKEVEN
  ENDIF
ENDIF

The argument-free form refers to the latest execution, here the long entry, so the comparison measures open profit in points from the fill price.

Example 3, Percent-based emergency exit from the last fill (ProOrder)

probuilder
// Close the long if price drops 2 percent below the last entry
IF longonmarket AND close < TRADEPRICE(1) * 0.98 THEN
  SELL AT MARKET
ENDIF

Expressing the exit as a percentage of the recorded entry price keeps the rule portable across instruments with different point values.

Common errors and gotchas

  • Orders, not positions. N counts individual executions, including exits. After a position closes, TRADEPRICE(1) refers to the exit fill, not the old entry. For the average price of the currently open position, use POSITIONPRICE.
  • Unguarded calls before any trade. Early in a backtest, before the first execution, there is no trade history to read. Wrap TRADEPRICE logic in an ONMARKET or LONGONMARKET guard so it only runs with a live position behind it.
  • Cumulated entries diverge from the average. Under CumulateOrders, close - tradeprice measures distance from the latest add-on, while profit is driven by the position average. Mixing the two produces stops that look tighter or looser than intended.
  • Backtest fills versus live fills. ProBacktest records idealized execution prices. Live ProOrder fills include slippage, so distance thresholds calibrated to the tick can behave differently in production.
  • TRADEINDEX, the bar index of a past trade, same indexing as TRADEPRICE.
  • POSITIONPRICE, the weighted average price of the open position.
  • POSITIONPERF, the performance of the open position.
  • LONGONMARKET, true while a long position is open.
  • ONMARKET, true while any position is open.
  • BREAKEVEN, moves the stop or target to the entry price.
  • %TRAILING, trailing stop in percent.
  • CumulateOrders, allows several entries to stack into one position.