ProBacktest/probacktest · proorder

LongTriggered

LongTriggered in ProBacktest and ProOrder returns true when a long entry executed on a given bar. Use it to detect fills from pending orders bar by bar.

Syntax

probuilder
LongTriggered
probuilder
LongTriggered[N]

Parameters

NameTypeDefaultDescription
Ninteger0Bar offset relative to the current bar. 0 is the current bar, 1 is the previous bar, and so on.

How it works

Each time a strategy opens a long position, ProBacktest and ProOrder record the fill on the bar where it happened. LongTriggered reads that record: it returns true on a bar where a long entry executed and false everywhere else. The optional index shifts the lookup into the past, so LongTriggered[1] asks whether a long entry filled on the previous bar.

The distinction from LongOnMarket matters most with pending orders. A stop or limit entry can fill intrabar and its protective stop or target can also trigger within the same bar. In that case LongOnMarket is already false again by the time the code runs at bar close, and the trade would be invisible to position-state checks. LongTriggered still reports true for that bar, so the strategy can react to the fill even when the position no longer exists.

The flag is tied to entries only. Exits, stop hits, and target hits do not set it. For short entries the counterpart is ShortTriggered.

Examples

Example 1, Counting long entries (ProOrder)

probuilder
// increment a counter each time a long entry fills
if LongTriggered then
  buycount = buycount + 1
endif

Every bar where a long entry executed increases buycount by one. The counter can then gate further logic, for example limiting the number of entries per session.

Example 2, Pause after a fresh fill (ProBacktest)

probuilder
// entry signal: close crosses above the 50-bar average
signal = close crosses over Average[50](close)

// skip new entries on the bar right after a fill
IF signal AND NOT LongTriggered[1] AND NOT LongOnMarket THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

LongTriggered[1] checks whether a long entry filled on the previous bar. The condition blocks back-to-back entries on consecutive bars, a simple cooldown filter.

Example 3, Detecting a same-bar entry and exit (ProOrder)

probuilder
// pending entry above the prior high, protective stop attached
IF NOT OnMarket THEN
  BUY 1 CONTRACT AT high[1] STOP
  SET STOP PLOSS 20
ENDIF

// a fill happened this bar but no position remains:
// the stop was hit within the same bar
IF LongTriggered AND NOT LongOnMarket THEN
  stoppedSameBar = 1
ENDIF

When LongTriggered is true while LongOnMarket is false on the same bar, the entry and its stop both executed inside that bar. The flag stoppedSameBar can then be used to stand aside or adjust the next entry.

Common errors and gotchas

  • Default index is the current bar. LongTriggered without brackets means LongTriggered[0]. Code that intends to check the previous bar must write LongTriggered[1] explicitly. Assuming the bare form looks one bar back is a frequent misreading.
  • Confusing it with LongOnMarket. LongOnMarket reports open-position state, LongTriggered reports the entry fill event. A trade that opens and closes within one bar shows LongTriggered = TRUE and LongOnMarket = FALSE on that bar.
  • Entries only. The flag never reacts to exits or stop hits by itself. Detecting a same-bar stop-out requires combining it with a position-state check, as in Example 3.
  • Negative or non-integer index. N must be a non-negative integer. Passing a negative offset or a fractional value is invalid, there is no lookahead into future bars.
  • ShortTriggered, the equivalent flag for short entries.
  • LONGONMARKET, true while a long position is open.
  • SHORTONMARKET, true while a short position is open.
  • ONMARKET, true while any position is open.
  • BUY, opens a long position.
  • MARKET, immediate execution at the current market price.
  • TRADEPRICE, the entry price of the current position.
  • TRADEINDEX, the bar index of the current position's entry.