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

如何在Pine Script DeMarker指标中添加第二个可选择时间框架?

问题描述

我正在创建一个可选择时间框架的DeMarker指标,目前所有功能正常,但无法在输入项中选择第二个时间框架。此指标基于PasqualeAntonino的代码修改而来,我仅调整了外观并添加了移动平均线。以下是对应的代码:

//@version=5
indicator(title="DeMarker", shorttitle="DeM", format=format.price, precision=2, timeframe="",    timeframe_gaps=true)

len1 = input.int(14, title="Demarker Length")
len0 = input.int(14, title="Demarker Length2")
demax = high > high[1] ? high - high[1] : 0   
demax1 = high > high[1] ? high - high[1] : 0   
demin = low < low[1] ? low[1] - low : 0
demin1 = low < low[1] ? low[1] - low : 0
demax_av = ta.sma(demax, len1)
demax_av1 = ta.sma(demax1, len0)
demin_av = ta.sma(demin, len1)
demin_av1 = ta.sma(demin1, len0)
dmark = demax_av / (demax_av + demin_av)
dmark2 = demax_av1 / (demax_av1 + demin_av1)
len2 = input.int(21, title="Moving Average Length")
len3 = input.int(21, title="Moving Average Length2")
dma = ta.ema(dmark, len2)
dma2 = ta.ema(dmark, len3)

//Plots
h1 = hline(0.7, linestyle=hline.style_solid, color=color.new(#ef5350, 20), title="Upper Line")
h2 = hline(0.5, linestyle=hline.style_solid, color=color.new(#787b86, 50), title="Center Line")
h3 = hline(0.3, linestyle=hline.style_solid, color=color.new(#26a69a, 20), title="Lower Line")
fill(h1, h3, color=color.new(#787b86, 90), title="Background")
plot(dma, color=#787b86, linewidth=1, title="Moving Average 1")
plot(dma2, color=#787b86, linewidth=1, title="Moving Average 2")
plot(dmark, color=#2962ff, linewidth=2, title="Demarker")
plot(dmark2, color=#2962ff, linewidth=2, title="Demarker2")

请问需要添加什么代码才能实现第二个时间框架的可选择输入?

解决方案

要实现第二个时间框架的选择,你需要使用Pine Script v5的request.security()函数跨时间框架获取数据,并添加对应的时间框架输入项。具体修改如下:

修改后的完整代码

//@version=5
indicator(title="DeMarker", shorttitle="DeM", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

// 基础输入项
len1 = input.int(14, title="Demarker Length (Current TF)")
len0 = input.int(14, title="Demarker Length (Second TF)")
// 新增第二个时间框架选择输入
tf2 = input.timeframe("D", title="Second Timeframe")

// 当前时间框架的DeMarker计算
demax = high > high[1] ? high - high[1] : 0   
demin = low < low[1] ? low[1] - low : 0
demax_av = ta.sma(demax, len1)
demin_av = ta.sma(demin, len1)
dmark = demax_av / (demax_av + demin_av)

// 第二个时间框架的DeMarker计算
// 通过request.security获取目标时间框架的高低价
[sec_high, sec_low] = request.security(tf2, 0, [high, low])
demax1 = sec_high > sec_high[1] ? sec_high - sec_high[1] : 0   
demin1 = sec_low < sec_low[1] ? sec_low[1] - sec_low : 0
demax_av1 = ta.sma(demax1, len0)
demin_av1 = ta.sma(demin1, len0)
dmark2 = demax_av1 / (demax_av1 + demin_av1)

// 移动平均线输入
len2 = input.int(21, title="Moving Average Length (Current TF)")
len3 = input.int(21, title="Moving Average Length (Second TF)")
dma = ta.ema(dmark, len2)
// 对应第二个DeMarker的均线
dma2 = ta.ema(dmark2, len3)

//Plots
h1 = hline(0.7, linestyle=hline.style_solid, color=color.new(#ef5350, 20), title="Upper Line")
h2 = hline(0.5, linestyle=hline.style_solid, color=color.new(#787b86, 50), title="Center Line")
h3 = hline(0.3, linestyle=hline.style_solid, color=color.new(#26a69a, 20), title="Lower Line")
fill(h1, h3, color=color.new(#787b86, 90), title="Background")
plot(dma, color=#787b86, linewidth=1, title="Moving Average (Current TF)")
plot(dma2, color=#ff9800, linewidth=1, title="Moving Average (Second TF)")
plot(dmark, color=#2962ff, linewidth=2, title="Demarker (Current TF)")
plot(dmark2, color=#e91e63, linewidth=2, title="Demarker (Second TF)")

关键修改说明

  • tf2 = input.timeframe("D", title="Second Timeframe"):新增时间框架选择控件,默认值为日线,用户可自由切换其他时间周期。
  • [sec_high, sec_low] = request.security(tf2, 0, [high, low]):从指定的第二个时间框架中获取K线的高低价,0表示使用当前K线时间戳对齐数据,保证指标显示同步。
  • 调整dma2的计算对象为dmark2,并修改第二个时间框架指标的颜色,方便视觉区分不同周期的信号。

内容的提问来源于stack exchange,提问作者Ricardo Rodrigues

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 07:44:57