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

Pine Script中能否为input.text_area的defval赋值变量?

Pine Script中无法为input.text_area的defval赋值变量,附替代实现方案

结论先行:截至Pine Script v5版本,input.text_area()的defval参数确实要求传入编译期常量,无法直接使用动态生成的变量(比如依赖syminfo.ticker这类运行时才能确定的值),你尝试的str.tostring()等方法本质还是生成变量,所以不符合要求。

替代实现方案

虽然不能直接设置动态默认值,但可以通过「常量默认值+运行时替换」的方式实现类似需求:

  • 先给输入框设置一个简单的常量默认值
  • 脚本运行时,判断用户是否修改过输入内容,未修改则使用动态生成的文本;若用户自定义了内容,则保留用户输入

示例代码:

//@version=5
indicator("Dynamic Alert Text Demo", overlay=true)

// 创建带常量默认值的输入框
i_alert_txt_entry_long = input.text_area(defval = "long entry", title = "Long Entry Message", group = "Alerts")

// 动态生成目标文本
dynamic_alert_text = syminfo.ticker + " long entry"

// 核心逻辑:判断是否使用动态文本还是用户输入
final_alert_text = i_alert_txt_entry_long == "long entry" ? dynamic_alert_text : i_alert_txt_entry_long

// 测试:在最新K线显示最终文本
if barstate.islast
    label.new(bar_index, high, final_alert_text, color=color.orange, textcolor=color.white)

方案说明

  • 首次加载脚本时,输入框显示的是预设的常量默认值,但实际脚本会自动替换为包含合约代码的动态文本
  • 一旦用户修改了输入框内容,脚本会优先使用用户自定义的文本,不会再替换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 22:10:35