如何判断当前时间周期?求替代假函数的时间框架检测函数
Hey there! Let's break down your questions clearly—since your example syntax looks a lot like Pine Script (used in TradingView), I'll focus on that first, plus a quick note on MQL (MetaTrader) in case you're using that platform.
1. Real Functions to Detect Time Periods
Most trading scripting languages have built-in tools to check the current time frame, so you don't need to write custom is5mTimePeriod functions. Here's how it works in the two most common platforms:
Pine Script (TradingView)
Pine Script has predefined boolean variables for every standard time frame. You can use these directly:
timeframe.is.m1= 1-minutetimeframe.is.m5= 5-minutetimeframe.is.m15= 15-minutetimeframe.is.m30= 30-minutetimeframe.is.h1= 1-hour (60-minute)timeframe.is.h4= 4-hourtimeframe.is.d= Dailytimeframe.is.w= Weeklytimeframe.is.m= Monthly
You can also use timeframe.period to get the string representation of the current time frame (e.g., "M5" for 5-minute, "H1" for 1-hour) if you need more flexibility.
MQL4/MQL5 (MetaTrader)
In MetaTrader, use the Period() function which returns an integer code for the current time frame. The key codes are:
PERIOD_M1= 1PERIOD_M5= 5PERIOD_M15= 15PERIOD_M30= 30PERIOD_H1= 60PERIOD_H4= 240PERIOD_D1= 1440PERIOD_W1= 10080PERIOD_MN1= 43200
2. Setting Variables Based on Current Time Frame
Let's translate your example into working code using the real functions above.
Pine Script Example
//@version=5 indicator("Time Frame Variable Example", overlay=true) a = 0 // Assign value to 'a' based on time frame a := timeframe.is.m5 ? 1 : timeframe.is.m15 ? 2 : timeframe.period == "M45" ? 3 : // For non-standard 45min frame, check the string directly timeframe.is.h1 ? 4 : 0 // Print the value to confirm var label infoLabel = label.new(bar_index, high, str.tostring(a), color=color.blue) label.set_xy(infoLabel, bar_index, high) label.set_text(infoLabel, "Time Frame Value: " + str.tostring(a))
Note: For non-standard time frames like 45-minute, if the predefined timeframe.is.m45 doesn't exist, checking timeframe.period == "M45" is a reliable workaround.
MQL4 Example
//+------------------------------------------------------------------+ //| Example.mq4 | //| Copyright 2024, MetaQuotes Software Corp. | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Software Corp." #property version "1.00" #property strict int a = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Assign value to 'a' based on time frame switch(Period()) { case PERIOD_M5: a = 1; break; case PERIOD_M15: a = 2; break; case PERIOD_M45: // Confirm your platform supports 45min periods first a = 3; break; case PERIOD_H1: a = 4; break; default: a = 0; break; } Print("Time Frame Value: ", a); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+
That's it! These snippets use native platform functions, so they're reliable and save you from building custom detection logic.
内容的提问来源于stack exchange,提问作者N9va

