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

多时间框架TMO指标按当前图表自动预设时间框架可行性问询

可以实现自动预设多时间框架TMO的需求

完全可以根据当前图表的时间框架自动为TMO指标的三个绘图分配对应的时间框架。以下是具体实现方案和修改后的代码:

实现思路

  1. 使用Pine Script v5的timeframe.period函数获取当前图表的时间框架标识(如"5"代表5分钟,"15"代表15分钟)
  2. 通过条件判断语句,根据当前时间框架为TMO1、TMO2、TMO3分配预设的目标时间框架
  3. 可选添加手动覆盖开关,允许用户在需要时自定义时间框架

修改后的完整代码

// @version=5
int MAX_BARS_BACK = 1000
indicator("TMO Ultra", overlay=false, max_bars_back = MAX_BARS_BACK, precision = 0) 

///////////////////////////////////////////////////////////////
//////////// 自动时间框架预设逻辑
///////////////////////////////////////////////////////////////
current_tf = timeframe.period

// 初始化时间框架变量
tf1 = current_tf
tf2 = ""
tf3 = ""

// 根据当前时间框架设置预设规则
if current_tf == "5"
    tf2 = "15"
    tf3 = "30"
elif current_tf == "15"
    tf2 = "45"
    tf3 = "120"
elif current_tf == "60"
    tf2 = "90"
    tf3 = "120"
elif current_tf == "30"
    tf2 = "60"
    tf3 = "180"
// 可根据需要添加更多时间框架的规则
elif current_tf == "45"
    tf2 = "90"
    tf3 = "240"

// 手动覆盖选项(可选)
use_auto_tf = input.bool(true, "启用自动时间框架预设")
manual_tf1 = input.timeframe(current_tf, "手动TMO1时间框架")
manual_tf2 = input.timeframe(tf2, "手动TMO2时间框架")
manual_tf3 = input.timeframe(tf3, "手动TMO3时间框架")

// 最终生效的时间框架
TimeFrame1 = use_auto_tf ? tf1 : manual_tf1
TimeFrame2 = use_auto_tf ? tf2 : manual_tf2
TimeFrame3 = use_auto_tf ? tf3 : manual_tf3

///////////////////////////////////////////////////////////////
//////////// MTF TMO 核心计算逻辑
///////////////////////////////////////////////////////////////

// Inputs
tmolength = input.int(14,title="Length")
calcLength = input(5, title="Calc Length")
smoothLength = input(3, title="Smooth Length")

// TMO 1 Calculations
srcc1 = request.security(syminfo.tickerid, TimeFrame1, close)
srco1 = request.security(syminfo.tickerid, TimeFrame1, open)
o1 =  srco1
c1 =  srcc1
data1 = 0
for i1 = 1 to tmolength -1
    if c1 > o1[i1]
        data1 := data1 + 1
    if c1 < o1[i1]
        data1 := data1 - 1
EMA1 = ta.ema(data1,calcLength)
Main1 = request.security(syminfo.tickerid, TimeFrame1, ta.ema(EMA1, smoothLength))
Signal1 = request.security(syminfo.tickerid, TimeFrame1, ta.ema(Main1, smoothLength))

// TMO 2 Calculations
srcc2 = request.security(syminfo.tickerid, TimeFrame2, close)
srco2 = request.security(syminfo.tickerid, TimeFrame2, open)
o2 =  srco2
c2 =  srcc2
data2 = 0
for i2 = 1 to tmolength -1
    if c2 > o2[i2]
        data2 := data2 + 1
    if c2 < o2[i2]
        data2 := data2 - 1
EMA2 = ta.ema(data2,calcLength)
Main2 = request.security(syminfo.tickerid, TimeFrame2, ta.ema(EMA2, smoothLength))
Signal2 = request.security(syminfo.tickerid, TimeFrame2, ta.ema(Main2, smoothLength))

