Allows to write conditions and/or declare variables in the selected timeframe (description of the ProBacktest command only on this page).
The syntax of the TIMEFRAME statement is TIMEFRAME (TF, mode) which allows you to put the code in a timeframe different from the main TF, or TIMEFRAME (default) to return to the default TF. “mode” is optional (the default is current bar).
Examples:
- TIMEFRAME(weekly, UpdateOnClose)
- TIMEFRAME(30 minutes, default)
- TIMEFRAME(5 minutes)
1 |
timeframe(5 minutes,updateonclose) |
List of TimeFrames available: x second (s), x minute (s), x hour (s), hourly, x day (s), daily, x week (s), weekly, x month (s), monthly, yearly
Available modes:
- UpdateOnClose: only closed bars are used
- Default: The variables defined in superior timeframes take into account each update of the main timeframe
Using a code on a 1 minute timeframe with:
1 |
timeframe(1 hour,updateonclose) |
will return values of the closed bars of the 1-hour timeframe for all codes below this instruction and each 1 minute (code is read at Close), while with:
1 |
timeframe(1 hour,default) |
or
1 |
timeframe(1 hour) |
you’ll get the current time values (not closed bars) of the 1-hour timeframe, and each 1 minute (code is read at Close).
Example #1:
The strategy is a 1-hour timeframe check for the RSI to cross over the level 50.
Then the order management is made in the 1-minute timeframe:
- launch an order if the “buycondition” is true (could also been coded in the 1-hour TF, since the crossover only occur on closed bars with “updateonclose”)
- manage the stoploss to put it at open price + 5 points if the price has reached at least 15 points of profit (see picture below for a better comprehension)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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) //create an order at market if the RSI has crosses the level 50 on the 1 hour timeframe if buycondition then buy 10 contract at market endif //if not order on market, reset the breakeven status if not onmarket then breakeven=0 endif //check if the current order has made 15 points of profit if longonmarket and close-tradeprice>=15*pointsize then breakeven=1 endif //put stoploss at open price + 5 points if breakeven=1 then sell at tradeprice+5*pointsize stop endif graph breakeven |
Order opened at 03:00 (because the RSI crossed the level 50) and the breakeven function put a stoploss at 08:47. Condition are well checked and executed each 1 minute.
Hello,
Please add in the list of timeframes one missing parameter “default” even if it is explained higher in the text.
List of TimeFrames available: default,x second (s), x minute (s), x hour (s), hourly, x day (s), daily, x week (s), weekly, x month (s), monthly, yearly
Hello,
I dont get the timeframe(1 hour) text, saying “you’ll get the current time values (not closed bars) of the 1-hour timeframe, and each 1 minute (code is read at Close).”
Does it mean, when I use timeframe(1 hour), I ll get the price and time each minute, and not when its closing a 1 hour candle? If not, what does “and each 1 minute (code is read at Close).” mean?
It means that on each 1-minute candlestick you get the value of the 1-hour timeframe in its current state. If you add UPDATEONCLOSE for the 1-hour timeframe, the value of that timeframe would the last 1-hour closed value.
Hello Nicolas, thanks for clarifying. It is still not really clear to me.
I might ask another question.
If I am on TIMEFRAME(DAILY,DEFAULT) and I am running it on the 5 minutes chart as lowest timeframe.
In there I have a AVERAGE(10)(DHIGH(0)-DLOW(0)) DHIGH and DLOW are getting updates on a 5 min base since they are in DEFAULT mode.
But somehow (when I plot these) they differ with TIMEFRAME(5 MINUTES, UPDATEONCLOSE).
TIMEFRAME(DAILY,DEFAULT)
result_1 = AVERAGE(10)(DHIGH(0)-DLOW(0))
TIMEFRAME(5 MINUTES,UPDATEONCLOSE)
result_2 = AVERAGE(10)(DHIGH(0)-DLOW(0))
result_1 != result_2