如何在Pine Script策略脚本中添加可选择买卖的TradingView警报
TradingView策略警报设置方案(不占用消息区)
问题描述
我想在自己的Pine Script策略脚本中添加「买入」和「卖出」警报,让用户能在TradingView警报窗口中选择触发买入或卖出警报。由于警报消息区要用来给3commas机器人发送指令,不能占用这个区域,请问能不能在TradingView警报弹窗里设置显示买卖选项且不使用消息区(如图所示)?

当前策略代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Hawkreign //@version=5 strategy(title="SGA Alert", overlay=true, default_qty_type = strategy.cash, default_qty_value = 100) // Input options src = input.source(close, title = "SGA" ) // Long and Short Conditions on the SGA long = ta.rising(src, 1) short = ta.falling(src, 1) // Long and Short Memory longmem = false shortmem = false // Memory Conditions longmem := long ? true : short ? false : longmem[1] shortmem := short ? true : long ? false : shortmem[1] // Entry and Exit Variables buy = long and not (longmem[1]) sell = short and not (shortmem[1]) // Long and Short Background color bgcolor(buy ? color(color.rgb(11, 238, 18)): na) bgcolor(sell ? color(color.rgb(134, 7, 7)): na) // Long Entry and Exit if (buy) strategy.entry("buy", strategy.long) strategy.close("sell") // Short Entry and Exit if (sell) strategy.entry("sell", strategy.short) strategy.close("buy") plot(src)
解决方案
完全可以实现,通过给买入、卖出信号注册独立的警报条件,就能在TradingView警报窗口的「条件」下拉菜单中直接选择触发类型,消息区可以完全留给3commas的指令使用。
修改后的代码
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Hawkreign //@version=5 strategy(title="SGA Alert", overlay=true, default_qty_type = strategy.cash, default_qty_value = 100) // Input options src = input.source(close, title = "SGA" ) // Long and Short Conditions on the SGA long = ta.rising(src, 1) short = ta.falling(src, 1) // Long and Short Memory longmem = false shortmem = false // Memory Conditions longmem := long ? true : short ? false : longmem[1] shortmem := short ? true : long ? false : shortmem[1] // Entry and Exit Variables buy = long and not (longmem[1]) sell = short and not (shortmem[1]) // 注册买入/卖出警报条件,用于TradingView警报窗口的条件选择 alertcondition(buy, title="买入信号", message="") // 消息留空,不占用消息区 alertcondition(sell, title="卖出信号", message="") // Long and Short Background color bgcolor(buy ? color(color.rgb(11, 238, 18)): na) bgcolor(sell ? color(color.rgb(134, 7, 7)): na) // Long Entry and Exit if (buy) strategy.entry("buy", strategy.long) strategy.close("sell") // Short Entry and Exit if (sell) strategy.entry("sell", strategy.short) strategy.close("buy") plot(src)
使用步骤
- 将修改后的代码替换原有脚本并保存
- 打开TradingView的警报设置窗口
- 在「条件」下拉菜单中,选择「SGA Alert: 买入信号」或「SGA Alert: 卖出信号」
- 在「消息」栏直接填写3commas机器人所需的指令字符串
- 设置好其他警报参数(触发频率、通知方式等)后保存即可
内容的提问来源于stack exchange,提问作者HuskyReign
相关产品推荐
相关产品推荐

