BarPeriod
The duration of one bar in minutes (default = 60). The minimum bar period is one second (1./60) for normal scripts, and 1 ms (0.001/60) for HFT simulation. The maximum bar period is one day (1440). The bar period is the basic time frame in the script. It determines the width of a candle in the chart, and the frequency of the run function that is called after every bar. If not explicitly set up in the script, it can be determined with the [Period] slider. Price-movement bars (see bar function) have no fixed bar period; in that case BarPeriod should be set to the average duration of a bar.
Type:
var
BarOffset
Bar start time offset in minutes; must be smaller than BarPeriod. Bars and frames normally start at a date/time boundary; f.i. 60-minute bars start at every full hour, and daily bars start at every midnight (except for weekends). This can be changed with BarOffset. For daily bars, BarOffset determines the UTC minute into the day when the candle opens, and can be used to shift the bar begin to a certain local or global time. BarOffset is automatically set up by NumSampleCycles by dividing a bar into equal time intervals and decreasing the offset by one interval per cycle; on the last cycle, BarOffset is 0.
BarZone
Time zone that determines the close time of daily bars. Can be set to UTC for UTC time (default), ET for New York, WET for London, CET for Frankfurt, AEST for Sydney, JST for Tokyo, or any number giving the zone offset in hours to UTC. Daylight saving time is used except for UTC and JST. Please see the remarks about StartWeek and EndWeek, which are in UTC; adapt them to the local market hours if necessary.
For trading on different asset-dependent time zones use AssetMarket.
TimeFrame
Time frame in bars (default = 1) used for all subsequent price, time, series, advise, and trade calls. This variable can be set within a strategy for using multiple time frames,
for skipping bars, for synchronizing time frames to external events, or for for
emulating special bars such as Range or Renko Bars. For instance, with a bar period of 5 minutes (BarPeriod = 5) and a time frame of 12 bars (TimeFrame = 12), series and trade signals use a one hour time frame (5*12 = 60 minutes).
For skipping bars, set TimeFrame to 0; count all the skipped bars; and
for ending the period, set TimeFrame to the negative number of skipped bars. See remarks and example; see also AssetFrame.
FrameOffset
Time frame offset in bars (default = 0 = no offset) used for all subsequent price, time, series, and trade calls; must be smaller than TimeFrame. When fixed time frames are used, this variable determines the bar number within a time frame when series are shifted and trades are executed.
This variable allows to generate trade signals at different times dependent on the asset.
Type:
int
Remarks:
- Use the bar function for special bars that are not bound to a certain bar period.
- BarPeriod can be set to any value between 0.00166 and 1440 in the script. Bar periods above 1 are rounded to a multiple of minutes. The [Period] slider moves in fixed steps corresponding to the usual bar periods of trading platforms; directly setting BarPeriod places the slider at the position closest to the BarPeriod value. If BarPeriod is set directly in the script, the slider can not be moved.
- In [Test] mode, bar generation is based on the UTC time stamps of the historical price data.
For M1 data it is supposed that the time stamps indicate the end (not the
start) of a tick. Otherwise use the TickFix
variable for adjusting M1 data with different time stamps. In [Trade] mode, bar generation is based on UTC time from the PC clock, not on the broker's server time for avoiding sync problems by connection interruptions and Internet lag.
- On bar periods less than ~ 10 minutes, setting the TICKS flag and using high resolution .t1 price data is recommended for backtesting. It's mandatory on bar periods of less than a minute. For this set History to ".t1" and BarPeriod to the number of seconds divided by 60, f.i. BarPeriod = 5./60. for 5 seconds. Mind the decimal; 5/60 would be considered an int and thus evaluate to 0. For extremely short bar periods, make sure that TickTime is always less than a bar period.
- For bar periods longer than a day, set BarPeriod = 1440 and use TimeFrame for setting up weekly or monthly time periods. A week can contain 5, 6, or 7 trading days dependent on the Weekend setting.
- If multiple time frames and different trade time offsets are not required, set the time frame with BarPeriod only. This uses the lowest possible number of bars and results in the highest speed for testing and training.
- BarPeriod, BarOffset, and BarZone affect the sampling of the price data; therefore they must be set in the first run before any asset() call, can not change afterwards, and can not be optimized in WFO mode. TimeFrame, FrameOffset, and AssetZone can be set anytime and can be optimized without restrictions.
- Because bars start and end at day/hour boundaries, Friday daily bars end at Saturday 00:00 midnight when Weekend is at 1. The markets are closed at that time, therefore no trades can normally be entered after a Friday bar. For trading on Fridays, either don't use daily bars or set BarOffset accordingly so that the Friday bar ends earlier when the markets are still open.
- TimeFrame is _not_ equivalent to BarPeriod. A script with BarPeriod 60 and TimeFrame 1 is not the same as a script with BarPeriod 1 and TimeFrame 60! The differences:
- Barperiod affects only the bar chart and the frequency of the
run function, while TimeFrame determines the
price functions, the shifting of
series, and the bar at which
advise and trade calls are executed.
- Trades entered in the run function are not executed when the
TimeFrame has not ended, but trade entry/exit conditions are tested at the end of every bar period.
- Any calculations in the run function are executed at any bar period, unless they are explicitely restricted to time frames. Use the
frame function for this if needed.
- Beginning and ending at day/hour boundaries is guaranteed for BarPeriod, but not for
TimeFrame due to the different number of bars per day when holidays or weekends lie in the simulation period. For letting a time frame start at always the same hour, use the
hour() or lhour() functions. For aligning daily time frames to a certain hour or time zone, either set
TimeFrame by script to the negative number of day bars at midnight (see example) and to
0 otherwise, or use the AssetFrame variable, or use the
frameSync() function.
- Use BarPeriod = 1440 and TimeFrame = framesync(7) for weekly time frames. The week begin can be set with
FrameOffset; otherwise the week begins on Monday.
- TimeFrame has no effect on variables in bar period units, f.i.
LookBack, LifeTime, or
WaitTime. For extending those time periods, multiply the variable value with the number of bars per time frame.
- TimeFrame does not affect a few indicators that do not use a
Data series, but directly the current asset price series. This is mentioned in the description of the indicator.
Examples:
#define H24 (1440/BarPeriod)
#define H4 (240/BarPeriod)
#define H1 (60/BarPeriod)
...
BarPeriod = 60; // 1 hour (60 minutes) bars
...
// create a 4-hour price series ///////////////////////////////////
TimeFrame = H4;
vars PriceH4 = series(price());
// create a 24-hour price series /////////////////////////////////
TimeFrame = H24;
vars PriceH24 = series(price());
// skip bars outside market hours (equivalent to AssetMarket) //////
// equivalent to AssetZone
static int SkippedBars = 0;
if(!market(ET,0)) {
TimeFrame = 0;
SkippedBars--; // count negative number of bars outside market hours
} else if(TimeFrame == 0) {
TimeFrame = SkippedBars;
SkippedBars = 0;
} else
TimeFrame = 1;
vars PriceInMarketHours = series(price());
// create a daily price series (equivalent to AssetZone) ////////
static int BarsPerDay = 0;
if(hour(0) < hour(1)) { // day change
TimeFrame = BarsPerDay; // end the frame
BarsPerDay = 0;
} else {
TimeFrame = 0; // inside the frame
BarsPerDay--; // count negative number of bars per day
}
vars PriceD1 = series(price());
// alternative: a midnight-aligned price series using frameSync()
StartWeek = 10000;
TimeFrame = frameSync(H24);
vars PriceD1 = series(price());
// back to 1-hour time frames
TimeFrame = 1;
...
See also:
Bars and Candles, Bar, BarOffset, LookBack, dayHigh, Date, Weekend, AssetFrame, frameSync, bar
► latest version online