本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Live 4H Candle", overlay = true, max_boxes_count = 2, max_lines_count = 4, max_labels_count = 4)
// --- Inputs ---
string candleTimeframeInput = input.timeframe("240", "Candle timeframe", tooltip = "Higher-timeframe candle to display; 240 is 4 hours.")
int candleOffsetInput = input.int(10, "Candle offset (chart bars)", minval = 1, maxval = 100, tooltip = "Places the displayed candle this many chart bars to the right of the latest bar.")
int candleWidthInput = input.int(4, "Candle width (chart bars)", minval = 1, maxval = 20, tooltip = "Horizontal width of the displayed candle body.")
color bullColorInput = input.color(#089981, "Bullish candle", group = "Style", tooltip = "Color used when the higher-timeframe candle closes above its open.")
color bearColorInput = input.color(#f23645, "Bearish candle", group = "Style", tooltip = "Color used when the higher-timeframe candle closes below its open.")
color priceColorInput = input.color(#5b9cf6, "Current price marker", group = "Style", tooltip = "Color for the line and label at the latest chart price.")
// --- Higher-timeframe data ---
[htfOpen, htfHigh, htfLow, htfClose] = request.security(syminfo.tickerid, candleTimeframeInput, [open, high, low, close], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
// --- Persistent drawings ---
var box candleBody = na
var line candleWick = na
var line lowerWick = na
var line priceLine = na
var label priceLabel = na
var label rangeLabel = na
if barstate.islast and not na(htfOpen) and not na(htfHigh) and not na(htfLow) and not na(htfClose)
bool bullishCandle = htfClose >= htfOpen
color candleColor = bullishCandle ? bullColorInput : bearColorInput
int centerX = bar_index + candleOffsetInput
int halfWidth = int(math.floor(candleWidthInput / 2.0))
int leftX = centerX - halfWidth
int rightX = centerX + int(math.ceil(candleWidthInput / 2.0))
int priceLabelX = rightX + 2
float bodyTop = math.max(htfOpen, htfClose)
float bodyBottom = math.min(htfOpen, htfClose)
float rangeSize = htfHigh - htfLow
float rangeLabelY = htfLow - math.max(rangeSize * 0.12, syminfo.mintick * 20)
string rangeText = "H " + str.tostring(htfHigh, format.mintick) + " / L " + str.tostring(htfLow, format.mintick) + "\nRange " + str.tostring(rangeSize, format.mintick)
if na(candleBody)
candleBody := box.new(left = leftX, top = bodyTop, right = rightX, bottom = bodyBottom, xloc = xloc.bar_index, border_color = candleColor, border_width = 2, bgcolor = color.new(candleColor, 0))
candleWick := line.new(x1 = centerX, y1 = htfHigh, x2 = centerX, y2 = bodyTop, xloc = xloc.bar_index, color = candleColor, width = 2)
lowerWick := line.new(x1 = centerX, y1 = bodyBottom, x2 = centerX, y2 = htfLow, xloc = xloc.bar_index, color = candleColor, width = 2)
priceLine := line.new(x1 = rightX, y1 = close, x2 = priceLabelX, y2 = close, xloc = xloc.bar_index, color = priceColorInput, width = 2)
priceLabel := label.new(x = priceLabelX, y = close, text = str.tostring(close, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_left, color = color.new(priceColorInput, 100), textcolor = priceColorInput, size = size.small)
rangeLabel := label.new(x = centerX, y = rangeLabelY, text = rangeText, xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_center, color = color.new(bullColorInput, 100), textcolor = bullColorInput, size = size.small)
else
box.set_lefttop(candleBody, leftX, bodyTop)
box.set_rightbottom(candleBody, rightX, bodyBottom)
box.set_border_color(candleBody, candleColor)
box.set_bgcolor(candleBody, color.new(candleColor, 0))
line.set_xy1(candleWick, centerX, htfHigh)
line.set_xy2(candleWick, centerX, bodyTop)
line.set_color(candleWick, candleColor)
line.set_xy1(lowerWick, centerX, bodyBottom)
line.set_xy2(lowerWick, centerX, htfLow)
line.set_color(lowerWick, candleColor)
line.set_xy1(priceLine, rightX, close)
line.set_xy2(priceLine, priceLabelX, close)
line.set_color(priceLine, priceColorInput)
label.set_xy(priceLabel, priceLabelX, close)
label.set_text(priceLabel, str.tostring(close, format.mintick))
label.set_color(priceLabel, color.new(priceColorInput, 100))
label.set_xy(rangeLabel, centerX, rangeLabelY)
label.set_text(rangeLabel, rangeText)
label.set_color(rangeLabel, color.new(bullColorInput, 100))