修改TradingView Pine Script实现Heiken Ashi蜡烛变色触发警报
修复Heiken Ashi蜡烛颜色变化警报问题
你的脚本当前会对所有蜡烛触发警报,核心原因是alertcondition直接使用了c2和o2作为触发条件,没有检测蜡烛颜色切换的临界点。要实现仅在颜色变化时触发警报,需要对比当前蜡烛与前一根蜡烛的颜色状态。
修改后的完整代码
//@version=2 study(title = "Smoothed Heiken Ashi Candles", shorttitle="Smoothed Ha Candles", overlay=true) len=input(10) o=ema(open,len) c=ema(close,len) h=ema(high,len) l=ema(low,len) haclose = (o+h+l+c)/4 haopen = na(haopen[1]) ? (o + c)/2 : (haopen[1] + haclose[1]) / 2 hahigh = max (h, max(haopen,haclose)) halow = min (l, min(haopen,haclose)) len2=input(10) o2=ema(haopen, len2) c2=ema(haclose, len2) h2=ema(hahigh, len2) l2=ema(halow, len2) // 定义当前与前一根蜡烛的颜色状态 is_current_green = c2 >= o2 is_prev_red = o2[1] > c2[1] is_current_red = o2 > c2 is_prev_green = c2[1] >= o2[1] // 触发条件:红转绿触发买入,绿转红触发卖出 buy_signal = is_current_green and is_prev_red sell_signal = is_current_red and is_prev_green col=is_current_green ? lime : red plotcandle(o2, h2, l2, c2, title="heikin smoothed", color=col) alertcondition(buy_signal, title="Buy", message="Green candle detected - Buy") alertcondition(sell_signal, title="Sell", message="Red candle detected - Sell")
关键修改说明
- 新增状态变量:分别判断当前蜡烛颜色、前一根蜡烛的颜色
- 买入信号逻辑:当前蜡烛为绿色,且前一根蜡烛为红色(红转绿的临界点)
- 卖出信号逻辑:当前蜡烛为红色,且前一根蜡烛为绿色(绿转红的临界点)
- 更新
alertcondition触发条件:仅在颜色切换的临界点触发警报
修改后,警报只会在蜡烛颜色发生变化的K线上触发,不会对所有蜡烛重复报警。
内容的提问来源于stack exchange,提问作者user1466588
相关产品推荐
相关产品推荐

