POSITIONPERF
POSITIONPERF returns the percentage performance of an open or closed position in ProBacktest and ProOrder. PositionPerf(0) reads the current open trade.
Syntax
PositionPerf(N)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
N | integer | 0 | Position index. 0 reads the currently open position. A value greater than 0 reads the N-th most recently closed position, so 1 is the last closed trade. |
How it works
POSITIONPERF measures how well a single position performed, expressed as the ratio of its gain or loss to its cost. A reading of 0.05 corresponds to a 5 percent gain and -0.02 to a 2 percent loss, so multiply by 100 when a percent figure is needed for display.
The index argument selects which position to evaluate. With N = 0, or with the argument omitted, the function reports the running performance of the position that is currently open. With N > 0 it walks backward through closed trades, PositionPerf(1) being the most recent exit, PositionPerf(2) the one before it, and so on. This makes it the natural building block for trade-by-trade logic such as loss streaks or performance filters.
The calculation is gross. Brokerage fees, spreads modeled as costs, and financing are not deducted, so live net results are lower than the raw readings. For the cumulative cash result of the whole strategy, use STRATEGYPROFIT instead; POSITIONPERF always describes one position at a time.
Examples
Example 1, Reading the last closed trade (ProBacktest)
// Gain-to-cost ratio of the most recently closed position
positionPerformance = PositionPerf(1)
GRAPH positionPerformance * 100 AS "Last trade performance in percent"Retrieves the performance of the last completed trade and plots it as a percentage. This is the reference example from the source documentation with a display line added.
Example 2, Emergency exit on open-trade drawdown (ProOrder)
// Close a long when the open position loses more than 2 percent
IF longonmarket AND positionperf(0) < -0.02 THEN
SELL AT MARKET
ENDIFThe open position is monitored bar by bar and abandoned once its running performance drops below -0.02, a percent-based safety exit that works independently of any point-based stop.
Example 3, Reducing size after two consecutive losers (ProOrder)
// Halve exposure after two losing trades in a row
baseSize = 4
IF positionperf(1) < 0 AND positionperf(2) < 0 THEN
tradeSize = max(1, baseSize / 2)
ELSE
tradeSize = baseSize
ENDIF
IF NOT onmarket AND close CROSSES OVER average[50](close) THEN
BUY tradeSize CONTRACTS AT MARKET
ENDIFThe two most recent closed trades are inspected before each entry. After back-to-back losses the strategy trades at half size until a trade closes positive.
Common errors and gotchas
- Ratio, not percent. The function returns a ratio, so a 3 percent gain reads as 0.03. Comparing against whole numbers such as
positionperf(0) > 3almost never triggers. Multiply by 100 only for display. - Gross of costs. Fees and commissions are excluded, so a trade that reads slightly positive can be a net loss live. Thresholds close to zero are the most affected.
- No open position. With nothing on market, the index 0 reading is meaningless for decision logic. Guard reads of
positionperf(0)withONMARKET,LONGONMARKET, orSHORTONMARKET. - Per-position scope. POSITIONPERF never aggregates. Summing account-level results by looping over indexes is fragile; STRATEGYPROFIT already provides the cumulative cash figure.
Related instructions
STRATEGYPROFIT, cumulative realized profit of the strategy in cash.POSITIONPRICE, the average entry price of the open position.TRADEPRICE, the entry price of individual orders.TRADEINDEX, the bar index at which an order executed.COUNTOFPOSITION, the signed size of the open position.ONMARKET, true while any position is open.LONGONMARKET, true while a long position is open.SELL, closes a long position.
