timeframe.period in MQL5: ENUM_TIMEFRAMES and PeriodSeconds
Short answer
The chart timeframe is _Period, an ENUM_TIMEFRAMES value, and PeriodSeconds(tf) gives its length in seconds. Do arithmetic through PeriodSeconds, because the enum's value is not a minute count above M30.
Pine describes the chart timeframe as a string ("1", "60", "240", "D", "W", "M") with helpers around it. MQL5 describes it as an ENUM_TIMEFRAMES value: _Period or Period() for the chart, constants such as PERIOD_H1 for others, and PeriodSeconds(tf) for the length in seconds. The enum's numeric value is not a minute count above M30, so anything arithmetic must go through PeriodSeconds.
What timeframe.* computes
timeframe.period: the chart timeframe as a string; minutes below a day, then"D","W","M"with an optional multiplier ("3D").timeframe.in_seconds(tf): the length oftfin seconds, or of the chart timeframe without an argument. A month counts as 30 days.timeframe.multiplier: the number in front of the unit: 60 for"60", 1 for"D".timeframe.isintraday,isdaily,isweekly,ismonthly,isseconds,isminutes,isdwm: booleans about the chart timeframe.timeframe.change(tf): true on the first chart bar of a newtfperiod, for example the first bar of each day.
The MQL5 equivalent
The example reproduces each value on the chart and prints the length of H4 for timeframe.in_seconds("240"). timeframe.change("D") becomes a calendar comparison of two bar open times with TimeToStruct.
//@version=6
indicator("timeframe demo")
string tf = timeframe.period
int secs = timeframe.in_seconds()
int h4secs = timeframe.in_seconds("240")
int mult = timeframe.multiplier
bool newDay = timeframe.change("D")
plot(newDay ? 1 : 0, "new day", style=plot.style_histogram)
if barstate.islast
label.new(bar_index, 0, tf + " / " + str.tostring(secs) + "s / x" + str.tostring(mult) + " / H4=" + str.tostring(h4secs))#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrDodgerBlue
#property indicator_label1 "new day"
double NewDayBuf[];
// Pine's timeframe.period spelling for the chart period: "1", "60", "240", "D", "W", "M"
string TfString(const ENUM_TIMEFRAMES tf)
{
int sec = PeriodSeconds(tf);
if(sec < 60) return IntegerToString(sec) + "S";
if(sec < 86400) return IntegerToString(sec / 60);
if(sec == 86400) return "D";
if(sec == 604800) return "W";
return "M";
}
// Pine's timeframe.multiplier: the number in front of the unit
int TfMultiplier(const ENUM_TIMEFRAMES tf)
{
int sec = PeriodSeconds(tf);
if(sec < 60) return sec;
if(sec < 86400) return sec / 60;
return 1;
}
// Pine's timeframe.change("D"): this bar opened on a different day than the previous one
bool NewDay(const datetime &time[], const int i)
{
if(i == 0) return true;
MqlDateTime a, b;
TimeToStruct(time[i], a);
TimeToStruct(time[i - 1], b);
return a.day != b.day || a.mon != b.mon || a.year != b.year;
}
int OnInit()
{
SetIndexBuffer(0, NewDayBuf, INDICATOR_DATA);
// Pine: timeframe.period, timeframe.in_seconds(), timeframe.multiplier
IndicatorSetString(INDICATOR_SHORTNAME,
"tf " + TfString(_Period) + " / " + IntegerToString(PeriodSeconds()) + "s / x" + IntegerToString(TfMultiplier(_Period)));
// Pine: timeframe.in_seconds("240")
Print("H4 is ", PeriodSeconds(PERIOD_H4), " seconds; chart period enum is ", EnumToString(_Period));
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const datetime &time[], const double &open[],
const double &high[], const double &low[],
const double &close[], const long &tick_volume[],
const long &volume[], const int &spread[])
{
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
NewDayBuf[i] = NewDay(time, i) ? 1.0 : 0.0;
return rates_total;
}Months have different lengths on both platforms, so a fixed seconds value for M or PERIOD_MN1 is nominal; detect month changes from the calendar fields, as NewDay does for days.
Where the two differ
| Pine | MQL5 | Note |
|---|---|---|
timeframe.period ("60") | _Period (PERIOD_H1) | enum value of H1 is 16385, not 60; EnumToString gives "PERIOD_H1" |
timeframe.in_seconds() | PeriodSeconds() | exact |
timeframe.in_seconds("240") | PeriodSeconds(PERIOD_H4) | exact for periods MT5 has |
timeframe.multiplier | derive from PeriodSeconds() | no built-in |
timeframe.isintraday | PeriodSeconds() < 86400 | exact |
timeframe.change("D") | day of time[i] differs from day of time[i-1] | server-time days, not exchange days |
Custom periods ("45", "3D") | none | MT5 has a fixed list from M1 to MN1 |
Seconds timeframes ("30S") | none on standard charts | MT5's smallest period is M1 |
What the Stratlate converter emits
timeframe.period is emitted as pine_tf_string(), a runtime function that rebuilds TradingView's spelling from PeriodSeconds(), so comparisons such as timeframe.period == "60" work; timeframe.multiplier and the is* predicates are expressions over PeriodSeconds(), and timeframe.change(tf) is pine_timeframe_change(tf, time, bar), which compares days, weeks and months on the calendar. The call form timeframe.in_seconds(...) is currently emitted as (Period() * 60): with an argument it is reported as an approximation (PM241, chart period used), and without one it carries no note, although Period() is an enum value and the result is only right on M1 to M30. If a converted script uses timeframe.in_seconds(), replace that expression with PeriodSeconds().
Pitfalls
- Doing arithmetic on
_Period:_Period * 60on an H1 chart is 983100, not 3600. Symptom: lengths scaled by the timeframe are enormous above M30 and correct below. - Comparing
_Periodwith a Pine string: build the string with a helper as above, or compare enums directly (_Period == PERIOD_H1). - Assuming
timeframe.change("D")fires at midnight exchange time: in MT5 the day rolls over at server midnight. - Translating
timeframe.change("W")as "Monday": a market that opens on Sunday evening server time starts its week on Sunday's bar; compare week numbers, not weekday names. - Asking for a period MT5 lacks:
PeriodSecondsaccepts only enum values; a string like"45"needs your own minutes-to-seconds conversion.
FAQ
What does Period() return in MQL5?
An ENUM_TIMEFRAMES value. For M1 to M30 the number equals the minutes, but from H1 upwards it is an encoded constant (H1 is 16385). Use PeriodSeconds() for the length and EnumToString() for a readable name.
How do I get timeframe.in_seconds for another timeframe?
PeriodSeconds(PERIOD_H4) and so on, for any timeframe MetaTrader has. Pine timeframes without an MT5 period, such as "45", have to be converted by hand: minutes times 60.
How do I write timeframe.change("D") in MQL5?
Compare the calendar day of the current bar's open time with the previous bar's, using TimeToStruct on time[i] and time[i - 1]. Treat bar 0 as a change. Weeks and months work the same way with week numbers or the month field.
Convert the whole script
Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter