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

如何获取当日首根Heikin Ashi K线颜色以触发交易信号?

解决方案:获取当日首根60分钟Heikin Ashi K线颜色触发信号

原代码的核心问题在于:

  • isFirstBar = ta.change(time("60"))仅检测60分钟K线的切换,未限定为当日的第一根60分钟K线
  • 通过直接比较颜色值判断涨跌,存在输入颜色变更导致逻辑失效的风险

以下是修正后的代码,核心逻辑是先定位当日首根60分钟Heikin Ashi K线,再提取其涨跌状态:

//@version=5
indicator("当日首根Heikin Ashi入场信号", overlay=false)

// === Heikin Ashi 参数设置 ===
bullColor = input.color(#00E600, title="阳线颜色", group="Heikin Ashi")
bearColor = input.color(#FF0000, title="阴线颜色", group="Heikin Ashi")
timeframe = input.timeframe("60", title="目标时间框架", group="Heikin Ashi")

// === 核心逻辑:获取当日首根60分钟Heikin Ashi K线 ===
// 1. 构建Heikin Ashi合约ticker
ha_ticker = ticker.heikinashi(syminfo.tickerid)
// 2. 请求60分钟Heikin Ashi的开、收盘价
[ha_open, ha_close] = request.security(ha_ticker, timeframe, [open, close])
// 3. 判断当前60分钟K线是否是当日第一根
is_new_day = ta.change(time("D"))  // 当日第一根任何时间框架K线触发
is_first_60min_of_day = ta.newbar(timeframe) and is_new_day
// 4. 缓存当日首根Heikin Ashi的涨跌状态(避免重复触发)
var bool daily_first_ha_bull = na
var bool daily_first_ha_bear = na
if is_first_60min_of_day
    daily_first_ha_bull := ha_close > ha_open
    daily_first_ha_bear := ha_close < ha_open
// 重置每日状态
if is_new_day
    daily_first_ha_bull := na
    daily_first_ha_bear := na

// === 入场信号 ===
longEntry = daily_first_ha_bull
shortEntry = daily_first_ha_bear

// === 信号绘制 ===
plotshape(longEntry, title="多单入场", style=shape.circle, location=location.bottom, color=bullColor, size=size.small)
plotshape(shortEntry, title="空单入场", style=shape.circle, location=location.bottom, color=bearColor, size=size.small)

关键改动说明:

  • 用ta.change(time("D"))判断新交易日,结合ta.newbar(timeframe)锁定当日首根60分钟K线
  • 直接通过ha_close > ha_open判断Heikin Ashi涨跌,而非依赖颜色值,逻辑更可靠
  • 使用var变量缓存当日首根K线的涨跌状态,确保信号仅触发一次
  • 新增交易日重置逻辑,避免跨交易日状态污染

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 08:35:03