本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Recent Regular and HA Candles", overlay = true)
//Display recent price action while keeping the rest of your chart uncluttered.
//This indicator displays a selectable number of regular candles, Heikin Ashi candles, or both. It is designed for traders who use moving average ribbons or other overlays to view trend history but still want recent candle structure visible.
//FEATURES
//• Independently enable regular and Heikin Ashi candles.
//• Set a separate candle count, transparency, and bullish/bearish colors for each type.
//• Apply independent vertical offsets in ticks to separate the two candle displays while preserving their alignment in time.
//• Add an optional reference EMA to each candle type, selectable from 20, 50, 100, or 200 periods.
//• Adjust each EMA’s color and width.
//• Display optional labels identifying candle offsets and reference EMAs.
//HOW TO USE
//Hide the chart’s main candles, then enable the desired candle types in the indicator settings. Leave the vertical offset at zero to display candles at their original price values. Use a positive offset to move them upward or a negative offset to move them downward.
//The indicator uses the chart’s timeframe and explicitly requests regular and Heikin Ashi data, so the selected displays work whether the underlying chart is set to regular candles or Heikin Ashi.
//Each reference EMA is calculated from its corresponding candle type’s close values using the available historical data. It is displayed over the selected recent candle count and receives the same vertical offset as its candles.
//NOTES
//The candle count includes the current developing candle, which updates as new data arrives.
//Heikin Ashi candles use synthetic prices. Vertically offset candles and EMAs are shifted visual references; their displayed positions do not represent their original price values.
//This indicator is a chart display tool and does not generate trade signals.
//Developed with assistance from ChatGPT.
// Regular candle settings
showRegular = input.bool(true, "Show regular candles", group = "Regular Candles")
regularCount = input.int(10, "Number of candles", minval = 1, group = "Regular Candles")
regularTransparency = input.int(30, "Transparency", minval = 0, maxval = 100, group = "Regular Candles")
regularOffsetTicks = input.int(0, "Vertical offset (ticks)", group = "Regular Candles", tooltip = "Positive moves candles up; negative moves them down. Zero preserves their price position.")
regularBull = input.color(color.lime, "Bullish color", group = "Regular Candles", inline = "regc")
regularBear = input.color(color.red, "Bearish color", group = "Regular Candles", inline = "regc")
// Heikin Ashi candle settings
showHA = input.bool(false, "Show HA candles", group = "Heikin Ashi Candles")
haCount = input.int(10, "Number of candles", minval = 1, group = "Heikin Ashi Candles")
haTransparency = input.int(30, "Transparency", minval = 0, maxval = 100, group = "Heikin Ashi Candles")
haOffsetTicks = input.int(20, "Vertical offset (ticks)", group = "Heikin Ashi Candles", tooltip = "Positive moves candles up; negative moves them down. Offset candles are shifted for comparison and do not mark their original price values.")
haBull = input.color(color.white, "Bullish color", group = "Heikin Ashi Candles", inline = "HAc")
haBear = input.color(color.fuchsia, "Bearish color", group = "Heikin Ashi Candles", inline = "HAc")
// Regular Reference EMA settings
regularShowEMA = input.bool(false, "Show reference EMA", group = "Regular Reference EMA")
regularEMALength = input.int(20, "Length", options = [20, 50, 100, 200], group = "Regular Reference EMA", inline = "refema")
regularEMAWidth = input.int(2, "Width", options = [1, 2, 3, 4], group = "Regular Reference EMA", inline = "refema")
regularEMAColor = input.color(color.yellow, "", group = "Regular Reference EMA", inline = "refema")
// HA Reference EMA settings
haShowEMA = input.bool(false, "Show reference EMA", group = "HA Reference EMA")
haEMALength = input.int(20, "Length", options = [20, 50, 100, 200], group = "HA Reference EMA", inline = "refema")
haEMAWidth = input.int(2, "Width", options = [1, 2, 3, 4], group = "HA Reference EMA", inline = "refema")
haEMAColor = input.color(color.aqua, "", group = "HA Reference EMA", inline = "refema")
// Request both candle types using the chart timeframe.
regularTicker = ticker.standard(syminfo.tickerid)
haTicker = ticker.heikinashi(regularTicker)
[regO, regH, regL, regC, regularEMA] = request.security(regularTicker, timeframe.period, [open, high, low, close, ta.ema(close, regularEMALength)], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
[haO, haH, haL, haC, haEMA] = request.security(haTicker, timeframe.period, [open, high, low, close, ta.ema(close, haEMALength)], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
// Convert tick offsets to price units.
regularShift = regularOffsetTicks * syminfo.mintick
haShift = haOffsetTicks * syminfo.mintick
// Determine colors from the original candle values.
regularColor = color.new(regC >= regO ? regularBull : regularBear, regularTransparency)
haColor = color.new(haC >= haO ? haBull : haBear, haTransparency)
// Shift all four values equally to preserve candle shapes.
// Transparency applies to bodies, wicks, and borders.
plotcandle(showRegular ? regO + regularShift : na, regH + regularShift, regL + regularShift, regC + regularShift, title = "Regular candles", color = regularColor, wickcolor = regularColor, bordercolor = regularColor, show_last = regularCount)
plotcandle(showHA ? haO + haShift : na, haH + haShift, haL + haShift, haC + haShift, title = "HA candles", color = haColor, wickcolor = haColor, bordercolor = haColor, show_last = haCount)
// EMAs use the full source history, then display only over the recent candles.
// Each EMA follows its candle type's visibility and vertical offset.
plot(showRegular and regularShowEMA ? regularEMA + regularShift : na, title = "Regular reference EMA", color = regularEMAColor, linewidth = regularEMAWidth, show_last = regularCount)
plot(showHA and haShowEMA ? haEMA + haShift : na, title = "HA reference EMA", color = haEMAColor, linewidth = haEMAWidth, show_last = haCount)
// Offset label settings
showOffsetLabels = input.bool(true, "Show offset labels", group = "Offset Labels")
// One reusable label for each candle type.
var label regularOffsetLabel = na
var label haOffsetLabel = na
if barstate.islast
// Regular candle offset label
if showOffsetLabels and showRegular and regularOffsetTicks != 0 and not na(regC)
regularText = "Regular offset: " + (regularOffsetTicks > 0 ? "+" : "") + str.tostring(regularOffsetTicks) + " ticks"
if na(regularOffsetLabel)
regularOffsetLabel := label.new(bar_index + 1, regC + regularShift, regularText, style = label.style_label_left, color = color.new(color.black, 20), textcolor = color.white, size = size.small)
else
label.set_xy(regularOffsetLabel, bar_index + 1, regC + regularShift)
label.set_text(regularOffsetLabel, regularText)
else
if not na(regularOffsetLabel)
label.delete(regularOffsetLabel)
regularOffsetLabel := na
// Heikin Ashi candle offset label
if showOffsetLabels and showHA and haOffsetTicks != 0 and not na(haC)
haText = "HA offset: " + (haOffsetTicks > 0 ? "+" : "") + str.tostring(haOffsetTicks) + " ticks"
if na(haOffsetLabel)
haOffsetLabel := label.new(bar_index + 1, haC + haShift, haText, style = label.style_label_left, color = color.new(color.black, 20), textcolor = color.white, size = size.small)
else
label.set_xy(haOffsetLabel, bar_index + 1, haC + haShift)
label.set_text(haOffsetLabel, haText)
else
if not na(haOffsetLabel)
label.delete(haOffsetLabel)
haOffsetLabel := na
// Optional reference EMA labels
showEMALabels = input.bool(true, "Show reference EMA labels", group = "Offset Labels")
var label regularEMALabel = na
var label haEMALabel = na
if barstate.islast
if showEMALabels and showRegular and regularShowEMA and not na(regularEMA)
regularEMAText = "Regular EMA " + str.tostring(regularEMALength)
if na(regularEMALabel)
regularEMALabel := label.new(bar_index + 1, regularEMA + regularShift, regularEMAText, style = label.style_label_left, color = color.new(color.black, 20), textcolor = regularEMAColor, size = size.small)
else
label.set_xy(regularEMALabel, bar_index + 1, regularEMA + regularShift)
label.set_text(regularEMALabel, regularEMAText)
label.set_textcolor(regularEMALabel, regularEMAColor)
else
if not na(regularEMALabel)
label.delete(regularEMALabel)
regularEMALabel := na
if showEMALabels and showHA and haShowEMA and not na(haEMA)
haEMAText = "HA EMA " + str.tostring(haEMALength)
if na(haEMALabel)
haEMALabel := label.new(bar_index + 1, haEMA + haShift, haEMAText, style = label.style_label_left, color = color.new(color.black, 20), textcolor = haEMAColor, size = size.small)
else
label.set_xy(haEMALabel, bar_index + 1, haEMA + haShift)
label.set_text(haEMALabel, haEMAText)
label.set_textcolor(haEMALabel, haEMAColor)
else
if not na(haEMALabel)
label.delete(haEMALabel)
haEMALabel := na