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

如何修改Pine Script中str.replace_all实现双变量替换

修改Pine Script告警消息以支持多变量替换

核心解决方法

直接嵌套调用str.replace_all函数即可——先完成{{entrySize}}的替换,再用替换后的字符串去处理{{tp_price}}变量。

具体修改步骤

  1. 更新输入框默认文本
    把entrystring的defval替换为你提供的包含{{tp_price}}的目标字符串。

  2. 调整alertMsg1的赋值逻辑
    将原来单次替换的代码改成嵌套两次str.replace_all,依次完成两个变量的替换。

修改后的完整代码

useentryalert = input(defval=true, title="Use Entry Alert", group="Alert Messages")
// 更新默认值,新增{{tp_price}}变量
entrystring = input.text_area(title="Entry Alert Message", defval='TVM:{"exchange":"binance-futures","account":"*","symbol":"@@@/USDT","type":"market","side":"buy","amount":{{entrySize}},"tp_price":{{tp_price}},"tp_lo_price":{{tp_price}},"token":"1234"}:MVT', confirm=false, group="Alert Messages")

// 嵌套调用str.replace_all,同时替换两个变量
alertMsg1 = str.replace_all(str.replace_all(entrystring, "{{entrySize}}", str.tostring(entry_size)), "{{tp_price}}", str.tostring(tp_price))

if entry condition
     strategy.order("Short", strategy.short, qty=entry_size, limit=r_close, comment="Entry", alert_message=alertMsg1)

注意事项

  • 确保你的脚本中已经定义了tp_price变量(即你计算好的止盈价格数值),否则会触发报错。
  • 如果后续需要添加更多变量,继续嵌套str.replace_all即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 03:01:06