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

request.security()变量驱动时间框架编译异常原因及解决方法求助

关于Pine Script中request.security()时间帧参数的编译问题

我正尝试绕过request.security()的timeframe参数必须为简单字符串的限制,但一种场景可编译、另一种场景失败的情况完全无法理解。

我曾在if()语句中使用变量赋值并成功编译,但只要在这些if语句中放入非简单变量(哪怕该变量和request.security()调用无关),编译就会失败。仅修改一行代码就导致编译成败的差异,我在下方代码片段里加了注释说明这一点,希望有人能提供解决方法或合理解释。

可编译的代码如下(注释标注了导致编译失败的代码行):

var int higherTFOffset = input.int(0,"Higher TF",[0,1,2,3],tooltip = "# of Higher Timeframes to use, 0=Current Chart",group = "EMA Period Settings")
var int ema1Period = input.int(6,"EMA1 Periods",tooltip = "# of Periods for first EMA",group = "EMA Period Settings")


// array of possible timeframes to use based on current chart timeframe
var tfArray = array.new<string>(1,"1")
array.push(tfArray,"5")
array.push(tfArray,"15")
array.push(tfArray,"1H")
array.push(tfArray,"4H")
array.push(tfArray,"D")
array.push(tfArray,"W")
array.push(tfArray,"M")

// get the array index that matches the current chart timeframe
var curTFElement = array.indexof(tfArray,timeframe.period)
if(curTFElement == -1 or curTFElement + higherTFOffset > 7)
    higherTFOffset := 0

// this is the var actually used in the request.security() call
var tfToUse = "1"

/// if I comment the next line and uncomment the line after, it fails to compile
/// but calcTF has nothing to do with the request.security() call
/// and in either case the variable tfToUse is getting reassigned
var calcTF = "5"
//var calcTF = array.get(tfArray,curTFElement + higherTFOffset)

if(calcTF == "1")
    tfToUse := "1"
if(calcTF == "5")
    tfToUse := "5"
else 
    tfToUse := timeframe.period

ema1 = request.security(syminfo.tickerid,tfToUse,ta.ema(close,ema1Period))

这种情况完全不合理。我还尝试创建了一个自定义函数,将序列字符串转换为值,但也无效:

seriesToSimple(myString) =>
    string(myString)

我原本认为一定存在将变量类型转换为简单字符串的方法,或者可以通过与request.security()无关的变量控制if()语句来规避该问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 09:13:21