You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断当前时间周期?求替代假函数的时间框架检测函数

How to Detect Time Frames and Set Variables Based on Them

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-minute
  • timeframe.is.m5 = 5-minute
  • timeframe.is.m15 = 15-minute
  • timeframe.is.m30 = 30-minute
  • timeframe.is.h1 = 1-hour (60-minute)
  • timeframe.is.h4 = 4-hour
  • timeframe.is.d = Daily
  • timeframe.is.w = Weekly
  • timeframe.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 = 1
  • PERIOD_M5 = 5
  • PERIOD_M15 = 15
  • PERIOD_M30 = 30
  • PERIOD_H1 = 60
  • PERIOD_H4 = 240
  • PERIOD_D1 = 1440
  • PERIOD_W1 = 10080
  • PERIOD_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:32:22