Heikin Ashi + OHLC Candles

macro_megadeath · study · 57 行 · 点赞 9 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © macro_megadeath
 
//@version=6
indicator("Heikin Ashi + OHLC Candles", shorttitle="HA-OC", overlay=true)
 
// ─────────────────────────────────────────────────────────────
// HA candle (fixed width, no volume scaling) with a close-level tick.
//   - Candle : Heikin Ashi body + black center line (HA high -> HA low)
//   - Dot    : closing level only, a dot in the middle of the candle
//   - No open level
// ─────────────────────────────────────────────────────────────
 
var string GRP_C = "Colors"
var string GRP_T = "Close Level"
 
bullCol  = input.color(#26A69A, "Bull (HA close >= HA open)", group=GRP_C, display=display.none)
bearCol  = input.color(#EF5350, "Bear (HA close < HA open)", group=GRP_C, display=display.none)
wickCol  = input.color(color.black, "Center line", group=GRP_C,
     tooltip="Black works on light charts. Switch to white on a dark chart background.", display=display.none)
 
closeSrc = input.string("Real close", "Close level source", options=["Real close", "HA close"], group=GRP_T,
     tooltip="Real close = the actual last price of the bar (the HA close is already the body edge). HA close = the Heikin Ashi close.", display=display.none)
dotCol   = input.color(color.black, "Close marker color", group=GRP_T, display=display.none)
dotSize  = input.string("tiny", "Close marker size", options=["tiny", "small", "normal", "large"], group=GRP_T,
     tooltip="Triangle up = close above open, triangle down = close below open, cross = close equals open.", display=display.none)
openSrc  = input.string("Real open", "Open level source", options=["Real open", "HA open"], group=GRP_T,
     tooltip="Real open = the actual opening price of the bar. HA open = the Heikin Ashi open (the other body edge).", display=display.none)
openCol  = input.color(color.black, "Open dot color", group=GRP_T, display=display.none)
openSize = input.int(2, "Open dot size", minval=1, maxval=8, group=GRP_T, display=display.none)
 
// ===== Heikin Ashi =====
float haClose = (open + high + low + close) / 4
float haOpen  = na
haOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2
float haHigh  = math.max(high, math.max(haOpen, haClose))
float haLow   = math.min(low,  math.min(haOpen, haClose))
 
color candleCol = haClose >= haOpen ? bullCol : bearCol
plotcandle(haOpen, haHigh, haLow, haClose, title="HA Candle",
     color=candleCol, bordercolor=candleCol, wickcolor=wickCol)
 
float closeLevel = closeSrc == "Real close" ? close : haClose
float openLevel  = openSrc == "Real open" ? open : haOpen
 
// ===== Close marker: shape depends on close vs open =====
string mSize = dotSize == "large" ? size.large : dotSize == "normal" ? size.normal : dotSize == "small" ? size.small : size.tiny
bool closeUp   = closeLevel > openLevel
bool closeDown = closeLevel < openLevel
bool closeFlat = closeLevel == openLevel
 
plotshape(closeUp   ? closeLevel : na, title="Close up",   style=shape.triangleup,   location=location.absolute, color=dotCol, size=size.tiny)
plotshape(closeDown ? closeLevel : na, title="Close down", style=shape.triangledown, location=location.absolute, color=dotCol, size=size.tiny)
plotshape(closeFlat ? closeLevel : na, title="Close flat", style=shape.cross,        location=location.absolute, color=dotCol, size=size.tiny)
 
// ===== Open-level dot =====
plot(openLevel, title="Open dot", style=plot.style_circles, color=openCol, linewidth=openSize)

← 返回列表