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

求助Pine Script:基于Heikinashi蜡烛构建Supertrend与EMA云指标

基于Heikin Ashi的Supertrend+EMA云指标(普通蜡烛图叠加)

核心实现逻辑

  • 计算Heikin Ashi(HA)蜡烛的核心数据,所有指标均基于HA数据计算
  • 用HA收盘价生成EMA20指标
  • 用HA的高、低、收盘价计算Supertrend(10,7)指标
  • 绘制EMA20与Supertrend之间的填充区域(云),叠加在普通蜡烛图上
  • 添加普通蜡烛触碰云的信号标记,方便观察价格动作

完整Pine Script代码

//@version=5
indicator("HA Supertrend-EMA Cloud", overlay=true)

// 1. 计算Heikin Ashi蜡烛核心数据
ha_close = ta.close + ta.open + ta.high + ta.low
ha_close := ha_close / 4
ha_open = na(ha_open[1]) ? (ta.open + ta.close) / 2 : (ha_open[1] + ha_close[1]) / 2
ha_high = math.max(ta.high, math.max(ha_open, ha_close))
ha_low = math.min(ta.low, math.min(ha_open, ha_close))

// 2. 基于HA收盘价计算EMA20
ema_20 = ta.ema(ha_close, 20)

// 3. 基于HA数据计算Supertrend(10,7)
atr_length = 10
atr_multiplier = 7.0
atr = ta.atr(atr_length)
upper_band = (ha_high + ha_low) / 2 + atr_multiplier * atr
lower_band = (ha_high + ha_low) / 2 - atr_multiplier * atr
supertrend = na(supertrend[1]) ? upper_band : 
             supertrend[1] == upper_band[1] ? math.min(upper_band, upper_band[1]) : math.max(lower_band, lower_band[1])
supertrend := ta.close > supertrend[1] ? lower_band : ta.close < supertrend[1] ? upper_band : supertrend[1]

// 4. 绘制云区域(EMA20与Supertrend之间的填充)
cloud_top = math.max(ema_20, supertrend)
cloud_bottom = math.min(ema_20, supertrend)
fill(cloud_top, cloud_bottom, color=color.new(color.blue, 80), title="HA Cloud")

// 5. 检测普通蜡烛触碰云的信号
touch_cloud = (ta.low <= cloud_top and ta.low >= cloud_bottom) or 
              (ta.high <= cloud_top and ta.high >= cloud_bottom) or 
              (ta.open <= cloud_top and ta.open >= cloud_bottom) or 
              (ta.close <= cloud_top and ta.close >= cloud_bottom)

// 标记触碰信号
plotshape(touch_cloud, title="Touch Cloud", location=location.belowbar, color=color.red, style=shape.triangleup, size=size.small)
plotshape(touch_cloud, title="Touch Cloud", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)

// 可选:绘制EMA20和Supertrend线(辅助观察)
plot(ema_20, color=color.green, title="HA EMA20")
plot(supertrend, color=color.orange, title="HA Supertrend(10,7)")

代码逐段解释

  1. Heikin Ashi计算:
    遵循HA蜡烛的标准公式:HA收盘价为普通蜡烛开高低收的平均值,HA开盘价为前一根HA开收的平均值,HA高低取普通蜡烛与HA开收的极值。
  2. EMA20计算:直接调用ta.ema()函数,传入HA收盘价和周期20即可。
  3. Supertrend计算:
    先计算HA数据的ATR(真实波幅),再基于HA中位价生成上下轨,最后根据价格穿越情况切换Supertrend的方向,完全复用标准Supertrend逻辑,仅替换数据源为HA数据。
  4. 云区域绘制:用fill()函数填充EMA20与Supertrend的区间,设置半透明蓝色避免遮挡普通蜡烛。
  5. 触碰检测:判断普通蜡烛的开、高、低、收任意价格落在云的上下边界之间,就用上下箭头标记信号。

内容的提问来源于stack exchange,提问作者Sandeep Bhamra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 21:32:20