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

TradingView Pine Script多时间框架报错修复请求

问题解决:Pine Script多时间框架指标+向右延伸线报错修复

错误原因

给indicator()加timeframe参数后报错,是因为这个参数会让指标在指定时间框架上运行,但你代码里用了line.new()、line.set_xy()这类有副作用的绘图函数——Pine Script不允许在通过indicator(timeframe)切换时间框架的指标里使用这类函数,否则会触发兼容性报错。

修复方案

放弃用indicator()的timeframe参数切换时间框架,改用request.security()来获取目标时间框架的数据,让指标始终在当前图表的时间框架上运行,这样就能保留画线功能。

修改后的完整代码:

//@version=5
indicator('test', overlay=true)

// 让用户选择目标时间框架,替代原indicator的timeframe参数
target_tf = input.timeframe("", title="选择指标时间框架")

//DATA
// 通过request.security获取目标时间框架的数据源
WMAsource = request.security(target_tf, "", close)
WMAlength = input.int(56, minval=1, title='WMA Length')
atrlen1 = input.int(100, minval=1, title='ATR Length')
mult1 = 2

ma1 = ta.wma(WMAsource, WMAlength)
// 获取目标时间框架的TR值
WMArange = request.security(target_tf, "", ta.tr)
rangema1 = ta.wma(WMArange, atrlen1)

up1 = ma1 + rangema1 * mult1

//--------------------------------

//PLOTS

lbullt_tit = '1hr Light Bull Threshold'

//WMA Plots
u4 = plot(up1, color=color.new(color.gray, 0), title=lbullt_tit)

//-----------------------------------
//plot extend right lines

var bool show_hlines = input(true, 'Show horizontal lines')
var bool show_extlines = not show_hlines

var color_lbullt = color.gray

var line_lbullt = line.new(x1=na, y1=na, x2=na, y2=na,xloc=xloc.bar_time, extend=extend.right, color=show_hlines ? color_lbullt : na, style=line.style_dashed)

f_moveLine(_id, _x, _y) =>
    line.set_xy1(_id, _x, _y)
    line.set_xy2(_id, _x + 1, _y)

if barstate.islast
    f_moveLine(line_lbullt, time, up1)

plot(up1, color=show_extlines ? color_lbullt : na, linewidth=1, title=lbullt_tit)

关键修改点

  • 新增target_tf输入框,让用户选择时间框架,替代原indicator()的timeframe参数
  • 把close和ta.tr都通过request.security(target_tf, "", ...)获取目标时间框架的数据,确保计算逻辑基于用户选择的时间框架
  • 保留原有的向右延伸线绘图逻辑,因为现在指标在图表主时间框架运行,不再触发副作用函数的兼容性问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 12:05:30