TIMEFRAME (ProBacktest / ProOrder)

Category: ProBacktest

The TIMEFRAME function in ProBuilder language is used to specify the timeframe for which the trading conditions or variables are evaluated. This is particularly useful in multi-timeframe analysis, where a strategy might involve indicators or conditions across different time periods.

Syntax

TIMEFRAME(TF, mode)
TIMEFRAME(default)

The TIMEFRAME function can be used in two ways:

  • TIMEFRAME(TF, mode): Applies the specified timeframe TF to the code that follows. The mode parameter is optional and determines how the data is updated.
  • TIMEFRAME(default): Returns the evaluation of conditions or variables to the default timeframe of the chart.

Parameters

  • TF: The timeframe to apply. It can be specified in several formats such as ‘x seconds’, ‘x minutes’, ‘x hours’, ‘hourly’, ‘x days’, ‘daily’, ‘x weeks’, ‘weekly’, ‘x months’, ‘monthly’, ‘yearly’.
  • mode: Determines how the data within the specified timeframe is updated. Options include:
    • UpdateOnClose: Only closed bars are used for calculations.
    • Default: Variables consider each update of the main timeframe.

Example Usage

Consider a strategy where an RSI indicator on a 1-hour timeframe is used to trigger trades, but trade management (like setting stop losses) happens on a 1-minute timeframe.

defparam cumulateorders = false

// Declare the strategy on the 1 hour timeframe
TIMEFRAME(1 hour, updateonclose)
myrsi = rsi[14]
buycondition = myrsi crosses over 50

// Orders management on the 1 minute timeframe
TIMEFRAME(1 minute, default)
if buycondition then
  buy 10 contract at market
endif

if not onmarket then
  breakeven = 0
endif

if longonmarket and close - tradeprice >= 15 * pointsize then
  breakeven = 1
endif

if breakeven = 1 then
  sell at tradeprice + 5 * pointsize stop
endif

graph breakeven

In this example, the RSI indicator is calculated based on the 1-hour timeframe with closed bars only. If the RSI crosses over 50, a buy order is placed in the 1-minute timeframe. The script also manages the stop loss based on the conditions defined.

Additional Information

Using different timeframes in a single strategy allows for more flexibility and detailed control over trade entry and management. It is crucial to understand the implications of the mode parameter, as it affects how data is refreshed and therefore can impact the execution of your trading strategy.

Related Instructions:

  • GetTimeframe constants
  • Logo Logo
    Loading...