基于BB %B指标的确认蜡烛信号Pine Script V4告警问题
Pine Script Ver.4 实现BB %B双蜡烛确认的告警与绘图
核心逻辑实现
直接针对你需要的两个信号条件,代码里需同时判断蜡烛颜色和**%B穿越动作**的延续确认:
- 做多信号:前一根绿色蜡烛(收盘价>开盘价)上穿%B的0值,当前K线依然是绿色蜡烛,视为确认信号
- 做空信号:前一根红色蜡烛(收盘价<开盘价)下穿%B的1.0值,当前K线依然是红色蜡烛,视为确认信号
完整代码
//@version=4 study("BB %B Double Candle Confirmation", overlay=true) // 计算布林带 %B length = input(20, title="BB Length") src = input(close, title="Source") mult = input(2.0, title="BB Std Dev") basis = sma(src, length) dev = mult * stdev(src, length) upper = basis + dev lower = basis - dev bb_percentB = (src - lower) / (upper - lower) // 定义信号条件 longCondition = close[1] > open[1] and crossover(bb_percentB[1], 0) and close > open shortCondition = close[1] < open[1] and crossunder(bb_percentB[1], 1.0) and close < open // 绘制确认信号箭头 plotarrow(longCondition ? 1 : na, title="Long Confirm", colorup=color.green, maxheight=15, minheight=10) plotarrow(shortCondition ? -1 : na, title="Short Confirm", colordown=color.red, maxheight=15, minheight=10) // 设置告警条件 alertcondition(longCondition, title="Long Confirm Alert", message="BB %B 上穿0,双绿蜡烛确认做多") alertcondition(shortCondition, title="Short Confirm Alert", message="BB %B 下穿1.0,双红蜡烛确认做空")
关键代码解释
crossover(bb_percentB[1], 0):判断前一根K线的%B指标上穿0值,配合close[1] > open[1]确保这根K线是绿色蜡烛- 当前K线的
close > open(做多)/close < open(做空):完成第二根同色蜡烛的确认 plotarrow:仅在确认信号触发时绘制箭头,过滤无效信号alertcondition:直接绑定确认信号条件,触发时推送对应告警信息
内容的提问来源于stack exchange,提问作者Timmy
相关产品推荐
相关产品推荐

