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.
User-defined 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/end time offset in minutes; 0 or a positive number smaller than BarPeriod
(default = 930 with daily bars, otherwise 0). Bars and frames normally start at a date/time boundary; f.i. 60-minute bars start at every full hour, and daily bars
normally start at UTC midnight when BarOffset is 0. For daily bars,
use BarOffset shift the bar begin to the desired trading time
of the day; the 930 default sets it to 15:30 in the bar
time zone. BarOffset is also automatically set by NumSampleCycles
for dividing a bar into equal time intervals and decreasing the offset by one interval per cycle; on the last cycle, BarOffset
is 0.
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 synchronizing time frames to a
certain event, time, or date, skip the bars inbetween by setting TimeFrame to 0.
Count the skipped bars, and set TimeFrame to the negative number of skipped bars
when the event happens. The time frame synchronizing mechanism only affects the
shifting of series; price and other functions are
unaffected. See remarks and example; see also frameSync
and 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
standard 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
is locked. For bar periods of less than one minute, you'll normally need
tick history (History = ".t1").
- In [Test] mode, bar generation is based on the
UTC time stamps of ticks in the historical price data.
All ticks that fall in the bar period contribute to the bar. For instance, a
10-minute bar ending at 12:00 contains all ticks with timestamps from
11:50:00.000 until 11:59:59.999. If the tick covers several prices, its time stamp
should be from its close price. If historical time stamps are from the open or
the middle price instead, you can use the TickFix
variable for adjusting them. 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 short bar periods it is recommended to set the TICKS flag and use high resolution .t1 price data. 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. For instance, TimeFrame = frameSync(7)
activates weekly time frames. The week begin can be set with
FrameOffset; otherwise the week begins on Monday.
- BarPeriod and BarOffset affect the
sampling of price data. Therefore they must be set in the first run
before selecting the
asset, must not change afterwards, and must not be
optimized. TimeFrame and FrameOffset can be set anytime and can be optimized without restrictions.
- Historical price data should have sufficient resolution so
that any BarPeriod contains at least one historical tick
during market hours. If BarOffset is nonzero, historical
and lookback data need accordingly higher resolution. For instance, with a
daily bar period price data must have 30-minutes resolution when
BarOffset is set so that bars end at 15:30.
- If BarOffset is 0, bars begin and end at day/hour boundaries.
When daily bars would begin and end at 00:00
midnight, their OHLC candles would contain the prices from the previous, not
from the current day. Therefore BarOffset has a
default value of 930 (equivalent to 15:30) on daily bars.
Alternatively, set BarOffset to the
time when you want the script to trade; for example, BarOffset
= 60*9+30; for trading at market open at 9:30.
- All time zone settings assume UTC time for price history. If price history is
in local time, either convert it to UTC or don't use
time zone settings in the backtest.
TimeFrame vs. BarPeriod:
- 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,
due to subtle differences listed below. If multiple time frames or 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 affects only the bar chart and the frequency of the
run function, while TimeFrame determines the
price functions, the shifting of
series, and the bars 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.
- TimeFrame has no effect on variables in bar period units, f.i.
LookBack, UnstablePeriod, 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.
- Accumulative indicators such as EMA, ATR,
or most spectral filters, require
UnstablePeriod to be adapted to the time frame.
Even then they normally have slightly different return values dependent on
the BarPeriod due initial differences, especially at the
begin of the simulation.
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, NumBars,
SampleOffset, LookBack, dayHigh, Date, BarMode, AssetFrame, frameSync, bar
► latest version online