// TMO 3 Calculations
srcc3 = request.security(syminfo.tickerid, TimeFrame3, close)
srco3 = request.security(syminfo.tickerid, TimeFrame3, open)
o3 =  srco3
c3 =  srcc3
data3 = 0
for i3 = 1 to tmolength -1
    if c3 > o3[i3]
        data3 := data3 + 1
    if c3 < o3[i3]
        data3 := data3 - 1
EMA3 = ta.ema(data3,calcLength)
Main3 = request.security(syminfo.tickerid, TimeFrame3, ta.ema(EMA3, smoothLength))
Signal3 = request.security(syminfo.tickerid, TimeFrame3, ta.ema(Main3, smoothLength))

// TMO 4 Calculations(保留原代码,可根据需要调整或移除)
TimeFrame4 = input.timeframe('180', "TMO 4", options=['1','2','3','5','9','12','15','20','30','45',"60","120","180","240","540",'D','2D','3D','4D','6D','9D','W','2W','3W','6W','9W','M','2M','3M'])
srcc4 = request.security(syminfo.tickerid, TimeFrame4, close)
srco4 = request.security(syminfo.tickerid, TimeFrame4, open)
o4 =  srco4
c4 =  srcc4
data4 = 0
for i4 = 1 to tmolength -1
    if c4 > o4[i4]
        data4 := data4 + 1
    if c4 < o4[i4]
        data4 := data4 - 1
EMA4 = ta.ema(data4,calcLength)
Main4 = request.security(syminfo.tickerid, TimeFrame4, ta.ema(EMA4, smoothLength))
Signal4 = request.security(syminfo.tickerid, TimeFrame4, ta.ema(Main4, smoothLength))

///////////////////////////////////////////////////////////////
//////////// 绘图逻辑
///////////////////////////////////////////////////////////////

// TMO 4 Plots
color4 = Main4 > Signal4 ? #045007 : #4d0c0c
mainLine4 = plot(15*Main4/tmolength, title="TMO 4 Main", color=color4,display=display.none)
signalLine4 = plot(15*Signal4/tmolength, title="TMO 4 Signal", color=color4, linewidth = 8, display=display.none)

// TMO 3 Plots
color3 = Main3 > Signal3 ? #0532ff : #ff7b00
mainLine3 = plot(15*Main3/tmolength, title="TMO 3 Main", color=color3,display=display.none)
signalLine3 = plot(15*Signal3/tmolength, title="TMO 3 Signal", color=color3, linewidth = 6)
SgL3=15*Signal3/tmolength

// TMO 2 Plots
color2 = Main2 > Signal2 ? #28ffe2 : #d937ee
mainLine2 = plot(15*Main2/tmolength, title="TMO 2 Main", color=color2,display=display.none)
signalLine2 = plot(15*Signal2/tmolength, title="TMO 2 Signal", color=color2, linewidth = 4)
SgL2=15*Signal2/tmolength

// TMO 1 Plots
color1 = Main1 > Signal1 ? #58ff60 : #ff0000
mainLine1 = plot(15*Main1/tmolength, title="TMO 1 Main", color=color1,display=display.none)
signalLine1 = plot(15*Signal1/tmolength, title="TMO 1 Signal", color=color1, linewidth = 3)
SgL=15*Signal1/tmolength

关键说明

  • 自动预设规则已按照你的需求实现:5分钟图表对应TMO1=5、TMO2=15、TMO3=30;15分钟对应TMO1=15、TMO2=45、TMO3=120;1小时对应TMO1=60、TMO2=90、TMO3=120
  • 添加了"启用自动时间框架预设"开关,关闭后可手动修改每个TMO的时间框架
  • 可根据需要扩展更多时间框架的预设规则,只需在条件判断中添加对应分支即可
  • 原代码中的TMO4部分保留,你可以根据需求调整其时间框架规则或移除

内容的提问来源于stack exchange,提问作者Giancarlo Bianchi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 11:40:25