Pine Script v2中strategy.close无法加comment,如何生成警报文本?
在Pine Script v2中实现平仓警报文本的方法
Pine Script v2的strategy.close()确实不支持comment参数,你可以通过以下两种方式实现类似需求:
方法1:用strategy.exit()替代strategy.close()
strategy.exit()在v2中支持comment参数,可直接替代strategy.close(),同时指定平仓条件并添加注释(用于生成警报)。若无需止损/止盈,留空limit和stop参数即可。
修改后的代码示例:
//@version=2 strategy("My Strategy", overlay=true) // 假设已定义相关变量(示例值仅作演示) target02_trade_size = 1 target02_buy_entry = close > open target02_buy_close = close < open target02_sel_entry = close < open target02_sel_close = close > open strategy.entry('target_buy', long=strategy.long, qty=target02_trade_size, comment='buy 02', when=target02_buy_entry) // 用strategy.exit替代strategy.close,添加平仓注释 strategy.exit('exit_buy', 'target_buy', comment='close buy 02', when=target02_buy_close) strategy.entry('target_sell', long=strategy.short, qty=target02_trade_size, comment='sell 02', when=target02_sel_entry) // 修复原代码重复when参数的错误,添加平仓注释 strategy.exit('exit_sell', 'target_sell', comment='close sell 02', when=target02_sel_close)
方法2:用alertcondition()手动触发自定义警报
若不想替换strategy.close(),可单独用alertcondition()监测平仓条件,当条件满足时触发带自定义文本的警报。
代码示例:
//@version=2 strategy("My Strategy", overlay=true) // 假设已定义相关变量(示例值仅作演示) target02_trade_size = 1 target02_buy_entry = close > open target02_buy_close = close < open target02_sel_entry = close < open target02_sel_close = close > open strategy.entry('target_buy', long=strategy.long, qty=target02_trade_size, comment='buy 02', when=target02_buy_entry) strategy.close('target_buy', when=target02_buy_close) // 触发买入平仓的自定义警报 alertcondition(target02_buy_close, title="Close Buy 02", message="Closed target_buy position: buy 02 closed") strategy.entry('target_sell', long=strategy.short, qty=target02_trade_size, comment='sell 02', when=target02_sel_entry) strategy.close('target_sell', when=target02_sel_close) // 触发卖出平仓的自定义警报 alertcondition(target02_sel_close, title="Close Sell 02", message="Closed target_sell position: sell 02 closed")
使用此方法时,需在TradingView警报设置中选择对应的alertcondition来触发,警报文本可完全自定义。
内容的提问来源于stack exchange,提问作者heider khan
相关产品推荐
相关产品推荐

