TRADEINDEX

Category: ProBacktest

TRADEINDEX is a function in ProBuilder language used to determine the bar index of a bar at which a specific past trade was executed. This function is particularly useful for strategies that require analysis of trade timing, such as assessing the number of bars since the last trade to manage entry or exit conditions.

Syntax:

TRADEINDEX(N)

Where N is the number of the trade in reverse order (1 being the most recent trade, 2 the second most recent, etc.).

Example Usage:

To exit all positions if at least 5 bars have elapsed since the last trade was executed:

if onmarket and BARINDEX - TRADEINDEX(1) >= 5 then
    SELL AT MARKET
    EXITSHORT AT MARKET
endif

This example checks if the market is currently entered (onmarket) and calculates the difference between the current bar index and the bar index of the last trade. If this difference is 5 or more, it triggers a sell to exit all positions.

Additional Example:

Consider a trading strategy that uses the MACD indicator to determine entry and exit points, with additional orders placed based on price movement and time elapsed since the last trade:

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

// First order
IF NOT LongOnMarket AND long THEN
    BUY 1 CONTRACTS AT MARKET
ENDIF

If LongOnMarket AND exit THEN
    SELL AT MARKET
ENDIF

// Additional order condition
IF BARINDEX - TRADEINDEX(1) > 5 AND Close - TRADEPRICE(1) > 10 AND LongOnMarket THEN
    BUY 1 CONTRACTS AT MARKET
ENDIF

// Trailing stop for all orders
SET STOP %TRAILING 1.5

In this strategy, the TRADEINDEX function is used to ensure that at least 5 bars have passed since the last trade before considering an additional buy order. This is combined with a condition that the current closing price must be more than 10 points higher than the price at which the last trade was executed.

Conclusion:

The TRADEINDEX function is essential for strategies that depend on the timing of previous trades to make informed decisions about when to enter or exit the market. It provides a straightforward method to access historical trade execution points relative to the current market position.

Related Instructions:

  • BarIndex instructions
  • POSITIONPRICE probacktest
  • TRADEPRICE probacktest
  • Logo Logo
    Loading...