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

如何在TradingView策略警报消息中加入绘图的入场价格?

TradingView策略警报添加入场价格的问题解决方法

问题背景

需要在TradingView策略的警报消息中加入入场价格,已通过strategy.opentrades.entry_price()获取并绘制了lastEntryPrice,但尝试用{{plot("Last entry price")}}等模板变量嵌入警报消息时全部失败。

失败原因

  1. TradingView警报模板的{{plot()}}变量仅支持引用指标脚本中的绘图,无法访问策略脚本里的plot内容。
  2. 策略内部计算的变量(如lastEntryPrice)不能直接通过警报模板的变量调用,必须在触发订单时主动将价格嵌入alert_message参数。

解决方案

直接在策略代码中构造包含入场价格的警报消息,传递给strategy.entry的alert_message参数,无需依赖警报模板的复杂变量。

修改后的完整代码示例

//@version=5
strategy("带入场价的策略警报", overlay=true)

// 替换为你实际的条件变量
afterStartDate = true
Buy = close > open 
Sell = close < open 

// 生成标准化警报消息的函数
createAlertMessage(action, entryPrice) =>
    json.stringify({
        "ticker": syminfo.ticker,
        "action": action,
        "sentiment": strategy.market_position,
        "quantity": strategy.order.contracts,
        "entry_price": entryPrice
    })

// 多单入场逻辑
if afterStartDate and Buy
    longEntryPrice = close // 这里用当前收盘价作为入场价,若为限价单可替换为指定价格
    buyAlertMessage = createAlertMessage("BUY", longEntryPrice)
    strategy.entry("BUY", strategy.long, alert_message = buyAlertMessage)

// 空单入场逻辑
if afterStartDate and Sell
    shortEntryPrice = close
    sellAlertMessage = createAlertMessage("SELL", shortEntryPrice)
    strategy.entry("SELL", strategy.short, alert_message = sellAlertMessage)

// 图表显示最后入场价(仅用于可视化,不影响警报)
lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
plot(lastEntryPrice, color=color.fuchsia, style=plot.style_circles, linewidth=3, title="Last entry price")

配置说明

  1. 代码中通过createAlertMessage函数构造JSON格式的警报内容,直接包含入场价格entry_price字段。
  2. 设置警报时,选择**"Alert() function message"**选项,无需手动编写模板变量,消息内容会直接使用代码中生成的JSON。
  3. 若使用限价/止损单,将close替换为你指定的入场价格变量即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 11:06:20