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

如何修复PineScript V5中request.security_lower_tf低周期报错问题

解决PineScript V5跨周期指标报错问题

问题原因

原代码使用request.security_lower_tf()获取日线收盘价,但该函数要求图表周期必须大于等于指定的目标周期(此处为日线)。当图表周期低于日线时,会触发报错:

Study Error
The chart's timeframe must be greater or
equal to the 'D' timeframe used with
'request.security_lower_tf()'.

修改方案

通过判断当前图表周期与日线的关系,仅在周期符合条件时执行日线收盘价相关逻辑;低于日线周期时,自动跳过该部分代码,仅显示移动平均线。

修改后的代码

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

// 判断当前周期是否大于等于日线
is_valid_tf = timeframe.in_seconds() >= timeframe.in_seconds("D")

/////////////// Start security_lower_tf Daily close ///////////////
var float element = na

if is_valid_tf
    array<float> close_values = request.security_lower_tf(syminfo.tickerid, "D", close)
    array_size = array.size(close_values)
    if array_size
        last_element_index = array_size - 1
        element := array.get(close_values, last_element_index)

// 仅在有效周期下显示日线收盘价线
plot(element, color=color.red, display=is_valid_tf ? display.data_window : display.none)
/////////////// End security_lower_tf Daily close ///////////////

/////// Start Moving Average /////// 
fastAverage = ta.sma(close, 10)
slowAverage = ta.sma(close, 50)

plot(fastAverage, color=color.navy)
plot(slowAverage, color=color.fuchsia)

关键修改说明

  1. 周期有效性判断:用timeframe.in_seconds()将当前周期和日线周期转换为秒数进行比较,确保只有在周期符合要求时才执行跨周期逻辑
  2. 条件执行代码:把request.security_lower_tf()相关代码包裹在if is_valid_tf分支中,避免在低周期下触发函数限制
  3. 动态控制显示:通过display参数控制日线收盘价线的显示状态,低周期下自动隐藏该线条,仅保留移动平均线

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:27:12